Files
TEngine/Assets/TEngine.Demo/TEngine.ActorDemo/ActorTestMain.cs
ALEXTANG a23f74c2f9 增加Demo示例
增加Demo示例
2022-09-23 10:38:22 +08:00

249 lines
6.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<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;
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<ModelComponent>();
actor.Attach<FsmComponent>();
actor.Attach<AnimatorComponent>();
}
}