SupaLidlGame/BoundingBoxes/Hurtbox.cs

91 lines
2.2 KiB
C#
Raw Normal View History

2022-11-13 19:52:09 -08:00
using Godot;
using SupaLidlGame.Characters;
2022-11-19 21:21:12 -08:00
using SupaLidlGame.Utils;
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.BoundingBoxes;
public partial class Hurtbox : BoundingBox, IFaction
2022-11-13 19:52:09 -08:00
{
2023-06-03 18:21:46 -07:00
[Signal]
public delegate void ReceivedDamageEventHandler(
float damage,
Character inflictor,
float knockback,
2023-07-21 02:54:13 -07:00
Vector2 knockbackDir = default);
/// <summary>
/// The timer to use for invincibility frames
/// </summary>
[Export]
public Timer InvincibilityTimer { get; set; }
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
public override void _Ready()
{
if (GetParent() is IFaction factionEntity)
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
Faction = factionEntity.Faction;
2022-11-19 21:21:12 -08:00
}
2023-07-21 02:54:13 -07:00
if (InvincibilityTimer is not null)
{
InvincibilityTimer.Timeout += () =>
{
GD.Print("invincibility off");
Monitorable = true;
};
}
2023-06-03 18:21:46 -07:00
}
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
public void InflictDamage(
float damage,
Character inflictor,
float knockback,
Vector2 knockbackOrigin = default,
Vector2 knockbackVector = default)
{
2023-07-23 11:05:01 -07:00
if (!IsInstanceValid(this))
{
// this should fix the error of the object being invalid
return;
}
2023-07-21 02:54:13 -07:00
Vector2 knockbackDir = knockbackVector;
if (knockbackDir == default)
{
if (knockbackOrigin == default)
{
if (inflictor is null)
{
knockbackOrigin = GlobalPosition + Vector2.Down;
}
else
{
knockbackOrigin = inflictor.GlobalPosition;
}
}
knockbackDir = knockbackOrigin.DirectionTo(GlobalPosition);
}
if (InvincibilityTimer is not null)
{
if (!InvincibilityTimer.IsStopped())
{
return;
}
InvincibilityTimer.Start();
//Monitorable = false;
SetDeferred("monitorable", false);
GD.Print("invincible");
}
2023-07-13 23:46:58 -07:00
EmitSignal(
SignalName.ReceivedDamage,
damage,
inflictor,
knockback,
2023-07-21 02:54:13 -07:00
knockbackDir);
2022-11-13 19:52:09 -08:00
}
}