cirnogodot/Scripts/Components/Actors/GenericDamageReceiver.cs

74 lines
2 KiB
C#
Raw Normal View History

2025-03-12 22:01:45 +01:00
using System.Linq;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.Actors;
public partial class GenericDamageReceiver : Area2D, IHittable
{
[Export] public ActorResourceProvider HealthProvider { get; private set; }
[Export] public bool Invulnerable { get; private set; } = false;
[Export] public BulletOwner BulletGroup { get; set; } = BulletOwner.None;
[Export] public PackedScene Debris { get; set; }
[Export] public Array<DamageResistance> DamageResistances { get; set; } = [];
private Node2D _parent;
2025-03-14 23:04:59 +01:00
public bool Enabled { get; private set; } = true;
2025-03-12 22:01:45 +01:00
public override void _Ready()
{
_parent = GetParent<Node2D>();
HealthProvider.FillResource();
HealthProvider.ResourceDepleted += OnDeath;
}
2025-03-14 23:04:59 +01:00
public void ChangeState(bool enabled)
{
Enabled = enabled;
}
2025-03-12 22:01:45 +01:00
private void _on_damage_hitbox_area_entered(Area2D area)
{
2025-03-14 23:04:59 +01:00
if (!Enabled) return;
2025-03-12 22:01:45 +01:00
if (Invulnerable) return;
if (area is not Bullet bullet) return;
if (BulletGroup is BulletOwner.None)
{
this.Hit(bullet.Damage, bullet.DamageType);
bullet.RequestCollisionDestruction();
return;
}
if (bullet.BulletInfo.Owner == BulletGroup) return;
this.Hit(bullet.Damage, bullet.DamageType);
bullet.RequestCollisionDestruction();
}
public void Hit(float damage, DamageType damageType = DamageType.Neutral)
{
2025-03-14 23:04:59 +01:00
if (!Enabled) return;
2025-03-12 22:01:45 +01:00
if (Invulnerable) return;
var dmg = DamageResistances.Aggregate(damage, (current, resistance) => current * resistance.CalculateDamage(current, damageType));
HealthProvider.CurrentResource -= dmg;
}
private void OnDeath()
{
if (Debris is not null)
{
_parent.CreateSibling<Node2D>(Debris);
}
_parent.QueueFree();
}
}