added player hurtbox

This commit is contained in:
Lilith Schier 2026-08-01 20:07:49 +02:00
parent 7bb6854335
commit 5a6c711296
8 changed files with 54 additions and 35 deletions

View file

@ -96,6 +96,8 @@ parry={
2d_physics/layer_5="enemy_hitbox" 2d_physics/layer_5="enemy_hitbox"
2d_physics/layer_6="water" 2d_physics/layer_6="water"
2d_physics/layer_7="platform" 2d_physics/layer_7="platform"
2d_physics/layer_8="player"
2d_physics/layer_9="enemy"
[physics] [physics]

View file

@ -16,7 +16,7 @@ public sealed partial class Hitbox : Area2D
private int LocalCountLimit { get; set; } = 1; private int LocalCountLimit { get; set; } = 1;
[Signal] [Signal]
public delegate void BodyHitEventHandler(Hitbox hitbox, Node2D body); public delegate void TargetHitEventHandler(Hitbox hitbox, Node2D body);
private readonly Dictionary<Node2D, ImmunityEntry> _immunityEntries = []; private readonly Dictionary<Node2D, ImmunityEntry> _immunityEntries = [];
private readonly HashSet<Node2D> _bodies = []; private readonly HashSet<Node2D> _bodies = [];
@ -25,7 +25,10 @@ public sealed partial class Hitbox : Area2D
public void OnReady() public void OnReady()
{ {
BodyEntered += OnEntered; BodyEntered += OnEntered;
BodyExited += OnLeft; BodyExited += OnExited;
AreaEntered += OnEntered;
AreaExited += OnExited;
} }
public void OnResolved() => HitboxOwner.OnHitboxAdded(this); public void OnResolved() => HitboxOwner.OnHitboxAdded(this);
@ -38,7 +41,7 @@ public sealed partial class Hitbox : Area2D
_bodies.Add(body); _bodies.Add(body);
} }
private void OnLeft(Node2D body) private void OnExited(Node2D body)
{ {
_bodies.Remove(body); _bodies.Remove(body);
} }
@ -48,16 +51,16 @@ public sealed partial class Hitbox : Area2D
foreach (var body in _bodies) CheckForHit(body, delta); 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; entry.Immunity -= (float)delta;
if (_checkingForHits && entry.Immunity <= 0f && entry.HitCount < LocalCountLimit) if (_checkingForHits && entry.Immunity <= 0f && entry.HitCount < LocalCountLimit)
{ {
entry.Immunity += LocalImmunity; entry.Immunity += LocalImmunity;
entry.HitCount += 1; entry.HitCount += 1;
EmitSignalBodyHit(this, body); EmitSignalTargetHit(this, target);
} }
entry.Immunity = Mathf.Max(0f, entry.Immunity); entry.Immunity = Mathf.Max(0f, entry.Immunity);

View file

@ -10,11 +10,6 @@ public sealed partial class Hurtbox : Area2D, IAttackable
[Dependency] [Dependency]
private IHurtboxOwner HurtboxOwner => this.DependOn<IHurtboxOwner>(() => new DummyHurtboxOwner()); private IHurtboxOwner HurtboxOwner => this.DependOn<IHurtboxOwner>(() => 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 delegate void AttackedEventHandler(Hurtbox hurtbox, in Attack attack);
public event AttackedEventHandler? Attacked; public event AttackedEventHandler? Attacked;

View file

@ -9,4 +9,4 @@ script = ExtResource("1_chyeu")
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=2048987579] [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=2048987579]
disabled = true disabled = true
debug_color = Color(0.99430805, 0, 0.2314094, 0.41960785) debug_color = Color(0.42675468, 0.60404867, 0, 0.41960785)

View file

