SupaLidlGame/BoundingBoxes/Hitbox.cs

106 lines
2.8 KiB
C#
Raw Normal View History

2022-11-13 19:52:09 -08:00
using System.Collections.Generic;
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
2023-03-26 10:53:58 -07:00
private bool _isDisabled = false;
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
{
2023-03-26 10:53:58 -07:00
_isDisabled = value;
if (_collisionShape is not null)
2022-11-25 09:11:46 -08:00
{
2023-03-26 10:53:58 -07:00
_collisionShape.Disabled = value;
if (value)
{
DamageStartTime = Time.GetTicksMsec();
}
2022-11-25 09:11:46 -08:00
}
}
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");
2023-03-26 10:53:58 -07:00
IsDisabled = _isDisabled; // sets _collisionShape.Disabled
2022-11-19 21:21:12 -08:00
}
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
{
2023-05-26 22:28:08 -07:00
GD.Print("hit");
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
}
}