From 6818a441dfebfa9d2d115046ac485b2922c31e9e Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 16 May 2021 08:33:41 +0200 Subject: [PATCH] chore: extract battle aglorythm in battleutils --- sonic-radiance.love/datas/cbs.lua | 4 ++ .../game/utils/battle/init.lua | 38 +++++++++++++ .../controllers/fighters/parent.lua | 53 +++++++------------ 3 files changed, 60 insertions(+), 35 deletions(-) diff --git a/sonic-radiance.love/datas/cbs.lua b/sonic-radiance.love/datas/cbs.lua index 7d1028e..cbafe36 100644 --- a/sonic-radiance.love/datas/cbs.lua +++ b/sonic-radiance.love/datas/cbs.lua @@ -3,4 +3,8 @@ local CONST = {} CONST.ACCURACY = 100 CONST.ATKWRONGTYPE = 0.66 +CONST.DAMAGE = {} +CONST.DAMAGE.DIVISOR = 10 +CONST.DAMAGE.DEFFACTOR = 0.66 + return CONST \ No newline at end of file diff --git a/sonic-radiance.love/game/utils/battle/init.lua b/sonic-radiance.love/game/utils/battle/init.lua index 42bdf43..5f05529 100644 --- a/sonic-radiance.love/game/utils/battle/init.lua +++ b/sonic-radiance.love/game/utils/battle/init.lua @@ -1,3 +1,41 @@ local BattleUtils = {} +local CONSTS = require "datas.cbs" + +function BattleUtils.computeLaunchingDamages(base, stats, isSpecial) + local damages = base / CONSTS.DAMAGE.DIVISOR + if (isSpecial) then + damages = damages * stats.power + else + damages = damages * stats.attack + end + return damages +end + +function BattleUtils.computeReceivingDamages(base, stats, isSpecial, isDefending) + local damages = base + if (isSpecial) then + damages = damages / stats.mind + else + damages = damages / stats.defense + end + + if (isDefending) then + damages = damages * CONSTS.DAMAGE.DEFFACTOR + end + + return damages +end + +function BattleUtils.applyProtectTypes(value, type, protectype) + if (utils.table.contain(protectype, "aerial") and type == "basic") then + value = value * CONSTS.ATKWRONGTYPE + end + return value +end + +function BattleUtils.isAttackSuccess(statutList) + local accuracy = 100 + return (math.random(100) <= accuracy) +end return BattleUtils diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua index ea64b2d..c990bae 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua @@ -1,6 +1,7 @@ local FighterParent = Object:extend() local CONST = require "datas.cbs" +local battleutils = require "game.utils.battle" function FighterParent:new(owner, isHero, id) self.owner = owner @@ -75,50 +76,32 @@ function FighterParent:haveProtecType(type) end function FighterParent:sendDamage(target, value, type, element, isSpecial) - local stats = self:getStats() - local value = value / 10 + local damage = battleutils.computeLaunchingDamages(value, self:getStats(), isSpecial) - if (isSpecial) then - value = value * stats.power - else - value = value * stats.attack - end - - core.debug:print("cbs/battler", "Sending " .. value .." damage at " .. target.name) - - return target:receiveDamage(value, type, element, isSpecial, self) + core.debug:print("cbs/battler", "Sending " .. damage .." damage at " .. target.name) + return target:receiveDamage(damage, type, element, isSpecial, self) end function FighterParent:receiveDamage(value, type, element, isSpecial, fromWho) - local accuracy = 100 - local stats = self:getStats() - local isSuccess = true + local damages = battleutils.computeReceivingDamages(value, self:getStats(), isSpecial, self.isDefending) + damages = battleutils.applyProtectTypes(damages, type, self:getProtecTypes()) - if (isSpecial) then - value = value / stats.mind - else - value = value / stats.defense - end - - if (self:haveProtecType("aerial") and type == "basic") then - value = value * CONST.ATKWRONGTYPE - end - - isSuccess = (math.random(100) <= accuracy) - - if (isSuccess) then - core.debug:print("cbs/fighter", "Taken " .. value .. " damage" ) - if (self.isDefending) then - self:setHP(value * -0.66, true) - else - self:setHP(value * -1, true) - self.actor:getHurt() - end + if (battleutils.isAttackSuccess(nil)) then + self:applyDamage(damages) else self.actor:avoidedAttack() + return false end - return isSuccess + return true +end + +function FighterParent:applyDamage(damage) + core.debug:print("cbs/fighter", "Taken " .. damage .. " damage" ) + self:setHP(damage * -1, true) + if (not self.isDefending) then + self.actor:getHurt() + end end function FighterParent:getAbstract()