SupaLidlGame/BoundingBoxes/Hitbox.cs

99 lines
2.5 KiB
C#
Raw Normal View History

2022-11-13 19:52:09 -08:00
using System.Collections.Generic;
2022-11-25 09:11:46 -08:00
using System.Linq;
2022-11-13 19:52:09 -08:00
using Godot;
using SupaLidlGame.Characters;
2022-11-25 09:11:46 -08:00
using SupaLidlGame.Items;
2022-11-13 19:52:09 -08:00
namespace SupaLidlGame.BoundingBoxes
{
2022-11-25 09:11:46 -08:00
public partial class Hitbox : BoundingBox
2022-11-13 19:52:09 -08:00
{
2022-11-25 09:11:46 -08:00
private HashSet<BoundingBox> _ignoreList = new HashSet<BoundingBox>();
[Signal]
public delegate void HitEventHandler(BoundingBox box);
2022-11-13 19:52:09 -08:00
[Export]
2022-11-19 21:21:12 -08:00
public float Damage { get; set; }
2022-11-13 19:52:09 -08:00
2022-11-19 21:21:12 -08:00
/// <summary>
/// Getter/setter for the CollisionShape2D's Disabled property.
/// </summary>
2022-11-13 19:52:09 -08:00
[Export]
2022-11-19 21:21:12 -08:00
public bool IsDisabled
{
get => _collisionShape.Disabled;
2022-11-25 09:11:46 -08:00
set
{
_collisionShape.Disabled = value;
if (value)
{
DamageStartTime = Time.GetTicksMsec();
}
}
2022-11-19 21:21:12 -08:00
}
2022-11-13 19:52:09 -08:00
[Export]
public float Knockback { get; set; }
public Character Inflictor { get; set; }
2022-11-25 09:11:46 -08:00
public ulong DamageStartTime { get; set; }
2022-11-19 21:21:12 -08:00
private CollisionShape2D _collisionShape;
2022-11-25 09:11:46 -08:00
private bool _isParrying = false;
2022-11-19 21:21:12 -08:00
public override void _Ready()
2022-11-13 19:52:09 -08:00
{
2022-11-19 21:21:12 -08:00
_collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
}
2022-11-13 19:52:09 -08:00
2022-11-25 09:11:46 -08:00
private bool ShouldParry(Hitbox box)
{
Node parent = GetParent<Node>();
// if this hitbox does not even belong to a weapon, skip
if (parent is not Weapon)
{
return false;
}
var weapon = parent as Weapon;
// if we hit a hitbox, we can parry if it can be parried
if (box.GetParent<Node>() is Weapon other)
{
return weapon.IsParryable && other.IsParryable;
}
return false;
}
2022-11-19 21:21:12 -08:00
public void _on_area_entered(Area2D area)
{
2022-11-25 09:11:46 -08:00
if (area is BoundingBox box)
2022-11-13 19:52:09 -08:00
{
2022-11-19 21:21:12 -08:00
// we don't want to hurt teammates
2022-11-25 09:11:46 -08:00
if (Faction != box.Faction)
2022-11-13 19:52:09 -08:00
{
2022-11-25 09:11:46 -08:00
if (!_ignoreList.Contains(box))
2022-11-19 21:21:12 -08:00
{
2022-11-25 09:11:46 -08:00
_ignoreList.Add(box);
EmitSignal(SignalName.Hit, box);
2022-11-19 21:21:12 -08:00
}
2022-11-13 19:52:09 -08:00
}
}
}
public void ResetIgnoreList() => _ignoreList.Clear();
2022-11-25 09:11:46 -08:00
public bool HasHit(BoundingBox box) => _ignoreList.Contains(box);
public HashSet<BoundingBox> Hits
{
get => _ignoreList;
}
2022-11-13 19:52:09 -08:00
}
}