cambridge/scene/game.lua

90 lines
2.2 KiB
Lua
Raw Normal View History

2019-05-22 22:57:34 -05:00
local GameScene = Scene:extend()
GameScene.title = "Game"
2019-05-22 22:57:34 -05:00
require 'load.save'
function GameScene:new(game_mode, ruleset, inputs)
2020-11-08 15:19:01 -06:00
self.retry_mode = game_mode
self.retry_ruleset = ruleset
self.secret_inputs = inputs
self.game = game_mode(self.secret_inputs, config.gamemodesettings[game_mode.hash])
self.ruleset = ruleset(self.game)
self.game:initialize(self.ruleset)
2020-11-08 15:19:01 -06:00
self.inputs = {
left=false,
right=false,
up=false,
down=false,
rotate_left=false,
rotate_left2=false,
rotate_right=false,
rotate_right2=false,
rotate_180=false,
hold=false,
}
2020-12-20 14:08:53 -06:00
self.paused = false
DiscordRPC:update({
details = self.game.rpc_details,
state = self.game.name,
})
2019-05-22 22:57:34 -05:00
end
function GameScene:update()
2020-12-20 14:08:53 -06:00
if love.window.hasFocus() and not self.paused then
2020-11-08 15:19:01 -06:00
local inputs = {}
for input, value in pairs(self.inputs) do
inputs[input] = value
end
self.game:update(inputs, self.ruleset)
2020-12-21 15:20:25 -06:00
self.game.grid:update()
2019-05-22 22:57:34 -05:00
end
end
function GameScene:render()
self.game:draw(self.paused)
2019-05-22 22:57:34 -05:00
end
function GameScene:onInputPress(e)
if (
self.game.game_over or self.game.completed
) and (
e.input == "menu_decide" or
e.input == "menu_back" or
e.input == "retry"
) then
2019-05-22 22:57:34 -05:00
highscore_entry = self.game:getHighscoreData()
highscore_hash = self.game.hash .. "-" .. self.ruleset.hash
submitHighscore(highscore_hash, highscore_entry)
self.game:onExit()
scene = e.input == "retry" and GameScene(self.retry_mode, self.retry_ruleset, self.secret_inputs) or ModeSelectScene()
2020-11-08 15:19:01 -06:00
elseif e.input == "retry" then
switchBGM(nil)
self.game:onExit()
scene = GameScene(self.retry_mode, self.retry_ruleset, self.secret_inputs)
2020-12-20 14:08:53 -06:00
elseif e.input == "pause" and not (self.game.game_over or self.game.completed) then
self.paused = not self.paused
2020-12-20 15:55:34 -06:00
if self.paused then pauseBGM()
else resumeBGM() end
2020-11-08 15:19:01 -06:00
elseif e.input == "menu_back" then
2021-02-19 14:58:00 -06:00
self.game:onExit()
scene = ModeSelectScene()
2020-11-08 15:19:01 -06:00
elseif e.input and string.sub(e.input, 1, 5) ~= "menu_" then
self.inputs[e.input] = true
end
end
function GameScene:onInputRelease(e)
2020-11-08 15:19:01 -06:00
if e.input and string.sub(e.input, 1, 5) ~= "menu_" then
self.inputs[e.input] = false
2019-05-22 22:57:34 -05:00
end
end
function submitHighscore(hash, data)
if not highscores[hash] then highscores[hash] = {} end
table.insert(highscores[hash], data)
saveHighscores()
end
return GameScene