mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:35:34 +00:00
Navigation improvements
This commit is contained in:
parent
944249d408
commit
05047da6b3
7 changed files with 1033 additions and 549 deletions
|
|
@ -8,47 +8,77 @@ public partial class NavigationProvider3D : Node
|
|||
private Vector3? _lastTargetPosition;
|
||||
private CharacterBody3D _characterBody;
|
||||
//private NavigationAgent3D _navigationAgent;
|
||||
|
||||
[Export] public NavigationAgent3D NavigationAgent { get; private set; }
|
||||
|
||||
|
||||
[ExportCategory("References")]
|
||||
[Export]
|
||||
public EnemyStorage3D StorageModule { get; private set; }
|
||||
public NavigationAgent3D NavigationAgent { get; private set; }
|
||||
|
||||
[Export] public EnemyStorage3D StorageModule { get; private set; }
|
||||
|
||||
[ExportCategory("Properties")]
|
||||
[Export]
|
||||
public float ProcessingRate { get; private set; } = 0.1f;
|
||||
|
||||
private Vector3? _nextPathPosition;
|
||||
|
||||
public void Init(CharacterBody3D characterBody)
|
||||
{
|
||||
_characterBody = characterBody;
|
||||
|
||||
if (NavigationAgent is not null) return;
|
||||
//_navigationAgent = this.GetNode<NavigationAgent3D>("NavigationAgent");
|
||||
|
||||
// var timer = new Timer();
|
||||
// this.AddChild(timer);
|
||||
//
|
||||
// timer.Timeout += TimerOnTimeout;
|
||||
//
|
||||
// timer.Start(ProcessingRate);
|
||||
}
|
||||
|
||||
|
||||
private void TimerOnTimeout()
|
||||
{
|
||||
if (!_lastTargetPosition.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (NavigationAgent.IsNavigationFinished())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_nextPathPosition = NavigationAgent.GetNextPathPosition();
|
||||
}
|
||||
|
||||
public void SetTarget(Vector3? target)
|
||||
{
|
||||
_lastTargetPosition = 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;
|
||||
_nextPathPosition = NavigationAgent.GetNextPathPosition();
|
||||
|
||||
var newVelocity = !_nextPathPosition.HasValue
|
||||
? Vector3.Zero
|
||||
: currentAgentPosition.DirectionTo(_nextPathPosition.Value) * movementSpeed;
|
||||
|
||||
newVelocity += StorageModule.KnockbackVelocity;
|
||||
|
||||
|
||||
if (NavigationAgent.AvoidanceEnabled)
|
||||
{
|
||||
NavigationAgent.SetVelocity(newVelocity);
|
||||
|
|
@ -57,10 +87,8 @@ public partial class NavigationProvider3D : Node
|
|||
{
|
||||
_on_navigation_agent_3d_velocity_computed(newVelocity);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void _on_navigation_agent_3d_velocity_computed(Vector3 safeVelocity)
|
||||
{
|
||||
if (_characterBody is null) return;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue