using Godot; using System; using System.Diagnostics; 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; } private float _currentHealth; 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"); CreateDebris(); QueueFree(); } private void CreateDebris() { if (DebrisScene == null) return; var debris = DebrisScene.Instantiate(); Owner.AddChild(debris); debris.Transform = this.GlobalTransform; debris.Position = this.Position; } public void Hit(float damage) { if (_isDestroyed) return; _currentHealth -= damage; if (!(_currentHealth <= 0)) return; _isDestroyed = true; Explode(); } public bool IsDestroyed() { return _isDestroyed; } }