added enemy attack
This commit is contained in:
parent
96bec5a6b7
commit
562a33bd8e
54 changed files with 1950 additions and 48 deletions
84
src/combat/Hitbox.cs
Normal file
84
src/combat/Hitbox.cs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Dancer;
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public sealed partial class Hitbox : Area2D
|
||||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
|
||||
[Dependency]
|
||||
private IHitboxOwner HitboxOwner => this.DependOn<IHitboxOwner>(() => new DummyHitboxOwner());
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
private float LocalImmunity { get; set; }
|
||||
private int LocalCountLimit { get; set; } = 1000;
|
||||
|
||||
[Signal]
|
||||
public delegate void BodyHitEventHandler(Hitbox hitbox, Node2D body);
|
||||
|
||||
private readonly Dictionary<Node2D, ImmunityEntry> _immunityEntries = [];
|
||||
private readonly HashSet<Node2D> _bodies = [];
|
||||
|
||||
public void OnResolved()
|
||||
{
|
||||
BodyEntered += OnEntered;
|
||||
HitboxOwner.OnHitboxAdded(this);
|
||||
}
|
||||
|
||||
public void OnExitTree() => HitboxOwner.OnHitboxRemoved(this);
|
||||
|
||||
private void OnEntered(Node2D body)
|
||||
{
|
||||
CheckForHit(body);
|
||||
_bodies.Add(body);
|
||||
}
|
||||
|
||||
private void OnLeft(Node2D body)
|
||||
{
|
||||
_bodies.Remove(body);
|
||||
}
|
||||
|
||||
public void OnPhysicsProcess(double delta)
|
||||
{
|
||||
foreach (var body in _bodies) CheckForHit(body);
|
||||
}
|
||||
|
||||
private void CheckForHit(Node2D body, double delta = 0.0)
|
||||
{
|
||||
if (!_immunityEntries.TryGetValue(body, out var entry)) _immunityEntries.Add(body, entry = new());
|
||||
|
||||
entry.Immunity -= (float)delta;
|
||||
if (entry.Immunity <= 0f && entry.HitCount < LocalCountLimit)
|
||||
{
|
||||
entry.Immunity += LocalImmunity;
|
||||
entry.HitCount += 1;
|
||||
EmitSignalBodyHit(this, body);
|
||||
}
|
||||
|
||||
entry.Immunity = Mathf.Max(0f, entry.Immunity);
|
||||
}
|
||||
|
||||
private sealed class ImmunityEntry
|
||||
{
|
||||
public float Immunity { get; set; }
|
||||
public int HitCount { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public interface IHitboxOwner
|
||||
{
|
||||
public void OnHitboxAdded(Hitbox hitbox);
|
||||
public void OnHitboxRemoved(Hitbox hitbox);
|
||||
}
|
||||
|
||||
public class DummyHitboxOwner : IHitboxOwner
|
||||
{
|
||||
public void OnHitboxAdded(Hitbox hitbox)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnHitboxRemoved(Hitbox hitbox)
|
||||
{
|
||||
}
|
||||
}
|
||||
1
src/combat/Hitbox.cs.uid
Normal file
1
src/combat/Hitbox.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b4k42r866lkut
|
||||
Loading…
Add table
Add a link
Reference in a new issue