using System; using System.Collections.Generic; using BITKit.StateMachine; using UnityEngine; using UnityEngine.InputSystem; namespace BITKit.Entities { /// /// 期望状态与当前状态 /// /// [Serializable] public struct ExpectState { /// /// 期望状态 /// public T shouldBe; /// /// 当前状态 /// public T being; /// /// 强制状态 /// public T force; public bool UnExpect => !shouldBe.Equals(being); public void Reset() { shouldBe = being = force = default; } public void Release() { being = shouldBe; } public void Release(T value) { force = default; being = shouldBe = value; } public static implicit operator ExpectState(T value) { return new ExpectState { shouldBe = value, being = value }; } public static implicit operator T(ExpectState value) { return value.being; } } /// /// Entity移动组件接口定义 /// public interface IEntityMovement:IStateMachine { /// /// 世界空间的速度 /// Vector3 Velocity { get; } /// /// 在地面的速度 = with {y=0} /// Vector3 GroundVelocity { get; } /// /// 是否在地面上 /// bool IsGrounded { get; } /// /// 同步移动,用于网络或强制同步移动信息 /// /// /// /// /// void SyncMovement(Vector3 velocity, Vector3 position,Quaternion rotation,bool isGrounded); /// /// 基于相对坐标的移动 /// /// void Movement(Vector3 relativeVector); /// /// 基于InputAction的移动 /// /// void Movement(InputAction.CallbackContext context); void ExecuteCommand(T command=default); } public interface IEntityMovementState:IState { void UpdateVelocity(ref Vector3 currentVelocity,float deltaTime); void UpdateRotation(ref Quaternion currentRotation,float deltaTime); void BeforeUpdateMovement(float deltaTime); void AfterUpdateMovement(float deltaTime); void ExecuteCommand(T command); } public interface IPlayerMovementCommand{} [Serializable] public class MonoMovementProxy:IEntityMovement { [SerializeField] private MonoBehaviour monoBehaviour; private IEntityMovement _entityMovementImplementation=>monoBehaviour as IEntityMovement; public Vector3 Velocity => _entityMovementImplementation.Velocity; public Vector3 GroundVelocity => _entityMovementImplementation.GroundVelocity; public bool IsGrounded => _entityMovementImplementation.IsGrounded; public void SyncMovement(Vector3 velocity, Vector3 position, Quaternion rotation, bool isGrounded) { _entityMovementImplementation.SyncMovement(velocity, position, rotation, isGrounded); } public void Movement(Vector3 relativeVector) { _entityMovementImplementation.Movement(relativeVector); } public void Movement(InputAction.CallbackContext context) { _entityMovementImplementation.Movement(context); } public void ExecuteCommand(T command)=>_entityMovementImplementation.ExecuteCommand(command); public bool Enabled { get => _entityMovementImplementation.Enabled; set => _entityMovementImplementation.Enabled = value; } public IEntityMovementState CurrentState { get => _entityMovementImplementation.CurrentState; set => _entityMovementImplementation.CurrentState = value; } public event Action OnStateChanged { add => _entityMovementImplementation.OnStateChanged += value; remove => _entityMovementImplementation.OnStateChanged -= value; } public IDictionary StateDictionary => _entityMovementImplementation.StateDictionary; public void Initialize() { _entityMovementImplementation.Initialize(); } public void UpdateState() { _entityMovementImplementation.UpdateState(); } public void DisposeState() { _entityMovementImplementation.DisposeState(); } public void TransitionState() where State : IEntityMovementState { _entityMovementImplementation.TransitionState(); } public void TransitionState(IEntityMovementState state) { _entityMovementImplementation.TransitionState(state); } } }