Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Prevent global missuse
- local mt = getmetatable(_G)
- if mt == nil then
- mt = {}
- setmetatable(_G, mt)
- end
- mt.__declared = {}
- mt.__newindex = function (t, n, v)
- if not mt.__declared[n] then
- local w = debug.getinfo(2, 'S').what
- if w ~= 'main' and w ~= 'C' then
- error('assign to undeclared variable \'' .. n .. '\'', 2)
- end
- mt.__declared[n] = true
- end
- rawset(t, n, v)
- end
- mt.__index = function (t, n)
- if not mt.__declared[n] and debug.getinfo(2, 'S').what ~= 'C' then
- error('variable \'' .. n .. '\' is not declared', 2)
- end
- return rawget(t, n)
- end
- -------------------------------------------
- -- Module table
- -------------------------------------------
- local _M = {}
- -------------------------------------------
- -- Define global shortcuts
- -------------------------------------------
- if system.getInfo('environment') ~= 'simulator' then
- io.output():setvbuf('no')
- else
- _M.isSimulator = true
- end
- local platform = system.getInfo('platformName')
- if platform == 'Android' then
- _M.isAndroid = true
- elseif platform == 'iPhone OS' then
- _M.isiOS = true
- end
- -- Target 480x320 screen
- _W = display.contentWidth
- _H = display.contentHeight
- _T = display.screenOriginY -- Top
- _L = display.screenOriginX -- Left
- _R = display.viewableContentWidth - _L -- Right
- _B = display.viewableContentHeight - _T-- Bottom
- _CX = math.floor(_W / 2)
- _CY = math.floor(_H / 2)
- _COLORS = {}
- _COLORS['white'] = {255, 255, 255}
- _COLORS['black'] = {0, 0, 0}
- _COLORS['red'] = {255, 0, 0}
- _COLORS['green'] = {0, 255, 0}
- _COLORS['blue'] = {0, 0, 255}
- _COLORS['yellow'] = {255, 255, 0}
- _COLORS['cyan'] = {0, 255, 255}
- _COLORS['magenta'] = {255, 0, 255}
- local _AUDIO = {}
- _AUDIO['drop'] = 'sounds/drop.wav'
- _AUDIO['pick'] = 'sounds/pick.wav'
- local ext = '.m4a'
- if _M.isAndroid or _M.isSimulator then
- ext = '.ogg'
- end
- _AUDIO['music'] = 'sounds/music/music' .. ext
- local mCeil = math.ceil
- local mFloor = math.floor
- local mAbs = math.abs
- local mAtan2 = math.atan2
- local mSin = math.sin
- local mCos = math.cos
- local mPi = math.pi
- local mSqrt = math.sqrt
- local mRandom = math.random
- local tInsert = table.insert
- local tRemove = table.remove
- local tForEach = table.foreach
- local tShuffle = table.shuffle
- local sSub = string.sub
- _M.duration = 200
- -- Set reference point
- function _M.setRP (object, ref_point)
- ref_point = string.lower(ref_point)
- if ref_point == 'topleft' then
- object:setReferencePoint(display.TopLeftReferencePoint)
- elseif ref_point == 'topright' then
- object:setReferencePoint(display.TopRightReferencePoint)
- elseif ref_point == 'topcenter' then
- object:setReferencePoint(display.TopCenterReferencePoint)
- elseif ref_point == 'bottomleft' then
- object:setReferencePoint(display.BottomLeftReferencePoint)
- elseif ref_point == 'bottomright' then
- object:setReferencePoint(display.BottomRightReferencePoint)
- elseif ref_point == 'bottomcenter' then
- object:setReferencePoint(display.BottomCenterReferencePoint)
- elseif ref_point == 'centerleft' then
- object:setReferencePoint(display.CenterLeftReferencePoint)
- elseif ref_point == 'centerright' then
- object:setReferencePoint(display.CenterRightReferencePoint)
- elseif ref_point == 'center' then
- object:setReferencePoint(display.CenterReferencePoint)
- end
- end
- function _M.setFillColor (object, color)
- if type(color) == 'string' then
- color = _COLORS[color]
- end
- object:setFillColor(color[1], color[2], color[3])
- end
- function _M.setTextColor (object, color)
- if type(color) == 'string' then
- color = _COLORS[color]
- end
- local color = table.copy(color)
- if not color[4] then color[4] = 255 end
- object:setTextColor(color[1], color[2], color[3], color[4])
- end
- function _M.setStrokeColor (object, color)
- if type(color) == 'string' then
- color = _COLORS[color]
- end
- local color = table.copy(color)
- if not color[4] then color[4] = 255 end
- object:setStrokeColor(color[1], color[2], color[3], color[4])
- end
- function _M.setColor (object, color)
- if type(color) == 'string' then
- color = _COLORS[color]
- end
- local color = table.copy(color)
- if not color[4] then color[4] = 255 end
- object:setColor(color[1], color[2], color[3], color[4])
- end
- function _M.newImage(filename, params)
- params = params or {}
- local w, h = params.w or _W, params.h or _H
- local image = display.newImageRect(filename, w, h)
- if params.rp then
- _M.setRP(image, params.rp)
- end
- image.x = params.x or 0
- image.y = params.y or 0
- if params.g then
- params.g:insert(image)
- end
- return image
- end
- function _M.transition(object, params)
- params = params or {}
- params.delay = params.delay or 0
- object.alpha = 0
- local transParams = {time = 800, alpha = 1, transition = _M.ease.easeOut}
- if params.delay > 0 then
- object.isVisible = false
- transParams.onStart = function (obj) obj.isVisible = true end
- transParams.delay = params.delay * 30
- end
- transition.to(object, transParams)
- end
- function _M.alert(txt)
- if type(txt) == 'string' then
- native.showAlert(_APP_NAME, txt, {'OK'})
- end
- end
- _M.loadedSounds = {}
- function _M:loadSound (sound_type)
- if not self.loadedSounds[sound_type] then
- local filename = _AUDIO[sound_type]
- self.loadedSounds[sound_type] = audio.loadSound(filename)
- end
- return self.loadedSounds[sound_type]
- end
- local audioChannel, otherAudioChannel, currentSong, curAudio, prevAudio = 1
- audio.crossFadeBackground = function (path, force)
- if _M.music_on then
- local musicPath = _AUDIO[path]
- if currentSong == musicPath and audio.getVolume{channel = audioChannel} > 0.1 and not force then return false end
- audio.fadeOut({channel=audioChannel, time=1000})
- if audioChannel==1 then audioChannel,otherAudioChannel=2,1 else audioChannel,otherAudioChannel=1,2 end
- audio.setVolume( 0.5, {channel = audioChannel})
- curAudio = audio.loadStream( musicPath )
- audio.play(curAudio, {channel=audioChannel, loops=-1, fadein=1000})
- prevAudio = curAudio
- currentSong = musicPath
- audio.currentBackgroundChannel = audioChannel
- end
- end
- audio.reserveChannels(2)
- audio.currentBackgroundChannel = 1
- audio.playSFX = function (snd, params)
- if _M.sound_on then
- local channel
- if type(snd) == 'string' then channel=audio.play(audio.loadSound(_AUDIO[snd]), params)
- else channel=audio.play(snd, params) end
- audio.setVolume(1, {channel = channel})
- return channel
- end
- end
- return _M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement