Collisions and cooldowns

This commit is contained in:
MaddoScientisto 2024-08-18 11:33:17 +02:00
commit 6ff1ab76ed
9 changed files with 105 additions and 3848 deletions

View file

@ -70,7 +70,12 @@ public partial class Bullet : Area2D
private void _on_area_entered(Area2D area)
{
// Replace with function body.
if (area.IsInGroup("Solid"))
{
QueueFree();
return;
}
if (area.IsInGroup("Destroyable") && area is IDestructible destructible)
{
//Debug.WriteLine("Collision with destroyable object area");

View file

@ -3,7 +3,7 @@ using Godot;
using System;
using System.Diagnostics;
public partial class Enemy : Area2D
public partial class Enemy : Area2D, IDestructible
{
private InteractionController _cachedPlayer;
private EnemyState _currentState = EnemyState.Idle;
@ -11,10 +11,21 @@ public partial class Enemy : Area2D
[Export]
public PackedScene BulletScene { get; set; }
[Export] public float Health = 4f;
[Export] public double RateOfFire = 0.4f;
private float _currentHealth = 0f;
private bool _isDestroyed = false;
private Timer _cooldownTimer;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_currentHealth = Health;
_cooldownTimer = GetNode<Timer>("./ShootTimer");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
@ -51,7 +62,7 @@ public partial class Enemy : Area2D
return;
}
if (IsPlayerInSight())
if (_cooldownTimer.IsStopped() && IsPlayerInSight())
{
// SHOOT
var bullet = this.CreateChild<Bullet>(BulletScene);
@ -60,6 +71,7 @@ public partial class Enemy : Area2D
// bullet.Transform = this.GlobalTransform;
// bullet.Position = this.Position;
bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
_cooldownTimer.Start(RateOfFire);
}
}
@ -99,6 +111,35 @@ public partial class Enemy : Area2D
}
}
// Bullets collision
private void _on_area_entered(Area2D area)
{
}
public void Hit(float damage)
{
if (_isDestroyed) return;
_currentHealth -= damage;
if (!(_currentHealth <= 0)) return;
_isDestroyed = true;
Explode();
}
private void Explode()
{
Debug.WriteLine("Ded");
//CreateParticles();
//CreateDebris();
QueueFree();
}
public bool IsDestroyed()
{
return _isDestroyed;
}
private enum EnemyState
{
Idle,