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.ATKWRONGTYPE = 0.66
CONST.DAMAGE = {}
CONST.DAMAGE.DIVISOR = 10
CONST.DAMAGE.DEFFACTOR = 0.66
return CONST

View file

@ -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

View file

@ -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()