Explosions

This commit is contained in:
MaddoScientisto 2025-02-09 21:34:50 +01:00
commit b4a35ac3d4
7 changed files with 134 additions and 30 deletions

View file

@ -15,6 +15,8 @@ public partial class Barrel : Area2D, IDestructible
[Export] public PackedScene ExplosionParticles { get; set; }
[Export] public PackedScene ExplosionScene { get; set; }
private float _currentHealth = 0f;
private bool _isDestroyed = false;
@ -32,21 +34,61 @@ public partial class Barrel : Area2D, IDestructible
private void Explode()
{
Debug.WriteLine("Boom");
//ApplyExplosionDamage();
CreateExplosion();
CreateParticles();
CreateDebris();
QueueFree();
}
private void CreateExplosion()
{
if (ExplosionScene == null) return;
var explosion = this.CreateSibling<Bullet>(ExplosionScene);
explosion.Speed = 0;
}
private void ApplyExplosionDamage()
{
var spaceState = GetWorld2D().DirectSpaceState;
var query = new PhysicsShapeQueryParameters2D();
var shape = new CircleShape2D { Radius = ExplosionRadius };
query.SetShape(shape);
query.Transform = new Transform2D(0, GlobalPosition);
query.CollideWithBodies = false;
query.CollideWithAreas = true;
//query.CollisionMask = ;
var results = spaceState.IntersectShape(query);
GD.Print($"Shapes in range: {results.Count}");
foreach (var result in results)
{
if (result.TryGetValue("collider", out var colliderObj))
{
var collider = colliderObj.As<Node2D>();
if (collider.GetParent<Area2D>() is IDestructible destructible)
{
GD.Print($"HITTING {collider.Name}");
destructible.Hit(ExplosionDamage);
}
else {
GD.Print($"Collider {collider.Name} was not idestructible");
}
}
}
}
private void CreateDebris()
{
this.CreateSibling<Barrel>(DebrisScene);
// if (DebrisScene == null) return;
// var debris = DebrisScene.Instantiate<Barrel>();
// Owner.CallDeferred("add_child", debris); //.AddChild(debris);
// debris.Transform = this.GlobalTransform;
// debris.Position = this.Position;
}
private void CreateParticles()
@ -59,32 +101,15 @@ public partial class Barrel : Area2D, IDestructible
particle.Emitting = true;
// if (ExplosionParticles == null) return;
// var emitter = ExplosionParticles.Instantiate<GpuParticles2D>();
// Owner.CallDeferred("add_child", emitter);
// emitter.Transform = this.GlobalTransform;
// emitter.Position = this.Position;
//
// emitter.Emitting = true;
}
// private T CreateChild<T>(PackedScene prefab) where T : Node2D
// {
// if (prefab == null) return null;
// var newInstance = prefab.Instantiate<T>();
// Owner.CallDeferred("add_child", newInstance);
// newInstance.Transform = this.GlobalTransform;
// newInstance.Position = this.Position;
//
// return newInstance;
// }
public void Hit(float damage)
{
if (_isDestroyed) return;
_currentHealth -= damage;
if (!(_currentHealth <= 0)) return;
if (_currentHealth > 0) return;
_isDestroyed = true;
Explode();
}