BITKit/Packages/Runtime~/Unity/Scripts/Entity/Components/EntityMovement/NavAgentMovement.cs

90 lines
1.9 KiB
C#

using System;
using BITKit.Entities;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
namespace BITKit
{
public class NavAgentMovement: StateBasedComponent<IEntityMovementState>,IEntityMovement,IHealthCallback
{
#region
[SerializeField] private NavMeshAgent agent;
[SerializeField] private new Rigidbody rigidbody;
#endregion
#region
public Vector3 Velocity { get; private set; }
public Vector3 GroundVelocity { 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;
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>(T command)
{
throw new NotImplementedException();
}
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)
{
}
}
}