Disabled navigation

This commit is contained in:
Marco 2025-01-24 15:24:37 +01:00
commit 3681614196
7 changed files with 126 additions and 4 deletions

View file

@ -10,19 +10,33 @@ public partial class Enemy : Area2D, IDestructible
[Export] public float Health = 4f;
[Export] public float WalkSpeed = 2500f;
[Export] public Weapon EquippedWeapon;
private float _currentHealth = 0f;
private bool _isDestroyed = false;
private NavigationAgent2D _navigationAgent;
[Export] public bool NavigationEnabled { get; set; } = false;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_currentHealth = Health;
_navigationAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");
CallDeferred("Setup");
}
private void Setup()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
@ -48,6 +62,18 @@ public partial class Enemy : Area2D, IDestructible
public override void _PhysicsProcess(double delta)
{
HandlePlayerDetection();
if (NavigationEnabled)
{
var moveLocation = _navigationAgent.GetNextPathPosition();
if (_currentState is EnemyState.Primed)
{
this.Position = moveLocation;
}
}
}
private void HandlePlayerDetection()
@ -59,7 +85,14 @@ public partial class Enemy : Area2D, IDestructible
if (IsPlayerInSight())
{
// Update player position only if player is in sight
if (NavigationEnabled)
{
_navigationAgent.SetTargetPosition(_cachedPlayer.GlobalPosition);
}
Shoot();
}
}
@ -161,6 +194,7 @@ public partial class Enemy : Area2D, IDestructible
private enum EnemyState
{
Idle,
Primed
Primed,
Patrolling
}
}