141 lines
4.1 KiB
C#
141 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BITKit.StateMachine;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace BITKit.Entities
|
|
{
|
|
/// <summary>
|
|
/// 期望状态与当前状态
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
[Serializable]
|
|
public struct ExpectState<T>
|
|
{
|
|
/// <summary>
|
|
/// 期望状态
|
|
/// </summary>
|
|
public T shouldBe;
|
|
|
|
/// <summary>
|
|
/// 当前状态
|
|
/// </summary>
|
|
public T being;
|
|
|
|
/// <summary>
|
|
/// 强制状态
|
|
/// </summary>
|
|
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>(T value)
|
|
{
|
|
return new ExpectState<T> { shouldBe = value, being = value };
|
|
}
|
|
public static implicit operator T(ExpectState<T> value)
|
|
{
|
|
return value.being;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entity移动组件接口定义
|
|
/// </summary>
|
|
public interface IEntityMovement:IStateMachine<IEntityMovementState>
|
|
{
|
|
Vector3 Position { get; set; }
|
|
Quaternion Rotation { get; set; }
|
|
Vector3 Forward { get; }
|
|
/// <summary>
|
|
/// 视角前方,通常是摄像机的前方
|
|
/// </summary>
|
|
Vector3 ViewForward { get; }
|
|
/// <summary>
|
|
/// 视角中心,通常是摄像机的位置
|
|
/// </summary>
|
|
Vector3 ViewCenter { get; }
|
|
/// <summary>
|
|
/// 注视点
|
|
/// </summary>
|
|
Vector3 FocusPoint { get; }
|
|
/// <summary>
|
|
/// 视角旋转,通常是摄像机的旋转
|
|
/// </summary>
|
|
Quaternion ViewRotation { get; }
|
|
/// <summary>
|
|
/// 基于运动的速度,是相对于标准化移动速度的相对速度
|
|
/// </summary>
|
|
Vector3 LocomotionBasedVelocity { get; }
|
|
/// <summary>
|
|
/// 世界空间的速度
|
|
/// </summary>
|
|
Vector3 Velocity { get; }
|
|
/// <summary>
|
|
/// 在地面的速度 = with <see cref="Velocity"/> {y=0}
|
|
/// </summary>
|
|
Vector3 GroundVelocity { get; }
|
|
/// <summary>
|
|
/// 旋转速度
|
|
/// </summary>
|
|
Vector3 AngularVelocity { get; }
|
|
/// <summary>
|
|
/// 是否在地面上
|
|
/// </summary>
|
|
bool IsGrounded { get; }
|
|
/// <summary>
|
|
/// 同步移动,用于网络或强制同步移动信息
|
|
/// </summary>
|
|
/// <param name="velocity"></param>
|
|
/// <param name="position"></param>
|
|
/// <param name="rotation"></param>
|
|
/// <param name="isGrounded"></param>
|
|
void SyncMovement(Vector3 velocity, Vector3 position,Quaternion rotation,bool isGrounded);
|
|
/// <summary>
|
|
/// 基于相对坐标的移动
|
|
/// </summary>
|
|
/// <param name="relativeVector"></param>
|
|
void Movement(Vector3 relativeVector);
|
|
/// <summary>
|
|
/// 基于InputAction的移动
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
void Movement(InputAction.CallbackContext context);
|
|
/// <summary>
|
|
/// 执行命令
|
|
/// </summary>
|
|
/// <param name="command"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
void ExecuteCommand<T>(T command=default);
|
|
/// <summary>
|
|
/// 执行命令的回调
|
|
/// </summary>
|
|
event Action<object> OnCommand;
|
|
}
|
|
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>(T command);
|
|
}
|
|
} |