local FirstPersonEventDispatcher = require"Battle/Logic/Event/FirstPersonEventDispatcher" local CharacterData = require "Battle/Logic/Entity/CharacterData" local EventSystem = require('Event/EventSystem') local ChangeAvatarSystem = {} local LuaConst=LuaConst local FP=FP function ChangeAvatarSystem.OnLogicUpdate(entity) local component_Record = entity.components.record local timer = component_Record.ChangeAvatarTimer if not timer then return end timer = timer - LuaConst.TimeSpanMs if timer<= FP.Const.Zero then if entity.components.attr:IsAlive() then -- 死亡不再变回来,有复活在回来 ChangeAvatarSystem.ChangeAvatar(entity, 0) timer = nil end end component_Record.ChangeAvatarTimer = timer end function ChangeAvatarSystem.OnSwitchHero(fromEntity, toEntity, dontBackground) if not dontBackground then ChangeAvatarSystem.OnBackground(fromEntity) end end function ChangeAvatarSystem.OnBackground(entity) local component_Base = entity.components.base local logicInitData = entity._logicInitData if component_Base.CurCharacterData ~= logicInitData._characterData then ChangeAvatarSystem.ChangeAvatar(entity,0) end if component_Base.CurModelData ~= logicInitData._modelData then ChangeAvatarSystem.ChangeModel(entity,0) end end --[[ 触发事件 --]] function ChangeAvatarSystem.OnTriggerAction(entity, skillEvent, modelOnly) if modelOnly then ChangeAvatarSystem.ChangeModel(entity, skillEvent.EventIntParam) else -- 大变身 local inheritSkillCD = skillEvent.EventType == LuaConst.SkillEventType.ChangeAvatarInheritSkillCD if skillEvent.EventIntParam == 0 then -- EventIntParam 为0 时,表示技能主动变回原身 ChangeAvatarSystem.ChangeAvatar(entity, 0, nil, nil, nil, nil, nil, inheritSkillCD) else local skillChangeMonsters = entity.components.base.CurCharacterData.ChangeMonsterList if skillChangeMonsters then local characterID = skillChangeMonsters[skillEvent.EventIntParam] if characterID then ChangeAvatarSystem.ChangeAvatar(entity, characterID, skillEvent.EventParam, true, true, nil,nil, inheritSkillCD) else logging.error(LTAG.ERROR, "策划配表错误 : 技能变身时,发现skill大变身事件值不对;怪物id =", entity.components.base.CurCharacterData.ID, ",技能id =", entity.components.skillBase.CurrentSkillID) end else logging.error(LTAG.ERROR, "策划配表错误 : 技能变身时,发现skill大变身字段没有值;怪物id =", entity.components.base.CurCharacterData.ID) end end end end --小变身(只变模型数据) function ChangeAvatarSystem.ChangeModel(entity, modelID, keepAnim, canRideHorse, withMagicWeapon) local component_Base = entity.components.base component_Base:SetCurModelData(modelID == 0 and entity._logicInitData._modelData or DataSheetManager.MonsterModelData:GetDataByID(modelID)) local canRide = canRideHorse if canRideHorse == nil then -- 不要用语法糖 and .. or .. canRide = true end entity.components.replace:UpdateCurModelCacheData() local isWithMagicWeapon = withMagicWeapon if modelID == 0 then -- 还原为最初模型,需要尝试佩戴神兵 isWithMagicWeapon = true SystemManager.JustChangeExpAnim(entity,LuaConst.Animation_idle_loop) -- 为了回收变身模型再次使用能从entry进入,强制设置为idle,否则会动画卡住。(董卓P2卡在skill2) end SystemManager.OnChangeModel(entity, keepAnim, canRide, isWithMagicWeapon) if modelID == 0 then--如果对象池回收时重设animator则这代码不需要 SystemManager.JustChangeExpAnim(entity,LuaConst.Animation_idle_loop) -- 恢复为原模型后,如果变身前原模型带有动画,动画被卡住(loop的话会循环播放) end end --大变身(改变Character数据) isInheritSkillCD:继承同按钮位置的技能CD function ChangeAvatarSystem.ChangeAvatar(entity, characterID, duration, withBornSkill, stopRideHorse, withMagicWeapon, isFreeChange, isInheritSkillCD, inheritSkillKeyPosList) local component_Base = entity.components.base SystemManager.BeforeChangeAvatar(entity) SystemManager.ChangeToSwitchAll(entity) -- 大变身前技能强制后摇 local comp_Base = entity.components.base local bornBuffs = comp_Base.CurCharacterData.BornBuffs for i,buffId in ipairs(bornBuffs) do -- 删除变身前天生buff SystemManager.DeleteBuff(entity, buffId, entity) end local component_Record = entity.components.record local characterData local modelData local recover = characterID == 0 local isWithMagicWeapon = withMagicWeapon -- 注:当模型表的equip配置为-1时,会强行使用武将的神兵外显 local isFree = isFreeChange -- 目前只用在了控制是否要 :神兵升级技能替换原有技能 if recover then characterData = entity._logicInitData._characterData modelData = entity._logicInitData._modelData isWithMagicWeapon = true isFree = false else characterData = component_Base.AllCharacterData[characterID] if not characterData then characterData = CharacterData:new(characterID, entity._logicInitData.CharacterType) component_Base.AllCharacterData[characterID] = characterData end modelData = DataSheetManager.MonsterModelData:GetDataByID(characterData.ModelID) end component_Base.CurCharacterData = characterData component_Base:SetCurModelData(modelData) component_Base:SetMonsterFeature() if duration and duration > FP.Const.Zero then component_Record.ChangeAvatarTimer = duration else component_Record.ChangeAvatarTimer = nil end entity.components.replace:UpdateCurModelCacheData() SystemManager.OnChangeAvatar(entity, recover, withBornSkill, stopRideHorse, isWithMagicWeapon, isFree, isInheritSkillCD, inheritSkillKeyPosList) SystemManager.SetNextState(LuaConst.StateType.Idle, entity, 0) -- 主视角事件统一用 FirstPersonEventDispatcher 派发 FirstPersonEventDispatcher.Player_ChangeAvatar(entity) end -- 小变身恢复模型 function ChangeAvatarSystem.ResetOriginalModel(entity) local comp_Base = entity.components.base local comp_Record = entity.components.record --不处于大变身中,且模型数据改变了,才Reset回去 if not entity:IsAvatarChanged() and comp_Base.CurModelData ~= entity._logicInitData._modelData then ChangeAvatarSystem.ChangeModel(entity,0) end end function ChangeAvatarSystem.OnWinBattle(entity) local comp_Base = entity.components.base local comp_Record = entity.components.record if comp_Record.ChangeAvatarTimer and comp_Base.CurCharacterData ~= entity._logicInitData._characterData then ChangeAvatarSystem.ChangeAvatar(entity,0) end if not comp_Record.ChangeAvatarTimer and comp_Base.CurModelData ~= entity._logicInitData._modelData then ChangeAvatarSystem.ChangeModel(entity,0) end end function ChangeAvatarSystem.OnRevive(entity) local comp_Base = entity.components.base local comp_Record = entity.components.record if comp_Base.CurCharacterData ~= entity._logicInitData._characterData then ChangeAvatarSystem.ChangeAvatar(entity,0) end if comp_Base.CurModelData ~= entity._logicInitData._modelData then ChangeAvatarSystem.ChangeModel(entity,0) end end return ChangeAvatarSystem