From 5a6c711296021487cb5df382e9f9c77f6220e761 Mon Sep 17 00:00:00 2001 From: Lilith Schier Date: Sat, 1 Aug 2026 20:07:49 +0200 Subject: [PATCH] added player hurtbox --- project.godot | 2 ++ src/combat/Hitbox.cs | 15 +++++++++------ src/combat/Hurtbox.cs | 5 ----- src/combat/hurtbox.tscn | 2 +- src/creatures/birb/Birb.cs | 11 ++++++----- src/creatures/birb/birb.tscn | 16 ++++++++-------- src/player/Player.cs | 25 +++++++++++++++++++++---- src/player/player.tscn | 13 +++++++------ 8 files changed, 54 insertions(+), 35 deletions(-) diff --git a/project.godot b/project.godot index ee1884f..0a6f3e2 100644 --- a/project.godot +++ b/project.godot @@ -96,6 +96,8 @@ parry={ 2d_physics/layer_5="enemy_hitbox" 2d_physics/layer_6="water" 2d_physics/layer_7="platform" +2d_physics/layer_8="player" +2d_physics/layer_9="enemy" [physics] diff --git a/src/combat/Hitbox.cs b/src/combat/Hitbox.cs index c52e3af..f3fe1ea 100644 --- a/src/combat/Hitbox.cs +++ b/src/combat/Hitbox.cs @@ -16,7 +16,7 @@ public sealed partial class Hitbox : Area2D private int LocalCountLimit { get; set; } = 1; [Signal] - public delegate void BodyHitEventHandler(Hitbox hitbox, Node2D body); + public delegate void TargetHitEventHandler(Hitbox hitbox, Node2D body); private readonly Dictionary _immunityEntries = []; private readonly HashSet _bodies = []; @@ -25,7 +25,10 @@ public sealed partial class Hitbox : Area2D public void OnReady() { BodyEntered += OnEntered; - BodyExited += OnLeft; + BodyExited += OnExited; + + AreaEntered += OnEntered; + AreaExited += OnExited; } public void OnResolved() => HitboxOwner.OnHitboxAdded(this); @@ -38,7 +41,7 @@ public sealed partial class Hitbox : Area2D _bodies.Add(body); } - private void OnLeft(Node2D body) + private void OnExited(Node2D body) { _bodies.Remove(body); } @@ -48,16 +51,16 @@ public sealed partial class Hitbox : Area2D foreach (var body in _bodies) CheckForHit(body, delta); } - private void CheckForHit(Node2D body, double delta = 0.0) + private void CheckForHit(Node2D target, double delta = 0.0) { - if (!_immunityEntries.TryGetValue(body, out var entry)) _immunityEntries.Add(body, entry = new()); + if (!_immunityEntries.TryGetValue(target, out var entry)) _immunityEntries.Add(target, entry = new()); entry.Immunity -= (float)delta; if (_checkingForHits && entry.Immunity <= 0f && entry.HitCount < LocalCountLimit) { entry.Immunity += LocalImmunity; entry.HitCount += 1; - EmitSignalBodyHit(this, body); + EmitSignalTargetHit(this, target); } entry.Immunity = Mathf.Max(0f, entry.Immunity); diff --git a/src/combat/Hurtbox.cs b/src/combat/Hurtbox.cs index 779d581..be78fe6 100644 --- a/src/combat/Hurtbox.cs +++ b/src/combat/Hurtbox.cs @@ -10,11 +10,6 @@ public sealed partial class Hurtbox : Area2D, IAttackable [Dependency] private IHurtboxOwner HurtboxOwner => this.DependOn(() => new DummyHurtboxOwner()); - [Export(PropertyHint.None, "suffix:s")] - private float LocalImmunity { get; set; } - [Export] - private int LocalCountLimit { get; set; } = 1; - public delegate void AttackedEventHandler(Hurtbox hurtbox, in Attack attack); public event AttackedEventHandler? Attacked; diff --git a/src/combat/hurtbox.tscn b/src/combat/hurtbox.tscn index 25114e9..d764c9f 100644 --- a/src/combat/hurtbox.tscn +++ b/src/combat/hurtbox.tscn @@ -9,4 +9,4 @@ script = ExtResource("1_chyeu") [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=2048987579] disabled = true -debug_color = Color(0.99430805, 0, 0.2314094, 0.41960785) +debug_color = Color(0.42675468, 0.60404867, 0, 0.41960785) diff --git a/src/creatures/birb/Birb.cs b/src/creatures/birb/Birb.cs index 8802734..3dfaf15 100644 --- a/src/creatures/birb/Birb.cs +++ b/src/creatures/birb/Birb.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Linq; using Bt = Dancer.Behavior; namespace Dancer; @@ -64,7 +65,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, new Bt.Selector([ new Bt.MemoSequence([ new Bt.Sequence([ - new Bt.Check(() => StabHitbox.GetOverlappingBodies().Contains(Target)) + new Bt.Check(() => StabHitbox.GetOverlappingAreas().Any(area => area.FindParent(Target!.Name) == Target)) { Name = "Target in stab hitbox?" }, new Bt.MemoSequence([ new Bt.Action(Stop) { Name = "Stop" }, @@ -195,12 +196,12 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, IHitboxOwner IProvide.Value() => this; - public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnBodyHit; - public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnBodyHit; + public void OnHitboxAdded(Hitbox hitbox) => hitbox.TargetHit += OnTargetHit; + public void OnHitboxRemoved(Hitbox hitbox) => hitbox.TargetHit -= OnTargetHit; - private void OnBodyHit(Hitbox hitbox, Node2D body) + private void OnTargetHit(Hitbox hitbox, Node2D target) { - if (body is not IAttackable attackable) return; + if (target is not IAttackable attackable) return; attackable.GetAttacked(new() { Attacker = this, diff --git a/src/creatures/birb/birb.tscn b/src/creatures/birb/birb.tscn index a1b0d08..2275d2d 100644 --- a/src/creatures/birb/birb.tscn +++ b/src/creatures/birb/birb.tscn @@ -47,7 +47,7 @@ animations = [{ "texture": SubResource("AtlasTexture_ha735") }], "loop": 1, -"name": &"prepare", +"name": &"prepare_attack", "speed": 5.0 }, { "frames": [{ @@ -177,7 +177,7 @@ tracks/1/keys = { [sub_resource type="Animation" id="Animation_3ly8m"] resource_name = "stab" -length = 0.8333333 +length = 1.0833334 step = 0.016666668 tracks/0/type = "value" tracks/0/imported = false @@ -198,7 +198,7 @@ tracks/1/path = NodePath("Body:frame") tracks/1/interp = 1 tracks/1/loop_wrap = true tracks/1/keys = { -"times": PackedFloat32Array(0, 0.1, 0.5, 0.55, 0.6333333), +"times": PackedFloat32Array(0, 0.1, 0.75000006, 0.8000001, 0.8833334), "transitions": PackedFloat32Array(1, 1, 1, 1, 1), "update": 1, "values": [0, 1, 2, 3, 4] @@ -210,7 +210,7 @@ tracks/2/path = NodePath(".") tracks/2/interp = 1 tracks/2/loop_wrap = true tracks/2/keys = { -"times": PackedFloat32Array(0.8333333), +"times": PackedFloat32Array(1.0833334), "transitions": PackedFloat32Array(1), "values": [{ "args": [], @@ -224,7 +224,7 @@ tracks/3/path = NodePath("Body/StabHitbox") tracks/3/interp = 1 tracks/3/loop_wrap = true tracks/3/keys = { -"times": PackedFloat32Array(0.5, 0.6333333), +"times": PackedFloat32Array(0.75000006, 0.8833334), "transitions": PackedFloat32Array(1, 1), "values": [{ "args": [], @@ -258,7 +258,6 @@ offset = Vector2(9, -4) [node name="StabHitbox" type="Area2D" parent="Body" unique_id=1373491990] unique_name_in_owner = true -visible = false position = Vector2(20, 9) collision_layer = 16 collision_mask = 2 @@ -276,7 +275,7 @@ shape = SubResource("CapsuleShape2D_feb5d") visible = false position = Vector2(0, -22) collision_layer = 0 -collision_mask = 2 +collision_mask = 128 monitorable = false [node name="CollisionShape2D" type="CollisionShape2D" parent="Detection" unique_id=1217467707] @@ -287,7 +286,7 @@ debug_color = Color(0, 0.6, 0.69803923, 0.16862746) visible = false position = Vector2(0, -22) collision_layer = 0 -collision_mask = 2 +collision_mask = 128 monitorable = false [node name="CollisionShape2D" type="CollisionShape2D" parent="Chase" unique_id=567870323] @@ -315,5 +314,6 @@ show_percentage = false unique_name_in_owner = true libraries/ = SubResource("AnimationLibrary_1i6dk") +[connection signal="area_entered" from="Body/StabHitbox" to="." method="OnStabHitboxAreaEntered"] [connection signal="body_entered" from="Detection" to="." method="OnDetectionBodyEntered"] [connection signal="body_exited" from="Chase" to="." method="OnChaseBodyExited"] diff --git a/src/player/Player.cs b/src/player/Player.cs index 5cc90ac..459ba27 100644 --- a/src/player/Player.cs +++ b/src/player/Player.cs @@ -3,8 +3,8 @@ using System.Collections.Generic; namespace Dancer; [Meta(typeof(IAutoNode))] -public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, - IProvide +public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, IHurtboxOwner, + IProvide, IProvide { public override void _Notification(int what) => this.Notify(what); @@ -456,8 +456,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, GlobalDamageImmunityTimer.Start(); } - public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnEnemyHit; - public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnEnemyHit; + public void OnHitboxAdded(Hitbox hitbox) => hitbox.TargetHit += OnEnemyHit; + public void OnHitboxRemoved(Hitbox hitbox) => hitbox.TargetHit -= OnEnemyHit; IHitboxOwner IProvide. Value() => this; @@ -466,6 +466,23 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, { } + + public void OnAttacked(Hurtbox hurtbox, in Attack attack) + { + TakeDamage(attack.Damage); + } + + public void OnHurtboxAdded(Hurtbox hurtbox) + { + hurtbox.Attacked += OnAttacked; + } + + public void OnHurtboxRemoved(Hurtbox hurtbox) + { + hurtbox.Attacked -= OnAttacked; + } + + public IHurtboxOwner Value() => this; } public static class GameActionExtensions diff --git a/src/player/player.tscn b/src/player/player.tscn index 27a24e8..22a8a03 100644 --- a/src/player/player.tscn +++ b/src/player/player.tscn @@ -317,18 +317,20 @@ graph_offset = Vector2(-48.670364, -38) [node name="Player" type="CharacterBody2D" unique_id=500011307] physics_interpolation_mode = 1 auto_translate_mode = 1 -collision_layer = 0 +collision_layer = 128 collision_mask = 65 floor_constant_speed = true floor_max_angle = 0.24783675 script = ExtResource("1_244u8") [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=84142517] +visible = false position = Vector2(0, -14) shape = SubResource("RectangleShape2D_jiejy") [node name="SubmersedSensor" type="Area2D" parent="." unique_id=1595680866] unique_name_in_owner = true +visible = false collision_layer = 0 collision_mask = 32 @@ -379,16 +381,15 @@ metadata/_aseprite_wizard_config_ = { metadata/_aseprite_wizard_source_file_hash_ = "61168df4a163c039cfaab97f4fccb905" [node name="Hurtbox" type="Area2D" parent="Body" unique_id=1918418417] -visible = false -collision_layer = 8 -collision_mask = 4 +collision_layer = 2 +collision_mask = 0 +monitoring = false script = ExtResource("4_5f2sf") [node name="CollisionShape2D" type="CollisionShape2D" parent="Body/Hurtbox" unique_id=2048987579] position = Vector2(0, -14) shape = SubResource("RectangleShape2D_qi477") -disabled = true -debug_color = Color(0.99430805, 0, 0.2314094, 0.41960785) +debug_color = Color(0.36554977, 0.61805135, 9.62615e-07, 0.41960785) [node name="HandAttachment" type="Node2D" parent="Body" unique_id=1002134601] unique_name_in_owner = true