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

90 lines
1.9 KiB
C#
Raw Normal View History

2023-08-23 01:59:26 +08:00
using System;
2023-06-29 14:57:11 +08:00
using BITKit.Entities;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
namespace BITKit
{
2023-08-23 01:59:26 +08:00
public class NavAgentMovement: StateBasedComponent<IEntityMovementState>,IEntityMovement,IHealthCallback
2023-06-29 14:57:11 +08:00
{
#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;
2023-08-23 01:59:26 +08:00
private IEntityMovement _entityMovementImplementation;
2023-06-29 14:57:11 +08:00
#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)
{
2023-08-23 01:59:26 +08:00
throw new NotImplementedException();
}
public void ExecuteCommand<T>(T command)
{
throw new NotImplementedException();
2023-06-29 14:57:11 +08:00
}
public void OnSetAlive(bool alive)
{
switch (alive)
{
case false:
isDead = true;
break;
case true when isDead:
{
2023-08-23 01:59:26 +08:00
var _transform = transform;
_transform.position = new Vector3()
2023-06-29 14:57:11 +08:00
{
x=recordPosition.x,
y=0,
z=recordPosition.x,
};
rigidbody.position = recordPosition;
2023-08-23 01:59:26 +08:00
_transform.rotation *= recordRotation;
2023-06-29 14:57:11 +08:00
rigidbody.rotation = recordRotation;
isDead = false;
break;
}
}
}
public void OnSetHP(int hp)
{
}
}
}