cirnogodot/Scripts/Components/Actors/EnemyNavigationMovement.cs

160 lines
4.7 KiB
C#
Raw Normal View History

2025-02-18 22:14:42 +01:00
using Godot;
using Cirno.Scripts;
using Cirno.Scripts.Components;
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Components.Actors;
2025-02-19 11:06:31 +01:00
public partial class EnemyNavigationMovement : MovementHandler
2025-02-18 22:14:42 +01:00
{
public override Vector2 FacingDirection
{
get => _parent.FacingDirection;
set => _parent.FacingDirection = value;
}
public override Vector2 MovementDirection
{
get => _parent.MovementDirection;
set => _parent.MovementDirection = value;
}
2025-02-19 11:06:31 +01:00
[Export] private bool _navigationEnabled = false;
2025-02-18 22:14:42 +01:00
[Export] public float AlarmReactRange = 200f;
[Export] public float PlayerDisengageRange = 500f;
[Export(PropertyHint.Layers2DPhysics)]
public uint CollisionMask { get; set; }
2025-02-18 22:14:42 +01:00
public bool NavigationEnabled
2025-02-19 11:06:31 +01:00
{
get => _actorAi is not null && _actorAi.Ai is AiState.Enabled && _navigationEnabled && _navigationAgent != null;
set => _navigationEnabled = value;
}
2025-02-18 22:14:42 +01:00
2025-02-19 11:06:31 +01:00
[Export] private PlayerDetection _playerDetection;
2025-02-18 22:14:42 +01:00
private ActorAi _actorAi;
private NavigationAgent2D _navigationAgent;
private AlarmManager _alarmManager;
private Vector2? _lastPlayerPosition = null;
private bool IsPlayerInRange => _playerDetection is { IsPlayerInRange: true };
2025-02-19 11:06:31 +01:00
private bool IsPlayerInSight =>
_playerDetection is not null && _playerDetection.IsPlayerInSight(CollisionMask);
2025-02-18 22:14:42 +01:00
2025-02-19 11:06:31 +01:00
public override void Init(Actor parent)
2025-02-18 22:14:42 +01:00
{
base.Init(parent);
2025-02-19 11:06:31 +01:00
2025-02-18 22:14:42 +01:00
MovementDirection = Vector2.Zero;
FacingDirection = Vector2.Down;
2025-02-19 11:06:31 +01:00
_actorAi = parent.GetNode<ActorAi>("ActorAi");
_navigationAgent = _parent.GetNodeOrNull<NavigationAgent2D>("NavigationAgent2D");
_alarmManager = this.GetAlarmManager();
2025-02-18 22:14:42 +01:00
if (_alarmManager != null)
2025-02-19 11:06:31 +01:00
{
_alarmManager.AlarmEnabled += AlarmManagerOnAlarmEnabled;
}
2025-02-18 22:14:42 +01:00
}
private void AlarmManagerOnAlarmEnabled(Vector2 location)
2025-02-19 11:06:31 +01:00
{
if (NavigationEnabled && location.DistanceTo(this.GlobalPosition) <= AlarmReactRange)
{
GD.Print($"Enemy {Name} alerted");
_actorAi.State = EnemyState.Alert;
_lastPlayerPosition = location;
}
}
2025-02-18 22:14:42 +01:00
public override void Move(double delta)
{
2025-02-19 11:06:31 +01:00
if (_actorAi.Ai is not AiState.Enabled)
2025-02-18 22:14:42 +01:00
return;
2025-02-19 11:06:31 +01:00
switch (_actorAi.State)
2025-02-18 22:14:42 +01:00
{
case EnemyState.Idle:
if (_playerDetection != null && IsPlayerInSight)
2025-02-19 11:06:31 +01:00
{
_actorAi.State = EnemyState.Alert;
2025-02-18 23:39:22 +01:00
GD.Print("Switching to alert");
2025-02-19 11:06:31 +01:00
}
break;
2025-02-18 22:14:42 +01:00
case EnemyState.Alert:
2025-02-19 11:06:31 +01:00
// 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 = _parent.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;
2025-02-18 22:14:42 +01:00
}
}
public void _on_navigation_agent_2d_velocity_computed(Vector2 safeVelocity)
2025-02-19 11:06:31 +01:00
{
_parent.Velocity = safeVelocity;
if (_parent.Velocity.Length() > 0)
{
2025-02-18 23:39:22 +01:00
_parent.FacingDirection = _parent.Velocity.Normalized();
}
2025-02-19 11:06:31 +01:00
}
2025-02-18 22:14:42 +01:00
}