broken composition and signals

This commit is contained in:
2026-01-17 17:02:31 +01:00
parent 0436053c62
commit 4ccdbc0ee6
22 changed files with 302 additions and 36 deletions

View File

@@ -1,10 +1,10 @@
using Godot;
using System;
using System.Linq;
using Movementtests.interfaces;
[GlobalClass]
public partial class FirstEnemy : CharacterBody3D, IDamageable, IKillable, IKnockbackable, IDamageMaker
public partial class FirstEnemy : CharacterBody3D, IDamageable, IDamageMaker, IHealthable, IKillable
{
[Export]
public Node3D Target { get; set; }
@@ -16,6 +16,12 @@ public partial class FirstEnemy : CharacterBody3D, IDamageable, IKillable, IKnoc
public RDamage GetDamageDealt { get; set; }
[Export]
public RDamageModifier[] DamageModifiers { get; set; }
[Export]
public CHealth Health { get; set; }
[Export]
public RDeathEffect[] DeathEffects { get; set; }
private RayCast3D _wallInFrontRayCast;
private Area3D _damageBox;
@@ -24,11 +30,22 @@ public partial class FirstEnemy : CharacterBody3D, IDamageable, IKillable, IKnoc
{
_wallInFrontRayCast = GetNode<RayCast3D>("WallInFrontRayCast");
_damageBox = GetNode<Area3D>("DamageBox");
_damageBox.BodyEntered += OnDamageBoxTriggered;
foreach (var damageable in DamageModifiers)
damageable.DamageTaken += ReduceHealth;
Health.Dead += Kill;
}
public override void _PhysicsProcess(double delta)
{
if (Engine.IsEditorHint())
{
return;
}
var target = Target.GlobalPosition;
var direction = (target - GlobalPosition).Normalized();
@@ -53,9 +70,34 @@ public partial class FirstEnemy : CharacterBody3D, IDamageable, IKillable, IKnoc
if(body is IDamageable damageable) damageable.TakeDamage(GetDamageDealt);
}
public void TakeDamage(RDamage damage)
public event Action<float> DamageTaken;
public float TakeDamage(RDamage damage)
{
var finalDamage = 0f;
foreach (var damageable in DamageModifiers.ToIDamageables())
damageable.TakeDamage(damage);
finalDamage += damageable.TakeDamage(damage);
DamageTaken?.Invoke(finalDamage);
return finalDamage;
}
public float CurrentHealth
{
get => Health.CurrentHealth;
set => Health.CurrentHealth = value;
}
public void ReduceHealth(float amount)
{
Health.ReduceHealth(amount);
}
public void Kill()
{
foreach (var killable in DeathEffects.ToIKillables())
{
killable.Kill();
}
QueueFree();
}
}

View File

@@ -3,7 +3,7 @@ using System;
using Movementtests.interfaces;
[GlobalClass]
public partial class FlyingEnemy : CharacterBody3D, IDamageable, IKillable, IKnockbackable, IDamageMaker
public partial class FlyingEnemy : CharacterBody3D, IDamageable, IHealthable, IKnockbackable, IDamageMaker
{
[Export]
public Node3D Target { get; set; }
@@ -17,6 +17,11 @@ public partial class FlyingEnemy : CharacterBody3D, IDamageable, IKillable, IKno
[Export]
public RDamageModifier[] DamageModifiers { get; set; }
[Export]
public CHealth Health { get; set; }
[Export]
public RDeathEffect[] DeathEffects { get; set; }
private RayCast3D _groundDistanceRaycast;
private Area3D _damageBox;
@@ -32,6 +37,10 @@ public partial class FlyingEnemy : CharacterBody3D, IDamageable, IKillable, IKno
_damageBox.BodyEntered += OnDamageBoxTriggered;
_randomDirection = new Vector3(GD.RandRange(-1, 1), 1, GD.RandRange(-1, 1)).Normalized();
foreach (var damageable in DamageModifiers)
damageable.DamageTaken += ReduceHealth;
Health.Dead += Kill;
}
public override void _PhysicsProcess(double delta)
@@ -84,9 +93,29 @@ public partial class FlyingEnemy : CharacterBody3D, IDamageable, IKillable, IKno
if(body is IDamageable damageable) damageable.TakeDamage(GetDamageDealt);
}
public void TakeDamage(RDamage damage)
public event Action<float> DamageTaken;
public float TakeDamage(RDamage damage)
{
var finalDamage = 0f;
foreach (var damageable in DamageModifiers.ToIDamageables())
damageable.TakeDamage(damage);
finalDamage += damageable.TakeDamage(damage);
DamageTaken?.Invoke(finalDamage);
return finalDamage;
}
public void ReduceHealth(float amount)
{
GD.Print("Reducing health by " + amount);
Health.ReduceHealth(amount);
}
public void Kill()
{
foreach (var killable in DeathEffects.ToIKillables())
{
killable.Kill();
}
QueueFree();
}
}

