ActorDemo

ActorDemo
This commit is contained in:
ALEXTANG
2022-09-23 15:34:19 +08:00
parent f775c2abfb
commit 9f14200da7
5 changed files with 346 additions and 211 deletions

View File

@@ -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<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
{
private uint _actorId = 0;