Navigation for Actors

This commit is contained in:
MaddoScientisto 2025-02-18 23:39:22 +01:00
commit b78692f150
4 changed files with 11 additions and 8 deletions

View file

@ -228,7 +228,7 @@ radius = 85.0529
[node name="ActorEnemyTest" type="CharacterBody2D"] [node name="ActorEnemyTest" type="CharacterBody2D"]
collision_layer = 16 collision_layer = 16
collision_mask = 115 collision_mask = 113
script = ExtResource("1_k5cyk") script = ExtResource("1_k5cyk")
MovementSpeed = 30.0 MovementSpeed = 30.0

View file

@ -517,7 +517,7 @@ text = "Briefing Test"
label_settings = ExtResource("14_c4c20") label_settings = ExtResource("14_c4c20")
[node name="ActorEnemyTest" parent="Computer4" instance=ExtResource("55_chha6")] [node name="ActorEnemyTest" parent="Computer4" instance=ExtResource("55_chha6")]
position = Vector2(100, 104) position = Vector2(142, 51)
[node name="Ammo1" parent="." instance=ExtResource("34_17pjh")] [node name="Ammo1" parent="." instance=ExtResource("34_17pjh")]
position = Vector2(-792, -396) position = Vector2(-792, -396)

View file

@ -53,7 +53,7 @@ public partial class EnemyNavigationMovement : MovementHandler
FacingDirection = Vector2.Down; FacingDirection = Vector2.Down;
_actorAi = parent.GetNode<ActorAi>("ActorAi"); _actorAi = parent.GetNode<ActorAi>("ActorAi");
_navigationAgent = GetNodeOrNull<NavigationAgent2D>("NavigationAgent2D"); _navigationAgent = _parent.GetNodeOrNull<NavigationAgent2D>("NavigationAgent2D");
_alarmManager = this.GetAlarmManager(); _alarmManager = this.GetAlarmManager();
if (_alarmManager != null) if (_alarmManager != null)
@ -85,6 +85,7 @@ public partial class EnemyNavigationMovement : MovementHandler
if (_playerDetection != null && _playerDetection.IsPlayerInSight(_parent.CollisionMask)) if (_playerDetection != null && _playerDetection.IsPlayerInSight(_parent.CollisionMask))
{ {
_actorAi.State = EnemyState.Alert; _actorAi.State = EnemyState.Alert;
GD.Print("Switching to alert");
} }
break; break;
case EnemyState.Alert: case EnemyState.Alert:
@ -102,7 +103,7 @@ public partial class EnemyNavigationMovement : MovementHandler
_navigationAgent.SetTargetPosition(_lastPlayerPosition.Value); _navigationAgent.SetTargetPosition(_lastPlayerPosition.Value);
} }
var currentAgentPosition = GlobalPosition; var currentAgentPosition = _parent.GlobalPosition;
var nextPathPosition = _navigationAgent.GetNextPathPosition(); var nextPathPosition = _navigationAgent.GetNextPathPosition();
@ -116,6 +117,7 @@ public partial class EnemyNavigationMovement : MovementHandler
if (IsPlayerInSight) if (IsPlayerInSight)
{ {
//Shoot(); //Shoot();
} }
// TODO: If player totally left the max range it should stop shooting and go back to idle // TODO: If player totally left the max range it should stop shooting and go back to idle
@ -140,7 +142,9 @@ public partial class EnemyNavigationMovement : MovementHandler
if (IsPlayerInSight) if (IsPlayerInSight)
{ {
//Shoot(); //Shoot();
} }
} }
break; break;
@ -151,6 +155,9 @@ public partial class EnemyNavigationMovement : MovementHandler
public void _on_navigation_agent_2d_velocity_computed(Vector2 safeVelocity) public void _on_navigation_agent_2d_velocity_computed(Vector2 safeVelocity)
{ {
_parent.Velocity = safeVelocity; _parent.Velocity = safeVelocity;
if (_parent.Velocity.Length() > 0) {
_parent.FacingDirection = _parent.Velocity.Normalized();
}
} }

View file

@ -307,10 +307,6 @@ public partial class Enemy : CharacterBody2D
GD.Print("Assuming direct control"); GD.Print("Assuming direct control");
Ai = AiState.Controlled; Ai = AiState.Controlled;
} }
} }
public enum EnemyState public enum EnemyState