Reverted enemy behavior

This commit is contained in:
MaddoScientisto 2024-11-11 16:53:03 +01:00
commit 0733ab4a11

View file

@ -13,59 +13,40 @@ public partial class Enemy : Area2D, IDestructible
[Export] public float Health = 4f; [Export] public float Health = 4f;
[Export] public double RateOfFire = 0.4f; // Time between shots within a burst [Export] public double RateOfFire = 0.4f;
[Export] public float BulletSpeed = 100f; [Export] public float BulletSpeed = 100f;
[Export] public int BurstCount = 3; // Number of shots per burst
[Export] public float BurstCooldown = 1f; // Time between bursts
[Export] public bool AimAtPlayer = true; // Whether to aim at player or shoot straight
private float _currentHealth = 0f; private float _currentHealth = 0f;
private bool _isDestroyed = false; private bool _isDestroyed = false;
private Timer _burstTimer; private Timer _cooldownTimer;
private Timer _shotTimer;
private int _shotsFired = 0;
// Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
_currentHealth = Health; _currentHealth = Health;
_cooldownTimer = GetNode<Timer>("./ShootTimer");
// Initialize timers
_burstTimer = new Timer();
_shotTimer = new Timer();
AddChild(_burstTimer);
AddChild(_shotTimer);
_burstTimer.WaitTime = BurstCooldown;
_burstTimer.OneShot = true;
_shotTimer.WaitTime = RateOfFire;
_shotTimer.OneShot = true;
_shotTimer.Timeout += OnShotTimerTimeout;
_burstTimer.Timeout += OnBurstTimerTimeout;
// Debug
DebugStats.Instance.AddProperty(this, "_currentHealth", "");
} }
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) public override void _Process(double delta)
{ {
switch (_currentState) switch (_currentState)
{ {
case EnemyState.Idle: case EnemyState.Idle:
break; break;
case EnemyState.Primed: case EnemyState.Primed:
// Have the raycast follow the player and shoot if visible
//HandlePlayerDetection();
break; break;
//case EnemyState.Shooting:
// Shoot
//break;
default: default:
break; break;
} }
@ -83,54 +64,18 @@ public partial class Enemy : Area2D, IDestructible
return; return;
} }
if (_burstTimer.IsStopped() && !_shotTimer.IsStopped() && IsPlayerInSight()) if (_cooldownTimer.IsStopped() && IsPlayerInSight())
{
StartBurst();
}
}
private void StartBurst()
{
_shotsFired = 0;
_shotTimer.Start();
}
private void OnShotTimerTimeout()
{
if (_shotsFired < BurstCount)
{
Shoot();
_shotsFired++;
if (_shotsFired < BurstCount)
{
_shotTimer.Start(); // Restart for the next shot in the burst
}
}
else
{
_burstTimer.Start(); // Start cooldown between bursts
}
}
private void OnBurstTimerTimeout()
{
// Cooldown complete, can start new burst when conditions are met
}
private void Shoot()
{
var bullet = this.CreateChild<Bullet>(BulletScene);
if (AimAtPlayer)
{ {
// 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.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
bullet.Speed = BulletSpeed;
_cooldownTimer.Start(RateOfFire);
} }
else
{
bullet.SetDirection(Vector2.Right.Rotated(this.Rotation));
}
bullet.Speed = BulletSpeed;
} }
private bool IsPlayerInSight() private bool IsPlayerInSight()
@ -139,11 +84,16 @@ public partial class Enemy : Area2D, IDestructible
var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, CollisionMask, new Godot.Collections.Array<Rid> { GetRid() }); var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, CollisionMask, new Godot.Collections.Array<Rid> { GetRid() });
var result = spaceState.IntersectRay(query); var result = spaceState.IntersectRay(query);
return result.Count == 0; // True if no obstacles between enemy and player // 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"]);
} }
private void _on_player_detection_area_entered(Area2D area) private void _on_player_detection_area_entered(Area2D area)
{ {
// Assume area is player for now
if (area is InteractionController player) if (area is InteractionController player)
{ {
Debug.WriteLine("Enemy detection area Entered by interaction controller"); Debug.WriteLine("Enemy detection area Entered by interaction controller");
@ -164,21 +114,24 @@ public partial class Enemy : Area2D, IDestructible
} }
} }
// Bullets collision
private void _on_area_entered(Area2D area) private void _on_area_entered(Area2D area)
{ {
// Collision handling
} }
private void Explode() private void Explode()
{ {
Debug.WriteLine("Ded"); Debug.WriteLine("Ded");
//CreateParticles();
//CreateDebris();
QueueFree(); QueueFree();
} }
public void Hit(float damage) public void Hit(float damage)
{ {
if (_isDestroyed) return; if (_isDestroyed) return;
_currentHealth -= damage; _currentHealth -= damage;
if (!(_currentHealth <= 0)) return; if (!(_currentHealth <= 0)) return;
_isDestroyed = true; _isDestroyed = true;