2024-08-17 19:11:49 +02:00
|
|
|
using Cirno.Scripts;
|
2024-08-17 17:00:50 +02:00
|
|
|
using Godot;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2025-02-04 10:03:04 +01:00
|
|
|
using Cirno.Scripts.Components;
|
2025-03-20 18:22:40 +01:00
|
|
|
using Cirno.Scripts.Enums;
|
2025-02-04 10:03:04 +01:00
|
|
|
using Godot.Collections;
|
2024-08-17 17:00:50 +02:00
|
|
|
|
2025-01-31 16:06:15 +01:00
|
|
|
public partial class Enemy : CharacterBody2D
|
2024-08-17 17:00:50 +02:00
|
|
|
{
|
|
|
|
|
private InteractionController _cachedPlayer;
|
2025-02-05 19:41:49 +01:00
|
|
|
public InteractionController CachedPlayer
|
|
|
|
|
{
|
|
|
|
|
get => _cachedPlayer;
|
|
|
|
|
protected set => _cachedPlayer = value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 17:00:50 +02:00
|
|
|
private EnemyState _currentState = EnemyState.Idle;
|
|
|
|
|
|
2024-08-18 11:33:17 +02:00
|
|
|
[Export] public float Health = 4f;
|
|
|
|
|
|
2025-01-24 15:24:37 +01:00
|
|
|
[Export] public float WalkSpeed = 2500f;
|
|
|
|
|
|
2025-01-31 16:45:31 +01:00
|
|
|
[Export] public float PlayerDisengageRange = 500f;
|
|
|
|
|
|
2025-02-05 10:09:05 +01:00
|
|
|
[Export] public float AlarmReactRange = 200f;
|
|
|
|
|
|
2024-11-15 16:32:26 +01:00
|
|
|
[Export] public Weapon EquippedWeapon;
|
2025-02-14 13:27:30 +01:00
|
|
|
[Export] public Node2D DefeatScript;
|
2025-02-17 18:42:37 +01:00
|
|
|
|
|
|
|
|
[Export] public AiState Ai { get; private set; }
|
2025-03-02 18:07:13 +01:00
|
|
|
|
|
|
|
|
[Export] public PackedScene CorpseTemplate { get; private set; }
|
2024-08-18 17:38:32 +02:00
|
|
|
|
2025-02-05 19:41:49 +01:00
|
|
|
protected float _currentHealth = 0f;
|
2024-08-18 11:33:17 +02:00
|
|
|
|
|
|
|
|
private bool _isDestroyed = false;
|
2024-11-15 16:32:26 +01:00
|
|
|
|
2025-01-24 15:24:37 +01:00
|
|
|
private NavigationAgent2D _navigationAgent;
|
2025-02-14 09:33:10 +01:00
|
|
|
|
|
|
|
|
protected bool _invulnerable = false;
|
2025-02-17 18:42:37 +01:00
|
|
|
|
|
|
|
|
#region Manual Movement
|
|
|
|
|
|
|
|
|
|
private Vector2 _movementDirection { get; set; } = Vector2.Zero;
|
|
|
|
|
private Vector2 _facingDirection { get; set; } = Vector2.Right;
|
|
|
|
|
|
|
|
|
|
#endregion
|
2025-02-14 09:33:10 +01:00
|
|
|
|
2025-02-04 10:03:04 +01:00
|
|
|
private bool IsPlayerInRange => _playerDetection is { IsPlayerInRange: true };
|
|
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
private bool IsPlayerInSight => _playerDetection is not null && _playerDetection.IsPlayerInSight(CollisionMask);
|
|
|
|
|
|
2025-01-31 16:45:31 +01:00
|
|
|
private Vector2? _lastPlayerPosition = null;
|
2025-02-04 10:03:04 +01:00
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
private PlayerDetection _playerDetection;
|
2025-02-04 13:22:05 +01:00
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
private bool _navigationEnabled = false;
|
2025-01-31 16:45:31 +01:00
|
|
|
|
2025-02-05 10:09:05 +01:00
|
|
|
private AlarmManager _alarmManager;
|
2025-02-04 13:22:05 +01:00
|
|
|
public bool NavigationEnabled
|
|
|
|
|
{
|
2025-02-17 18:42:37 +01:00
|
|
|
get => Ai is AiState.Enabled && _navigationEnabled && _navigationAgent != null;
|
2025-02-04 13:22:05 +01:00
|
|
|
set => _navigationEnabled = value;
|
|
|
|
|
}
|
2024-11-13 11:20:27 +01:00
|
|
|
|
2025-02-13 11:15:06 +01:00
|
|
|
#region Events
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
|
public delegate void HealthChangedEventHandler(float newValue);
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-11-11 16:53:03 +01:00
|
|
|
// Called when the node enters the scene tree for the first time.
|
2024-08-17 17:00:50 +02:00
|
|
|
public override void _Ready()
|
|
|
|
|
{
|
2024-08-18 11:33:17 +02:00
|
|
|
_currentHealth = Health;
|
2025-01-24 15:24:37 +01:00
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
_navigationAgent = GetNodeOrNull<NavigationAgent2D>("NavigationAgent2D");
|
2025-02-07 14:58:59 +01:00
|
|
|
|
|
|
|
|
_alarmManager = this.GetAlarmManager();
|
2025-02-05 19:41:49 +01:00
|
|
|
|
|
|
|
|
if (_alarmManager != null)
|
|
|
|
|
{
|
|
|
|
|
_alarmManager.AlarmEnabled += AlarmManagerOnAlarmEnabled;
|
|
|
|
|
}
|
2024-08-17 17:00:50 +02:00
|
|
|
}
|
2025-02-05 10:09:05 +01:00
|
|
|
|
2025-02-07 14:58:59 +01:00
|
|
|
public override void _ExitTree()
|
|
|
|
|
{
|
|
|
|
|
if (_alarmManager == null) return;
|
|
|
|
|
_alarmManager.AlarmEnabled -= AlarmManagerOnAlarmEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 10:09:05 +01:00
|
|
|
private void AlarmManagerOnAlarmEnabled(Vector2 location)
|
|
|
|
|
{
|
|
|
|
|
if (NavigationEnabled && location.DistanceTo(this.GlobalPosition) <= AlarmReactRange)
|
|
|
|
|
{
|
|
|
|
|
GD.Print($"Enemy {Name} alerted");
|
|
|
|
|
this._currentState = EnemyState.Alert;
|
|
|
|
|
_lastPlayerPosition = location;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 16:53:03 +01:00
|
|
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
2024-08-17 17:00:50 +02:00
|
|
|
public override void _Process(double delta)
|
|
|
|
|
{
|
2025-02-17 18:42:37 +01:00
|
|
|
// switch (_currentState)
|
|
|
|
|
// {
|
|
|
|
|
// case EnemyState.Idle:
|
|
|
|
|
//
|
|
|
|
|
// break;
|
|
|
|
|
// case EnemyState.Alert:
|
|
|
|
|
// break;
|
|
|
|
|
//
|
|
|
|
|
// //case EnemyState.Shooting:
|
|
|
|
|
// // Shoot
|
|
|
|
|
// //break;
|
|
|
|
|
// default:
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (Ai is AiState.Controlled && !_isDestroyed)
|
2024-08-17 17:00:50 +02:00
|
|
|
{
|
2025-02-17 18:42:37 +01:00
|
|
|
_movementDirection = GetInput();
|
|
|
|
|
_facingDirection = _movementDirection;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector2 GetInput()
|
|
|
|
|
{
|
|
|
|
|
return Input.GetVector("left", "right", "up", "down");
|
|
|
|
|
}
|
2024-11-11 16:53:03 +01:00
|
|
|
|
2025-02-17 18:42:37 +01:00
|
|
|
public override void _PhysicsProcess(double delta)
|
|
|
|
|
{
|
|
|
|
|
if (_isDestroyed) return;
|
|
|
|
|
|
|
|
|
|
switch (Ai)
|
|
|
|
|
{
|
|
|
|
|
case AiState.Enabled:
|
|
|
|
|
HandleAi(delta);
|
|
|
|
|
return;
|
|
|
|
|
case AiState.Controlled:
|
|
|
|
|
HandleManualControl(delta);
|
2024-08-17 17:00:50 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-17 18:42:37 +01:00
|
|
|
private void HandleManualControl(double delta)
|
|
|
|
|
{
|
|
|
|
|
Velocity = _movementDirection * (float)(WalkSpeed * delta);
|
|
|
|
|
MoveAndSlide();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleAi(double delta)
|
2024-08-17 17:00:50 +02:00
|
|
|
{
|
2025-01-31 16:45:31 +01:00
|
|
|
switch (_currentState)
|
2025-01-24 15:24:37 +01:00
|
|
|
{
|
2025-01-31 16:45:31 +01:00
|
|
|
case EnemyState.Idle:
|
2025-02-04 10:03:04 +01:00
|
|
|
if (_playerDetection != null && _playerDetection.IsPlayerInSight(CollisionMask))
|
|
|
|
|
{
|
|
|
|
|
_currentState = EnemyState.Alert;
|
|
|
|
|
}
|
|
|
|
|
//HandlePlayerDetection();
|
2025-01-31 16:45:31 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case EnemyState.Alert:
|
|
|
|
|
|
|
|
|
|
// Update last known player position if it's in range
|
2025-02-04 10:03:04 +01:00
|
|
|
if (IsPlayerInRange)
|
2025-01-31 16:45:31 +01:00
|
|
|
{
|
2025-02-04 10:03:04 +01:00
|
|
|
_lastPlayerPosition = _playerDetection.CachedPlayer.GlobalPosition;
|
2025-01-31 16:45:31 +01:00
|
|
|
}
|
2025-01-31 15:05:45 +01:00
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
if (NavigationEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (_lastPlayerPosition.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_navigationAgent.SetTargetPosition(_lastPlayerPosition.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var currentAgentPosition = GlobalPosition;
|
2025-01-31 15:05:45 +01:00
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
var nextPathPosition = _navigationAgent.GetNextPathPosition();
|
2025-01-31 16:45:31 +01:00
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * (float)(WalkSpeed * delta);
|
2025-01-31 15:05:45 +01:00
|
|
|
|
|
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
// Navigation is over, can do other things like shooting
|
|
|
|
|
if (_navigationAgent.IsNavigationFinished())
|
|
|
|
|
{
|
|
|
|
|
// Shoot player
|
|
|
|
|
if (IsPlayerInSight)
|
|
|
|
|
{
|
|
|
|
|
Shoot();
|
|
|
|
|
}
|
2025-01-31 16:45:31 +01:00
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
// TODO: If player totally left the max range it should stop shooting and go back to idle
|
2025-01-31 16:45:31 +01:00
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2025-01-31 15:05:45 +01:00
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
if (_navigationAgent.AvoidanceEnabled)
|
|
|
|
|
{
|
|
|
|
|
_navigationAgent.SetVelocity(newVelocity);
|
2025-01-31 15:05:45 +01:00
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_on_navigation_agent_2d_velocity_computed(newVelocity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MoveAndSlide();
|
2025-01-31 15:05:45 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-02-04 13:22:05 +01:00
|
|
|
if (IsPlayerInSight)
|
|
|
|
|
{
|
|
|
|
|
Shoot();
|
|
|
|
|
}
|
2025-01-31 15:05:45 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-31 16:45:31 +01:00
|
|
|
break;
|
|
|
|
|
case EnemyState.Patrolling:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
2025-01-24 15:24:37 +01:00
|
|
|
}
|
2025-01-31 15:05:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void _on_navigation_agent_2d_velocity_computed(Vector2 safeVelocity)
|
|
|
|
|
{
|
|
|
|
|
this.Velocity = safeVelocity;
|
2025-01-24 15:24:37 +01:00
|
|
|
|
2024-08-17 17:00:50 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
protected virtual void Shoot()
|
2024-11-13 11:20:27 +01:00
|
|
|
{
|
2025-01-31 16:45:31 +01:00
|
|
|
if (EquippedWeapon == null || !_lastPlayerPosition.HasValue) return;
|
2025-02-04 13:22:05 +01:00
|
|
|
|
2025-01-31 16:45:31 +01:00
|
|
|
// Shoot at the player's last known position
|
|
|
|
|
EquippedWeapon.ShootDirection = (_lastPlayerPosition.Value - this.GlobalPosition).Normalized();
|
2024-11-15 16:32:26 +01:00
|
|
|
|
|
|
|
|
EquippedWeapon.Shoot();
|
2024-08-17 17:00:50 +02:00
|
|
|
}
|
2025-01-31 16:06:15 +01:00
|
|
|
|
|
|
|
|
private void _on_damage_hitbox_area_entered(Area2D area)
|
|
|
|
|
{
|
|
|
|
|
if (area is not Bullet bullet) return;
|
2025-06-08 16:33:38 +02:00
|
|
|
if (!bullet.Enabled) return;
|
2025-02-14 09:33:10 +01:00
|
|
|
if (_invulnerable) return;
|
|
|
|
|
if (bullet.BulletInfo.Owner == BulletOwner.Enemy) return;
|
|
|
|
|
|
2025-01-31 16:06:15 +01:00
|
|
|
this.Hit(bullet.Damage);
|
2025-02-20 12:17:21 +01:00
|
|
|
bullet.RequestCollisionDestruction();
|
2025-01-31 16:06:15 +01:00
|
|
|
}
|
2024-08-17 17:00:50 +02:00
|
|
|
|
2024-11-11 16:53:03 +01:00
|
|
|
// Bullets collision
|
2024-08-18 11:33:17 +02:00
|
|
|
private void _on_area_entered(Area2D area)
|
|
|
|
|
{
|
2024-11-11 16:53:03 +01:00
|
|
|
|
2024-08-18 11:33:17 +02:00
|
|
|
}
|
2025-01-31 15:05:45 +01:00
|
|
|
|
2025-02-14 13:27:30 +01:00
|
|
|
protected virtual void Explode()
|
2024-08-18 17:38:32 +02:00
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Ded");
|
2025-02-14 13:27:30 +01:00
|
|
|
if (DefeatScript is not null)
|
|
|
|
|
{
|
|
|
|
|
ActivateDefeatScript();
|
|
|
|
|
}
|
2024-11-11 16:53:03 +01:00
|
|
|
//CreateParticles();
|
2025-03-02 18:07:13 +01:00
|
|
|
CreateDebris();
|
2024-08-18 17:38:32 +02:00
|
|
|
QueueFree();
|
|
|
|
|
}
|
2025-03-02 18:07:13 +01:00
|
|
|
|
|
|
|
|
private void CreateDebris()
|
|
|
|
|
{
|
|
|
|
|
if (CorpseTemplate is not null)
|
|
|
|
|
{
|
|
|
|
|
this.CreateSibling<Node2D>(CorpseTemplate);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-14 13:27:30 +01:00
|
|
|
|
2025-03-22 17:47:24 +01:00
|
|
|
protected virtual void ActivateDefeatScript()
|
2025-02-14 13:27:30 +01:00
|
|
|
{
|
|
|
|
|
if (DefeatScript is not IActivable target)
|
|
|
|
|
{
|
|
|
|
|
GD.PrintErr($"Target {DefeatScript.Name} is not activable");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
target?.Activate();
|
|
|
|
|
|
|
|
|
|
GD.Print($"{DefeatScript.Name} activated");
|
|
|
|
|
}
|
2024-08-18 17:38:32 +02:00
|
|
|
|
2024-08-18 11:33:17 +02:00
|
|
|
public void Hit(float damage)
|
|
|
|
|
{
|
|
|
|
|
if (_isDestroyed) return;
|
2025-02-14 09:33:10 +01:00
|
|
|
if (_invulnerable) return;
|
2024-11-11 16:53:03 +01:00
|
|
|
|
2024-08-18 11:33:17 +02:00
|
|
|
_currentHealth -= damage;
|
2025-02-14 09:33:10 +01:00
|
|
|
EmitSignal(SignalName.HealthChanged, _currentHealth);
|
2024-08-18 11:33:17 +02:00
|
|
|
if (!(_currentHealth <= 0)) return;
|
|
|
|
|
_isDestroyed = true;
|
|
|
|
|
Explode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsDestroyed()
|
|
|
|
|
{
|
|
|
|
|
return _isDestroyed;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-17 18:42:37 +01:00
|
|
|
public void AssumeControl()
|
|
|
|
|
{
|
|
|
|
|
GD.Print("Assuming direct control");
|
|
|
|
|
Ai = AiState.Controlled;
|
|
|
|
|
}
|
2025-02-18 22:14:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum AiState
|
|
|
|
|
{
|
|
|
|
|
Enabled,
|
|
|
|
|
Disabled,
|
|
|
|
|
Controlled
|
2024-08-17 17:00:50 +02:00
|
|
|
}
|