BITFALL/Assets/Artists/Scripts/Entities/Movement/NavAgentMovement.cs

155 lines
3.5 KiB
C#
Raw Normal View History

2023-08-23 01:59:40 +08:00
using System;
2023-11-15 23:54:54 +08:00
using BITFALL;
2023-08-12 01:43:24 +08:00
using BITKit.Entities;
2023-12-16 23:30:08 +08:00
using BITKit.Entities.Physics;
2023-08-12 01:43:24 +08:00
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
namespace BITKit
{
2023-11-15 23:54:54 +08:00
[CustomType(typeof(IEntityMovement))]
2023-10-30 01:25:53 +08:00
public class NavAgentMovement: StateBasedBehavior<IEntityMovementState>,IEntityMovement
2023-08-12 01:43:24 +08:00
{
#region
[SerializeField] private NavMeshAgent agent;
[SerializeField] private new Rigidbody rigidbody;
2023-10-20 19:31:12 +08:00
[Inject] private IHealth _health;
2023-10-24 23:37:59 +08:00
[Inject(true)] private IEntityOverride _override;
2023-12-16 23:30:08 +08:00
[Inject(true)]
private IEntityPhysics _physics;
2023-08-12 01:43:24 +08:00
#endregion
2023-10-20 19:31:12 +08:00
public override void OnStart()
{
2023-10-24 23:37:59 +08:00
if (_override is not null)
{
_override.OnOverride += (x) =>
{
agent.isStopped = x;
};
}
2023-10-20 19:31:12 +08:00
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHP;
2023-11-15 23:54:54 +08:00
recordRotation = Transform.rotation;
recordPosition = Transform.position;
2023-10-24 23:37:59 +08:00
2023-10-20 19:31:12 +08:00
}
2023-08-12 01:43:24 +08:00
#region
2023-08-27 02:58:19 +08:00
2023-10-24 23:37:59 +08:00
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;
2023-11-30 00:23:23 +08:00
public Vector3 ViewForward => transform.forward;
public Vector3 ViewCenter => Vector3.up;
public Vector3 FocusPoint { get; }
2023-11-15 23:54:54 +08:00
public Quaternion ViewRotation => transform.rotation;
2023-08-27 02:58:19 +08:00
public Vector3 LocomotionBasedVelocity { get; private set; }
2023-08-12 01:43:24 +08:00
public Vector3 Velocity { get; private set; }
public Vector3 GroundVelocity { get; private set; }
2023-09-01 14:33:54 +08:00
public Vector3 AngularVelocity { get; private set; }
2023-08-12 01:43:24 +08:00
public bool IsGrounded { get; private set; }
private bool isDead;
private Vector3 recordPosition;
private Quaternion recordRotation;
2023-08-23 01:59:40 +08:00
private IEntityMovement _entityMovementImplementation;
2023-08-12 01:43:24 +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;
2023-11-30 00:23:23 +08:00
var localVelocity = transform.InverseTransformDirection(_groundVelocity);
2023-11-15 23:54:54 +08:00
UnityEntity.SetDirect(BITHash.Player.IsMoving,Velocity.sqrMagnitude>=0.16f);
UnityEntity.SetDirect(BITHash.Player.SqrMagnitude,Velocity.sqrMagnitude);
2023-11-30 00:23:23 +08:00
UnityEntity.SetDirect(BITHash.Player.Vertical,localVelocity.z);
UnityEntity.SetDirect(BITHash.Player.Horizontal,localVelocity.x);
2023-10-20 19:31:12 +08:00
2023-08-12 01:43:24 +08:00
if (!isDead) return;
recordPosition = rigidbody.position;
recordRotation = rigidbody.rotation * transform.rotation;
}
public void SyncMovement(Vector3 velocity, Vector3 position, Quaternion rotation, bool isGrounded)
{
}
2023-12-15 20:03:44 +08:00
public void OnMovement(Vector3 relativeVector)
2023-08-12 01:43:24 +08:00
{
}
2023-12-15 20:03:44 +08:00
public void OnMovement(InputAction.CallbackContext context)
2023-08-12 01:43:24 +08:00
{
2023-08-23 01:59:40 +08:00
throw new NotImplementedException();
}
public void ExecuteCommand<T>(T command)
{
throw new NotImplementedException();
2023-08-12 01:43:24 +08:00
}
2023-09-01 14:33:54 +08:00
public event Action<object> OnCommand;
2023-08-12 01:43:24 +08:00
public void OnSetAlive(bool alive)
{
switch (alive)
{
case false:
isDead = true;
2023-12-16 23:30:08 +08:00
if (_physics is not null)
{
_physics.Velocity = agent.velocity;
}
agent.ResetPath();
2023-08-12 01:43:24 +08:00
break;
case true when isDead:
{
2023-08-23 01:59:40 +08:00
var _transform = transform;
_transform.position = new Vector3()
2023-08-12 01:43:24 +08:00
{
x=recordPosition.x,
y=0,
z=recordPosition.x,
};
2023-11-30 00:23:23 +08:00
//rigidbody.position = recordPosition;
2023-08-12 01:43:24 +08:00
2023-11-30 00:23:23 +08:00
//_transform.rotation *= recordRotation;
2023-08-12 01:43:24 +08:00
2023-11-30 00:23:23 +08:00
//rigidbody.rotation = recordRotation;
2023-08-12 01:43:24 +08:00
isDead = false;
break;
}
}
}
public void OnSetHP(int hp)
{
}
2023-11-15 23:54:54 +08:00
public void FootStep(){}
2023-08-12 01:43:24 +08:00
}
}