fix(cbs): make sure that ennemies' destruction don't freeze the game

This commit is contained in:
Kazhnuz 2019-08-15 19:00:01 +02:00
parent 7f214577a4
commit b3428da090
3 changed files with 30 additions and 4 deletions

View file

@ -10,6 +10,11 @@ function Battler:new(world, x, y, z)
self.debugActiveTimer = 0
end
function Battler:destroy()
Battler.super.destroy(self)
self.world:destroyBattler(self)
end
function Battler:setActive()
core.debug:print("cbs/actor","actor " .. self.id .. " is active")
self.isActive = true
@ -19,11 +24,10 @@ end
function Battler:update(dt)
if (self.isActive) then
self.debugActiveTimer = self.debugActiveTimer + dt
core.debug:print("cbs/battler", "debug timer is " .. math.floor(self.debugActiveTimer*100) .. " for battler " .. self.id)
if self.debugActiveTimer >= 0.5 then
core.debug:print("cbs/battler", "counter ended, switching active battler")
self.world:switchActiveBattler()
self.isActive = false
self.world:switchActiveBattler()
end
end
end

View file

@ -34,7 +34,7 @@ end
function Parent:destroy()
self.world:destroyActor(self)
self.destroyed = true
self.isDestroyed = true
end
function Parent:update(dt)

View file

@ -116,6 +116,22 @@ function World:addEnnemy(x, y, id)
table.insert(self.battlers, enn)
end
function World:destroyBattler(actorToDestroy)
-- remove the actor from the battler liste
for i, actor in ipairs(self.battlers) do
if actor == actorToDestroy then
table.remove(self.battlers, i)
end
end
-- Also remove all actions related to the actor
for i, action in ipairs(self.actionlist) do
if action.actor == actorToDestroy then
table.remove(self.actionlist, i)
end
end
end
function World:generateActionList()
self.actionlist = {}
@ -197,15 +213,21 @@ end
function World:switchActiveBattler()
if (self.turns.isFinished) or (self.turns.current >= #self.actionlist) then
core.debug:print("cbs/world", "turn finished")
self.turns.current = 1
self.turns.isFinished = false
self.turns.number = self.turns.number + 1
self:recalculateTurns()
else
self.turns.current = self.turns.current + 1
core.debug:print("cbs/world", "switching to action number " .. self.turns.current)
end
if (self.actionlist[self.turns.current].actor.isDestroyed == true) then
self:switchActiveBattler()
else
self.actionlist[self.turns.current].actor:setActive()
end
self.turns.changeBattler = false
end