Advertisement
lvs

app.lua

lvs
Sep 16th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.74 KB | None | 0 0
  1. -- Prevent global missuse
  2. local mt = getmetatable(_G)
  3. if mt == nil then
  4.   mt = {}
  5.   setmetatable(_G, mt)
  6. end
  7.  
  8. mt.__declared = {}
  9.  
  10. mt.__newindex = function (t, n, v)
  11.   if not mt.__declared[n] then
  12.     local w = debug.getinfo(2, 'S').what
  13.     if w ~= 'main' and w ~= 'C' then
  14.       error('assign to undeclared variable \'' .. n .. '\'', 2)
  15.     end
  16.     mt.__declared[n] = true
  17.   end
  18.   rawset(t, n, v)
  19. end
  20.  
  21. mt.__index = function (t, n)
  22.   if not mt.__declared[n] and debug.getinfo(2, 'S').what ~= 'C' then
  23.     error('variable \'' .. n .. '\' is not declared', 2)
  24.   end
  25.   return rawget(t, n)
  26. end
  27.  
  28.  
  29. -------------------------------------------
  30. -- Module table
  31. -------------------------------------------
  32. local _M = {}
  33.  
  34. -------------------------------------------
  35. -- Define global shortcuts
  36. -------------------------------------------
  37.  
  38. if system.getInfo('environment') ~= 'simulator' then
  39.     io.output():setvbuf('no')
  40. else
  41.     _M.isSimulator = true
  42. end
  43. local platform = system.getInfo('platformName')
  44. if platform == 'Android' then
  45.     _M.isAndroid = true
  46. elseif platform == 'iPhone OS' then
  47.     _M.isiOS = true
  48. end
  49.  
  50. -- Target 480x320 screen
  51.  
  52. _W = display.contentWidth
  53. _H = display.contentHeight
  54. _T = display.screenOriginY -- Top
  55. _L = display.screenOriginX -- Left
  56. _R = display.viewableContentWidth - _L -- Right
  57. _B = display.viewableContentHeight - _T-- Bottom
  58. _CX = math.floor(_W / 2)
  59. _CY = math.floor(_H / 2)
  60.  
  61. _COLORS = {}
  62. _COLORS['white'] = {255, 255, 255}
  63. _COLORS['black'] = {0, 0, 0}
  64.  
  65. _COLORS['red'] = {255, 0, 0}
  66. _COLORS['green'] = {0, 255, 0}
  67. _COLORS['blue'] = {0, 0, 255}
  68.  
  69. _COLORS['yellow'] = {255, 255, 0}
  70. _COLORS['cyan'] = {0, 255, 255}
  71. _COLORS['magenta'] = {255, 0, 255}
  72.  
  73. local _AUDIO = {}
  74. _AUDIO['drop'] = 'sounds/drop.wav'
  75. _AUDIO['pick'] = 'sounds/pick.wav'
  76.  
  77. local ext = '.m4a'
  78. if _M.isAndroid or _M.isSimulator then
  79.     ext = '.ogg'
  80. end
  81.  
  82. _AUDIO['music'] = 'sounds/music/music' .. ext
  83.  
  84. local mCeil = math.ceil
  85. local mFloor = math.floor
  86. local mAbs = math.abs
  87. local mAtan2 = math.atan2
  88. local mSin = math.sin
  89. local mCos = math.cos
  90. local mPi = math.pi
  91. local mSqrt = math.sqrt
  92. local mRandom = math.random
  93. local tInsert = table.insert
  94. local tRemove = table.remove
  95. local tForEach = table.foreach
  96. local tShuffle = table.shuffle
  97. local sSub = string.sub
  98.  
  99. _M.duration = 200
  100.  
  101. -- Set reference point
  102. function _M.setRP (object, ref_point)
  103.     ref_point = string.lower(ref_point)
  104.     if ref_point == 'topleft' then
  105.         object:setReferencePoint(display.TopLeftReferencePoint)
  106.     elseif ref_point == 'topright' then
  107.         object:setReferencePoint(display.TopRightReferencePoint)
  108.     elseif ref_point == 'topcenter' then
  109.         object:setReferencePoint(display.TopCenterReferencePoint)
  110.     elseif ref_point == 'bottomleft' then
  111.         object:setReferencePoint(display.BottomLeftReferencePoint)
  112.     elseif ref_point == 'bottomright' then
  113.         object:setReferencePoint(display.BottomRightReferencePoint)
  114.     elseif ref_point == 'bottomcenter' then
  115.         object:setReferencePoint(display.BottomCenterReferencePoint)
  116.     elseif ref_point == 'centerleft' then
  117.         object:setReferencePoint(display.CenterLeftReferencePoint)
  118.     elseif ref_point == 'centerright' then
  119.         object:setReferencePoint(display.CenterRightReferencePoint)
  120.     elseif ref_point == 'center' then
  121.         object:setReferencePoint(display.CenterReferencePoint)
  122.     end
  123. end
  124.  
  125. function _M.setFillColor (object, color)
  126.     if type(color) == 'string' then
  127.         color = _COLORS[color]
  128.     end
  129.     object:setFillColor(color[1], color[2], color[3])
  130. end
  131.  
  132. function _M.setTextColor (object, color)
  133.     if type(color) == 'string' then
  134.         color = _COLORS[color]
  135.     end
  136.     local color = table.copy(color)
  137.     if not color[4] then color[4] = 255 end
  138.     object:setTextColor(color[1], color[2], color[3], color[4])
  139. end
  140.  
  141. function _M.setStrokeColor (object, color)
  142.     if type(color) == 'string' then
  143.         color = _COLORS[color]
  144.     end
  145.     local color = table.copy(color)
  146.     if not color[4] then color[4] = 255 end
  147.     object:setStrokeColor(color[1], color[2], color[3], color[4])
  148. end
  149.  
  150. function _M.setColor (object, color)
  151.     if type(color) == 'string' then
  152.         color = _COLORS[color]
  153.     end
  154.     local color = table.copy(color)
  155.     if not color[4] then color[4] = 255 end
  156.     object:setColor(color[1], color[2], color[3], color[4])
  157. end
  158.  
  159. function _M.newImage(filename, params)
  160.     params = params or {}
  161.     local w, h = params.w or _W, params.h or _H
  162.     local image = display.newImageRect(filename, w, h)
  163.     if params.rp then
  164.         _M.setRP(image, params.rp)
  165.     end
  166.     image.x = params.x or 0
  167.     image.y = params.y or 0
  168.     if params.g then
  169.         params.g:insert(image)
  170.     end
  171.     return image
  172. end
  173.  
  174. function _M.transition(object, params)
  175.     params = params or {}
  176.     params.delay = params.delay or 0
  177.     object.alpha = 0
  178.     local transParams = {time = 800, alpha = 1, transition = _M.ease.easeOut}
  179.     if params.delay > 0 then
  180.         object.isVisible = false
  181.         transParams.onStart = function (obj) obj.isVisible = true end
  182.         transParams.delay = params.delay * 30
  183.     end
  184.     transition.to(object, transParams)
  185. end
  186.  
  187. function _M.alert(txt)
  188.     if type(txt) == 'string' then
  189.         native.showAlert(_APP_NAME, txt, {'OK'})
  190.     end
  191. end
  192.  
  193. _M.loadedSounds = {}
  194. function _M:loadSound (sound_type)
  195.     if not self.loadedSounds[sound_type] then
  196.         local filename = _AUDIO[sound_type]
  197.         self.loadedSounds[sound_type] = audio.loadSound(filename)
  198.     end
  199.     return self.loadedSounds[sound_type]
  200. end
  201.  
  202. local audioChannel, otherAudioChannel, currentSong, curAudio, prevAudio = 1
  203. audio.crossFadeBackground = function (path, force)
  204.     if _M.music_on then
  205.         local musicPath = _AUDIO[path]
  206.         if currentSong == musicPath and audio.getVolume{channel = audioChannel} > 0.1 and not force then return false end
  207.         audio.fadeOut({channel=audioChannel, time=1000})
  208.         if audioChannel==1 then audioChannel,otherAudioChannel=2,1 else audioChannel,otherAudioChannel=1,2 end
  209.         audio.setVolume( 0.5, {channel = audioChannel})
  210.         curAudio = audio.loadStream( musicPath )
  211.         audio.play(curAudio, {channel=audioChannel, loops=-1, fadein=1000})
  212.         prevAudio = curAudio
  213.         currentSong = musicPath
  214.         audio.currentBackgroundChannel = audioChannel
  215.     end
  216. end
  217. audio.reserveChannels(2)
  218. audio.currentBackgroundChannel = 1
  219.  
  220. audio.playSFX = function (snd, params)
  221.     if _M.sound_on then
  222.         local channel
  223.         if type(snd) == 'string' then channel=audio.play(audio.loadSound(_AUDIO[snd]), params)
  224.         else channel=audio.play(snd, params) end
  225.         audio.setVolume(1, {channel = channel})
  226.         return channel
  227.     end
  228. end
  229.  
  230. return _M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement