cirnogodot/Scripts/Enemy.cs

264 lines
5.5 KiB
C#
Raw Normal View History

using Cirno.Scripts;
2024-08-17 17:00:50 +02:00
using Godot;
using System;
using System.Diagnostics;
2025-01-31 16:06:15 +01:00
public partial class Enemy : CharacterBody2D
2024-08-17 17:00:50 +02:00
{
private InteractionController _cachedPlayer;
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;
2024-11-15 16:32:26 +01:00
[Export] public Weapon EquippedWeapon;
2024-08-18 17:38:32 +02:00
2024-08-18 11:33:17 +02:00
private float _currentHealth = 0f;
private bool _isDestroyed = false;
2024-11-15 16:32:26 +01:00
2025-01-24 15:24:37 +01:00
private NavigationAgent2D _navigationAgent;
2025-01-31 16:45:31 +01:00
private bool _isPlayerInRange = false;
private Vector2? _lastPlayerPosition = null;
2025-01-24 15:24:37 +01:00
[Export] public bool NavigationEnabled { get; set; } = false;
2024-11-13 11:20:27 +01:00
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
_navigationAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");
2025-01-31 16:45:31 +01:00
//CallDeferred("Setup");
2024-08-17 17:00:50 +02:00
}
2025-01-31 16:45:31 +01:00
// private void Setup()
// {
//
// }
2025-01-24 15:24:37 +01:00
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:
HandlePlayerDetection();
break;
case EnemyState.Alert:
// Update last known player position if it's in range
if (_isPlayerInRange)
{
_lastPlayerPosition = _cachedPlayer.GlobalPosition;
}
if (_lastPlayerPosition.HasValue)
{
_navigationAgent.SetTargetPosition(_lastPlayerPosition.Value);
}
2025-01-31 15:05:45 +01:00
var currentAgentPosition = GlobalPosition;
var nextPathPosition = _navigationAgent.GetNextPathPosition();
2025-01-31 16:45:31 +01:00
2025-01-31 15:05:45 +01:00
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * (float)(WalkSpeed * delta);
// Navigation is over, can do other things like shooting
if (_navigationAgent.IsNavigationFinished())
{
// Shoot player
2025-01-31 16:45:31 +01:00
Shoot();
// TODO: If player totally left the max range it should stop shooting and go back to idle
2025-01-31 15:05:45 +01:00
return;
}
if (_navigationAgent.AvoidanceEnabled)
{
_navigationAgent.SetVelocity(newVelocity);
}
else
{
_on_navigation_agent_2d_velocity_computed(newVelocity);
}
MoveAndSlide();
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
}
private void HandlePlayerDetection()
{
2025-01-31 16:45:31 +01:00
if (_cachedPlayer == null)
2024-08-17 17:00:50 +02:00
{
return;
}
2024-11-15 16:32:26 +01:00
if (IsPlayerInSight())
{
2025-01-24 15:24:37 +01:00
// Update player position only if player is in sight
2025-01-31 15:05:45 +01:00
// if (NavigationEnabled)
// {
// _navigationAgent.SetTargetPosition(_cachedPlayer.GlobalPosition);
// }
2025-01-31 16:45:31 +01:00
//Shoot();
2025-01-24 15:24:37 +01:00
2025-01-31 16:45:31 +01:00
_currentState = EnemyState.Alert;
2024-11-13 11:20:27 +01:00
}
}
private void Shoot()
{
2025-01-31 16:45:31 +01:00
if (EquippedWeapon == null || !_lastPlayerPosition.HasValue) return;
2024-11-15 16:32:26 +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();
// // SHOOT
// var bullet = this.CreateChild<Bullet>(BulletScene);
// // var bullet = BulletScene.Instantiate<Bullet>();
// // Owner.AddChild(bullet);
// // bullet.Transform = this.GlobalTransform;
// // bullet.Position = this.Position;
// bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
// bullet.Speed = BulletSpeed;
//
// _ammo -= 1;
//
// if (_ammo <= 0)
// {
// _ammo = BulletCount;
// _cooldownTimer.Start(ReloadTime);
// }
// else
// {
// _cooldownTimer.Start(RateOfFire);
// }
2024-08-17 17:00:50 +02:00
}
private bool IsPlayerInSight()
{
var spaceState = GetWorld2D().DirectSpaceState;
2024-08-17 17:00:50 +02:00
var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, CollisionMask, new Godot.Collections.Array<Rid> { GetRid() });
var result = spaceState.IntersectRay(query);
2024-11-11 16:53:03 +01:00
// If count is 0 then the player is in sight, otherwise there is level geometry in the way
return result.Count == 0;
// if (result.Count > 0)
// GD.Print("Hit at point: ", result["position"]);
2024-08-17 17:00:50 +02:00
}
private void _on_player_detection_area_entered(Area2D area)
{
2024-11-11 16:53:03 +01:00
// Assume area is player for now
2024-08-17 17:00:50 +02:00
if (area is InteractionController player)
{
Debug.WriteLine("Enemy detection area Entered by interaction controller");
_cachedPlayer = player;
2025-01-31 16:45:31 +01:00
_isPlayerInRange = true;
// if (_currentState is EnemyState.Idle)
// {
// _currentState = EnemyState.Primed;
// }
2024-08-17 17:00:50 +02:00
}
}
private void _on_player_detection_area_exited(Area2D area)
{
2025-01-31 16:45:31 +01:00
_isPlayerInRange = false;
// if (_currentState is EnemyState.Primed)
// {
// _currentState = EnemyState.Idle;
// }
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;
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
}
}