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,4 +1,4 @@
[gd_scene load_steps=30 format=3 uid="uid://bc054js8ep2b"]
[gd_scene load_steps=37 format=3 uid="uid://bc054js8ep2b"]
[ext_resource type="Script" uid="uid://c2mo5hc1qb6kf" path="res://Scripts/Components/Actors/Actor.cs" id="1_ugrra"]
[ext_resource type="SpriteFrames" uid="uid://ch2ll1on8im2p" path="res://Resources/Sprites/FairyGuard.tres" id="2_i2plx"]
@ -13,6 +13,7 @@
[ext_resource type="Resource" uid="uid://cdfmedtgp2rcn" path="res://Resources/Weapons/EnemyWeapon.tres" id="11_kuimj"]
[ext_resource type="Script" uid="uid://2cijskgyt2xb" path="res://Scripts/Components/Actors/DamageReceiverActorModule.cs" id="12_fu5g7"]
[ext_resource type="Script" uid="uid://m0ag88kn0c40" path="res://Scripts/Components/Actors/DeathAnimationHandler.cs" id="13_e2vvk"]
[ext_resource type="Script" uid="uid://ddsqqfx1usc3j" path="res://Scripts/Resources/DamageResistance.cs" id="13_hwppe"]
[ext_resource type="Resource" uid="uid://dk2rbf88a5irh" path="res://Resources/Bullets/Explosion_Harmless.tres" id="14_881we"]
[ext_resource type="Script" uid="uid://7g3luecewcp5" path="res://Scripts/Components/Actors/ActorDefeatScriptHandler.cs" id="15_17yce"]
[ext_resource type="PackedScene" uid="uid://cm564ue740cpx" path="res://Scenes/Actors/DeadFairyGuard.tscn" id="15_gsthm"]
@ -29,6 +30,42 @@ radius = 5.0
[sub_resource type="CircleShape2D" id="CircleShape2D_sthwe"]
radius = 85.0529
[sub_resource type="Resource" id="Resource_swk2c"]
script = ExtResource("13_hwppe")
DamageType = 2
Attribute = 2
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_evv7k"]
script = ExtResource("13_hwppe")
DamageType = 3
Attribute = 2
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_y1chq"]
script = ExtResource("13_hwppe")
DamageType = 4
Attribute = 2
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_yrg2w"]
script = ExtResource("13_hwppe")
DamageType = 0
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_i6q2w"]
script = ExtResource("13_hwppe")
DamageType = 5
Attribute = 2
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_ry6ip"]
script = ExtResource("13_hwppe")
DamageType = 1
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="CircleShape2D" id="CircleShape2D_0tkae"]
radius = 5.09902
@ -119,6 +156,7 @@ WeaponData = ExtResource("11_kuimj")
script = ExtResource("12_fu5g7")
HealthProvider = NodePath("../HealthProvider")
BulletGroup = 2
DamageResistances = Array[ExtResource("13_hwppe")]([SubResource("Resource_swk2c"), SubResource("Resource_evv7k"), SubResource("Resource_y1chq"), SubResource("Resource_yrg2w"), SubResource("Resource_i6q2w"), SubResource("Resource_ry6ip")])
[node name="Area2D" type="Area2D" parent="DamageReceiver"]
collision_layer = 16

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=61 format=3 uid="uid://c4pr2707hbeph"]
[gd_scene load_steps=75 format=3 uid="uid://c4pr2707hbeph"]
[ext_resource type="Script" uid="uid://d2ubk5gucny6s" path="res://Scripts/Components/FSM/PlayerFSMProxy.cs" id="1_g3wua"]
[ext_resource type="Script" uid="uid://bw2hakslndaxm" path="res://Scripts/Components/FSM/PlayerStateMachine.cs" id="1_mpmil"]
@ -28,6 +28,7 @@
[ext_resource type="Script" uid="uid://dv205x8msohpv" path="res://Scripts/Components/Actors/ActivationProvider.cs" id="22_12cwd"]
[ext_resource type="PackedScene" uid="uid://chkpk7erlqajg" path="res://Scenes/Selector.tscn" id="23_5tmtw"]
[ext_resource type="AudioStream" uid="uid://myr6n2c1u503" path="res://SFX/581602__samsterbirdies__beep-error.mp3" id="24_5tmtw"]
[ext_resource type="Script" uid="uid://ddsqqfx1usc3j" path="res://Scripts/Resources/DamageResistance.cs" id="25_12erk"]
[ext_resource type="Script" uid="uid://cem7a1agaqtrt" path="res://Scenes/InteractionController.cs" id="27_vwjki"]
[ext_resource type="Script" uid="uid://d0bebi2vn171l" path="res://Scripts/Components/FSM/Player/PlayerFSMItemUseModule.cs" id="29_m323t"]
[ext_resource type="Texture2D" uid="uid://ddwhrlrgj6i00" path="res://Sprites/Actors/Cirno.png" id="30_li0ug"]
@ -106,6 +107,84 @@ animations = [{
"speed": 5.0
}]
[sub_resource type="Resource" id="Resource_c4ide"]
script = ExtResource("25_12erk")
DamageType = 0
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_fjg1v"]
script = ExtResource("25_12erk")
DamageType = 1
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_hed00"]
script = ExtResource("25_12erk")
DamageType = 2
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_q2flx"]
script = ExtResource("25_12erk")
DamageType = 3
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_k7ckg"]
script = ExtResource("25_12erk")
DamageType = 4
Attribute = 4
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_qlmfy"]
script = ExtResource("25_12erk")
DamageType = 5
Attribute = 4
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_v2m0j"]
script = ExtResource("25_12erk")
DamageType = 0
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_awrib"]
script = ExtResource("25_12erk")
DamageType = 1
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_nfrn8"]
script = ExtResource("25_12erk")
DamageType = 2
Attribute = 2
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_nfq4p"]
script = ExtResource("25_12erk")
DamageType = 3
Attribute = 1
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_m1iep"]
script = ExtResource("25_12erk")
DamageType = 0
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_ckpe7"]
script = ExtResource("25_12erk")
DamageType = 4
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="Resource" id="Resource_p5rkw"]
script = ExtResource("25_12erk")
DamageType = 5
Attribute = 0
metadata/_custom_type_script = "uid://ddsqqfx1usc3j"
[sub_resource type="CircleShape2D" id="CircleShape2D_igu66"]
radius = 2.23607
@ -351,6 +430,8 @@ collision_mask = 392
script = ExtResource("14_igu66")
_healthProvider = NodePath("HealthProvider")
_shieldProvider = NodePath("ShieldProvider")
ShieldDamageResistances = Array[ExtResource("25_12erk")]([SubResource("Resource_c4ide"), SubResource("Resource_fjg1v"), SubResource("Resource_hed00"), SubResource("Resource_q2flx"), SubResource("Resource_k7ckg"), SubResource("Resource_qlmfy")])
HealthDamageResistances = Array[ExtResource("25_12erk")]([SubResource("Resource_v2m0j"), SubResource("Resource_awrib"), SubResource("Resource_nfrn8"), SubResource("Resource_nfq4p"), SubResource("Resource_m1iep"), SubResource("Resource_ckpe7"), SubResource("Resource_p5rkw")])
[node name="HealthProvider" type="Node2D" parent="DamageReceiver"]
script = ExtResource("15_5qlss")

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

View file

@ -6,4 +6,5 @@ public enum DamageAttribute
Resist,
Weak,
Immune,
Skip
}

View file

@ -31,6 +31,8 @@ public partial class DamageResistance : Resource
case DamageAttribute.Immune:
return 0;
break;
case DamageAttribute.Skip:
return 0;
}
return 1;
}