From 45c58bcbcd2c98a1b027f423cb5c4624a11f003f Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 15 Aug 2019 15:06:13 +0200 Subject: [PATCH] feat(cbs): initial implementation of damage system --- .../scenes/battlesystem/actors/battler.lua | 33 +++++++++++++++++++ .../scenes/battlesystem/actors/ennemy.lua | 11 +++++++ .../scenes/battlesystem/actors/hero.lua | 15 +++++++++ .../scenes/battlesystem/actors/parent.lua | 6 ++++ .../scenes/battlesystem/world.lua | 8 +++++ 5 files changed, 73 insertions(+) diff --git a/sonic-radiance.love/scenes/battlesystem/actors/battler.lua b/sonic-radiance.love/scenes/battlesystem/actors/battler.lua index 50bd9d3..0ab2a85 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/battler.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/battler.lua @@ -32,4 +32,37 @@ function Battler:validateAction() end +function Battler:sendDamage(x, y, value, accuracy, isSpecial, isAerial) + local stats = self:getStats() + local value = value / 10 + + if (isSpecial) then + value = value * stats.power + else + value = value * stats.attack + end + core.debug:print("battler", "sending " .. value .." damage at position <" .. x .. ";" .. y .. ">") + + local fromEnnemy = self.isEnnemy + + local other = self.world:getActorInCase(x, y) + if (other ~= nil) then + core.debug:print("battler", "sending damage to actor at position <" .. x .. ";" .. y .. ">") + other:receiveDamage(value, accuracy, isSpecial, isAerial, fromWho) + end +end + +function Battler:receiveDamage(value, accuracy, isSpecial, isAerial) + local stats = self:getStats() + if (fromEnnemy ~= self.isEnnemy) then + if (isSpecial) then + value = value / stats.mind + else + value = value / stats.defense + end + core.debug:print("battler", "taken " .. value .. " damage" ) + self:setHP(value * -1, true) + end +end + return Battler diff --git a/sonic-radiance.love/scenes/battlesystem/actors/ennemy.lua b/sonic-radiance.love/scenes/battlesystem/actors/ennemy.lua index 32d6a2e..987227f 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/ennemy.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/ennemy.lua @@ -43,6 +43,17 @@ function Ennemy:receiveDatas() self.data = game.ennemies:getEnnemyData(self.ennid) end +function Ennemy:setHP(value, relative) + if (relative) then + value = self.hp + value + end + + self.hp = value + if (self.hp <= 0) then + self:destroy() + end +end + function Ennemy:getStats() return self.data.stats end diff --git a/sonic-radiance.love/scenes/battlesystem/actors/hero.lua b/sonic-radiance.love/scenes/battlesystem/actors/hero.lua index f685162..f6bfdff 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/hero.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/hero.lua @@ -186,16 +186,31 @@ end function Hero:animationEnded(animation) if (animation == "hit1") then + self:sendDamage(self.x + self.direction, self.y, 33, 100, false, false) self:changeAnimation("hit2") elseif (animation == "hit2") then + self:sendDamage(self.x + self.direction, self.y, 33, 100, false, false) self:changeAnimation("hit3") elseif (animation == "hit3") then + self:sendDamage(self.x + self.direction, self.y, 33, 100, false, false) core.debug:print("cbs/hero", "normal combo finished, switching actor") self:changeAnimation("idle") self.world:switchActiveBattler( ) end end +-- STATS FUNCTIONS +-- Handle character stats and stuff + +function Hero:setHP(value, relative) + if (relative) then + local hp = game.characters.list[self.charid].stats.hp + value = hp + value + end + + game.characters.list[self.charid].stats.hp = value +end + -- DRAW FUNCTIONS -- Draw everything related to the hero diff --git a/sonic-radiance.love/scenes/battlesystem/actors/parent.lua b/sonic-radiance.love/scenes/battlesystem/actors/parent.lua index 6ef5024..553bcb8 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/parent.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/parent.lua @@ -23,6 +23,7 @@ function Parent:new(world, x, y, z) self.isHero = false self.isActor = false self.isEnnemy = false + self.isDestroyed = false self:register() end @@ -31,6 +32,11 @@ function Parent:register() self.world:registerActor(self) end +function Parent:destroy() + self.world:destroyActor(self) + self.destroyed = true +end + function Parent:update(dt) self:updateSprite(dt) end diff --git a/sonic-radiance.love/scenes/battlesystem/world.lua b/sonic-radiance.love/scenes/battlesystem/world.lua index 5696faa..69f38f3 100644 --- a/sonic-radiance.love/scenes/battlesystem/world.lua +++ b/sonic-radiance.love/scenes/battlesystem/world.lua @@ -63,6 +63,14 @@ function World:registerActor(actor) actor.id = self.globalID end +function World:destroyActor(actorToDestroy) + for i, actor in ipairs(self.actors) do + if actor == actorToDestroy then + table.remove(self.actors, i) + end + end +end + -- INFO FUNCTIONS -- Get info from the world