@ -1,4 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using Bt = Dancer.Behavior; using Bt = Dancer.Behavior;
namespace Dancer; namespace Dancer;
@ -64,7 +65,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
new Bt.Selector([ new Bt.Selector([
new Bt.MemoSequence([ new Bt.MemoSequence([
new Bt.Sequence([ 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?" }, { Name = "Target in stab hitbox?" },
new Bt.MemoSequence([ new Bt.MemoSequence([
new Bt.Action(Stop) { Name = "Stop" }, new Bt.Action(Stop) { Name = "Stop" },
@ -195,12 +196,12 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
IHitboxOwner IProvide<IHitboxOwner>.Value() => this; IHitboxOwner IProvide<IHitboxOwner>.Value() => this;
public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnBodyHit; public void OnHitboxAdded(Hitbox hitbox) => hitbox.TargetHit += OnTargetHit;
public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnBodyHit; 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() attackable.GetAttacked(new()
{ {
Attacker = this, Attacker = this,

View file

@ -47,7 +47,7 @@ animations = [{
"texture": SubResource("AtlasTexture_ha735") "texture": SubResource("AtlasTexture_ha735")
}], }],
"loop": 1, "loop": 1,
"name": &"prepare", "name": &"prepare_attack",
"speed": 5.0 "speed": 5.0
}, { }, {
"frames": [{ "frames": [{
@ -177,7 +177,7 @@ tracks/1/keys = {
[sub_resource type="Animation" id="Animation_3ly8m"] [sub_resource type="Animation" id="Animation_3ly8m"]
resource_name = "stab" resource_name = "stab"
length = 0.8333333 length = 1.0833334
step = 0.016666668 step = 0.016666668
tracks/0/type = "value" tracks/0/type = "value"
tracks/0/imported = false tracks/0/imported = false
@ -198,7 +198,7 @@ tracks/1/path = NodePath("Body:frame")
tracks/1/interp = 1 tracks/1/interp = 1
tracks/1/loop_wrap = true tracks/1/loop_wrap = true
tracks/1/keys = { 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), "transitions": PackedFloat32Array(1, 1, 1, 1, 1),
"update": 1, "update": 1,
"values": [0, 1, 2, 3, 4] "values": [0, 1, 2, 3, 4]
@ -210,7 +210,7 @@ tracks/2/path = NodePath(".")
tracks/2/interp = 1 tracks/2/interp = 1
tracks/2/loop_wrap = true tracks/2/loop_wrap = true
tracks/2/keys = { tracks/2/keys = {
"times": PackedFloat32Array(0.8333333), "times": PackedFloat32Array(1.0833334),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1),
"values": [{ "values": [{
"args": [], "args": [],
@ -224,7 +224,7 @@ tracks/3/path = NodePath("Body/StabHitbox")
tracks/3/interp = 1 tracks/3/interp = 1
tracks/3/loop_wrap = true tracks/3/loop_wrap = true
tracks/3/keys = { tracks/3/keys = {
"times": PackedFloat32Array(0.5, 0.6333333), "times": PackedFloat32Array(0.75000006, 0.8833334),
"transitions": PackedFloat32Array(1, 1), "transitions": PackedFloat32Array(1, 1),
"values": [{ "values": [{
"args": [], "args": [],
@ -258,7 +258,6 @@ offset = Vector2(9, -4)
[node name="StabHitbox" type="Area2D" parent="Body" unique_id=1373491990] [node name="StabHitbox" type="Area2D" parent="Body" unique_id=1373491990]
unique_name_in_owner = true unique_name_in_owner = true
visible = false
position = Vector2(20, 9) position = Vector2(20, 9)
collision_layer = 16 collision_layer = 16
collision_mask = 2 collision_mask = 2
@ -276,7 +275,7 @@ shape = SubResource("CapsuleShape2D_feb5d")
visible = false visible = false
position = Vector2(0, -22) position = Vector2(0, -22)
collision_layer = 0 collision_layer = 0
collision_mask = 2 collision_mask = 128
monitorable = false monitorable = false
[node name="CollisionShape2D" type="CollisionShape2D" parent="Detection" unique_id=1217467707] [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 visible = false
position = Vector2(0, -22) position = Vector2(0, -22)
collision_layer = 0 collision_layer = 0
collision_mask = 2 collision_mask = 128
monitorable = false monitorable = false
[node name="CollisionShape2D" type="CollisionShape2D" parent="Chase" unique_id=567870323] [node name="CollisionShape2D" type="CollisionShape2D" parent="Chase" unique_id=567870323]
@ -315,5 +314,6 @@ show_percentage = false
unique_name_in_owner = true unique_name_in_owner = true
libraries/ = SubResource("AnimationLibrary_1i6dk") 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_entered" from="Detection" to="." method="OnDetectionBodyEntered"]
[connection signal="body_exited" from="Chase" to="." method="OnChaseBodyExited"] [connection signal="body_exited" from="Chase" to="." method="OnChaseBodyExited"]

View file

@ -3,8 +3,8 @@ using System.Collections.Generic;
namespace Dancer; namespace Dancer;
[Meta(typeof(IAutoNode))] [Meta(typeof(IAutoNode))]
public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, IHurtboxOwner,
IProvide<IHitboxOwner> IProvide<IHitboxOwner>, IProvide<IHurtboxOwner>
{ {
public override void _Notification(int what) => this.Notify(what); public override void _Notification(int what) => this.Notify(what);
@ -456,8 +456,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
GlobalDamageImmunityTimer.Start(); GlobalDamageImmunityTimer.Start();
} }
public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnEnemyHit; public void OnHitboxAdded(Hitbox hitbox) => hitbox.TargetHit += OnEnemyHit;
public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnEnemyHit; public void OnHitboxRemoved(Hitbox hitbox) => hitbox.TargetHit -= OnEnemyHit;
IHitboxOwner IProvide<IHitboxOwner>. IHitboxOwner IProvide<IHitboxOwner>.
Value() => this; 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 public static class GameActionExtensions

View file

@ -317,18 +317,20 @@ graph_offset = Vector2(-48.670364, -38)
[node name="Player" type="CharacterBody2D" unique_id=500011307] [node name="Player" type="CharacterBody2D" unique_id=500011307]
physics_interpolation_mode = 1 physics_interpolation_mode = 1
auto_translate_mode = 1 auto_translate_mode = 1
collision_layer = 0 collision_layer = 128
collision_mask = 65 collision_mask = 65
floor_constant_speed = true floor_constant_speed = true
floor_max_angle = 0.24783675 floor_max_angle = 0.24783675
script = ExtResource("1_244u8") script = ExtResource("1_244u8")
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=84142517] [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=84142517]
visible = false
position = Vector2(0, -14) position = Vector2(0, -14)
shape = SubResource("RectangleShape2D_jiejy") shape = SubResource("RectangleShape2D_jiejy")
[node name="SubmersedSensor" type="Area2D" parent="." unique_id=1595680866] [node name="SubmersedSensor" type="Area2D" parent="." unique_id=1595680866]
unique_name_in_owner = true unique_name_in_owner = true
visible = false
collision_layer = 0 collision_layer = 0
collision_mask = 32 collision_mask = 32
@ -379,16 +381,15 @@ metadata/_aseprite_wizard_config_ = {
metadata/_aseprite_wizard_source_file_hash_ = "61168df4a163c039cfaab97f4fccb905" metadata/_aseprite_wizard_source_file_hash_ = "61168df4a163c039cfaab97f4fccb905"
[node name="Hurtbox" type="Area2D" parent="Body" unique_id=1918418417] [node name="Hurtbox" type="Area2D" parent="Body" unique_id=1918418417]
visible = false collision_layer = 2
collision_layer = 8 collision_mask = 0
collision_mask = 4 monitoring = false
script = ExtResource("4_5f2sf") script = ExtResource("4_5f2sf")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Body/Hurtbox" unique_id=2048987579] [node name="CollisionShape2D" type="CollisionShape2D" parent="Body/Hurtbox" unique_id=2048987579]
position = Vector2(0, -14) position = Vector2(0, -14)
shape = SubResource("RectangleShape2D_qi477") shape = SubResource("RectangleShape2D_qi477")
disabled = true debug_color = Color(0.36554977, 0.61805135, 9.62615e-07, 0.41960785)
debug_color = Color(0.99430805, 0, 0.2314094, 0.41960785)
[node name="HandAttachment" type="Node2D" parent="Body" unique_id=1002134601] [node name="HandAttachment" type="Node2D" parent="Body" unique_id=1002134601]
unique_name_in_owner = true unique_name_in_owner = true