aboutsummaryrefslogtreecommitdiff
path: root/main.lua
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-12-30 15:26:58 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-12-30 15:26:58 +0000
commitcadf402db8462a013f77a051914c9b953cf97d71 (patch)
tree7361a7c1945043097326f0b8da5d191c5ed9e4e1 /main.lua
parent2307c68184744567d1c2d50bbbcd25a8dd2d0d02 (diff)
downloadstorage-chaos-cadf402db8462a013f77a051914c9b953cf97d71.tar.gz
storage-chaos-cadf402db8462a013f77a051914c9b953cf97d71.zip
Full undo (rewind)
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua20
1 files changed, 8 insertions, 12 deletions
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)