better healthbars and one for the player as well
This commit is contained in:
@@ -57,7 +57,7 @@ public partial class Enemy : CharacterBody3D,
|
||||
// Private stuff
|
||||
private Area3D _damageBox;
|
||||
private Node3D _target;
|
||||
private CHealthbar _healthbar;
|
||||
private Healthbar _healthbar;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -79,7 +79,7 @@ public partial class Enemy : CharacterBody3D,
|
||||
if (CHealth is null) GD.PrintErr("This node needs a 'CHealth' child of type IHealthable!");
|
||||
if (CKnockback is null) GD.PrintErr("This node needs a 'CKnockback' child of type IKnockbackable!");
|
||||
|
||||
_healthbar = GetNode<CHealthbar>("CHealthBar");
|
||||
_healthbar = GetNode<CHealthbar>("CHealthBar").Healthbar;
|
||||
|
||||
if (RMovement != null) CMovement!.RMovement = RMovement;
|
||||
if (RHealth != null)
|
||||
@@ -88,6 +88,7 @@ public partial class Enemy : CharacterBody3D,
|
||||
CHealth.CurrentHealth = RHealth.StartingHealth;
|
||||
}
|
||||
if (RKnockback != null) CKnockback!.RKnockback = RKnockback;
|
||||
_healthbar.Initialize(CHealth!.CurrentHealth);
|
||||
}
|
||||
|
||||
public void SetupSignals()
|
||||
@@ -96,7 +97,7 @@ public partial class Enemy : CharacterBody3D,
|
||||
CDamageable.DamageTaken += (source, record) => ReduceHealth(source, record);
|
||||
CDamageable.DamageTaken += (source, record) => RegisterKnockback(new KnockbackRecord(record));
|
||||
CHealth.HealthDepleted += Kill;
|
||||
HealthChanged += (source, record) => _healthbar.OnHealthChanged(record);
|
||||
HealthChanged += (source, record) => _healthbar.SetHealth(record.CurrentHealth);
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=21 format=3 uid="uid://cmlud1hwkd6sv"]
|
||||
[gd_scene load_steps=22 format=3 uid="uid://cmlud1hwkd6sv"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bn7sc6id7n166" path="res://scenes/enemies/Enemy.cs" id="1_q8l7o"]
|
||||
[ext_resource type="Script" uid="uid://b6y3ugfydvch0" path="res://components/damage/RDamageModifier.cs" id="2_1bsgx"]
|
||||
@@ -13,6 +13,9 @@
|
||||
[ext_resource type="PackedScene" uid="uid://bctpe34ddamg5" path="res://components/knockback/CKnockback.tscn" id="10_dejyg"]
|
||||
[ext_resource type="Resource" uid="uid://dt7a1io5o0b8s" path="res://scenes/enemies/flying_enemy/flying_enemy_knockback.tres" id="11_mpa2u"]
|
||||
|
||||
[sub_resource type="ViewportTexture" id="ViewportTexture_ykkxn"]
|
||||
viewport_path = NodePath("SubViewport")
|
||||
|
||||
[sub_resource type="Resource" id="Resource_jnv07"]
|
||||
script = ExtResource("2_1bsgx")
|
||||
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
|
||||
@@ -62,7 +65,8 @@ RHealth = ExtResource("2_ma2bq")
|
||||
metadata/_custom_type_script = "uid://bjwrpv3jpsc1e"
|
||||
|
||||
[node name="CHealthBar" parent="." instance=ExtResource("7_ykkxn")]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.70000005, 0)
|
||||
transform = Transform3D(0.3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.70000005, 0)
|
||||
texture = SubResource("ViewportTexture_ykkxn")
|
||||
|
||||
[node name="CDamageable" type="Node" parent="."]
|
||||
script = ExtResource("8_uotso")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=21 format=3 uid="uid://dxt0e2ugmttqq"]
|
||||
[gd_scene load_steps=22 format=3 uid="uid://dxt0e2ugmttqq"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bn7sc6id7n166" path="res://scenes/enemies/Enemy.cs" id="1_r6506"]
|
||||
[ext_resource type="Resource" uid="uid://otfc2snh8umc" path="res://scenes/enemies/grounded_enemy/grounded_enemy_damage.tres" id="2_bn56u"]
|
||||
@@ -13,6 +13,9 @@
|
||||
[ext_resource type="PackedScene" uid="uid://bctpe34ddamg5" path="res://components/knockback/CKnockback.tscn" id="10_jqqi6"]
|
||||
[ext_resource type="Resource" uid="uid://cektf6waf4s04" path="res://scenes/enemies/grounded_enemy/grounded_enemy_knockback.tres" id="11_8k3xb"]
|
||||
|
||||
[sub_resource type="ViewportTexture" id="ViewportTexture_18xwy"]
|
||||
viewport_path = NodePath("SubViewport")
|
||||
|
||||
[sub_resource type="Resource" id="Resource_qj0ob"]
|
||||
script = ExtResource("2_r3cnf")
|
||||
Modifier = 1.0
|
||||
@@ -62,7 +65,8 @@ RHealth = ExtResource("2_w4lm8")
|
||||
metadata/_custom_type_script = "uid://bjwrpv3jpsc1e"
|
||||
|
||||
[node name="CHealthBar" parent="." instance=ExtResource("7_18xwy")]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.2, 0)
|
||||
transform = Transform3D(0.4, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.2, 0)
|
||||
texture = SubResource("ViewportTexture_18xwy")
|
||||
|
||||
[node name="CDamageable" type="Node" parent="."]
|
||||
script = ExtResource("7_1tw73")
|
||||
|
||||
58
scenes/ui/Healthbar.cs
Normal file
58
scenes/ui/Healthbar.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class Healthbar : ProgressBar
|
||||
{
|
||||
private Timer _damageCatchUpTimer;
|
||||
private ProgressBar _damagedHealth;
|
||||
|
||||
private float _currentHealth;
|
||||
public float CurrentHealth
|
||||
{
|
||||
get => _currentHealth;
|
||||
set => SetHealth(value);
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_damageCatchUpTimer = GetNode<Timer>("DamageCatchUp");
|
||||
_damagedHealth = GetNode<ProgressBar>("Damagebar");
|
||||
|
||||
_damageCatchUpTimer.Timeout += OnDamageCatchUp;
|
||||
Visible = false;
|
||||
}
|
||||
|
||||
public void Initialize(float initialHealth)
|
||||
{
|
||||
_currentHealth = initialHealth;
|
||||
MaxValue = initialHealth;
|
||||
Value = initialHealth;
|
||||
_damagedHealth.MaxValue = initialHealth;
|
||||
_damagedHealth.Value = initialHealth;
|
||||
}
|
||||
|
||||
public void SetHealth(float newHealth)
|
||||
{
|
||||
var previousHealth = _currentHealth;
|
||||
_currentHealth = Mathf.Min(newHealth, (float) MaxValue);
|
||||
_currentHealth = newHealth;
|
||||
Value = _currentHealth;
|
||||
|
||||
Visible = _currentHealth < MaxValue;
|
||||
|
||||
if (_currentHealth < previousHealth)
|
||||
{
|
||||
_damageCatchUpTimer.Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
_damagedHealth.Value = _currentHealth;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDamageCatchUp()
|
||||
{
|
||||
_damagedHealth.Value = _currentHealth;
|
||||
}
|
||||
}
|
||||
1
scenes/ui/Healthbar.cs.uid
Normal file
1
scenes/ui/Healthbar.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://l5cjcaehyssk
|
||||
49
scenes/ui/healthbar.tscn
Normal file
49
scenes/ui/healthbar.tscn
Normal file
@@ -0,0 +1,49 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://cyw8p0p6a78tl"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://l5cjcaehyssk" path="res://scenes/ui/Healthbar.cs" id="1_0k5hr"]
|
||||
|
||||
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_0sgot"]
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_0k5hr"]
|
||||
bg_color = Color(0.698864, 0.047356047, 0, 1)
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_0sgot"]
|
||||
bg_color = Color(0.14767182, 0.14767182, 0.14767176, 1)
|
||||
expand_margin_left = 2.0
|
||||
expand_margin_top = 2.0
|
||||
expand_margin_right = 2.0
|
||||
expand_margin_bottom = 2.0
|
||||
shadow_color = Color(0, 0, 0, 0.27450982)
|
||||
shadow_offset = Vector2(0, 1)
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_kl70x"]
|
||||
bg_color = Color(0.8862616, 0.88626146, 0.8862615, 1)
|
||||
|
||||
[node name="Healthbar" type="ProgressBar"]
|
||||
z_index = 10
|
||||
custom_minimum_size = Vector2(512, 12)
|
||||
offset_right = 512.0
|
||||
offset_bottom = 12.0
|
||||
theme_override_styles/background = SubResource("StyleBoxEmpty_0sgot")
|
||||
theme_override_styles/fill = SubResource("StyleBoxFlat_0k5hr")
|
||||
value = 60.0
|
||||
show_percentage = false
|
||||
script = ExtResource("1_0k5hr")
|
||||
|
||||
[node name="Damagebar" type="ProgressBar" parent="."]
|
||||
z_index = -10
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme_override_styles/background = SubResource("StyleBoxFlat_0sgot")
|
||||
theme_override_styles/fill = SubResource("StyleBoxFlat_kl70x")
|
||||
value = 80.0
|
||||
show_percentage = false
|
||||
|
||||
[node name="DamageCatchUp" type="Timer" parent="."]
|
||||
wait_time = 0.8
|
||||
one_shot = true
|
||||
ignore_time_scale = true
|
||||
Reference in New Issue
Block a user