undo function
This commit is contained in:
parent
bab3bda564
commit
8cec0ef36c
126
game.lua
126
game.lua
|
@ -68,6 +68,16 @@ function Game:step(ox,oy)
|
||||||
if self.level_data[self.floor].walls[ty][tx]==1 then
|
if self.level_data[self.floor].walls[ty][tx]==1 then
|
||||||
if self.player_item == "pickaxe" then
|
if self.player_item == "pickaxe" then
|
||||||
--push to undo memory
|
--push to undo memory
|
||||||
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"dig",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
wx = tx,
|
||||||
|
wy = ty,
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove wall
|
--remove wall
|
||||||
self.level_data[self.floor].walls[ty][tx]=0
|
self.level_data[self.floor].walls[ty][tx]=0
|
||||||
--remove item
|
--remove item
|
||||||
|
@ -84,7 +94,16 @@ function Game:step(ox,oy)
|
||||||
break
|
break
|
||||||
elseif entities[i].type == "enemy" then
|
elseif entities[i].type == "enemy" then
|
||||||
if self.player_item == "vorpal" then
|
if self.player_item == "vorpal" then
|
||||||
--push to undo memory
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"vorpal",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
ent_type = entities[i].type,
|
||||||
|
ent = entities[i],
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove entity
|
--remove entity
|
||||||
entities[i].type = nil
|
entities[i].type = nil
|
||||||
--remove item
|
--remove item
|
||||||
|
@ -94,6 +113,17 @@ function Game:step(ox,oy)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
--push to undo memory
|
--push to undo memory
|
||||||
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"fight",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
power = self.player_power,
|
||||||
|
ent_type = entities[i].type,
|
||||||
|
ent = entities[i],
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove entity
|
--remove entity
|
||||||
entities[i].type = nil
|
entities[i].type = nil
|
||||||
--absorb power
|
--absorb power
|
||||||
|
@ -103,6 +133,16 @@ function Game:step(ox,oy)
|
||||||
elseif entities[i].type == "enemy_neg" then
|
elseif entities[i].type == "enemy_neg" then
|
||||||
if self.player_item == "vorpal" then
|
if self.player_item == "vorpal" then
|
||||||
--push to undo memory
|
--push to undo memory
|
||||||
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"vorpal",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
ent_type = entities[i].type,
|
||||||
|
ent = entities[i],
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove entity
|
--remove entity
|
||||||
entities[i].type = nil
|
entities[i].type = nil
|
||||||
--remove item
|
--remove item
|
||||||
|
@ -112,6 +152,17 @@ function Game:step(ox,oy)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
--push to undo memory
|
--push to undo memory
|
||||||
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"fight",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
power = self.player_power,
|
||||||
|
ent_type = entities[i].type,
|
||||||
|
ent = entities[i],
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove entity
|
--remove entity
|
||||||
entities[i].type = nil
|
entities[i].type = nil
|
||||||
--absorb power
|
--absorb power
|
||||||
|
@ -120,6 +171,17 @@ function Game:step(ox,oy)
|
||||||
end
|
end
|
||||||
elseif entities[i].type == "elixir" then
|
elseif entities[i].type == "elixir" then
|
||||||
--push to undo memory
|
--push to undo memory
|
||||||
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"fight",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
power = self.player_power,
|
||||||
|
ent_type = entities[i].type,
|
||||||
|
ent = entities[i],
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove entity
|
--remove entity
|
||||||
entities[i].type = nil
|
entities[i].type = nil
|
||||||
--double power
|
--double power
|
||||||
|
@ -127,6 +189,17 @@ function Game:step(ox,oy)
|
||||||
break
|
break
|
||||||
elseif entities[i].type == "gate" then
|
elseif entities[i].type == "gate" then
|
||||||
--push to undo memory
|
--push to undo memory
|
||||||
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"fight",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
power = self.player_power,
|
||||||
|
ent_type = entities[i].type,
|
||||||
|
ent = entities[i],
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove entity
|
--remove entity
|
||||||
entities[i].type = nil
|
entities[i].type = nil
|
||||||
--divide power by two
|
--divide power by two
|
||||||
|
@ -138,6 +211,16 @@ function Game:step(ox,oy)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
--push to undo memory
|
--push to undo memory
|
||||||
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"item_get",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
ent_type = entities[i].type,
|
||||||
|
ent = entities[i],
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove entity
|
--remove entity
|
||||||
entities[i].type = nil
|
entities[i].type = nil
|
||||||
--equip item
|
--equip item
|
||||||
|
@ -149,6 +232,16 @@ function Game:step(ox,oy)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
--push to undo memory
|
--push to undo memory
|
||||||
|
self.undo_history[#self.undo_history+1] = {
|
||||||
|
"item_get",
|
||||||
|
{
|
||||||
|
floor = self.floor,
|
||||||
|
px = self.player_x,
|
||||||
|
py = self.player_y,
|
||||||
|
ent_type = entities[i].type,
|
||||||
|
ent = entities[i],
|
||||||
|
}
|
||||||
|
}
|
||||||
--remove entity
|
--remove entity
|
||||||
entities[i].type = nil
|
entities[i].type = nil
|
||||||
--equip item
|
--equip item
|
||||||
|
@ -179,6 +272,37 @@ function Game:undo()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
undo_item = self.undo_history[#self.undo_history]
|
||||||
|
if undo_item[1] == "vorpal" then
|
||||||
|
self.floor = undo_item[2].floor
|
||||||
|
self.player_x = undo_item[2].px
|
||||||
|
self.player_y = undo_item[2].py
|
||||||
|
undo_item[2].ent.type = undo_item[2].ent_type
|
||||||
|
self.player_item = "vorpal"
|
||||||
|
elseif undo_item[1] == "dig" then
|
||||||
|
self.floor = undo_item[2].floor
|
||||||
|
self.player_x = undo_item[2].px
|
||||||
|
self.player_y = undo_item[2].py
|
||||||
|
self.level_data[self.floor].walls[undo_item[2].wy][undo_item[2].wx]=1
|
||||||
|
self.player_item = "pickaxe"
|
||||||
|
elseif undo_item[1] == "item_get" then
|
||||||
|
self.floor = undo_item[2].floor
|
||||||
|
self.player_x = undo_item[2].px
|
||||||
|
self.player_y = undo_item[2].py
|
||||||
|
undo_item[2].ent.type = undo_item[2].ent_type
|
||||||
|
self.player_item = nil
|
||||||
|
elseif undo_item[1] == "fight" then
|
||||||
|
--works for elixirs and gates too
|
||||||
|
self.floor = undo_item[2].floor
|
||||||
|
self.player_x = undo_item[2].px
|
||||||
|
self.player_y = undo_item[2].py
|
||||||
|
self.player_power = undo_item[2].power
|
||||||
|
undo_item[2].ent.type = undo_item[2].ent_type
|
||||||
|
else
|
||||||
|
assert(false)
|
||||||
|
end
|
||||||
|
--remove the item from stack
|
||||||
|
self.undo_history[#self.undo_history] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function Game:update(dt)
|
function Game:update(dt)
|
||||||
|
|
Loading…
Reference in New Issue