basic healthbars for enemies
This commit is contained in:
@@ -16,7 +16,7 @@ public partial class Enemy : CharacterBody3D,
|
||||
{
|
||||
// Signals and events
|
||||
public event Action<IDamageable, DamageRecord> DamageTaken;
|
||||
public event Action<IHealthable, float> HealthChanged;
|
||||
public event Action<IHealthable, HealthChangedRecord> HealthChanged;
|
||||
public event Action<IHealthable> HealthDepleted;
|
||||
|
||||
// Public export components
|
||||
@@ -54,6 +54,7 @@ public partial class Enemy : CharacterBody3D,
|
||||
// Private stuff
|
||||
private Area3D _damageBox;
|
||||
private Node3D _target;
|
||||
private CHealthbar _healthbar;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -75,6 +76,8 @@ 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");
|
||||
|
||||
if (RMovement != null) CMovement!.RMovement = RMovement;
|
||||
if (RHealth != null)
|
||||
{
|
||||
@@ -86,9 +89,11 @@ public partial class Enemy : CharacterBody3D,
|
||||
|
||||
public void SetupSignals()
|
||||
{
|
||||
CDamageable.DamageTaken += ReduceHealth;
|
||||
// Anonymous function call to erase return values of ReduceHealth
|
||||
CDamageable.DamageTaken += (source, record) => ReduceHealth(source, record);
|
||||
CDamageable.DamageTaken += RegisterKnockback;
|
||||
CHealth.HealthDepleted += Kill;
|
||||
HealthChanged += (source, record) => _healthbar.OnHealthChanged(record);
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
@@ -147,11 +152,12 @@ public partial class Enemy : CharacterBody3D,
|
||||
return CDamageable.ComputeDamage(damageRecord);
|
||||
}
|
||||
|
||||
public void ReduceHealth(IDamageable source, DamageRecord damageRecord)
|
||||
public HealthChangedRecord ReduceHealth(IDamageable source, DamageRecord damageRecord)
|
||||
{
|
||||
if (CHealth is null) return;
|
||||
CHealth.ReduceHealth(source, damageRecord);
|
||||
HealthChanged?.Invoke(this, CHealth.CurrentHealth);
|
||||
if (CHealth is null) return new HealthChangedRecord(0, 0, 0);
|
||||
var record = CHealth.ReduceHealth(source, damageRecord);
|
||||
HealthChanged?.Invoke(this, record);
|
||||
return record;
|
||||
}
|
||||
|
||||
public void Kill(IHealthable source)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=20 format=3 uid="uid://cmlud1hwkd6sv"]
|
||||
[gd_scene load_steps=21 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"]
|
||||
@@ -7,6 +7,7 @@
|
||||
[ext_resource type="Resource" uid="uid://bwqjaom4k7rc3" path="res://scenes/enemies/flying_enemy/flying_enemy_movement.tres" id="4_dejyg"]
|
||||
[ext_resource type="Script" uid="uid://bjwrpv3jpsc1e" path="res://components/health/CHealth.cs" id="4_ys4jv"]
|
||||
[ext_resource type="PackedScene" uid="uid://dmw5ibwrb483f" path="res://components/movement/CFlyingMovement.tscn" id="7_vaeds"]
|
||||
[ext_resource type="PackedScene" uid="uid://bwx2um43k0ou4" path="res://components/health/CHealthbar.tscn" id="7_ykkxn"]
|
||||
[ext_resource type="Script" uid="uid://dtpxijlnb2c5" path="res://components/movement/RMovement.cs" id="8_on7rt"]
|
||||
[ext_resource type="Script" uid="uid://b0u23nkpaimyc" path="res://components/damage/CDamageable.cs" id="8_uotso"]
|
||||
[ext_resource type="PackedScene" uid="uid://bctpe34ddamg5" path="res://components/knockback/CKnockback.tscn" id="10_dejyg"]
|
||||
@@ -58,6 +59,9 @@ script = ExtResource("4_ys4jv")
|
||||
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, 2.2, 0)
|
||||
|
||||
[node name="CDamageable" type="Node" parent="."]
|
||||
script = ExtResource("8_uotso")
|
||||
DamageModifiers = Array[Object]([SubResource("Resource_jnv07"), SubResource("Resource_53j1c")])
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=19 format=3 uid="uid://dxt0e2ugmttqq"]
|
||||
[gd_scene load_steps=20 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"]
|
||||
@@ -7,6 +7,7 @@
|
||||
[ext_resource type="Resource" uid="uid://bohbojc68j7y1" path="res://scenes/enemies/grounded_enemy/grounded_enemy_health.tres" id="2_w4lm8"]
|
||||
[ext_resource type="Resource" uid="uid://bqq6uukbdfysr" path="res://scenes/enemies/grounded_enemy/grounded_enemy_movement.tres" id="4_na24f"]
|
||||
[ext_resource type="Script" uid="uid://b0u23nkpaimyc" path="res://components/damage/CDamageable.cs" id="7_1tw73"]
|
||||
[ext_resource type="PackedScene" uid="uid://bwx2um43k0ou4" path="res://components/health/CHealthbar.tscn" id="7_18xwy"]
|
||||
[ext_resource type="PackedScene" uid="uid://dbr7ioio158ew" path="res://components/movement/CGroundedMovement.tscn" id="7_qyswd"]
|
||||
[ext_resource type="Script" uid="uid://dtpxijlnb2c5" path="res://components/movement/RMovement.cs" id="8_6d4gl"]
|
||||
[ext_resource type="PackedScene" uid="uid://bctpe34ddamg5" path="res://components/knockback/CKnockback.tscn" id="10_jqqi6"]
|
||||
@@ -53,6 +54,9 @@ script = ExtResource("2_gsmti")
|
||||
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)
|
||||
|
||||
[node name="CDamageable" type="Node" parent="."]
|
||||
script = ExtResource("7_1tw73")
|
||||
DamageModifiers = Array[Object]([SubResource("Resource_qj0ob")])
|
||||
|
||||
Reference in New Issue
Block a user