View File

@@ -1,12 +1,50 @@
[gd_scene load_steps=9 format=3 uid="uid://dxt0e2ugmttqq"]
[gd_scene load_steps=21 format=3 uid="uid://dxt0e2ugmttqq"]
[ext_resource type="Script" uid="uid://bn7sc6id7n166" path="res://scenes/enemies/FirstEnemy.cs" id="1_4yfjf"]
[ext_resource type="Script" uid="uid://b6y3ugfydvch0" path="res://resource_definitions/RDamageModifier.cs" id="2_ylcc3"]
[ext_resource type="Script" uid="uid://b2vdwkiqauhk3" path="res://scenes/enemies/EnemyInitInputs.cs" id="2_42xu3"]
[ext_resource type="Script" uid="uid://bjwrpv3jpsc1e" path="res://scenes/health/CHealth.cs" id="2_gsmti"]
[ext_resource type="Script" uid="uid://b6y3ugfydvch0" path="res://resource_definitions/RDamageModifier.cs" id="2_r3cnf"]
[ext_resource type="Script" uid="uid://jitubgv6judn" path="res://resource_definitions/RDamage.cs" id="3_1tw73"]
[ext_resource type="Script" uid="uid://b4cwruitopcee" path="res://resource_definitions/RDeathEffect.cs" id="3_onjoj"]
[ext_resource type="Script" uid="uid://baiapod3csndf" path="res://resource_definitions/RHealth.cs" id="3_pvbr4"]
[sub_resource type="Resource" id="Resource_gsmti"]
script = ExtResource("2_ylcc3")
[sub_resource type="Resource" id="Resource_66m7n"]
script = ExtResource("2_42xu3")
Speed = 2.0
metadata/_custom_type_script = "uid://b2vdwkiqauhk3"
[sub_resource type="Resource" id="Resource_a6xb8"]
script = ExtResource("3_1tw73")
metadata/_custom_type_script = "uid://jitubgv6judn"
[sub_resource type="Resource" id="Resource_42xu3"]
script = ExtResource("2_r3cnf")
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
[sub_resource type="Resource" id="Resource_1tw73"]
script = ExtResource("2_r3cnf")
DamageType = 1
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
[sub_resource type="Resource" id="Resource_qj0ob"]
script = ExtResource("2_r3cnf")
DamageType = 2
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
[sub_resource type="Resource" id="Resource_d7vrc"]
script = ExtResource("2_r3cnf")
DamageType = 3
Modifier = 2.0
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
[sub_resource type="Resource" id="Resource_7gyf3"]
script = ExtResource("3_onjoj")
metadata/_custom_type_script = "uid://b4cwruitopcee"
[sub_resource type="Resource" id="Resource_r3cnf"]
script = ExtResource("3_pvbr4")
metadata/_custom_type_script = "uid://baiapod3csndf"
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_62kkh"]
[sub_resource type="CapsuleMesh" id="CapsuleMesh_3uydm"]
@@ -23,11 +61,20 @@ albedo_color = Color(0.06469653, 0.06469653, 0.06469653, 1)
[sub_resource type="BoxShape3D" id="BoxShape3D_4yfjf"]
size = Vector3(1, 2, 2)
[node name="FirstEnemy" type="CharacterBody3D"]
[node name="FirstEnemy" type="CharacterBody3D" node_paths=PackedStringArray("Health")]
collision_layer = 16
collision_mask = 273
script = ExtResource("1_4yfjf")
DamageModifiers = Array[Object]([SubResource("Resource_gsmti")])
Inputs = SubResource("Resource_66m7n")
GetDamageDealt = SubResource("Resource_a6xb8")
DamageModifiers = Array[Object]([SubResource("Resource_42xu3"), SubResource("Resource_1tw73"), SubResource("Resource_qj0ob"), SubResource("Resource_d7vrc")])
Health = NodePath("CHealth")
DeathEffects = Array[Object]([SubResource("Resource_7gyf3")])
[node name="CHealth" type="Node" parent="."]
script = ExtResource("2_gsmti")
HealthResource = SubResource("Resource_r3cnf")
metadata/_custom_type_script = "uid://bjwrpv3jpsc1e"
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)

View File

@@ -0,0 +1,7 @@
[gd_resource type="Resource" script_class="RHealth" load_steps=2 format=3 uid="uid://bohbojc68j7y1"]
[ext_resource type="Script" uid="uid://baiapod3csndf" path="res://resource_definitions/RHealth.cs" id="1_h6jgd"]
[resource]
script = ExtResource("1_h6jgd")
metadata/_custom_type_script = "uid://baiapod3csndf"

