mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 06:45:33 +00:00
Reverted enemy behavior
This commit is contained in:
parent
e6448c7f0a
commit
0733ab4a11
1 changed files with 33 additions and 80 deletions
113
Scripts/Enemy.cs
113
Scripts/Enemy.cs
|
|
@ -13,59 +13,40 @@ public partial class Enemy : Area2D, IDestructible
|
|||
|
||||
[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 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 bool _isDestroyed = false;
|
||||
|
||||
private Timer _burstTimer;
|
||||
private Timer _shotTimer;
|
||||
|
||||
private int _shotsFired = 0;
|
||||
private Timer _cooldownTimer;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
_currentHealth = Health;
|
||||
|
||||
// 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", "");
|
||||
_cooldownTimer = GetNode<Timer>("./ShootTimer");
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
switch (_currentState)
|
||||
{
|
||||
case EnemyState.Idle:
|
||||
|
||||
break;
|
||||
|
||||
case EnemyState.Primed:
|
||||
// Have the raycast follow the player and shoot if visible
|
||||
//HandlePlayerDetection();
|
||||
break;
|
||||
|
||||
//case EnemyState.Shooting:
|
||||
// Shoot
|
||||
//break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
@ -83,54 +64,18 @@ public partial class Enemy : Area2D, IDestructible
|
|||
return;
|
||||
}
|
||||
|
||||
if (_burstTimer.IsStopped() && !_shotTimer.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)
|
||||
if (_cooldownTimer.IsStopped() && IsPlayerInSight())
|
||||
{
|
||||
// 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;
|
||||
_cooldownTimer.Start(RateOfFire);
|
||||
}
|
||||
else
|
||||
{
|
||||
bullet.SetDirection(Vector2.Right.Rotated(this.Rotation));
|
||||
}
|
||||
|
||||
bullet.Speed = BulletSpeed;
|
||||
}
|
||||
|
||||
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 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)
|
||||
{
|
||||
// Assume area is player for now
|
||||
if (area is InteractionController player)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// Collision handling
|
||||
|
||||
}
|
||||
|
||||
private void Explode()
|
||||
{
|
||||
Debug.WriteLine("Ded");
|
||||
//CreateParticles();
|
||||
//CreateDebris();
|
||||
QueueFree();
|
||||
}
|
||||
|
||||
public void Hit(float damage)
|
||||
{
|
||||
if (_isDestroyed) return;
|
||||
|
||||
|
||||
_currentHealth -= damage;
|
||||
if (!(_currentHealth <= 0)) return;
|
||||
_isDestroyed = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue