cirnogodot/Scenes/Barrel.cs

60 lines
1.1 KiB
C#
Raw Normal View History

2024-06-09 00:34:36 +02:00
using Godot;
using System;
using System.Diagnostics;
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 00:34:36 +02:00
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)
{
}
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 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()
{
if (DebrisScene == null) return;
var debris = DebrisScene.Instantiate<Barrel>();
Owner.AddChild(debris);
debris.Transform = this.GlobalTransform;
debris.Position = this.Position;
}
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;
}
}