From 9f14200da7151ec3bbd88ee7047cb86005eba294 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Fri, 23 Sep 2022 15:34:19 +0800 Subject: [PATCH] ActorDemo ActorDemo --- .../TEngine.ActorDemo/ActorTestMain.cs | 211 -------------- .../TEngine.ActorDemo/PlayEntityMgr.cs | 75 +++++ .../TEngine.ActorDemo/PlayEntityMgr.cs.meta | 3 + .../TEngine.ActorDemo/PlayerActor.cs | 265 ++++++++++++++++++ .../TEngine.ActorDemo/PlayerActor.cs.meta | 3 + 5 files changed, 346 insertions(+), 211 deletions(-) create mode 100644 Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs create mode 100644 Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs.meta create mode 100644 Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs create mode 100644 Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs.meta diff --git a/Assets/TEngine.Demo/TEngine.ActorDemo/ActorTestMain.cs b/Assets/TEngine.Demo/TEngine.ActorDemo/ActorTestMain.cs index ece55a34..354d5d34 100644 --- a/Assets/TEngine.Demo/TEngine.ActorDemo/ActorTestMain.cs +++ b/Assets/TEngine.Demo/TEngine.ActorDemo/ActorTestMain.cs @@ -3,217 +3,6 @@ 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; diff --git a/Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs b/Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs new file mode 100644 index 00000000..bf0d1a17 --- /dev/null +++ b/Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs @@ -0,0 +1,75 @@ +using TEngine.Runtime.Entity; +using UnityEngine; + +namespace TEngine.Runtime.Actor +{ + public class PlayEntityMgr:UnitySingleton + { + protected override void OnLoad() + { + RegisterEvent(); + + base.OnLoad(); + } + + /// + /// RegisterEvent + /// + void RegisterEvent() + { + GameEventMgr.Instance.AddEventListener(EntityEvent.ShowEntitySuccess, OnShowEntitySuccess); + GameEventMgr.Instance.AddEventListener(EntityEvent.ShowEntityFailure, OnShowEntityFailure); + GameEventMgr.Instance.AddEventListener(EntityEvent.HideEntityComplete, OnHideEntityComplete); + } + + /// + /// OnShowEntitySuccess + /// + /// + /// + /// + private void OnShowEntitySuccess(IEntity entity, float duration, object userData) + { + Log.Warning("OnShowEntitySuccess" + entity.ToString() + " " + duration + " " + userData); + } + + /// + /// OnShowEntityFailure + /// + /// + /// + /// + /// + /// + private void OnShowEntityFailure(int entityId, string entityAssetName, string entityGroupName, + string errorMessage, object userData) + { + } + + /// + /// OnHideEntityComplete + /// + /// + /// + /// + /// + private void OnHideEntityComplete(int entityId, string entityAssetName, IEntityGroup entityGroup, + object userData) + { + + } + + /// + /// 玩家Actor实体管理器创建光源/ URP可最佳实践 + /// + /// + /// + /// + public void CreatePlayerEntity(int actorId,string entityPath, Vector3 position,Quaternion quaternion) + { + EntityData data = EntityData.Create(position,quaternion,actorId); + + EntitySystem.Instance.CreateEntity(entityPath, data); + } + } +} \ No newline at end of file diff --git a/Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs.meta b/Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs.meta new file mode 100644 index 00000000..50330d04 --- /dev/null +++ b/Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 17eab8dad38941328e42d077d6ff6c89 +timeCreated: 1663917080 \ No newline at end of file diff --git a/Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs b/Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs new file mode 100644 index 00000000..bd7e7308 --- /dev/null +++ b/Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs @@ -0,0 +1,265 @@ +using System.Collections.Generic; +using TEngine.Runtime.Entity; +using UnityEngine; + +namespace TEngine.Runtime.Actor +{ + public class ActorEntity : EntityLogicEx + { + protected override void OnInit(object userData) + { + base.OnInit(userData); + + var entityData = (EntityData)userData; + + var actor = ActorManager.Instance.GetActor((uint)entityData.UserData); + + actor.Get().BindModel(gameObject); + } + } + + /// + /// 玩家Actor,把Actor具象化成玩家 + /// + public class PlayerActor:GameActor + { + public override int GetActorType() + { + return ActorType.ActorPlayer; + } + + public override void Awake() + { + base.Awake(); + } + + public override void OnInit() + { + base.OnInit(); + } + } + + /// + /// Animator组件 + /// + 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(); + + InitModel(); + + BindOwnActor(); + } + + private void InitModel() + { + //普通创建实体 + // var obj = TResources.Load("Capsule"); + // _model = Object.Instantiate(obj); + + //通过Entity创建实体 + PlayEntityMgr.Instance.CreatePlayerEntity((int)OwnActor.ActorId,"Capsule",Vector3.zero, Quaternion.identity); + } + + public void BindModel(GameObject gameObject) + { + _model = gameObject; + + BindOwnActor(); + } + + private void BindOwnActor() + { + if (_model == null) + { + return; + } + + _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 + + } +} \ No newline at end of file diff --git a/Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs.meta b/Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs.meta new file mode 100644 index 00000000..e1973278 --- /dev/null +++ b/Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b11e802b8a2a446781dc687f828f448a +timeCreated: 1663916847 \ No newline at end of file