View File

@@ -1,7 +1,24 @@
[gd_scene load_steps=10 format=3 uid="uid://cmlud1hwkd6sv"]
[gd_scene load_steps=21 format=3 uid="uid://cmlud1hwkd6sv"]
[ext_resource type="Script" uid="uid://cmvep0qi7qlvf" path="res://scenes/enemies/FlyingEnemy.cs" id="1_b46rq"]
[ext_resource type="Script" uid="uid://b6y3ugfydvch0" path="res://resource_definitions/RDamageModifier.cs" id="2_1bsgx"]
[ext_resource type="Script" uid="uid://do0dic4r3ri0s" path="res://scenes/enemies/FlyingEnemyInputs.cs" id="2_aarbi"]
[ext_resource type="Script" uid="uid://jitubgv6judn" path="res://resource_definitions/RDamage.cs" id="2_irbqc"]
[ext_resource type="Script" uid="uid://b4cwruitopcee" path="res://resource_definitions/RDeathEffect.cs" id="4_12h4s"]
[ext_resource type="Script" uid="uid://bjwrpv3jpsc1e" path="res://scenes/health/CHealth.cs" id="4_ys4jv"]
[ext_resource type="Script" uid="uid://baiapod3csndf" path="res://resource_definitions/RHealth.cs" id="5_irbqc"]
[sub_resource type="Resource" id="Resource_7nxyi"]
script = ExtResource("2_aarbi")
Speed = 5.0
TargetHeight = 10.0
metadata/_custom_type_script = "uid://do0dic4r3ri0s"
[sub_resource type="Resource" id="Resource_o3msb"]
script = ExtResource("2_irbqc")
DamageDealt = 2.0
DamageType = 2
metadata/_custom_type_script = "uid://jitubgv6judn"
[sub_resource type="Resource" id="Resource_2pnje"]
script = ExtResource("2_1bsgx")
@@ -13,6 +30,26 @@ DamageType = 1
Modifier = 2.0
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
[sub_resource type="Resource" id="Resource_irbqc"]
script = ExtResource("2_1bsgx")
DamageType = 2
Modifier = 0.5
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
[sub_resource type="Resource" id="Resource_uotso"]
script = ExtResource("2_1bsgx")
DamageType = 3
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
[sub_resource type="Resource" id="Resource_ys4jv"]
script = ExtResource("4_12h4s")
metadata/_custom_type_script = "uid://b4cwruitopcee"
[sub_resource type="Resource" id="Resource_aarbi"]
script = ExtResource("5_irbqc")
StartingHealth = 50.0
metadata/_custom_type_script = "uid://baiapod3csndf"
[sub_resource type="SphereShape3D" id="SphereShape3D_b46rq"]
[sub_resource type="SphereMesh" id="SphereMesh_1bsgx"]
@@ -29,12 +66,21 @@ albedo_color = Color(0.06469653, 0.06469653, 0.06469653, 1)
[sub_resource type="BoxShape3D" id="BoxShape3D_4yfjf"]
size = Vector3(1, 1, 1.5)
[node name="FlyingEnemy" type="CharacterBody3D"]
[node name="FlyingEnemy" type="CharacterBody3D" node_paths=PackedStringArray("Health")]
collision_layer = 16
collision_mask = 273
motion_mode = 1
script = ExtResource("1_b46rq")
DamageModifiers = Array[Object]([SubResource("Resource_2pnje"), SubResource("Resource_1bsgx")])
Inputs = SubResource("Resource_7nxyi")
GetDamageDealt = SubResource("Resource_o3msb")
DamageModifiers = Array[Object]([SubResource("Resource_2pnje"), SubResource("Resource_1bsgx"), SubResource("Resource_irbqc"), SubResource("Resource_uotso")])
Health = NodePath("CHealth")
DeathEffects = Array[Object]([SubResource("Resource_ys4jv")])
[node name="CHealth" type="Node" parent="."]
script = ExtResource("4_ys4jv")
HealthResource = SubResource("Resource_aarbi")
metadata/_custom_type_script = "uid://bjwrpv3jpsc1e"
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("SphereShape3D_b46rq")

View File

@@ -0,0 +1,8 @@
[gd_resource type="Resource" script_class="RHealth" load_steps=2 format=3 uid="uid://dg1xbjhyhgnnk"]
[ext_resource type="Script" uid="uid://baiapod3csndf" path="res://resource_definitions/RHealth.cs" id="1_jht15"]
[resource]
script = ExtResource("1_jht15")
StartingHealth = 50.0
metadata/_custom_type_script = "uid://baiapod3csndf"