cirnogodot/Scripts/Components/Actors/PlayerDamageReceiver.cs

240 lines
7.9 KiB
C#
Raw Normal View History

2025-03-01 14:08:31 +01:00
using System;
2025-03-18 14:36:28 +01:00
using System.Linq;
2025-03-11 17:58:46 +01:00
using Cirno.Scripts.Components.FSM;
2025-03-18 14:36:28 +01:00
using Cirno.Scripts.Enums;
using Cirno.Scripts.Resources;
2025-03-01 14:08:31 +01:00
using Godot;
2025-03-18 14:36:28 +01:00
using Godot.Collections;
2025-03-01 14:08:31 +01:00
namespace Cirno.Scripts.Components.Actors;
public partial class PlayerDamageReceiver : Area2D
{
[Export]
public bool Enabled { get; set; } = false;
[Export]
public bool Invulnerable { get; private set; } = false;
[Export] public BulletOwner BulletGroup { get; set; } = BulletOwner.Player;
[ExportCategory("Extensions")]
[Export] public StringName HealthExtendName { get; private set; } = "HEALTH_EXTEND";
[Export] public StringName ShieldExtendName { get; private set; } = "SHIELD_EXTEND";
[Export] public float BaseHealth { get; private set; } = 32f;
[Export] public float BaseShield { get; private set; } = 32f;
2025-03-31 18:28:33 +02:00
[Export] public float BaseMotivation { get; private set; } = 100f;
[Export] public float HealthExtendAmount { get; private set; } = 4f;
[Export] public float ShieldExtendAmount { get; private set; } = 4f;
[ExportCategory("Providers")]
2025-03-01 14:08:31 +01:00
[Export]
private ActorResourceProvider _healthProvider;
[Export]
private ActorResourceProvider _shieldProvider;
2025-03-31 18:28:33 +02:00
[Export] private ActorResourceProvider _motivationProvider;
[ExportCategory("Damage Types")]
2025-03-03 17:55:53 +01:00
[Export] public StringName AcidGroupName { get; private set; } = "Acid";
2025-03-01 14:08:31 +01:00
2025-03-18 14:36:28 +01:00
[Export] public Array<DamageResistance> ShieldDamageResistances { get; set; } = [];
[Export] public Array<DamageResistance> HealthDamageResistances { get; set; } = [];
2025-03-01 14:08:31 +01:00
[Signal]
public delegate void HealthChangedEventHandler(float newValue, float maxValue);
2025-03-03 10:58:20 +01:00
[Signal]
public delegate void HealthDecreasedEventHandler(float value, float newValue, float maxValue);
2025-03-01 14:08:31 +01:00
[Signal]
public delegate void ShieldChangedEventHandler(float newValue, float maxValue);
2025-03-03 10:58:20 +01:00
[Signal]
public delegate void ShieldDecreasedEventHandler(float value, float newValue, float maxValue);
2025-03-01 14:08:31 +01:00
[Signal]
public delegate void DeathEventHandler();
public float CurrentHealth
{
get => _healthProvider.CurrentResource;
set => _healthProvider.CurrentResource = value;
}
public float CurrentShield
{
get => _shieldProvider.CurrentResource;
set => _shieldProvider.CurrentResource = value;
}
2025-03-31 18:28:33 +02:00
public float CurrentMotivation
{
get => _motivationProvider.CurrentResource;
set => _motivationProvider.CurrentResource = value;
}
2025-03-11 17:58:46 +01:00
private IStateMachine<PlayerState, CharacterBody2D> _stateMachine;
2025-03-01 14:08:31 +01:00
2025-03-11 17:58:46 +01:00
public void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
2025-03-01 14:08:31 +01:00
{
2025-03-11 17:58:46 +01:00
_stateMachine = machine;
2025-03-10 14:23:28 +01:00
Invulnerable = GlobalState.Instance.SessionSettings.GodMode;
2025-03-01 14:08:31 +01:00
_healthProvider.ResourceChanged += ((value, maxValue) =>
{
2025-03-03 09:20:11 +01:00
//if (!Enabled) return;
2025-03-03 10:58:20 +01:00
Hud.Instance?.UpdateHealth(value, maxValue);
2025-03-01 14:08:31 +01:00
EmitSignal(SignalName.HealthChanged, value, maxValue);
});
2025-03-03 10:58:20 +01:00
_healthProvider.ResourceDecreased += (value, newValue, maxValue) =>
{
EmitSignal(SignalName.HealthDecreased, value, newValue, maxValue);
};
_shieldProvider.ResourceDecreased += (value, newValue, maxValue) =>
{
EmitSignal(SignalName.ShieldDecreased, value, newValue, maxValue);
};
2025-03-01 14:08:31 +01:00
_shieldProvider.ResourceChanged += ((value, maxValue) =>
{
2025-03-03 09:20:11 +01:00
//if (!Enabled) return;
2025-03-03 10:58:20 +01:00
Hud.Instance?.UpdateShield(value, maxValue);
2025-03-01 14:08:31 +01:00
EmitSignal(SignalName.ShieldChanged, value, maxValue);
});
_healthProvider.ResourceDepleted += () =>
{
2025-03-03 09:20:11 +01:00
//if (!Enabled) return;
2025-03-01 14:08:31 +01:00
EmitSignal(SignalName.Death);
};
// Set max resources based on inventory
SetMaxResources();
InventoryManager.Instance.ItemAdded += (item, amount) =>
{
if (item.ItemKey != HealthExtendName && item.ItemKey != ShieldExtendName) return;
SetMaxResources();
};
2025-03-31 18:28:33 +02:00
_motivationProvider.ResourceChanged += (value, maxValue) =>
{
Hud.Instance?.UpdateMotivation(value, maxValue);
};
}
private void SetMaxResources()
{
var healthExtends = InventoryManager.Instance.GetItemCount(HealthExtendName);
var shieldExtends = InventoryManager.Instance.GetItemCount(ShieldExtendName);
_healthProvider.MaxResource = BaseHealth + (healthExtends * HealthExtendAmount);
_shieldProvider.MaxResource = BaseShield + (shieldExtends * ShieldExtendAmount);
2025-03-31 18:28:33 +02:00
_motivationProvider.CurrentResource = BaseMotivation;
2025-03-01 14:08:31 +01:00
}
2025-03-03 09:20:11 +01:00
public void RefillHealth()
{
_healthProvider.FillResource();
}
public void RefillShield()
{
_shieldProvider.FillResource();
}
2025-03-01 14:08:31 +01:00
private void _on_damage_hitbox_area_entered(Area2D area)
{
if (!Enabled) return;
if (Invulnerable) return;
2025-03-03 17:55:53 +01:00
if (area.IsInGroup(AcidGroupName))
{
// Handle acid death
AcidDeath();
return;
}
2025-03-01 14:08:31 +01:00
if (area is not Bullet bullet || bullet.BulletOwner == BulletGroup) return;
this.Hit(bullet.Damage, bullet.DamageType);
bullet.RequestCollisionDestruction();
}
2025-03-03 17:55:53 +01:00
private void AcidDeath()
{
if (!Enabled) return;
GD.Print("Acid death");
2025-03-11 17:58:46 +01:00
_stateMachine.SetState(PlayerState.Drowning);
//_healthProvider.CurrentResource = 0;
2025-03-03 17:55:53 +01:00
}
2025-03-18 14:36:28 +01:00
private void ApplyDamageToHealth(float damage, DamageType type = DamageType.Neutral)
{
if (HealthDamageResistances.Where(x => x.DamageType == type)
.Any(x => x.Attribute is DamageAttribute.Skip)) return;
var dmg = HealthDamageResistances.Aggregate(damage, (current, resistance) => current * resistance.CalculateDamage(current, type));
CurrentHealth -= dmg;
}
2025-03-01 14:08:31 +01:00
public void Hit(float damage, DamageType type = DamageType.Neutral)
{
if (!Enabled) 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-04-08 19:06:39 +02:00
2025-03-18 14:36:28 +01:00
// Check if the shield is empty or damage has skip attributes
if (CurrentShield <= 0 || ShieldDamageResistances.Where(x => x.DamageType == type).Any(x => x.Attribute is DamageAttribute.Skip))
{
// do not apply, go to health
2025-04-08 19:06:39 +02:00
ApplyDamageToHealth(difficultyReducedDmg, type);
2025-03-18 14:36:28 +01:00
}
else
{
2025-04-08 19:06:39 +02:00
var shieldDmg = ShieldDamageResistances.Aggregate(difficultyReducedDmg, (current, resistance) => current * resistance.CalculateDamage(current, type));
2025-03-18 14:36:28 +01:00
// apply and get remainder
var remainder = CurrentShield - shieldDmg;
CurrentShield = remainder; // Let the resource's self-balancing take care of any remainders
if (remainder < 0)
{
// Apply remainder to health
ApplyDamageToHealth(-remainder, type);
}
}
2025-04-08 19:06:39 +02:00
// return;
2025-03-18 14:36:28 +01:00
2025-04-08 19:06:39 +02:00
// if (CurrentShield > 0 && type is not DamageType.Explosive or DamageType.Acid)
// {
// // Reduce shield
// //PlayShieldAnimation(); // Let this be handled by event
// CurrentShield -= damage;
// if (CurrentShield < 0)
// {
// CurrentHealth -= Math.Abs(CurrentShield);
// CurrentShield = 0;
// }
// }
// else
// {
// if (type is DamageType.Fire)
// {
// CurrentHealth -= damage * 2;
// }
// else
// {
// CurrentHealth -= damage;
// }
//
// //Blink(); // Let this be handled by event
// }
//
// if (!(CurrentHealth <= 0)) return;
2025-03-01 14:08:31 +01:00
}
}