mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-12 04:55:55 +00:00
Enemy AI
This commit is contained in:
parent
383fc740df
commit
ede8f2028a
34 changed files with 1418 additions and 417 deletions
84
Scripts/Components/FSM/Enemy/3D/NavigationProvider3D.cs
Normal file
84
Scripts/Components/FSM/Enemy/3D/NavigationProvider3D.cs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy._3D;
|
||||
|
||||
public partial class NavigationProvider3D : Node
|
||||
{
|
||||
private Vector3? _lastTargetPosition;
|
||||
private CharacterBody3D _characterBody;
|
||||
//private NavigationAgent3D _navigationAgent;
|
||||
|
||||
[Export] public NavigationAgent3D NavigationAgent { get; private set; }
|
||||
|
||||
[Export]
|
||||
public EnemyStorage3D StorageModule { get; private set; }
|
||||
|
||||
public void Init(CharacterBody3D characterBody)
|
||||
{
|
||||
_characterBody = characterBody;
|
||||
|
||||
if (NavigationAgent is not null) return;
|
||||
//_navigationAgent = this.GetNode<NavigationAgent3D>("NavigationAgent");
|
||||
}
|
||||
|
||||
public void SetTarget(Vector3? target)
|
||||
{
|
||||
_lastTargetPosition = target;
|
||||
}
|
||||
|
||||
public void Move(float movementSpeed)
|
||||
{
|
||||
if (!_lastTargetPosition.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NavigationAgent.SetTargetPosition(_lastTargetPosition.Value);
|
||||
|
||||
var currentAgentPosition = _characterBody.GlobalPosition;
|
||||
|
||||
if (NavigationAgent.IsNavigationFinished())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var nextPathPosition = NavigationAgent.GetNextPathPosition();
|
||||
|
||||
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * movementSpeed;
|
||||
|
||||
newVelocity += StorageModule.KnockbackVelocity;
|
||||
|
||||
if (NavigationAgent.AvoidanceEnabled)
|
||||
{
|
||||
NavigationAgent.SetVelocity(newVelocity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_on_navigation_agent_3d_velocity_computed(newVelocity);
|
||||
}
|
||||
|
||||
_characterBody.MoveAndSlide();
|
||||
}
|
||||
|
||||
public void _on_navigation_agent_3d_velocity_computed(Vector3 safeVelocity)
|
||||
{
|
||||
if (_characterBody is null) return;
|
||||
_characterBody.Velocity = safeVelocity;
|
||||
if (_characterBody.Velocity.Length() > 0)
|
||||
{
|
||||
StorageModule.FacingDirection = _characterBody.Velocity.Normalized().ToVector2();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsNavigable(Vector3 newPos)
|
||||
{
|
||||
NavigationAgent.SetTargetPosition(newPos);
|
||||
return NavigationAgent.IsTargetReachable();
|
||||
}
|
||||
|
||||
public bool IsNavigationFinished()
|
||||
{
|
||||
return NavigationAgent.IsNavigationFinished();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue