1-- This file is a part of Avoision. 2-- 3-- Copyright (C) 2012 Jared Krinke. 4-- 5-- Avoision is free software; you can redistribute it and/or 6-- modify it under the terms of the GNU General Public License 7-- as published by the Free Software Foundation; either version 2 8-- of the License, or (at your option) any later version. 9-- 10-- This program is distributed in the hope that it will be useful, 11-- but WITHOUT ANY WARRANTY; without even the implied warranty of 12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13-- GNU General Public License for more details. 14-- 15-- You should have received a copy of the GNU General Public License 16-- along with this program; if not, write to the Free Software 17-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 19require("Avoision/Board.lua") 20require("Avoision/Display.lua") 21 22local gameLayerEventHandlers = { 23 up = function(self, pressed) 24 if not self.board:getPaused() then 25 self.board.player:setMovingUpState(pressed) 26 end 27 end, 28 29 down = function(self, pressed) 30 if not self.board:getPaused() then 31 self.board.player:setMovingDownState(pressed) 32 end 33 end, 34 35 left = function(self, pressed) 36 if not self.board:getPaused() then 37 self.board.player:setMovingLeftState(pressed) 38 end 39 end, 40 41 right = function(self, pressed) 42 if not self.board:getPaused() then 43 self.board.player:setMovingRightState(pressed) 44 end 45 end, 46 47 -- TODO: Should the game be able to be paused? 48 -- TODO: Allow the player to quit with cancel or something 49} 50 51GameLayer = { } 52 53function GameLayer.new(ended) 54 local gameLayer = Layer.new(33) 55 56 gameLayer.done = false 57 gameLayer.propagateAudio = false 58 gameLayer.board = gameLayer:addChild(Board.new(gameLayer, 59 function() 60 gameLayer.display:emphasizeScore() 61 end, function() 62 gameLayer.done = true 63 end)) 64 65 gameLayer.display = gameLayer:addChild(Display.new(gameLayer.board)) 66 gameLayer.eventHandlers = gameLayerEventHandlers 67 Common.input:setupEventHandlers(gameLayer) 68 69 function gameLayer.inputReceived(self, event, pressed) 70 local handler = self.eventHandlers[event] 71 local handled = false 72 73 if self.done then 74 -- If the game is over, allow any key to quit 75 self:endGame() 76 handled = true 77 elseif isFunction(handler) then 78 handler(self, pressed) 79 handled = true 80 end 81 82 return handled 83 end 84 85 function gameLayer.errorOccurred(self, message) 86 print(message) 87 88 if isObject(self) and isObject(self.board) and isObject(self.board.player) and isFunction(self.board.player.clearMovingStates) then 89 self.board.player:clearMovingStates() 90 end 91 end 92 93 function gameLayer.setDifficulty(self, difficulty) 94 self.board:setDifficulty(difficulty) 95 end 96 97 function gameLayer.getScore(self) 98 return self.board:getScore() 99 end 100 101 function gameLayer.reset(self) 102 self.done = false 103 self.board:reset() 104 self.display:reset() 105 end 106 107 function gameLayer.started(self) 108 self:playMusic("Music/SpelunkingForPleasure.ogg") 109 end 110 111 function gameLayer.clearInput(self) 112 self.board.player:clearMovingStates() 113 end 114 115 function gameLayer.start(self) 116 Layers.push(self) 117 end 118 119 function gameLayer.endGame(self) 120 local board = self.board 121 local difficulty = board:getDifficulty() 122 local score = board:getScore() 123 124 self:reset() 125 self.done = true 126 Audio.clearAudio() 127 Layers.pop() 128 ended(difficulty, score) 129 end 130 131 return gameLayer 132end 133 134