cirnogodot/Scripts/Enemy.cs

247 lines
5.1 KiB
C#
Raw Normal View History

using Cirno.Scripts;
2024-08-17 17:00:50 +02:00
using Godot;
using System;
using System.Diagnostics;
using Cirno.Scripts.Components;
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;
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;
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;
[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
{
get => _navigationEnabled && _navigationAgent != null;
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)
{
switch (_currentState)
{
case EnemyState.Idle:
2024-11-11 16:53:03 +01:00
2024-08-17 17:00:50 +02:00
break;
2025-01-31 16:45:31 +01:00
case EnemyState.Alert:
2024-08-17 17:00:50 +02:00
break;
2024-11-11 16:53:03 +01:00
//case EnemyState.Shooting:
// Shoot
//break;
2024-08-17 17:00:50 +02:00
default:
break;
}
}
public override void _PhysicsProcess(double delta)
{
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:
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
if (IsPlayerInRange)
2025-01-31 16:45:31 +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;
GD.Print("Enemy Received damage");
this.Hit(bullet.Damage);
bullet.QueueFree();
}
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
2024-08-18 17:38:32 +02:00
private void Explode()
{
Debug.WriteLine("Ded");
2024-11-11 16:53:03 +01:00
//CreateParticles();
//CreateDebris();
2024-08-18 17:38:32 +02:00
QueueFree();
}
2024-08-18 11:33:17 +02:00
public void Hit(float damage)
{
if (_isDestroyed) return;
2024-11-11 16:53:03 +01:00
2024-08-18 11:33:17 +02:00
_currentHealth -= damage;
2025-02-13 11:15:06 +01:00
EmitSignal(nameof(HealthChanged), _currentHealth);
2024-08-18 11:33:17 +02:00
if (!(_currentHealth <= 0)) return;
_isDestroyed = true;
Explode();
}
public bool IsDestroyed()
{
return _isDestroyed;
}
2024-08-17 17:00:50 +02:00
private enum EnemyState
{
Idle,
2025-01-31 16:45:31 +01:00
Alert,
2025-01-24 15:24:37 +01:00
Patrolling
2024-08-17 17:00:50 +02:00
}
}