BITFALL/Assets/BITKit/Unity/Scripts/Entity/Components/EntityMovement/EntityMovement.cs

141 lines
4.1 KiB
C#
Raw Normal View History

2023-08-23 01:59:40 +08:00
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>
{
2023-10-24 23:37:59 +08:00
Vector3 Position { get; set; }
Quaternion Rotation { get; set; }
Vector3 Forward { get; }
2023-11-30 00:23:23 +08:00
/// <summary>
/// 视角前方,通常是摄像机的前方
/// </summary>
Vector3 ViewForward { get; }
2023-09-01 14:33:54 +08:00
/// <summary>
/// 视角中心,通常是摄像机的位置
/// </summary>
Vector3 ViewCenter { get; }
/// <summary>
2023-11-30 00:23:23 +08:00
/// 注视点
/// </summary>
Vector3 FocusPoint { get; }
/// <summary>
2023-09-01 14:33:54 +08:00
/// 视角旋转,通常是摄像机的旋转
/// </summary>
Quaternion ViewRotation { get; }
2023-08-27 02:58:19 +08:00
/// <summary>
/// 基于运动的速度,是相对于标准化移动速度的相对速度
/// </summary>
Vector3 LocomotionBasedVelocity { get; }
2023-08-23 01:59:40 +08:00
/// <summary>
/// 世界空间的速度
/// </summary>
Vector3 Velocity { get; }
/// <summary>
/// 在地面的速度 = with <see cref="Velocity"/> {y=0}
/// </summary>
Vector3 GroundVelocity { get; }
/// <summary>
2023-09-01 14:33:54 +08:00
/// 旋转速度
/// </summary>
Vector3 AngularVelocity { get; }
/// <summary>
2023-08-23 01:59:40 +08:00
/// 是否在地面上
/// </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);
2023-09-01 14:33:54 +08:00
/// <summary>
/// 执行命令
/// </summary>
/// <param name="command"></param>
/// <typeparam name="T"></typeparam>
2023-08-23 01:59:40 +08:00
void ExecuteCommand<T>(T command=default);
2023-09-01 14:33:54 +08:00
/// <summary>
/// 执行命令的回调
/// </summary>
event Action<object> OnCommand;
2023-08-23 01:59:40 +08:00
}
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);
}
}