c1_lua/lua/app/module/battle/component/battle_unit_comp.lua
2023-04-17 17:13:24 +08:00

697 lines
20 KiB
Lua

local BattleConst = require "app/module/battle/battle_const"
local BattleHelper = require "app/module/battle/helper/battle_helper"
local BattleBuffHandle = require "app/module/battle/helper/battle_buff_handle"
local BattleUnitComp = class("BattleUnitComp", LuaComponent)
local UNIT_STATE = BattleConst.UNIT_STATE
local SIDE_ATK = BattleConst.SIDE_ATK
local SPINE_ANIMATION_NAME = BattleConst.SPINE_ANIMATION_NAME
local DEFAULT_FACTOR = BattleConst.DEFAULT_FACTOR
function BattleUnitComp:ctor()
end
function BattleUnitComp:initPosition()
if self.unitEntity:getSide() == SIDE_ATK then
self.baseObject:setLocalPosition(-BattleConst.INIT_POS_X, 0, 0)
self.body:setLocalScaleX(1)
self.direction = 1
else
self.baseObject:setLocalPosition(BattleConst.INIT_POS_X, 0, 0)
self.body:setLocalScaleX(-1)
self.direction = -1
end
end
function BattleUnitComp:playBorn()
self:changeState(UNIT_STATE.IDLE)
end
function BattleUnitComp:playSwitchIn()
self:changeState(UNIT_STATE.SWITCH_IN)
end
function BattleUnitComp:playSwitchOut()
self:changeState(UNIT_STATE.SWITCH_OUT)
end
function BattleUnitComp:getModelId()
return self.modelId
end
function BattleUnitComp:getMatchType()
return self.unitEntity:getMatchType()
end
function BattleUnitComp:_initBase()
self.isClear = false
self.isMove = false
self.deadTime = 0
self.attackTime = 0
self.currAttackDuration = 0
self.currAttackKeyTime = 0
self.switchTime = 0
self.isPlayHurt = 0
self.attackDurationMap = {}
self.buffList = {}
self.sameBuffCount = {}
self.shieldBuffList = {}
self.activeSkillIndex = nil
self.currActiveSkill = nil
self.targetX = nil
self.currState = UNIT_STATE.INIT
end
function BattleUnitComp:initWithEntity(modelId, entity, battleController, target)
self.modelId = modelId
self.unitEntity = entity
self.battleController = battleController
self.target = target
self.body = self.baseObject:getChildByName("body")
self.side = entity:getSide()
self:_initBase()
self:initPosition()
self:playBorn()
end
function BattleUnitComp:hideOutsideScreen()
if self.unitEntity:getSide() == SIDE_ATK then
self.baseObject:setLocalPosition(-GConst.UI_SCREEN_WIDTH/2 - BattleConst.UNIT_BODY_WIDTH, 0, 0)
else
self.baseObject:setLocalPosition(GConst.UI_SCREEN_WIDTH/2 + BattleConst.UNIT_BODY_WIDTH, 0, 0)
end
end
function BattleUnitComp:playAnimation(name, loop, forceRefresh)
if name == SPINE_ANIMATION_NAME.HIT then
self.isPlayHurt = 1
else
self.isPlayHurt = 0
end
self.currAnimationName = name
self.baseObject:playAnimation(name, loop, forceRefresh)
end
function BattleUnitComp:getAnimationDuration(aniName)
local duration = self.attackDurationMap[aniName]
if duration == nil then
duration = self.baseObject:getAnimationDuration(aniName)
self.attackDurationMap[aniName] = duration
end
return duration or 0
end
function BattleUnitComp:useSkill(index, callback)
self.actionOverCallback = callback
self.activeSkillIndex = nil
self.currActiveSkill = self.unitEntity:getActiveSkill(index)
if self.currActiveSkill == nil then
self.actionOverCallback = nil
callback()
return
end
if not self:changeState(UNIT_STATE.SKILL_ATTACK) then
self.actionOverCallback = nil
callback()
end
end
function BattleUnitComp:useAllSkills(callback)
self.actionOverCallback = callback
self.activeSkillIndex = 1
self.currActiveSkill = self.unitEntity:getActiveSkill(self.activeSkillIndex)
if self.currActiveSkill == nil then
self.actionOverCallback = nil
callback()
return
end
if not self:changeState(UNIT_STATE.SKILL_ATTACK) then
self.actionOverCallback = nil
callback()
end
end
function BattleUnitComp:useNormalSkill(count, callback)
self.baseObject:getTransform():SetAsLastSibling()
self.actionOverCallback = callback
self.normalSkillCount = count + self.unitEntity:getNormalAttackCount()
if not self:changeState(UNIT_STATE.NORMAL_ATTACK) then
self.actionOverCallback = nil
callback()
end
end
function BattleUnitComp:addShield(num, buffEffect)
if buffEffect then
table.insert(self.shieldBuffList, buffEffect)
end
self.unitEntity:addShield(num)
end
function BattleUnitComp:changeState(state)
if self.currState == state and not self:repeatCurrState() then
return false
end
if self.currState == UNIT_STATE.DEAD then -- 死亡后只能去死亡状态
if state ~= UNIT_STATE.DEAD then
return false
end
end
-- 离开当前状态
if self.currState == UNIT_STATE.IDLE then
self:exitIdleState()
elseif self.currState == UNIT_STATE.NORMAL_ATTACK then
self:exitNormalAttackState()
elseif self.currState == UNIT_STATE.SKILL_ATTACK then
self:exitSkillAttackState()
elseif self.currState == UNIT_STATE.DEAD then
self:exitDeadState()
elseif self.currState == UNIT_STATE.ENTER_BATTLEFIELD then
self:exitEnterBattlefieldState()
elseif self.currState == UNIT_STATE.SWITCH_IN then
self:exitSwitchInState()
elseif self.currState == UNIT_STATE.SWITCH_OUT then
self:exitSwitchOutState()
end
-- 进入目标状态
self.currState = state
if state == UNIT_STATE.IDLE then
self:enterIdleState()
elseif state == UNIT_STATE.NORMAL_ATTACK then
self:enterNormalAttackState()
elseif state == UNIT_STATE.SKILL_ATTACK then
self:enterSkillAttackState()
elseif state == UNIT_STATE.DEAD then
self:enterDeadState()
elseif self.currState == UNIT_STATE.BORN then
self:enterBornState()
elseif self.currState == UNIT_STATE.ENTER_BATTLEFIELD then
self:enterEnterBattlefieldState()
elseif self.currState == UNIT_STATE.SWITCH_IN then
self:enterSwitchInState()
elseif self.currState == UNIT_STATE.SWITCH_OUT then
self:enterSwitchOutState()
end
return true
end
function BattleUnitComp:repeatCurrState()
if self.currState == UNIT_STATE.NORMAL_ATTACK then
return true
elseif self.currState == UNIT_STATE.SKILL_ATTACK then
return true
end
return false
end
function BattleUnitComp:updateSwitchInState(dt)
self.switchTime = self.switchTime - dt
if self.switchTime < 0 then
self:changeState(UNIT_STATE.IDLE)
end
end
function BattleUnitComp:enterSwitchInState()
local aniName = SPINE_ANIMATION_NAME.BORN
self.switchTime = self:getAnimationDuration(aniName) + 0.1
self:initPosition()
self:playAnimation(aniName, false, true)
end
function BattleUnitComp:exitSwitchInState()
end
function BattleUnitComp:updateSwitchOutState(dt)
self.switchTime = self.switchTime - dt
if self.switchTime < 0 then
self:changeState(UNIT_STATE.IDLE)
end
end
function BattleUnitComp:enterSwitchOutState()
local aniName = SPINE_ANIMATION_NAME.OUT
self.switchTime = self:getAnimationDuration(aniName) + 0.1
self:playAnimation(aniName, false, true)
end
function BattleUnitComp:exitSwitchOutState()
self:hideOutsideScreen()
end
function BattleUnitComp:updateDead(dt)
self.deadTime = self.deadTime - dt
if self.deadTime <= 0 then
self:clear()
if self.deadOverCallback then
local callback = self.deadOverCallback
self.deadOverCallback = nil
callback()
end
end
end
function BattleUnitComp:exitDeadState()
end
function BattleUnitComp:enterDeadState()
local aniName = SPINE_ANIMATION_NAME.DEAD
self.deadTime = self:getAnimationDuration(aniName) + 0.1
self:playAnimation(aniName, false, false)
end
function BattleUnitComp:updateEnterBattlefieldState(dt)
if self.isMove then
local addX = dt*BattleConst.MOVE_SPEED*self.moveDirection
self.positionX = self.positionX + addX
if (self.moveDirection > 0 and self.positionX >= self.targetX) or (self.moveDirection < 0 and self.positionX <= self.targetX) then
self.isMove = false
self.positionX = self.targetX
self:changeState(UNIT_STATE.IDLE)
end
self.baseObject:setLocalPosition(self.positionX, 0, 0)
end
end
function BattleUnitComp:exitEnterBattlefieldState()
local callback = self.finishEnterBattlefieldCallback
self.finishEnterBattlefieldCallback = nil
if callback then
callback()
end
end
function BattleUnitComp:enterEnterBattlefieldState()
self:hideOutsideScreen()
self.isMove = true
self:playAnimation(SPINE_ANIMATION_NAME.MOVE, true, false)
self.positionX = self.baseObject:fastGetLocalPosition()
if self.side == BattleConst.SIDE_ATK then
self.targetX = -BattleConst.INIT_POS_X
self.moveDirection = 1
else
self.targetX = BattleConst.INIT_POS_X
self.moveDirection = -1
end
end
function BattleUnitComp:exitIdleState()
end
function BattleUnitComp:enterIdleState()
self:playAnimation(SPINE_ANIMATION_NAME.IDLE, true, false)
end
function BattleUnitComp:updateIdle(dt)
self:updateHurt(dt)
end
function BattleUnitComp:playHurt()
self.hurtTime = 0
if self.currHitDuration == nil then
self.currHitDuration = self:getAnimationDuration(SPINE_ANIMATION_NAME.HIT)
end
self:playAnimation(SPINE_ANIMATION_NAME.HIT, false, false)
end
function BattleUnitComp:updateHurt(dt)
if self.isPlayHurt == 0 then
return
end
if self.isPlayHurt == 1 then
self.hurtTime = self.hurtTime + dt
if self.hurtTime >= self.currHitDuration then
self:playAnimation(SPINE_ANIMATION_NAME.IDLE, true, false)
self.isPlayHurt = 0
end
end
end
function BattleUnitComp:enterSkillAttackState()
self.attackOver = false
self.attackTime = 0
self.currAttackKeyTime = 0.3
self.targetX = nil
if self.currActiveSkill:getMoveType() == BattleConst.SKILL_MOVE_TYPE.MOVE then
self.isMove = true
self:playAnimation(BattleConst.SPINE_ANIMATION_NAME.MOVE, true, false)
self.positionX = self.baseObject:fastGetLocalPosition()
if self.side == BattleConst.SIDE_ATK then
self.targetX = BattleConst.UNIT_FRONT_POS_X
self.moveDirection = 1
else
self.targetX = -BattleConst.UNIT_FRONT_POS_X
self.moveDirection = -1
end
else
self.isMove = false
self.attackTime = 0
local attackName = self.currActiveSkill:getSkillAttackName()
self.currAttackDuration = self:getAnimationDuration(attackName)
self:playAnimation(attackName, false, false)
end
end
function BattleUnitComp:exitSkillAttackState()
end
function BattleUnitComp:updateSkillAttack(dt)
if self.isMove then
local addX = dt*BattleConst.MOVE_SPEED*self.moveDirection
self.positionX = self.positionX + addX
if (self.moveDirection > 0 and self.positionX >= self.targetX) or (self.moveDirection < 0 and self.positionX <= self.targetX) then
self.isMove = false
self.positionX = self.targetX
if self.attackOver then -- 归位后该进行下一次攻击了
if self.activeSkillIndex then
self.activeSkillIndex = self.activeSkillIndex + 1
self.currActiveSkill = self.unitEntity:getActiveSkill(self.activeSkillIndex)
else
self.currActiveSkill = nil
end
if self.currActiveSkill == nil then
self:changeState(UNIT_STATE.IDLE)
local callback = self.actionOverCallback
self.actionOverCallback = nil
if callback then
callback()
end
return
else
if not self:changeState(UNIT_STATE.SKILL_ATTACK) then
local callback = self.actionOverCallback
self.actionOverCallback = nil
callback()
return
end
end
else -- 到位置该攻击了
self.attackTime = 0
local attackName = self.currActiveSkill:getSkillAttackName()
self.currAttackDuration = self:getAnimationDuration(attackName)
self:playAnimation(attackName, false, false)
end
end
self.baseObject:setLocalPosition(self.positionX, 0, 0)
return
end
self.attackTime = self.attackTime + dt
if self.attackTime >= self.currAttackDuration then
self.attackOver = true
local currActiveSkill = nil
if self.activeSkillIndex then
self.activeSkillIndex = self.activeSkillIndex + 1
currActiveSkill = self.unitEntity:getActiveSkill(self.activeSkillIndex)
end
if currActiveSkill == nil then
if self.targetX then -- 移动过,准备归位
self.isMove = true
self:playAnimation(BattleConst.SPINE_ANIMATION_NAME.MOVE, true, false)
self.positionX = self.baseObject:fastGetLocalPosition()
if self.side == BattleConst.SIDE_ATK then
self.targetX = -BattleConst.INIT_POS_X
self.moveDirection = -1
else
self.targetX = BattleConst.INIT_POS_X
self.moveDirection = 1
end
else
self:changeState(UNIT_STATE.IDLE)
local callback = self.actionOverCallback
self.actionOverCallback = nil
if callback then
callback()
end
end
return
else -- 继续攻击
self.currActiveSkill = currActiveSkill
self.attackTime = 0
self.currAttackKeyTime = 0.3
local attackName = self.currActiveSkill:getSkillAttackName()
self.currAttackDuration = self:getAnimationDuration(attackName)
self:playAnimation(attackName, false, false)
end
else
if self.currAttackKeyTime > 0 and self.attackTime >= self.currAttackKeyTime then -- 到达关键后使用
self.currAttackKeyTime = 0
self:onSkillTakeEffect(self.currActiveSkill)
end
end
end
function BattleUnitComp:enterNormalAttackState()
self.attackOver = false
self.attackTime = 0
self.currAttackKeyTime = 0.3
local skill = self.unitEntity:getNormalSkill()
if skill:getMoveType() == BattleConst.SKILL_MOVE_TYPE.MOVE then
self.isMove = true
self:playAnimation(BattleConst.SPINE_ANIMATION_NAME.MOVE, true, false)
self.positionX = self.baseObject:fastGetLocalPosition()
if self.side == BattleConst.SIDE_ATK then
self.targetX = BattleConst.UNIT_FRONT_POS_X
self.moveDirection = 1
else
self.targetX = -BattleConst.UNIT_FRONT_POS_X
self.moveDirection = -1
end
else
self.isMove = false
self.attackTime = 0
local attackName = skill:getRandomNormalAttackName()
self.currAttackDuration = self:getAnimationDuration(attackName)
self:playAnimation(attackName, false, false)
end
end
function BattleUnitComp:exitNormalAttackState()
end
function BattleUnitComp:updateNormalAttack(dt)
if self.isMove then
local addX = dt*BattleConst.MOVE_SPEED*self.moveDirection
self.positionX = self.positionX + addX
if (self.moveDirection > 0 and self.positionX >= self.targetX) or (self.moveDirection < 0 and self.positionX <= self.targetX) then
self.isMove = false
self.positionX = self.targetX
if self.attackOver then -- 归位后该下一步了
self:changeState(UNIT_STATE.IDLE)
local callback = self.actionOverCallback
self.actionOverCallback = nil
if callback then
callback()
end
else -- 到位置该攻击了
self.attackTime = 0
local skill = self.unitEntity:getNormalSkill()
local attackName = skill:getRandomNormalAttackName()
self.currAttackDuration = self:getAnimationDuration(attackName)
self:playAnimation(attackName, false, false)
end
end
self.baseObject:setLocalPosition(self.positionX, 0, 0)
return
end
self.attackTime = self.attackTime + dt
if self.attackTime >= self.currAttackDuration then
self.attackOver = true
self.normalSkillCount = self.normalSkillCount - 1
if self.normalSkillCount <= 0 then
local skill = self.unitEntity:getNormalSkill()
if skill:getMoveType() == BattleConst.SKILL_MOVE_TYPE.MOVE then
self.isMove = true
self:playAnimation(BattleConst.SPINE_ANIMATION_NAME.MOVE, true, false)
self.positionX = self.baseObject:fastGetLocalPosition()
if self.side == BattleConst.SIDE_ATK then
self.targetX = -BattleConst.INIT_POS_X
self.moveDirection = -1
else
self.targetX = BattleConst.INIT_POS_X
self.moveDirection = 1
end
else
self:changeState(UNIT_STATE.IDLE)
local callback = self.actionOverCallback
self.actionOverCallback = nil
if callback then
callback()
end
end
return
else -- 继续攻击
self.attackTime = 0
self.currAttackKeyTime = 0.3
local skill = self.unitEntity:getNormalSkill()
local attackName = skill:getRandomNormalAttackName()
self.currAttackDuration = self:getAnimationDuration(attackName)
self:playAnimation(attackName, false, false)
end
else
if self.currAttackKeyTime > 0 and self.attackTime >= self.currAttackKeyTime then -- 到达关键后使用
self.currAttackKeyTime = 0
local skill = self.unitEntity:getNormalSkill()
self:onSkillTakeEffect(skill)
end
end
end
function BattleUnitComp:addBuff(buffEffect)
table.insert(self.buffList, buffEffect)
self:updateBuffState(buffEffect.buff, 1)
end
function BattleUnitComp:updateBuffState(buff, num)
local buffName = buff:getName()
local buffNum = (self.sameBuffCount[buffName] or 0) + num
self.sameBuffCount[buffName] = buffNum
-- if buffNum > 0 and buffNum == num then
-- self:showBuffFx(buffName, buff:getBuffFxId())
-- elseif buffNum <= 0 then
-- self:hideBuffFx(buffName)
-- end
end
function BattleUnitComp:onSkillTakeEffect(skill)
if skill == self.unitEntity:getNormalSkill() then
self.battleController:addBattleExp(self.side)
end
local effectList = skill:getEffectList()
if effectList == nil then
return
end
local targetType = skill:getTargetType()
local target
if targetType == 1 then -- 自己
target = self
else
target = self.battleController:getOtherSideMainUnit(self.side)
end
local succ = false
for k, effect in ipairs(effectList) do
if self:takeEffect(effect, target) then
succ = true
end
end
end
function BattleUnitComp:takeEffect(buff, target)
local ratio = buff:getRatio()
if ratio < DEFAULT_FACTOR then
if BattleHelper:random(1, DEFAULT_FACTOR) > ratio then -- 没有通过命中概率
return false
end
end
local round = buff:getRound()
local buffEffect
if round > 0 then
buffEffect = BattleHelper:getBuffEffect()
buffEffect.buff = buff
buffEffect.result = nil
buffEffect.round = round
buffEffect.target = target
buffEffect.sender = self
target:addBuff(buffEffect)
end
local func = BattleBuffHandle.takeBuffEffect[buff:getBuffType()]
if func then
local result = func(self, buff, target, buffEffect)
if buffEffect then
buffEffect.result = result
end
local success = result ~= nil
if success then
local fxId = buff:getBuffHitFxId()
if fxId then
target:playHurtFx(fxId)
end
end
return success
end
return false
end
function BattleUnitComp:takeDamageOrCure(atker, buff, num, effectType, effectStatus)
if num == 0 then
return 0
end
self.unitEntity:takeDamageOrCure(num)
local x, y, z = self.baseObject:fastGetLocalPosition()
self:showEffectNumber(num, x, y)
self.battleController:refreshHp(self.side, self.unitEntity:getHp(), self.unitEntity:getHpPercent())
-- local shieldHp = self.unitEntity:getShieldHp()
-- if shieldHp and shieldHp.value > 0 then
-- local percent = self.unitEntity:getShieldHpPercent()
-- self.controlUnitComp:RefreshShieldBar(percent, true)
-- else
-- self.controlUnitComp:RefreshShieldBar(0, false)
-- end
end
function BattleUnitComp:showEffectNumber(num, x, y)
self.battleController:showEffectNumber(num, x, y)
end
function BattleUnitComp:playDead(callback)
self.deadOverCallback = callback
if not self:changeState(UNIT_STATE.DEAD) then
self.deadOverCallback = nil
callback()
end
end
function BattleUnitComp:playEnterBattlefield(callback)
self.finishEnterBattlefieldCallback = callback
self:hideOutsideScreen()
if not self:changeState(UNIT_STATE.ENTER_BATTLEFIELD) then
self.finishEnterBattlefieldCallback = nil
callback()
end
end
function BattleUnitComp:tick(dt)
if self.isClear then
return
end
if self.currState == UNIT_STATE.DEAD then
self:updateDead(dt)
return
end
if self.currState == UNIT_STATE.IDLE then
self:updateIdle(dt)
return
end
if self.currState == UNIT_STATE.NORMAL_ATTACK then
self:updateNormalAttack(dt)
elseif self.currState == UNIT_STATE.SKILL_ATTACK then
self:updateSkillAttack(dt)
elseif self.currState == UNIT_STATE.ENTER_BATTLEFIELD then
self:updateEnterBattlefieldState(dt)
elseif self.currState == UNIT_STATE.SWITCH_IN then
self:updateSwitchInState(dt)
elseif self.currState == UNIT_STATE.SWITCH_OUT then
self:updateSwitchOutState(dt)
end
end
function BattleUnitComp:getIsClear()
return self.isClear
end
function BattleUnitComp:clear()
if self.isClear then
return
end
self.isClear = true
end
function BattleUnitComp:recycle()
BattleHelper:recycleBattleHeroModel(self.modelId, self.baseObject)
end
return BattleUnitComp