mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
ActorDemo
ActorDemo
This commit is contained in:
@@ -3,217 +3,6 @@ using TEngine.Runtime;
|
|||||||
using TEngine.Runtime.Actor;
|
using TEngine.Runtime.Actor;
|
||||||
using UnityEngine;
|
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<Animator>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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<FsmComponent> Fsm;
|
|
||||||
|
|
||||||
protected override void Awake()
|
|
||||||
{
|
|
||||||
base.Awake();
|
|
||||||
|
|
||||||
List<FsmState<FsmComponent>> stateList = new List<FsmState<FsmComponent>>() { IdleState.Create(), MoveState.Create() };
|
|
||||||
|
|
||||||
Fsm = FsmManager.Instance.CreateFsm(OwnActor.ActorId.ToString(), this, stateList);
|
|
||||||
|
|
||||||
Fsm.Start<IdleState>();
|
|
||||||
}
|
|
||||||
|
|
||||||
#region 状态机
|
|
||||||
public class IdleState : FsmState<FsmComponent>, 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<FsmComponent> fsm)
|
|
||||||
{
|
|
||||||
base.OnInit(fsm);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnEnter(IFsm<FsmComponent> fsm)
|
|
||||||
{
|
|
||||||
Log.Warning("OnEnter IdleState");
|
|
||||||
base.OnEnter(fsm);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnUpdate(IFsm<FsmComponent> fsm, float elapseSeconds, float realElapseSeconds)
|
|
||||||
{
|
|
||||||
base.OnUpdate(fsm, elapseSeconds, realElapseSeconds);
|
|
||||||
|
|
||||||
foreach (var command in MOVE_COMMANDS)
|
|
||||||
{
|
|
||||||
//触发任何一个移动指令时
|
|
||||||
if (Input.GetKeyDown(command))
|
|
||||||
{
|
|
||||||
//记录这个移动指令
|
|
||||||
fsm.SetData<int>("MoveCommand", (int)command);
|
|
||||||
//切换到移动状态
|
|
||||||
ChangeState<MoveState>(fsm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnLeave(IFsm<FsmComponent> fsm, bool isShutdown)
|
|
||||||
{
|
|
||||||
base.OnLeave(fsm, isShutdown);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnDestroy(IFsm<FsmComponent> fsm)
|
|
||||||
{
|
|
||||||
base.OnDestroy(fsm);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IdleState Create()
|
|
||||||
{
|
|
||||||
IdleState state = MemoryPool.Acquire<IdleState>();
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Clear()
|
|
||||||
{
|
|
||||||
//此类无状态记录,Clear为空实现
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MoveState : FsmState<FsmComponent>, IMemory
|
|
||||||
{
|
|
||||||
private static readonly float EXIT_TIME = 0.01f;
|
|
||||||
private float exitTimer;
|
|
||||||
private KeyCode moveCommand;
|
|
||||||
|
|
||||||
protected override void OnInit(IFsm<FsmComponent> fsm)
|
|
||||||
{
|
|
||||||
base.OnInit(fsm);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnEnter(IFsm<FsmComponent> fsm)
|
|
||||||
{
|
|
||||||
base.OnEnter(fsm);
|
|
||||||
|
|
||||||
//进入移动状态时,获取移动指令数据
|
|
||||||
moveCommand = (KeyCode)(int)fsm.GetData<int>("MoveCommand");
|
|
||||||
|
|
||||||
Log.Warning("OnEnter MoveState" + moveCommand);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnUpdate(IFsm<FsmComponent> fsm, float elapseSeconds, float realElapseSeconds)
|
|
||||||
{
|
|
||||||
base.OnUpdate(fsm, elapseSeconds, realElapseSeconds);
|
|
||||||
|
|
||||||
//计时器累计时间
|
|
||||||
exitTimer += elapseSeconds;
|
|
||||||
|
|
||||||
//switch(moveCommand)
|
|
||||||
//{
|
|
||||||
//根据移动方向指令向对应方向移动
|
|
||||||
//}
|
|
||||||
|
|
||||||
if (moveCommand != 0)
|
|
||||||
{
|
|
||||||
exitTimer = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//达到指定时间后
|
|
||||||
if (exitTimer > EXIT_TIME)
|
|
||||||
{
|
|
||||||
//切换回空闲状态
|
|
||||||
ChangeState<IdleState>(fsm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnLeave(IFsm<FsmComponent> fsm, bool isShutdown)
|
|
||||||
{
|
|
||||||
base.OnLeave(fsm, isShutdown);
|
|
||||||
|
|
||||||
//推出移动状态时,把计时器清零
|
|
||||||
exitTimer = 0;
|
|
||||||
//清空移动指令
|
|
||||||
moveCommand = KeyCode.None;
|
|
||||||
fsm.RemoveData("MoveCommand");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnDestroy(IFsm<FsmComponent> fsm)
|
|
||||||
{
|
|
||||||
base.OnDestroy(fsm);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MoveState Create()
|
|
||||||
{
|
|
||||||
MoveState state = MemoryPool.Acquire<MoveState>();
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Clear()
|
|
||||||
{
|
|
||||||
//还原状态内数据
|
|
||||||
exitTimer = 0;
|
|
||||||
moveCommand = KeyCode.None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ActorTestMain : MonoBehaviour
|
public class ActorTestMain : MonoBehaviour
|
||||||
{
|
{
|
||||||
private uint _actorId = 0;
|
private uint _actorId = 0;
|
||||||
|
75
Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs
Normal file
75
Assets/TEngine.Demo/TEngine.ActorDemo/PlayEntityMgr.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
using TEngine.Runtime.Entity;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace TEngine.Runtime.Actor
|
||||||
|
{
|
||||||
|
public class PlayEntityMgr:UnitySingleton<PlayEntityMgr>
|
||||||
|
{
|
||||||
|
protected override void OnLoad()
|
||||||
|
{
|
||||||
|
RegisterEvent();
|
||||||
|
|
||||||
|
base.OnLoad();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RegisterEvent
|
||||||
|
/// </summary>
|
||||||
|
void RegisterEvent()
|
||||||
|
{
|
||||||
|
GameEventMgr.Instance.AddEventListener<IEntity, float, object>(EntityEvent.ShowEntitySuccess, OnShowEntitySuccess);
|
||||||
|
GameEventMgr.Instance.AddEventListener<int, string, string, string, object>(EntityEvent.ShowEntityFailure, OnShowEntityFailure);
|
||||||
|
GameEventMgr.Instance.AddEventListener<int, string, IEntityGroup, object>(EntityEvent.HideEntityComplete, OnHideEntityComplete);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OnShowEntitySuccess
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity"></param>
|
||||||
|
/// <param name="duration"></param>
|
||||||
|
/// <param name="userData"></param>
|
||||||
|
private void OnShowEntitySuccess(IEntity entity, float duration, object userData)
|
||||||
|
{
|
||||||
|
Log.Warning("OnShowEntitySuccess" + entity.ToString() + " " + duration + " " + userData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OnShowEntityFailure
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entityId"></param>
|
||||||
|
/// <param name="entityAssetName"></param>
|
||||||
|
/// <param name="entityGroupName"></param>
|
||||||
|
/// <param name="errorMessage"></param>
|
||||||
|
/// <param name="userData"></param>
|
||||||
|
private void OnShowEntityFailure(int entityId, string entityAssetName, string entityGroupName,
|
||||||
|
string errorMessage, object userData)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OnHideEntityComplete
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entityId"></param>
|
||||||
|
/// <param name="entityAssetName"></param>
|
||||||
|
/// <param name="entityGroup"></param>
|
||||||
|
/// <param name="userData"></param>
|
||||||
|
private void OnHideEntityComplete(int entityId, string entityAssetName, IEntityGroup entityGroup,
|
||||||
|
object userData)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 玩家Actor实体管理器创建光源/ URP可最佳实践
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="lightType"></param>
|
||||||
|
/// <param name="position"></param>
|
||||||
|
/// <param name="quaternion"></param>
|
||||||
|
public void CreatePlayerEntity(int actorId,string entityPath, Vector3 position,Quaternion quaternion)
|
||||||
|
{
|
||||||
|
EntityData data = EntityData.Create(position,quaternion,actorId);
|
||||||
|
|
||||||
|
EntitySystem.Instance.CreateEntity<ActorEntity>(entityPath, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 17eab8dad38941328e42d077d6ff6c89
|
||||||
|
timeCreated: 1663917080
|
265
Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs
Normal file
265
Assets/TEngine.Demo/TEngine.ActorDemo/PlayerActor.cs
Normal file
@@ -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<ModelComponent>().BindModel(gameObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 玩家Actor,把Actor具象化成玩家
|
||||||
|
/// </summary>
|
||||||
|
public class PlayerActor:GameActor
|
||||||
|
{
|
||||||
|
public override int GetActorType()
|
||||||
|
{
|
||||||
|
return ActorType.ActorPlayer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Awake()
|
||||||
|
{
|
||||||
|
base.Awake();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnInit()
|
||||||
|
{
|
||||||
|
base.OnInit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Animator组件
|
||||||
|
/// </summary>
|
||||||
|
public class AnimatorComponent : ActorComponent
|
||||||
|
{
|
||||||
|
private Animator _animator;
|
||||||
|
|
||||||
|
protected override void Awake()
|
||||||
|
{
|
||||||
|
base.Awake();
|
||||||
|
|
||||||
|
_animator = OwnActor.gameObject.AddComponent<Animator>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模型组件
|
||||||
|
/// </summary>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态机组件
|
||||||
|
/// </summary>
|
||||||
|
public class FsmComponent : ActorComponent
|
||||||
|
{
|
||||||
|
public IFsm<FsmComponent> Fsm;
|
||||||
|
|
||||||
|
protected override void Awake()
|
||||||
|
{
|
||||||
|
base.Awake();
|
||||||
|
|
||||||
|
List<FsmState<FsmComponent>> stateList = new List<FsmState<FsmComponent>>() { IdleState.Create(), MoveState.Create() };
|
||||||
|
|
||||||
|
Fsm = FsmManager.Instance.CreateFsm(OwnActor.ActorId.ToString(), this, stateList);
|
||||||
|
|
||||||
|
Fsm.Start<IdleState>();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region 状态机
|
||||||
|
public class IdleState : FsmState<FsmComponent>, 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<FsmComponent> fsm)
|
||||||
|
{
|
||||||
|
base.OnInit(fsm);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnEnter(IFsm<FsmComponent> fsm)
|
||||||
|
{
|
||||||
|
Log.Warning("OnEnter IdleState");
|
||||||
|
base.OnEnter(fsm);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnUpdate(IFsm<FsmComponent> fsm, float elapseSeconds, float realElapseSeconds)
|
||||||
|
{
|
||||||
|
base.OnUpdate(fsm, elapseSeconds, realElapseSeconds);
|
||||||
|
|
||||||
|
foreach (var command in MOVE_COMMANDS)
|
||||||
|
{
|
||||||
|
//触发任何一个移动指令时
|
||||||
|
if (Input.GetKeyDown(command))
|
||||||
|
{
|
||||||
|
//记录这个移动指令
|
||||||
|
fsm.SetData<int>("MoveCommand", (int)command);
|
||||||
|
//切换到移动状态
|
||||||
|
ChangeState<MoveState>(fsm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnLeave(IFsm<FsmComponent> fsm, bool isShutdown)
|
||||||
|
{
|
||||||
|
base.OnLeave(fsm, isShutdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnDestroy(IFsm<FsmComponent> fsm)
|
||||||
|
{
|
||||||
|
base.OnDestroy(fsm);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IdleState Create()
|
||||||
|
{
|
||||||
|
IdleState state = MemoryPool.Acquire<IdleState>();
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
//此类无状态记录,Clear为空实现
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MoveState : FsmState<FsmComponent>, IMemory
|
||||||
|
{
|
||||||
|
private static readonly float EXIT_TIME = 0.01f;
|
||||||
|
private float exitTimer;
|
||||||
|
private KeyCode moveCommand;
|
||||||
|
|
||||||
|
protected override void OnInit(IFsm<FsmComponent> fsm)
|
||||||
|
{
|
||||||
|
base.OnInit(fsm);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnEnter(IFsm<FsmComponent> fsm)
|
||||||
|
{
|
||||||
|
base.OnEnter(fsm);
|
||||||
|
|
||||||
|
//进入移动状态时,获取移动指令数据
|
||||||
|
moveCommand = (KeyCode)(int)fsm.GetData<int>("MoveCommand");
|
||||||
|
|
||||||
|
Log.Warning("OnEnter MoveState" + moveCommand);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnUpdate(IFsm<FsmComponent> fsm, float elapseSeconds, float realElapseSeconds)
|
||||||
|
{
|
||||||
|
base.OnUpdate(fsm, elapseSeconds, realElapseSeconds);
|
||||||
|
|
||||||
|
//计时器累计时间
|
||||||
|
exitTimer += elapseSeconds;
|
||||||
|
|
||||||
|
//switch(moveCommand)
|
||||||
|
//{
|
||||||
|
//根据移动方向指令向对应方向移动
|
||||||
|
//}
|
||||||
|
|
||||||
|
if (moveCommand != 0)
|
||||||
|
{
|
||||||
|
exitTimer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//达到指定时间后
|
||||||
|
if (exitTimer > EXIT_TIME)
|
||||||
|
{
|
||||||
|
//切换回空闲状态
|
||||||
|
ChangeState<IdleState>(fsm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnLeave(IFsm<FsmComponent> fsm, bool isShutdown)
|
||||||
|
{
|
||||||
|
base.OnLeave(fsm, isShutdown);
|
||||||
|
|
||||||
|
//推出移动状态时,把计时器清零
|
||||||
|
exitTimer = 0;
|
||||||
|
//清空移动指令
|
||||||
|
moveCommand = KeyCode.None;
|
||||||
|
fsm.RemoveData("MoveCommand");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnDestroy(IFsm<FsmComponent> fsm)
|
||||||
|
{
|
||||||
|
base.OnDestroy(fsm);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MoveState Create()
|
||||||
|
{
|
||||||
|
MoveState state = MemoryPool.Acquire<MoveState>();
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
//还原状态内数据
|
||||||
|
exitTimer = 0;
|
||||||
|
moveCommand = KeyCode.None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b11e802b8a2a446781dc687f828f448a
|
||||||
|
timeCreated: 1663916847
|
Reference in New Issue
Block a user