mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:05:34 +00:00
Enemy Navigation Movement
This commit is contained in:
parent
388747ccb3
commit
9d302e48e6
5 changed files with 247 additions and 15 deletions
12
Scripts/Components/Actors/ActorAi.cs
Normal file
12
Scripts/Components/Actors/ActorAi.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using Godot;
|
||||
|
||||
public partial class ActorAi : Node2D
|
||||
{
|
||||
|
||||
public EnemyState State { get; set; } = EnemyState.Idle;
|
||||
|
||||
[Export] // Temp until the special actor tha sets it
|
||||
public AiState Ai { get; set; } = AiState.Disabled;
|
||||
|
||||
|
||||
}
|
||||
157
Scripts/Components/Actors/EnemyNavigationMovement.cs
Normal file
157
Scripts/Components/Actors/EnemyNavigationMovement.cs
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
using Godot;
|
||||
using Cirno.Scripts;
|
||||
using Cirno.Scripts.Components;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cirno.Scripts.Components.Actors;
|
||||
|
||||
public partial class EnemyNavigationMovement : MovementHandler
|
||||
{
|
||||
public override Vector2 FacingDirection
|
||||
{
|
||||
get => _parent.FacingDirection;
|
||||
set => _parent.FacingDirection = value;
|
||||
}
|
||||
|
||||
public override Vector2 MovementDirection
|
||||
{
|
||||
get => _parent.MovementDirection;
|
||||
set => _parent.MovementDirection = value;
|
||||
}
|
||||
|
||||
[Export]
|
||||
private bool _navigationEnabled = false;
|
||||
|
||||
[Export] public float AlarmReactRange = 200f;
|
||||
|
||||
[Export] public float PlayerDisengageRange = 500f;
|
||||
|
||||
public bool NavigationEnabled
|
||||
{
|
||||
get => _actorAi is not null && _actorAi.Ai is AiState.Enabled && _navigationEnabled && _navigationAgent != null;
|
||||
set => _navigationEnabled = value;
|
||||
}
|
||||
|
||||
[Export]
|
||||
private PlayerDetection _playerDetection;
|
||||
|
||||
private ActorAi _actorAi;
|
||||
private NavigationAgent2D _navigationAgent;
|
||||
private AlarmManager _alarmManager;
|
||||
|
||||
private Vector2? _lastPlayerPosition = null;
|
||||
|
||||
private bool IsPlayerInRange => _playerDetection is { IsPlayerInRange: true };
|
||||
|
||||
private bool IsPlayerInSight => _playerDetection is not null && _playerDetection.IsPlayerInSight(_parent.CollisionMask);
|
||||
|
||||
public override void Init(Actor parent)
|
||||
{
|
||||
base.Init(parent);
|
||||
|
||||
MovementDirection = Vector2.Zero;
|
||||
FacingDirection = Vector2.Down;
|
||||
|
||||
_actorAi = parent.GetNode<ActorAi>("ActorAi");
|
||||
_navigationAgent = GetNodeOrNull<NavigationAgent2D>("NavigationAgent2D");
|
||||
_alarmManager = this.GetAlarmManager();
|
||||
|
||||
if (_alarmManager != null)
|
||||
{
|
||||
_alarmManager.AlarmEnabled += AlarmManagerOnAlarmEnabled;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void AlarmManagerOnAlarmEnabled(Vector2 location)
|
||||
{
|
||||
if (NavigationEnabled && location.DistanceTo(this.GlobalPosition) <= AlarmReactRange)
|
||||
{
|
||||
GD.Print($"Enemy {Name} alerted");
|
||||
_actorAi.State = EnemyState.Alert;
|
||||
_lastPlayerPosition = location;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Move(double delta)
|
||||
{
|
||||
if (_actorAi.Ai is not AiState.Enabled)
|
||||
return;
|
||||
|
||||
|
||||
switch (_actorAi.State)
|
||||
{
|
||||
case EnemyState.Idle:
|
||||
if (_playerDetection != null && _playerDetection.IsPlayerInSight(_parent.CollisionMask))
|
||||
{
|
||||
_actorAi.State = EnemyState.Alert;
|
||||
}
|
||||
break;
|
||||
case EnemyState.Alert:
|
||||
|
||||
// Update last known player position if it's in range
|
||||
if (IsPlayerInRange)
|
||||
{
|
||||
_lastPlayerPosition = _playerDetection.CachedPlayer.GlobalPosition;
|
||||
}
|
||||
|
||||
if (NavigationEnabled)
|
||||
{
|
||||
if (_lastPlayerPosition.HasValue)
|
||||
{
|
||||
_navigationAgent.SetTargetPosition(_lastPlayerPosition.Value);
|
||||
}
|
||||
|
||||
var currentAgentPosition = GlobalPosition;
|
||||
|
||||
var nextPathPosition = _navigationAgent.GetNextPathPosition();
|
||||
|
||||
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * _parent.MovementSpeed;
|
||||
|
||||
|
||||
// Navigation is over, can do other things like shooting
|
||||
if (_navigationAgent.IsNavigationFinished())
|
||||
{
|
||||
// Shoot player
|
||||
if (IsPlayerInSight)
|
||||
{
|
||||
//Shoot();
|
||||
}
|
||||
|
||||
// TODO: If player totally left the max range it should stop shooting and go back to idle
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (_navigationAgent.AvoidanceEnabled)
|
||||
{
|
||||
_navigationAgent.SetVelocity(newVelocity);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
_on_navigation_agent_2d_velocity_computed(newVelocity);
|
||||
}
|
||||
|
||||
_parent.MoveAndSlide();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (IsPlayerInSight)
|
||||
{
|
||||
//Shoot();
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void _on_navigation_agent_2d_velocity_computed(Vector2 safeVelocity)
|
||||
{
|
||||
_parent.Velocity = safeVelocity;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
23
Scripts/Components/Actors/EnemyPossessionMovement.cs
Normal file
23
Scripts/Components/Actors/EnemyPossessionMovement.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using Godot;
|
||||
|
||||
public partial class EnemyPossessionMovement : ActorFreeMovement
|
||||
{
|
||||
|
||||
private ActorAi _actorAi;
|
||||
|
||||
// State accessor
|
||||
|
||||
public override void Init(Actor parent)
|
||||
{
|
||||
base.Init(parent);
|
||||
|
||||
_actorAi = parent.GetNode<ActorAi>("ActorAi");
|
||||
}
|
||||
|
||||
public override void Move(double delta)
|
||||
{
|
||||
if (_actorAi.Ai is AiState.Controlled)
|
||||
base.Move(delta);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -308,17 +308,21 @@ public partial class Enemy : CharacterBody2D
|
|||
Ai = AiState.Controlled;
|
||||
}
|
||||
|
||||
protected enum EnemyState
|
||||
{
|
||||
Idle,
|
||||
Alert,
|
||||
Patrolling
|
||||
}
|
||||
|
||||
|
||||
public enum AiState
|
||||
{
|
||||
Enabled,
|
||||
Disabled,
|
||||
Controlled
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum EnemyState
|
||||
{
|
||||
Idle,
|
||||
Alert,
|
||||
Patrolling
|
||||
}
|
||||
|
||||
public enum AiState
|
||||
{
|
||||
Enabled,
|
||||
Disabled,
|
||||
Controlled
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue