more stuff
This commit is contained in:
60
scripts/components/HealthComponent.cs
Normal file
60
scripts/components/HealthComponent.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
|
||||
namespace NodeWar.scripts.components;
|
||||
|
||||
public partial class HealthComponent : ProgressBar
|
||||
{
|
||||
[Export]
|
||||
public Sprite2D Gfx { get; set; }
|
||||
|
||||
[Export]
|
||||
public float Health { get; set; } = 100;
|
||||
|
||||
[Export]
|
||||
public float MaxHealth { get; set; } = 100;
|
||||
|
||||
[Signal]
|
||||
public delegate float HealthChangedEventHandler(float oldHealth, float newHealth);
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
{
|
||||
Health -= damage;
|
||||
EmitSignal(SignalName.HealthChanged, Health + damage, Health);
|
||||
|
||||
if (Health <= 0)
|
||||
{
|
||||
Die();
|
||||
return;
|
||||
}
|
||||
|
||||
if (Gfx != null)
|
||||
{
|
||||
var originalColor = Gfx.Modulate;
|
||||
|
||||
Gfx.Modulate = new Color(10, 10, 10);
|
||||
|
||||
GetTree().CreateTimer(0.1f).Timeout += () =>
|
||||
{
|
||||
Gfx.Modulate = originalColor;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public void Die()
|
||||
{
|
||||
GD.Print("I am dying!");
|
||||
GetParent().QueueFree();
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Value = Health;
|
||||
MaxValue = MaxHealth;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
Value = Health;
|
||||
}
|
||||
}
|
||||
1
scripts/components/HealthComponent.cs.uid
Normal file
1
scripts/components/HealthComponent.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b26j7cq2pgk6q
|
||||
29
scripts/components/health_component.tscn
Normal file
29
scripts/components/health_component.tscn
Normal file
@@ -0,0 +1,29 @@
|
||||
[gd_scene format=3 uid="uid://dn6jqa3wj40ir"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://b26j7cq2pgk6q" path="res://scripts/components/HealthComponent.cs" id="1_kkm8u"]
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_21bcp"]
|
||||
bg_color = Color(0.4249828, 0.6869066, 0, 1)
|
||||
corner_radius_top_left = 6
|
||||
corner_radius_top_right = 6
|
||||
corner_radius_bottom_right = 6
|
||||
corner_radius_bottom_left = 6
|
||||
|
||||
[node name="HealthComponent" type="ProgressBar" unique_id=1360050278]
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -13.5
|
||||
offset_top = -64.0
|
||||
offset_right = 13.5
|
||||
offset_bottom = 64.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme_override_styles/fill = SubResource("StyleBoxFlat_21bcp")
|
||||
max_value = 10.0
|
||||
value = 5.36
|
||||
fill_mode = 3
|
||||
show_percentage = false
|
||||
script = ExtResource("1_kkm8u")
|
||||
Reference in New Issue
Block a user