diff --git a/Scripts/Enemy.cs b/Scripts/Enemy.cs index 6baf1cbe..88e91cc5 100644 --- a/Scripts/Enemy.cs +++ b/Scripts/Enemy.cs @@ -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("./ShootTimer"); } @@ -66,6 +73,12 @@ public partial class Enemy : Area2D, IDestructible if (_cooldownTimer.IsStopped() && IsPlayerInSight()) { + Shoot(); + } + } + + private void Shoot() + { // SHOOT var bullet = this.CreateChild(BulletScene); // var bullet = BulletScene.Instantiate(); @@ -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()