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.CreateChild(DebrisScene); // if (DebrisScene == null) return; // var debris = DebrisScene.Instantiate(); // 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.CreateChild(ExplosionParticles); if (particle == null) return; particle.Emitting = true; // if (ExplosionParticles == null) return; // var emitter = ExplosionParticles.Instantiate(); // Owner.CallDeferred("add_child", emitter); // emitter.Transform = this.GlobalTransform; // emitter.Position = this.Position; // // emitter.Emitting = true; } // private T CreateChild(PackedScene prefab) where T : Node2D // { // if (prefab == null) return null; // var newInstance = prefab.Instantiate(); // 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; } }