undo function
This commit is contained in:
parent
bab3bda564
commit
8cec0ef36c
128
game.lua
128
game.lua
|
@ -68,6 +68,16 @@ function Game:step(ox,oy)
|
|||
if self.level_data[self.floor].walls[ty][tx]==1 then
|
||||
if self.player_item == "pickaxe" then
|
||||
--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
|
||||
self.level_data[self.floor].walls[ty][tx]=0
|
||||
--remove item
|
||||
|
@ -84,7 +94,16 @@ function Game:step(ox,oy)
|
|||
break
|
||||
elseif entities[i].type == "enemy" 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
|
||||
entities[i].type = nil
|
||||
--remove item
|
||||
|
@ -94,6 +113,17 @@ function Game:step(ox,oy)
|
|||
return
|
||||
else
|
||||
--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
|
||||
entities[i].type = nil
|
||||
--absorb power
|
||||
|
@ -103,6 +133,16 @@ function Game:step(ox,oy)
|
|||
elseif entities[i].type == "enemy_neg" 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
|
||||
entities[i].type = nil
|
||||
--remove item
|
||||
|
@ -112,6 +152,17 @@ function Game:step(ox,oy)
|
|||
return
|
||||
else
|
||||
--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
|
||||
entities[i].type = nil
|
||||
--absorb power
|
||||
|
@ -120,6 +171,17 @@ function Game:step(ox,oy)
|
|||
end
|
||||
elseif entities[i].type == "elixir" then
|
||||
--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
|
||||
entities[i].type = nil
|
||||
--double power
|
||||
|
@ -127,6 +189,17 @@ function Game:step(ox,oy)
|
|||
break
|
||||
elseif entities[i].type == "gate" then
|
||||
--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
|
||||
entities[i].type = nil
|
||||
--divide power by two
|
||||
|
@ -138,6 +211,16 @@ function Game:step(ox,oy)
|
|||
return
|
||||
end
|
||||
--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
|
||||
entities[i].type = nil
|
||||
--equip item
|
||||
|
@ -149,6 +232,16 @@ function Game:step(ox,oy)
|
|||
return
|
||||
end
|
||||
--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
|
||||
entities[i].type = nil
|
||||
--equip item
|
||||
|
@ -178,7 +271,38 @@ function Game:undo()
|
|||
if #self.undo_history == 0 then
|
||||
return
|
||||
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
|
||||
|
||||
function Game:update(dt)
|
||||
|
|
Loading…
Reference in New Issue