-- This file is a part of Avoision. -- -- Copyright (C) 2012 Jared Krinke. -- -- Avoision is free software; you can redistribute it and/or -- modify it under the terms of the GNU General Public License -- as published by the Free Software Foundation; either version 2 -- of the License, or (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. require("Avoision/Board.lua") require("Avoision/Display.lua") local gameLayerEventHandlers = { up = function(self, pressed) if not self.board:getPaused() then self.board.player:setMovingUpState(pressed) end end, down = function(self, pressed) if not self.board:getPaused() then self.board.player:setMovingDownState(pressed) end end, left = function(self, pressed) if not self.board:getPaused() then self.board.player:setMovingLeftState(pressed) end end, right = function(self, pressed) if not self.board:getPaused() then self.board.player:setMovingRightState(pressed) end end, -- TODO: Should the game be able to be paused? -- TODO: Allow the player to quit with cancel or something } GameLayer = { } function GameLayer.new(ended) local gameLayer = Layer.new(33) gameLayer.done = false gameLayer.propagateAudio = false gameLayer.board = gameLayer:addChild(Board.new(gameLayer, function() gameLayer.display:emphasizeScore() end, function() gameLayer.done = true end)) gameLayer.display = gameLayer:addChild(Display.new(gameLayer.board)) gameLayer.eventHandlers = gameLayerEventHandlers Common.input:setupEventHandlers(gameLayer) function gameLayer.inputReceived(self, event, pressed) local handler = self.eventHandlers[event] local handled = false if self.done then -- If the game is over, allow any key to quit self:endGame() handled = true elseif isFunction(handler) then handler(self, pressed) handled = true end return handled end function gameLayer.errorOccurred(self, message) print(message) if isObject(self) and isObject(self.board) and isObject(self.board.player) and isFunction(self.board.player.clearMovingStates) then self.board.player:clearMovingStates() end end function gameLayer.setDifficulty(self, difficulty) self.board:setDifficulty(difficulty) end function gameLayer.getScore(self) return self.board:getScore() end function gameLayer.reset(self) self.done = false self.board:reset() self.display:reset() end function gameLayer.started(self) self:playMusic("Music/SpelunkingForPleasure.ogg") end function gameLayer.clearInput(self) self.board.player:clearMovingStates() end function gameLayer.start(self) Layers.push(self) end function gameLayer.endGame(self) local board = self.board local difficulty = board:getDifficulty() local score = board:getScore() self:reset() self.done = true Audio.clearAudio() Layers.pop() ended(difficulty, score) end return gameLayer end