New damage resistance system

This commit is contained in:
Marco 2025-03-18 14:36:28 +01:00
commit eeaf1526f8
5 changed files with 164 additions and 3 deletions

View file

@ -1,6 +1,10 @@
using System;
using System.Linq;
using Cirno.Scripts.Components.FSM;
using Cirno.Scripts.Enums;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.Actors;
@ -31,6 +35,9 @@ public partial class PlayerDamageReceiver : Area2D
[ExportCategory("Damage Types")]
[Export] public StringName AcidGroupName { get; private set; } = "Acid";
[Export] public Array<DamageResistance> ShieldDamageResistances { get; set; } = [];
[Export] public Array<DamageResistance> HealthDamageResistances { get; set; } = [];
[Signal]
public delegate void HealthChangedEventHandler(float newValue, float maxValue);
[Signal]
@ -148,11 +155,43 @@ public partial class PlayerDamageReceiver : Area2D
_stateMachine.SetState(PlayerState.Drowning);
//_healthProvider.CurrentResource = 0;
}
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;
}
public void Hit(float damage, DamageType type = DamageType.Neutral)
{
if (!Enabled) return;
// 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
ApplyDamageToHealth(damage, type);
}
else
{
var shieldDmg = ShieldDamageResistances.Aggregate(damage, (current, resistance) => current * resistance.CalculateDamage(current, type));
// 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);
}
}
return;
if (CurrentShield > 0 && type is not DamageType.Explosive or DamageType.Acid)
{
// Reduce shield