local StateSystem = {} local private = {} local LuaConst=LuaConst --加载需要的模块 local State_Idle = require "Battle/Logic/Entity/State/State_Idle" local State_Move = require "Battle/Logic/Entity/State/State_Move" local State_RunStop = require "Battle/Logic/Entity/State/State_RunStop" local State_Attack = require "Battle/Logic/Entity/State/State_Attack" local State_Common = require "Battle/Logic/Entity/State/State_Common" local State_Dead = require "Battle/Logic/Entity/State/State_Dead" local State_Wander = require "Battle/Logic/Entity/State/State_Wander" local State_RideShowPos = require "Battle/Logic/Entity/State/State_RideShowPos" local State_Interact = require "Battle/Logic/Entity/State/State_Interact" local State_Rotate = require "Battle/Logic/Entity/State/State_Rotate" function private.Init() StateSystem.StateDic = {} StateSystem.StateDic[LuaConst.StateType.Idle] = State_Idle:new() StateSystem.StateDic[LuaConst.StateType.Move] = State_Move:new() StateSystem.StateDic[LuaConst.StateType.RunStop] = State_RunStop:new() StateSystem.StateDic[LuaConst.StateType.Attack] = State_Attack:new() StateSystem.StateDic[LuaConst.StateType.Rotate] = State_Rotate:new() StateSystem.StateDic[LuaConst.StateType.Common] = State_Common:new() StateSystem.StateDic[LuaConst.StateType.Dead] = State_Dead:new() StateSystem.StateDic[LuaConst.StateType.Wander] = State_Wander:new() StateSystem.StateDic[LuaConst.StateType.HorseIdlePos] = State_RideShowPos:new() StateSystem.StateDic[LuaConst.StateType.Interact] = State_Interact:new() end function StateSystem.SetNextState(stateType, entity, skillID, animName, command ,commonByBuff) if stateType == LuaConst.StateType.None then return end local aiBaseComp = entity.components.aiBase if not aiBaseComp then return end --退出上一个State if aiBaseComp.CurStateNode then aiBaseComp.CurStateNode:StateOut(entity) end --设置新的State aiBaseComp.CurStateType = stateType aiBaseComp.CurStateNode = StateSystem.StateDic[aiBaseComp.CurStateType] if stateType == LuaConst.StateType.Common then aiBaseComp.CommonStateByBuff = commonByBuff -- 这有buff的地方才设置这个值 end --进入新的State entity.components.aiBase.CurStateNode:StateIn(entity, skillID, animName, command) end function StateSystem.Active(entity, activeType) --需要先ResumeBuffListener,因为Buff需要监听active这个事件 if entity.components.buff then entity.components.buff:ResumeBuffListener() end entity.components.base:SetActive(true, nil, activeType) end --needUpdate :隐藏了 ,但是遍历update时还继续更新 function StateSystem.DisActive(entity ,needUpdate) entity.components.base:SetActive(false ,needUpdate) if entity.components.buff then entity.components.buff:PauseBuffListener() end end function StateSystem.OnSwitchHero(fromEntity, toEntity, dontBackground) if not dontBackground then StateSystem.DisActive(fromEntity) end if not toEntity:IsActive() then StateSystem.Active(toEntity) end end function StateSystem.OnBackground(entity) StateSystem.DisActive(entity) end function StateSystem.OnRideHorse(entity) entity.components.aiBase.CurStateNode:RideHorse(entity) end function StateSystem.OnOffHorse(entity) entity.components.aiBase.CurStateNode:OffHorse(entity) end private.Init() return StateSystem