using System; using BITKit.Entities; using UnityEngine; using UnityEngine.AI; using UnityEngine.InputSystem; namespace BITKit { public class NavAgentMovement: StateBasedComponent,IEntityMovement { #region 属性 [SerializeField] private NavMeshAgent agent; [SerializeField] private new Rigidbody rigidbody; [Inject] private IHealth _health; [Inject(true)] private IEntityOverride _override; #endregion public override void OnStart() { if (_override is not null) { _override.OnOverride += (x) => { agent.isStopped = x; }; } _health.OnSetAlive += OnSetAlive; _health.OnSetHealthPoint += OnSetHP; } #region 接口实现 public Vector3 Position { get=>transform.position; set=>transform.position=value; } public Quaternion Rotation { get=>transform.rotation; set=>transform.rotation=value; } public Vector3 Forward => transform.forward; public Vector3 ViewCenter { get; set; } public Quaternion ViewRotation { get; set; } public Vector3 LocomotionBasedVelocity { get; private set; } public Vector3 Velocity { get; private set; } public Vector3 GroundVelocity { get; private set; } public Vector3 AngularVelocity { get; private set; } public bool IsGrounded { get; private set; } private bool isDead; private Vector3 recordPosition; private Quaternion recordRotation; private IEntityMovement _entityMovementImplementation; #endregion public override void OnUpdate(float deltaTime) { Velocity = agent.velocity; var _groundVelocity = Velocity; _groundVelocity.y = 0; GroundVelocity = _groundVelocity; IsGrounded = agent.isOnOffMeshLink is false; entity.Set("IsMoving",Velocity.sqrMagnitude>=0.16f); entity.Set("SqrMagnitude",Velocity.sqrMagnitude); if (!isDead) return; recordPosition = rigidbody.position; recordRotation = rigidbody.rotation * transform.rotation; } public void SyncMovement(Vector3 velocity, Vector3 position, Quaternion rotation, bool isGrounded) { } public void Movement(Vector3 relativeVector) { } public void Movement(InputAction.CallbackContext context) { throw new NotImplementedException(); } public void ExecuteCommand(T command) { throw new NotImplementedException(); } public event Action OnCommand; public void OnSetAlive(bool alive) { switch (alive) { case false: isDead = true; break; case true when isDead: { var _transform = transform; _transform.position = new Vector3() { x=recordPosition.x, y=0, z=recordPosition.x, }; rigidbody.position = recordPosition; _transform.rotation *= recordRotation; rigidbody.rotation = recordRotation; isDead = false; break; } } } public void OnSetHP(int hp) { } } }