cirnogodot/Scripts/Components/Actors/DamageReceiver3D.cs
2026-02-28 18:44:23 +01:00

131 lines
No EOL
3.9 KiB
C#

using System.Linq;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Utils;
using Cirno.Scripts.Weapons;
using Cirno.Scripts.Components.FSM.Enemy._3D;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.Actors;
public partial class DamageReceiver3D : Area3D, 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; } = [];
[Export] public bool DeleteParentOnDeath { get; private set; } = true;
[Signal]
public delegate void ShieldHitEventHandler();
[Signal]
public delegate void BulletHitEventHandler(Bullet3D bullet, Vector3 position, Vector3 direction);
//[Signal] public delegate void DeathEventHandler();
private Node3D _parent;
public bool Enabled { get; private set; } = true;
public override void _Ready()
{
_parent = GetParent<Node3D>();
HealthProvider.FillResource();
HealthProvider.ResourceDepleted += OnDeath;
}
public void ChangeState(bool enabled)
{
Enabled = enabled;
}
private void _on_damage_hitbox_area_entered(Area3D area)
{
if (!Enabled) return;
if (area is not Bullet3D bullet) return;
if (Invulnerable)
{
EmitSignalShieldHit();
return;
}
if (BulletGroup is BulletOwner.None)
{
this.Hit(bullet.Damage, bullet.DamageType);
EmitSignalBulletHit(bullet, area.GlobalPosition, (this.GlobalPosition - area.GlobalPosition).Normalized());
// Attribute XP if this hit was lethal
if (HealthProvider.CurrentResource <= 0 && bullet.BulletInfo?.SourceWeapon != null)
{
int xp = 0;
if (GetParent() is EnemyProxy3D enemyProxy && enemyProxy.EnemyResource is not null)
{
xp = (int)System.Math.Round(enemyProxy.EnemyResource.MotivationReward);
}
bullet.BulletInfo.SourceWeapon.GainExperience(xp);
}
bullet.RequestCollisionDestruction();
return;
}
if (bullet.BulletInfo.Owner == BulletGroup) return;
this.Hit(bullet.Damage, bullet.DamageType);
EmitSignalBulletHit(bullet, area.GlobalPosition, (this.GlobalPosition - area.GlobalPosition).Normalized());
// Attribute XP on lethal hit
if (HealthProvider.CurrentResource <= 0 && bullet.BulletInfo?.SourceWeapon != null)
{
int xp = 0;
if (GetParent() is EnemyProxy3D enemyProxy && enemyProxy.EnemyResource is not null)
{
xp = (int)System.Math.Round(enemyProxy.EnemyResource.MotivationReward);
}
bullet.BulletInfo.SourceWeapon.GainExperience(xp);
}
bullet.RequestCollisionDestruction();
}
public void Hit(float damage, DamageType damageType = DamageType.Neutral)
{
if (!Enabled) return;
if (Invulnerable) return;
// Change value based on difficulty
float difficultyReducedDmg = damage * GlobalState.Instance.SessionSettings.DifficultyDamageMultiplier;
var dmg = DamageResistances.Aggregate(difficultyReducedDmg,
(current, resistance) => current * resistance.CalculateDamage(current, damageType));
HealthProvider.CurrentResource -= dmg;
}
private void OnDeath()
{
if (Debris is not null)
{
_parent.CreateSibling<Node3D>(Debris);
}
// Not needed because the health provider is accessible
//EmitSignal(SignalName.Death);
if (DeleteParentOnDeath)
{
_parent.QueueFree();
}
}
}