Added ammo reload to enemy

This commit is contained in:
Marco 2024-11-13 11:20:27 +01:00
commit ed502eb061

View file

@ -15,6 +15,10 @@ public partial class Enemy : Area2D, IDestructible
[Export] public double RateOfFire = 0.4f;
[Export] public double ReloadTime = 1.0f;
[Export] public int BulletCount = 4;
[Export] public float BulletSpeed = 100f;
private float _currentHealth = 0f;
@ -23,10 +27,13 @@ public partial class Enemy : Area2D, IDestructible
private Timer _cooldownTimer;
private int _ammo = 0;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_currentHealth = Health;
_ammo = BulletCount;
_cooldownTimer = GetNode<Timer>("./ShootTimer");
}
@ -66,6 +73,12 @@ public partial class Enemy : Area2D, IDestructible
if (_cooldownTimer.IsStopped() && IsPlayerInSight())
{
Shoot();
}
}
private void Shoot()
{
// SHOOT
var bullet = this.CreateChild<Bullet>(BulletScene);
// var bullet = BulletScene.Instantiate<Bullet>();
@ -74,8 +87,18 @@ public partial class Enemy : Area2D, IDestructible
// bullet.Position = this.Position;
bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
bullet.Speed = BulletSpeed;
_cooldownTimer.Start(RateOfFire);
}
_ammo -= 1;
if (_ammo <= 0)
{
_ammo = BulletCount;
_cooldownTimer.Start(ReloadTime);
}
else
{
_cooldownTimer.Start(RateOfFire);
}
}
private bool IsPlayerInSight()