cirnogodot/Scripts/Components/Actors/GenericDamageReceiver.cs

103 lines
3 KiB
C#
Raw Normal View History

2025-03-12 22:01:45 +01:00
using System.Linq;
2025-04-08 19:06:39 +02:00
using Cirno.Scripts.Enums;
2025-03-12 22:01:45 +01:00
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; } = [];
2025-03-20 18:22:40 +01:00
[Export] public bool DeleteParentOnDeath { get; private set; } = true;
2025-04-09 15:12:08 +02:00
[Signal]
public delegate void ShieldHitEventHandler();
2025-03-20 18:22:40 +01:00
2025-05-06 16:06:00 +02:00
[Signal] public delegate void BulletHitEventHandler(Bullet bullet, Vector2 position, Vector2 direction);
2025-03-20 18:22:40 +01:00
//[Signal] public delegate void DeathEventHandler();
2025-03-12 22:01:45 +01:00
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 (area is not Bullet bullet) return;
2025-04-09 15:12:08 +02:00
if (Invulnerable)
{
EmitSignalShieldHit();
return;
};
2025-03-12 22:01:45 +01:00
if (BulletGroup is BulletOwner.None)
{
this.Hit(bullet.Damage, bullet.DamageType);
2025-05-06 16:06:00 +02:00
EmitSignalBulletHit(bullet, area.GlobalPosition, (this.GlobalPosition - area.GlobalPosition).Normalized());
2025-03-12 22:01:45 +01:00
bullet.RequestCollisionDestruction();
return;
}
if (bullet.BulletInfo.Owner == BulletGroup) return;
this.Hit(bullet.Damage, bullet.DamageType);
2025-05-06 16:06:00 +02:00
EmitSignalBulletHit(bullet, area.GlobalPosition, (this.GlobalPosition - area.GlobalPosition).Normalized());
2025-03-12 22:01:45 +01:00
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;
2025-04-08 19:06:39 +02:00
// Change value based on difficulty
2025-04-09 16:58:36 +02:00
float difficultyReducedDmg = damage * GlobalState.Instance.SessionSettings.DifficultyDamageMultiplier;
2025-03-12 22:01:45 +01:00
2025-04-08 19:06:39 +02:00
var dmg = DamageResistances.Aggregate(difficultyReducedDmg, (current, resistance) => current * resistance.CalculateDamage(current, damageType));
2025-03-12 22:01:45 +01:00
HealthProvider.CurrentResource -= dmg;
}
private void OnDeath()
{
if (Debris is not null)
{
_parent.CreateSibling<Node2D>(Debris);
}
2025-03-20 18:22:40 +01:00
// Not needed because the health provider is accessible
//EmitSignal(SignalName.Death);
if (DeleteParentOnDeath)
{
_parent.QueueFree();
}
2025-03-12 22:01:45 +01:00
}
}