---------------------------------------------------------------- -- UFO Dad Prototype -- Design by Kevin Oke -- Code by Karl Schmidt ---------------------------------------------------------------- dofile("MOAIPSM.lua") dofile("tunables.lua") function table_print (tt, indent, done) done = done or {} indent = indent or 0 if type(tt) == "table" then local sb = {} for key, value in pairs (tt) do table.insert(sb, string.rep (" ", indent)) -- indent it if type (value) == "table" and not done [value] then done [value] = true table.insert(sb, "{\n"); table.insert(sb, table_print (value, indent + 2, done)) table.insert(sb, string.rep (" ", indent)) -- indent it table.insert(sb, "}\n"); elseif "number" == type(key) then table.insert(sb, string.format("\"%s\"\n", tostring(value))) else table.insert(sb, string.format( "%s = \"%s\"\n", tostring (key), tostring(value))) end end return table.concat(sb) else return tt .. "\n" end end function setupScreenWithTitle( title, basePriority, showBackButton, showConfirmButton, confirmButtonText ) if basePriority == nil then basePriority = 0 end if showBackButton == nil then showBackButton = true end if showConfirmButton == nil then showConfirmButton = true end if confirmButtonText == nil then confirmButtonText = "CONFIRM" end if showConfirmButton then local mmConfirmGfx = MOAIGfxQuad2D.new() mmConfirmGfx:setTexture( UI_CONFIRM_IMAGE ) mmConfirmGfx:setRect( 0, 0, 24, 24 ) mmConfirmGfx:setUVRect ( 0, 0, 1, 1 ) local mmConfirm = MOAIProp2D.new() mmConfirm:setDeck( mmConfirmGfx ) mmConfirm:setPriority( basePriority + 2 ) layer:insertProp( mmConfirm ) mmConfirm:setLoc( 548, 512 ) end if showBackButton then local mmBackGfx = MOAIGfxQuad2D.new() mmBackGfx:setTexture( UI_BACK_IMAGE ) mmBackGfx:setRect( 0, 0, 24, 24 ) mmBackGfx:setUVRect ( 0, 0, 1, 1 ) local mmBack = MOAIProp2D.new() mmBack:setDeck( mmBackGfx ) mmBack:setPriority( basePriority + 2 ) layer:insertProp( mmBack ) if showConfirmButton then mmBack:setLoc( 672, 512 ) else mmBack:setLoc( 869, 512 ) end end local mmTitle = MOAITextBox.new () mmTitle:setFont ( font ) mmTitle:setRect ( 0, 0, 350, 40 ) mmTitle:setLoc( 305, 0 ) mmTitle:setAlignment ( MOAITextBox.CENTER_JUSTIFY ) layer:insertProp ( mmTitle ) mmTitle:setColor ( 0.91, 0.96, 0.96, 1 ) mmTitle:setString( title ) mmTitle:setPriority( basePriority + 3 ) if showConfirmButton then local mmConfirm = MOAITextBox.new () mmConfirm:setFont( smallFont ) mmConfirm:setRect( 0, 0, 240, 40 ) mmConfirm:setLoc( 575, 510 ) mmConfirm:setAlignment( MOAITextBox.LEFT_JUSTIFY ) layer:insertProp( mmConfirm ) mmConfirm:setColor( 0.51, 0.9, 1, 1 ) mmConfirm:setString( confirmButtonText ) mmConfirm:setPriority( 3 + basePriority ) end if showBackButton then local mmBack = MOAITextBox.new () mmBack:setFont( smallFont ) mmBack:setRect( 0, 0, 240, 40 ) if showConfirmButton then mmBack:setLoc( 700, 510 ) else mmBack:setLoc( 869 + 32, 510 ) end mmBack:setAlignment( MOAITextBox.LEFT_JUSTIFY ) layer:insertProp( mmBack ) mmBack:setColor( 0.51, 0.9, 1, 1 ) mmBack:setString( "BACK" ) mmBack:setPriority( 3 + basePriority ) end return mmTitle end GAME_STATE = GAME_STATE_SPLASH -- Initialization stuff MOAISim.openWindow ( WINDOW_TITLE, SCREEN_WIDTH, SCREEN_HEIGHT ) viewport = MOAIViewport.new () viewport:setSize ( SCREEN_WIDTH, SCREEN_HEIGHT ) viewport:setScale ( SCREEN_WIDTH, -SCREEN_HEIGHT ) viewport:setOffset( -1, 1 ) backgroundColour = MOAIColor.new () backgroundColour:setColor ( 0.11, 0.09, 0.15, 1 ) --backgroundColour:seekColor ( 0, 1, 0, 1, 1.5 ) -- this can animate the transition layer = MOAILayer2D.new () layer:setViewport ( viewport ) layer:setClearColor ( backgroundColour ) MOAISim.pushRenderPass ( layer ) local characterCodes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.?!-:+/\"\'" local fontFileName = "Russo_One.ttf" font = MOAIFont.new () font:loadFromTTF( fontFileName, characterCodes, 28 ) smallFont = MOAIFont.new () smallFont:loadFromTTF( fontFileName, characterCodes, 20 ) bigFont = MOAIFont.new() bigFont:loadFromTTF( fontFileName, "0123456789", 40 ) inputCallback = nil CURRENT_CHARACTER = nil CURRENT_LEVEL = 0 CURRENT_MAX_LEVEL = 0 CURRENT_LEADERBOARD_DATA = { } CURRENT_MUSIC_ON = true CURRENT_SFX_ON = true CURRENT_CONTROL_CFG = 0 CURRENT_MAX_POINTS = 0 CURRENT_MAX_MATCH = 0 local settingsFile = io.open( SETTINGS_FILEPATH, 'r' ) if settingsFile ~= nil then local maxLevelLine = io.read( settingsFile, "*l" ) if maxLevelLine ~= nil then local maxLevelText = string.sub( maxLevelLine, string.len('maxLevel=') + 1, string.len(maxLevelLine) ) if maxLevelText ~= nil and tonumber( maxLevelText ) ~= nil then CURRENT_MAX_LEVEL = math.ceil( tonumber( maxLevelText ) ) end end local musicOnLine = io.read( settingsFile, "*l" ) if musicOnLine ~= nil then local musicOnText = string.sub( musicOnLine, string.len('musicOn=') + 1, string.len(musicOnLine) ) if musicOnText ~= nil and tonumber( musicOnText ) ~= nil then CURRENT_MUSIC_ON = math.ceil( tonumber( musicOnText ) ) == 1 end end local sfxOnLine = io.read( settingsFile, "*l" ) if sfxOnLine ~= nil then local sfxOnText = string.sub( sfxOnLine, string.len('sfxOn=') + 1, string.len(sfxOnLine) ) if sfxOnText ~= nil and tonumber( sfxOnText ) ~= nil then CURRENT_SFX_ON = math.ceil( tonumber( sfxOnText ) ) == 1 end end local controlCfgLine = io.read( settingsFile, "*l" ) if controlCfgLine ~= nil then local controlCfgText = string.sub( controlCfgLine, string.len('controlCfg=') + 1, string.len(controlCfgLine) ) if controlCfgText ~= nil and tonumber( controlCfgText ) ~= nil then CURRENT_CONTROL_CFG = math.ceil( tonumber( controlCfgText ) ) end end local maxPointsLine = io.read( settingsFile, "*l" ) if maxPointsLine ~= nil then local maxPointsText = string.sub( maxPointsLine, string.len('maxPoints=') + 1, string.len(maxPointsLine) ) if maxPointsText ~= nil and tonumber( maxPointsText ) ~= nil then CURRENT_MAX_POINTS = math.ceil( tonumber( maxPointsText ) ) end end local maxMatchLine = io.read( settingsFile, "*l" ) if maxMatchLine ~= nil then local maxMatchText = string.sub( maxMatchLine, string.len('maxMatch=') + 1, string.len(maxMatchLine) ) if maxMatchText ~= nil and tonumber( maxMatchText ) ~= nil then CURRENT_MAX_MATCH = math.ceil( tonumber( maxMatchText ) ) end end io.close( settingsFile ) end function writeSettings() local settingsFile = io.open( SETTINGS_FILEPATH, 'w' ) io.write( settingsFile, 'maxLevel=' .. tostring(CURRENT_MAX_LEVEL) .. '\n' ) local currentMusicOn = '0' if CURRENT_MUSIC_ON then currentMusicOn = '1' end io.write( settingsFile, 'musicOn=' .. currentMusicOn .. '\n' ) local currentSFXOn = '0' if CURRENT_SFX_ON then currentSFXOn = '1' end io.write( settingsFile, 'sfxOn=' .. currentSFXOn .. '\n' ) io.write( settingsFile, 'controlCfg=' .. tostring(CURRENT_CONTROL_CFG) .. '\n' ) io.write( settingsFile, 'maxPoints=' .. tostring(CURRENT_MAX_POINTS) .. '\n' ) io.write( settingsFile, 'maxMatch=' .. tostring(CURRENT_MAX_MATCH) .. '\n' ) io.close( settingsFile ) end local leaderboardFile = io.open( LEADERBOARD_FILEPATH, 'r' ) if leaderboardFile ~= nil then local currentLine = io.read( leaderboardFile, "*l" ) while currentLine ~= nil do local lineData = string.gmatch(currentLine, "([^,]+),?") table.insert( CURRENT_LEADERBOARD_DATA, { lineData(0), lineData(1) } ) currentLine = io.read( leaderboardFile, "*l" ) end io.close( leaderboardFile ) end -- TODO: Keep track of locked/unlocked before each play session -- then after you are done, check those still locked. For any unlocked, -- play game over queue -- TODO: Add tracking to support other unlock conditions (points, max glom size) function isCharacterLocked( character ) local characterLocked = false local unlockCondition = character["UNLOCK_CONDITION"] if unlockCondition ~= nil then if unlockCondition["TYPE"] == "LEVEL" then if (CURRENT_MAX_LEVEL+1) < unlockCondition["VALUE"] then characterLocked = true end elseif unlockCondition["TYPE"] == "POINTS" then if CURRENT_MAX_POINTS < unlockCondition["VALUE"] then characterLocked = true end elseif unlockCondition["TYPE"] == "MATCH" then if CURRENT_MAX_MATCH < unlockCondition["VALUE"] then characterLocked = true end end end return characterLocked end print("Loading mainmenu") local curTime = MOAISim.getCurrentTime() dofile("mainmenu.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() print("Loading gameplay") dofile("gameplay.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() print("Loading characterSelect") dofile("characterSelect.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() print("Loading leaderboards") dofile("leaderboards.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() print("Loading options") dofile("options.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() print("Loading credits") dofile("credits.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() print("Loading splash") dofile("splash.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() print("Loading intro") dofile("intro.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() print("Loading tutorial") dofile("tutorial.lua") print( MOAISim.getCurrentTime() - curTime ) curTime = MOAISim.getCurrentTime() function changeGameState( newState ) if GAME_STATE == GAME_STATE_SPLASH then splashTeardown() elseif GAME_STATE == GAME_STATE_MENU then if newState ~= GAME_STATE_LEADERBOARDS and newState ~= GAME_STATE_OPTIONS then mainMenuTeardown() else mainMenuTeardownText() end if newState == GAME_STATE_SPLASH then splashFromMainMenu() end elseif GAME_STATE == GAME_STATE_GAME then gameplayTeardown() elseif GAME_STATE == GAME_STATE_CHARACTER_SELECT then characterSelectTeardown() elseif GAME_STATE == GAME_STATE_LEADERBOARDS then leaderboardsTeardown() elseif GAME_STATE == GAME_STATE_OPTIONS then optionsTeardown() elseif GAME_STATE == GAME_STATE_CREDITS then creditsTeardown() elseif GAME_STATE == GAME_STATE_INTRO then introTeardown() if newState == GAME_STATE_SPLASH then splashFromIntro() end elseif GAME_STATE == GAME_STATE_TUTORIAL then tutorialTeardown() else print("UNSUPPORTED STATE TRANSITION FROM " .. GAME_STATE) end GAME_STATE = newState if newState == GAME_STATE_SPLASH then splashSetup() elseif newState == GAME_STATE_MENU then mainMenuSetup() elseif newState == GAME_STATE_GAME then gameplaySetup() elseif newState == GAME_STATE_CHARACTER_SELECT then characterSelectSetup() elseif newState == GAME_STATE_LEADERBOARDS then leaderboardsSetup() elseif newState == GAME_STATE_OPTIONS then optionsSetup() elseif newState == GAME_STATE_CREDITS then creditsSetup() elseif newState == GAME_STATE_INTRO then introSetup() elseif newState == GAME_STATE_TUTORIAL then tutorialSetup() else print("UNSUPPORT STATE TRANSITION TO " .. newState) end end -- Asset loading (for now, until we data-drive this) print("Loading audio") curTime = MOAISim.getCurrentTime() MOAIUntzSystem.initialize( 44100, 1024 ) MOAIUntzSystem.setVolume(0.5) backgroundMusic = MOAIUntzSound.new() if MOAIEnvironment.osBrand == "PSM" then backgroundMusic:setBackground( true ) end backgroundMusic:load("audio/mus_main.mp3") backgroundMusic:setLooping( true ) if CURRENT_MUSIC_ON then backgroundMusic:setVolume( MUSIC_VOLUME ) else backgroundMusic:setVolume( 0 ) end SFX = { } for k, v in pairs(SFX_FILES) do soundObj = MOAIUntzSound.new() soundObj:load("audio/" .. v ) soundObj:setLooping( false ) SFX[k] = soundObj if not CURRENT_SFX_ON then soundObj:setVolume(0) end end SFX["SPATULA_AIRTIME"] = { SFX["SPATULA_AIRTIME_1"], SFX["SPATULA_AIRTIME_2"], SFX["SPATULA_AIRTIME_3"] } print("Done audio") print( MOAISim.getCurrentTime() - curTime ) function startGameCallback() changeGameState( GAME_STATE_INTRO ) end -- Go to the main menu -- Show the following options: -- Leaderboard -- Start Game -- Credits -- Options -- Navigate active menu option with buttons -- each screen needs a setup and teardown function onKeyDown( key, isDown ) inputCallback( key, isDown ) end -- Input initialization MOAIInputMgr.device.keyboard:setCallback( onKeyDown ) if MOAIEnvironment.osBrand == "PSM" then return { callInputCallbacks = callInputCallbacks, runThreadsCallback = runThreadsCallback, runTimersCallback = runTimersCallback, runAnimationsCallback = runAnimationsCallback, startGameCallback = startGameCallback, } end