cirnogodot/Scenes/Barrel.cs

97 lines
2.2 KiB
C#
Raw Normal View History

2024-06-09 00:34:36 +02:00
using Godot;
using System;
using System.Diagnostics;
2024-06-09 18:19:57 +02:00
using Cirno.Scripts;
2024-06-09 00:34:36 +02:00
public partial class Barrel : Area2D, IDestructible
{
2024-06-09 11:45:22 +02:00
[Export] public float Health = 1f;
2024-06-09 00:34:36 +02:00
2024-06-09 11:45:22 +02:00
[Export] public float ExplosionRadius = 1;
2024-06-09 00:34:36 +02:00
2024-06-09 11:45:22 +02:00
[Export] public float ExplosionDamage = 1;
[Export] public PackedScene DebrisScene { get; set; }
2024-06-09 18:19:57 +02:00
[Export] public PackedScene ExplosionParticles { get; set; }
2024-06-09 00:34:36 +02:00
2024-06-09 18:19:57 +02:00
private float _currentHealth = 0f;
2024-06-09 00:34:36 +02:00
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)
{
}
2024-06-09 11:45:22 +02:00
private void Explode()
{
2024-06-09 00:34:36 +02:00
Debug.WriteLine("Boom");
2024-06-09 18:19:57 +02:00
CreateParticles();
2024-06-09 11:45:22 +02:00
CreateDebris();
2024-06-09 00:34:36 +02:00
QueueFree();
}
2024-06-09 11:45:22 +02:00
private void CreateDebris()
{
2024-06-09 18:19:57 +02:00
this.CreateChild<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;
}
2024-06-09 18:19:57 +02:00
var particle = this.CreateChild<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;
2024-06-09 11:45:22 +02:00
}
2024-06-09 18:19:57 +02:00
// 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;
// }
2024-06-09 00:34:36 +02:00
public void Hit(float damage)
{
if (_isDestroyed) return;
2024-06-09 11:45:22 +02:00
_currentHealth -= damage;
if (!(_currentHealth <= 0)) return;
2024-06-09 00:34:36 +02:00
_isDestroyed = true;
Explode();
}
public bool IsDestroyed()
{
return _isDestroyed;
}
}