using System.Collections.Generic; using TEngine.Runtime; using TEngine.Runtime.Actor; using UnityEngine; namespace TEngine.Runtime.Actor { public class PlayerActor:GameActor { public override int GetActorType() { return ActorType.ActorPlayer; } public override void Awake() { base.Awake(); } public override void OnInit() { base.OnInit(); } } public class AnimatorComponent : ActorComponent { private Animator _animator; protected override void Awake() { base.Awake(); _animator = OwnActor.gameObject.AddComponent(); } } public class ModelComponent : ActorComponent { private GameObject _model; protected override void Awake() { base.Awake(); var obj = TResources.Load("Capsule"); _model = Object.Instantiate(obj); _model.transform.SetParent(OwnActor.gameObject.transform); } } public class FsmComponent : ActorComponent { public IFsm Fsm; protected override void Awake() { base.Awake(); List> stateList = new List>() { IdleState.Create(), MoveState.Create() }; Fsm = FsmManager.Instance.CreateFsm(OwnActor.ActorId.ToString(), this, stateList); Fsm.Start(); } #region 状态机 public class IdleState : FsmState, IMemory { //触发移动的指令列表 private static KeyCode[] MOVE_COMMANDS = { KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.A, KeyCode.W, KeyCode.S, KeyCode.D }; protected override void OnInit(IFsm fsm) { base.OnInit(fsm); } protected override void OnEnter(IFsm fsm) { Log.Warning("OnEnter IdleState"); base.OnEnter(fsm); } protected override void OnUpdate(IFsm fsm, float elapseSeconds, float realElapseSeconds) { base.OnUpdate(fsm, elapseSeconds, realElapseSeconds); foreach (var command in MOVE_COMMANDS) { //触发任何一个移动指令时 if (Input.GetKeyDown(command)) { //记录这个移动指令 fsm.SetData("MoveCommand", (int)command); //切换到移动状态 ChangeState(fsm); } } } protected override void OnLeave(IFsm fsm, bool isShutdown) { base.OnLeave(fsm, isShutdown); } protected override void OnDestroy(IFsm fsm) { base.OnDestroy(fsm); } public static IdleState Create() { IdleState state = MemoryPool.Acquire(); return state; } public void Clear() { //此类无状态记录,Clear为空实现 } } public class MoveState : FsmState, IMemory { private static readonly float EXIT_TIME = 0.01f; private float exitTimer; private KeyCode moveCommand; protected override void OnInit(IFsm fsm) { base.OnInit(fsm); } protected override void OnEnter(IFsm fsm) { base.OnEnter(fsm); //进入移动状态时,获取移动指令数据 moveCommand = (KeyCode)(int)fsm.GetData("MoveCommand"); Log.Warning("OnEnter MoveState" + moveCommand); } protected override void OnUpdate(IFsm fsm, float elapseSeconds, float realElapseSeconds) { base.OnUpdate(fsm, elapseSeconds, realElapseSeconds); //计时器累计时间 exitTimer += elapseSeconds; //switch(moveCommand) //{ //根据移动方向指令向对应方向移动 //} if (moveCommand != 0) { exitTimer = 0; } //达到指定时间后 if (exitTimer > EXIT_TIME) { //切换回空闲状态 ChangeState(fsm); } } protected override void OnLeave(IFsm fsm, bool isShutdown) { base.OnLeave(fsm, isShutdown); //推出移动状态时,把计时器清零 exitTimer = 0; //清空移动指令 moveCommand = KeyCode.None; fsm.RemoveData("MoveCommand"); } protected override void OnDestroy(IFsm fsm) { base.OnDestroy(fsm); } public static MoveState Create() { MoveState state = MemoryPool.Acquire(); return state; } public void Clear() { //还原状态内数据 exitTimer = 0; moveCommand = KeyCode.None; } } #endregion } } public class ActorTestMain : MonoBehaviour { private uint _actorId = 0; void Start() { //Demo示例,监听TEngine流程加载器OnStartGame事件 //抛出这个事件说明框架流程加载完成(热更新,初始化等) GameEventMgr.Instance.AddEventListener(TEngineEvent.OnStartGame,OnStartGame); } private void OnStartGame() { Log.Debug("TEngineEvent.OnStartGame"); //激活ActorManager ActorManager.Instance.Active(); //注册Actor类型 ActorManager.Instance.RegisterActorType(ActorType.ActorPlayer,typeof(PlayerActor)); //创建Actor var actor = ActorManager.Instance.CreateGameActor(ActorType.ActorPlayer, _actorId++, true); //附加组件(ActorGameObjet可视化) actor.Attach(); actor.Attach(); actor.Attach(); } }