From b3428da090164a422729bfee2d82e29ff81a6711 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 15 Aug 2019 19:00:01 +0200 Subject: [PATCH] fix(cbs): make sure that ennemies' destruction don't freeze the game --- .../scenes/battlesystem/actors/battler.lua | 8 +++++-- .../scenes/battlesystem/actors/parent.lua | 2 +- .../scenes/battlesystem/world.lua | 24 ++++++++++++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/sonic-radiance.love/scenes/battlesystem/actors/battler.lua b/sonic-radiance.love/scenes/battlesystem/actors/battler.lua index 0ab2a85..fb170a1 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/battler.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/battler.lua @@ -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 diff --git a/sonic-radiance.love/scenes/battlesystem/actors/parent.lua b/sonic-radiance.love/scenes/battlesystem/actors/parent.lua index 553bcb8..900ad02 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/parent.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/parent.lua @@ -34,7 +34,7 @@ end function Parent:destroy() self.world:destroyActor(self) - self.destroyed = true + self.isDestroyed = true end function Parent:update(dt) diff --git a/sonic-radiance.love/scenes/battlesystem/world.lua b/sonic-radiance.love/scenes/battlesystem/world.lua index 69f38f3..80f274c 100644 --- a/sonic-radiance.love/scenes/battlesystem/world.lua +++ b/sonic-radiance.love/scenes/battlesystem/world.lua @@ -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 - self.actionlist[self.turns.current].actor:setActive() + 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