SupaLidlGame/BoundingBoxes/Hitbox.cs

104 lines
2.4 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
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.BoundingBoxes;
public partial class Hitbox : BoundingBox
2022-11-13 19:52:09 -08:00
{
2023-06-03 18:21:46 -07:00
private HashSet<BoundingBox> _ignoreList = new HashSet<BoundingBox>();
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
[Signal]
public delegate void HitEventHandler(BoundingBox box);
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
[Export]
public float Damage { get; set; }
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
private bool _isDisabled = false;
2023-03-26 10:53:58 -07:00
2023-06-03 18:21:46 -07:00
/// <summary>
/// Getter/setter for the CollisionShape2D's Disabled property.
/// </summary>
[Export]
public bool IsDisabled
{
get => _collisionShape.Disabled;
set
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
_isDisabled = value;
if (_collisionShape is not null)
2022-11-25 09:11:46 -08:00
{
2023-06-03 18:21:46 -07:00
_collisionShape.Disabled = value;
if (value)
2022-11-25 09:11:46 -08:00
{
2023-06-03 18:21:46 -07:00
DamageStartTime = Time.GetTicksMsec();
2022-11-25 09:11:46 -08:00
}
}
2022-11-19 21:21:12 -08:00
}
2023-06-03 18:21:46 -07:00
}
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
[Export]
public float Knockback { get; set; }
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
public Character Inflictor { get; set; }
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
public ulong DamageStartTime { get; set; }
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
private CollisionShape2D _collisionShape;
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
private bool _isParrying = false;
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
public override void _Ready()
{
_collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
IsDisabled = _isDisabled; // sets _collisionShape.Disabled
}
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
private bool ShouldParry(Hitbox box)
{
Node parent = GetParent<Node>();
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
// if this hitbox does not even belong to a weapon, skip
if (parent is not Weapon)
{
return false;
}
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
var weapon = parent as Weapon;
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
// 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;
2022-11-25 09:11:46 -08:00
}
2023-06-03 18:21:46 -07:00
return false;
}
public void _on_area_entered(Area2D area)
{
if (area is BoundingBox box)
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
// we don't want to hurt teammates
if (Faction != box.Faction)
2022-11-13 19:52:09 -08:00
{
2023-06-03 18:21:46 -07:00
if (!_ignoreList.Contains(box))
2022-11-13 19:52:09 -08:00
{
2023-06-03 18:21:46 -07:00
_ignoreList.Add(box);
EmitSignal(SignalName.Hit, box);
2022-11-13 19:52:09 -08:00
}
}
}
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 ResetIgnoreList() => _ignoreList.Clear();
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
public bool HasHit(BoundingBox box) => _ignoreList.Contains(box);
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
public HashSet<BoundingBox> Hits
{
get => _ignoreList;
2022-11-13 19:52:09 -08:00
}
}