cirnogodot/Scenes/Barrel.cs
2025-01-27 17:13:26 +01:00

97 lines
2.2 KiB
C#

using Godot;
using System;
using System.Diagnostics;
using Cirno.Scripts;
public partial class Barrel : Area2D, IDestructible
{
[Export] public float Health = 1f;
[Export] public float ExplosionRadius = 1;
[Export] public float ExplosionDamage = 1;
[Export] public PackedScene DebrisScene { get; set; }
[Export] public PackedScene ExplosionParticles { get; set; }
private float _currentHealth = 0f;
private bool _isDestroyed = false;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_currentHealth = Health;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
private void Explode()
{
Debug.WriteLine("Boom");
CreateParticles();
CreateDebris();
QueueFree();
}
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()
{
if (ExplosionParticles == null) {
GD.PushWarning("Object has no particles associated");
return;
}
var particle = this.CreateSibling<GpuParticles2D>(ExplosionParticles);
if (particle == null) return;
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;
_isDestroyed = true;
Explode();
}
public bool IsDestroyed()
{
return _isDestroyed;
}
}