Added replay saving.

pull/48/head
BoatsandJoes 2021-12-04 20:35:15 -06:00
parent 9e59c158b2
commit 6c4551ebef
1 changed files with 33 additions and 0 deletions

View File

@ -7,6 +7,7 @@ local playedGoSE = false
local Grid = require 'tetris.components.grid' local Grid = require 'tetris.components.grid'
local Randomizer = require 'tetris.randomizers.randomizer' local Randomizer = require 'tetris.randomizers.randomizer'
local BagRandomizer = require 'tetris.randomizers.bag' local BagRandomizer = require 'tetris.randomizers.bag'
local binser = require 'libs.binser'
local GameMode = Object:extend() local GameMode = Object:extend()
@ -72,6 +73,8 @@ function GameMode:new(secret_inputs)
self.section_start_time = 0 self.section_start_time = 0
self.section_times = { [0] = 0 } self.section_times = { [0] = 0 }
self.secondary_section_times = { [0] = 0 } self.secondary_section_times = { [0] = 0 }
self.replay_inputs = {}
self.replay_pieces = {}
end end
function GameMode:getARR() return 1 end function GameMode:getARR() return 1 end
@ -86,6 +89,7 @@ function GameMode:getGravity() return 1/64 end
function GameMode:getNextPiece(ruleset) function GameMode:getNextPiece(ruleset)
local shape = self.used_randomizer:nextPiece() local shape = self.used_randomizer:nextPiece()
table.insert(self.replay_pieces,shape)
return { return {
skin = self:getSkin(), skin = self:getSkin(),
shape = shape, shape = shape,
@ -98,6 +102,10 @@ function GameMode:getSkin()
end end
function GameMode:initialize(ruleset) function GameMode:initialize(ruleset)
local dummy_entry = {}
dummy_entry["inputs"] = {}
dummy_entry["frames"] = 0
table.insert(self.replay_inputs, dummy_entry)
-- generate next queue -- generate next queue
self.used_randomizer = ( self.used_randomizer = (
table.equalvalues( table.equalvalues(
@ -129,6 +137,19 @@ function GameMode:update(inputs, ruleset)
end end
end end
-- check if inputs have changed since last frame
local last_input_index = table.maxn(self.replay_inputs)
if self.replay_inputs[last_input_index]["inputs"] ~= inputs then
-- insert new inputs into replay inputs table
local new_inputs = {}
new_inputs["inputs"] = inputs
new_inputs["frames"] = 0
table.insert(self.replay_inputs,new_inputs)
else
-- add 1 to input frame counter
self.replay_inputs[last_input_index]["frames"] = self.replay_inputs[last_input_index]["frames"] + 1
end
-- advance one frame -- advance one frame
if self:advanceOneFrame(inputs, ruleset) == false then return end if self:advanceOneFrame(inputs, ruleset) == false then return end
@ -342,6 +363,18 @@ function GameMode:onGameOver()
elseif self.game_over_frames < 2 * animation_length then elseif self.game_over_frames < 2 * animation_length then
-- Keep field hidden for a short time, then pop it back in (for screenshots). -- Keep field hidden for a short time, then pop it back in (for screenshots).
alpha = 1 alpha = 1
elseif self.game_over_frames == 2 * animation_length then
-- Save replay.
local replay = {}
replay["inputs"] = self.replay_inputs
replay["pieces"] = self.replay_pieces
replay["mode"] = self.name
replay["ruleset"] = self.ruleset.name
if love.filesystem.getInfo("replays") == nil then
love.filesystem.createDirectory("replays")
end
local replay_number = table.getn(love.filesystem.getDirectoryItems("replays")) + 1
love.filesystem.write("replays/"..replay_number..".lua", binser.serialize(replay))
end end
love.graphics.setColor(0, 0, 0, alpha) love.graphics.setColor(0, 0, 0, alpha)
love.graphics.rectangle( love.graphics.rectangle(