chore: extract battle aglorythm in battleutils

This commit is contained in:
Kazhnuz 2021-05-16 08:33:41 +02:00
parent e864e67717
commit 6818a441df
3 changed files with 60 additions and 35 deletions

View file

@ -3,4 +3,8 @@ local CONST = {}
CONST.ACCURACY = 100 CONST.ACCURACY = 100
CONST.ATKWRONGTYPE = 0.66 CONST.ATKWRONGTYPE = 0.66
CONST.DAMAGE = {}
CONST.DAMAGE.DIVISOR = 10
CONST.DAMAGE.DEFFACTOR = 0.66
return CONST return CONST

View file

@ -1,3 +1,41 @@
local BattleUtils = {} 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 return BattleUtils

View file

@ -1,6 +1,7 @@
local FighterParent = Object:extend() local FighterParent = Object:extend()
local CONST = require "datas.cbs" local CONST = require "datas.cbs"
local battleutils = require "game.utils.battle"
function FighterParent:new(owner, isHero, id) function FighterParent:new(owner, isHero, id)
self.owner = owner self.owner = owner
@ -75,50 +76,32 @@ function FighterParent:haveProtecType(type)
end end
function FighterParent:sendDamage(target, value, type, element, isSpecial) function FighterParent:sendDamage(target, value, type, element, isSpecial)
local stats = self:getStats() local damage = battleutils.computeLaunchingDamages(value, self:getStats(), isSpecial)
local value = value / 10
if (isSpecial) then core.debug:print("cbs/battler", "Sending " .. damage .." damage at " .. target.name)
value = value * stats.power return target:receiveDamage(damage, type, element, isSpecial, self)
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)
end end
function FighterParent:receiveDamage(value, type, element, isSpecial, fromWho) function FighterParent:receiveDamage(value, type, element, isSpecial, fromWho)
local accuracy = 100 local damages = battleutils.computeReceivingDamages(value, self:getStats(), isSpecial, self.isDefending)
local stats = self:getStats() damages = battleutils.applyProtectTypes(damages, type, self:getProtecTypes())
local isSuccess = true
if (isSpecial) then if (battleutils.isAttackSuccess(nil)) then
value = value / stats.mind self:applyDamage(damages)
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
else else
self.actor:avoidedAttack() self.actor:avoidedAttack()
return false
end 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 end
function FighterParent:getAbstract() function FighterParent:getAbstract()