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

@ -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<Node2D, ImmunityEntry> _immunityEntries = [];
private readonly HashSet<Node2D> _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);

View file

@ -10,11 +10,6 @@ public sealed partial class Hurtbox : Area2D, IAttackable
[Dependency]
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 event AttackedEventHandler? Attacked;

View file

@ -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)