cirnogodot/Scripts/Components/Actors/DamageReceiverActorModule.cs

93 lines
2.7 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;
2025-02-23 18:08:57 +01:00
namespace Cirno.Scripts.Components.Actors;
2025-03-12 22:01:45 +01:00
public partial class DamageReceiverActorModule : ActorModule, IHittable
2025-02-23 18:08:57 +01:00
{
protected Actor _actor;
[Export]
public ActorResourceProvider HealthProvider { get; private set; }
[Export]
public bool Invulnerable { get; private set; } = false;
[Export] public BulletOwner BulletGroup { get; set; } = BulletOwner.None;
2025-02-23 18:08:57 +01:00
2025-03-12 22:01:45 +01:00
[Export] public Array<DamageResistance> DamageResistances { get; set; } = [];
2025-02-23 18:08:57 +01:00
public override void Init(Actor actor)
{
_actor = actor;
2025-02-23 21:08:52 +01:00
this.HealthProvider.MaxResource = actor.Health;
2025-02-23 19:19:12 +01:00
HealthProvider.FillResource();
2025-02-23 18:08:57 +01:00
HealthProvider.ResourceDepleted += OnDeath;
}
public override void Update(double delta)
{
}
public override void PhysicsUpdate(double delta)
{
}
private void _on_damage_hitbox_area_entered(Area2D area)
{
if (_actor.IsDestroyed) return;
if (Invulnerable) return;
if (area is not Bullet bullet) return;
if (BulletGroup is BulletOwner.None)
{
2025-03-12 22:01:45 +01:00
this.Hit(bullet.Damage, bullet.DamageType);
2026-02-28 18:44:23 +01:00
// If this hit killed the actor, attribute XP to the source weapon if present
if (HealthProvider.CurrentResource <= 0 && bullet.BulletInfo?.SourceWeapon != null)
{
// Award XP equal to actor's MotivationReward rounded to int, or a fixed value.
var xp = (int)System.Math.Round(_actor.EnemyData?.MotivationReward ?? 0f);
bullet.BulletInfo.SourceWeapon.GainExperience(xp);
}
2025-02-23 18:08:57 +01:00
bullet.RequestCollisionDestruction();
return;
}
if (bullet.BulletInfo.Owner == BulletGroup) return;
2025-03-12 22:01:45 +01:00
this.Hit(bullet.Damage, bullet.DamageType);
2026-02-28 18:44:23 +01:00
// Attribute XP on lethal hit
if (HealthProvider.CurrentResource <= 0 && bullet.BulletInfo?.SourceWeapon != null)
{
var xp = (int)System.Math.Round(_actor.EnemyData?.MotivationReward ?? 0f);
bullet.BulletInfo.SourceWeapon.GainExperience(xp);
}
2025-02-23 18:08:57 +01:00
bullet.RequestCollisionDestruction();
}
public void Hit(float damage, DamageType damageType = DamageType.Neutral)
{
if (_actor.IsDestroyed) return;
if (Invulnerable) return;
2025-03-12 22:01:45 +01:00
var dmg = DamageResistances.Aggregate(damage, (current, resistance) => current * resistance.CalculateDamage(current, damageType));
HealthProvider.CurrentResource -= dmg;
2025-02-23 18:08:57 +01:00
}
protected void OnDeath()
{
_actor.IsDestroyed = true;
2025-02-23 19:19:12 +01:00
_actor.TriggerDeath();
2025-02-23 18:08:57 +01:00
}
}