From cadf402db8462a013f77a051914c9b953cf97d71 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sat, 30 Dec 2023 15:26:58 +0000 Subject: Full undo (rewind) --- main.lua | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index c3d1199..3391780 100644 --- a/main.lua +++ b/main.lua @@ -66,9 +66,7 @@ end -- set current level based on the index in the levels table function set_level(number) - level_undo = {} - level_undo_x = 0 - level_undo_y = 0 + state_undo = {} level = {} for y, row in ipairs(levels[number]) do @@ -154,9 +152,7 @@ function move(target_x, target_y) moves = moves + 1 -- save the previous move for UNDO - level_undo = undo - level_undo_x = undo_x - level_undo_y = undo_y + table.insert(state_undo, 1, {undo, undo_x, undo_y}) end -- keypressed handler when the level is compplete @@ -180,12 +176,12 @@ function keypressed_play(key) -- reset level set_level(current_level) return - elseif key == "u" and #level_undo > 0 then + elseif key == "u" and #state_undo > 0 then -- undo move - level = level_undo - player_x = level_undo_x - player_y = level_undo_y - level_undo = {} + local state = table.remove(state_undo, 1) + level = state[1] + player_x = state[2] + player_y = state[3] moves = moves - 1 end @@ -252,7 +248,7 @@ function love.draw() love.graphics.print("R:restart", 20, 205 * game.scale) -- change colour if there's not undo - if #level_undo == 0 then + if #state_undo == 0 then love.graphics.setColor(0.5, 0.3, 0.3) end love.graphics.print("U:undo", 20 + 10 * 8 * game.scale, 205 * game.scale) -- cgit v1.2.3