added enemy attack

This commit is contained in:
Lilith Schier 2026-06-13 16:07:00 +02:00
parent 96bec5a6b7
commit 562a33bd8e
54 changed files with 1950 additions and 48 deletions

View file

@ -5,7 +5,7 @@ using Chickensoft.LogicBlocks.Auto;
namespace Dancer;
[Meta(typeof(IAutoNode))]
public sealed partial class Birb : CharacterBody2D, IAttackable
public sealed partial class Birb : CharacterBody2D, IAttackable, IProvide<IHitboxOwner>, IHitboxOwner
{
public override void _Notification(int what) => this.Notify(what);
@ -35,35 +35,13 @@ public sealed partial class Birb : CharacterBody2D, IAttackable
public void OnReady()
{
this.Provide();
SetPhysicsProcess(true);
SetProcess(true);
}
public void OnPhysicsProcess(double delta)
{
// _timeThought += (float)delta;
// if (_timeThought >= ThinkTime)
// {
// _timeThought -= ThinkTime;
// if (_target is not null)
// {
// var xDifference = _target.Position.X - Position.X;
// var distance = Mathf.Abs(xDifference);
// if (Mathf.Abs(distance - AttackDistance) > Speed * ThinkTime)
// _targetVelocity = Mathf.Sign(xDifference) * Mathf.Sign(distance - AttackDistance) * Speed;
// else
// _targetVelocity = 0f;
// BodySprite.FlipH = xDifference < 0;
// }
// else
// _targetVelocity = 0f;
// }
//
// Velocity = Velocity with
// {
// X = Mathf.MoveToward(Velocity.X, _targetVelocity, (float)delta * 500f)
// };
Ai.Input(new Logic.Input.OnPhysicsProcess(delta));
if (!IsOnFloor()) Velocity += GetGravity() * (float)delta;
@ -71,7 +49,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable
MoveAndSlide();
}
public override void _Process(double delta)
public void OnProcess(double delta)
{
HealthBar.Value = _hp;
HealthBar.Visible = _hp < MaxHp;
@ -93,7 +71,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable
var targetVelocity = Mathf.Sign(xDifference) * speed;
Velocity = Velocity with
{
X = Mathf.MoveToward(Velocity.X, targetVelocity, (float)delta * Acceleration)
X = Mathf.MoveToward(Velocity.X, targetVelocity, (float)delta * Acceleration),
};
return speedRequiredToReachGoalNow < Speed;
}
@ -102,7 +80,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable
{
Velocity = Velocity with
{
X = Mathf.MoveToward(Velocity.X, 0, (float)delta * Acceleration)
X = Mathf.MoveToward(Velocity.X, 0, (float)delta * Acceleration),
};
}
@ -122,6 +100,20 @@ public sealed partial class Birb : CharacterBody2D, IAttackable
if (_hp <= 0.01) QueueFree();
}
IHitboxOwner IProvide<IHitboxOwner>.Value() => this;
public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnBodyHit;
public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnBodyHit;
private void OnBodyHit(Hitbox hitbox, Node2D body)
{
if (body is not IAttackable attackable) return;
attackable.GetAttacked(new()
{
Attacker = this,
Damage = 1f,
});
}
[Meta]
public sealed partial class Logic : AutoBlock
{