Destroyable chests

This commit is contained in:
MaddoScientisto 2025-03-12 22:01:45 +01:00
commit 80e0eda977
14 changed files with 253 additions and 27 deletions

View file

@ -1,8 +1,11 @@
using Godot;
using System.Linq;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.Actors;
public partial class DamageReceiverActorModule : ActorModule
public partial class DamageReceiverActorModule : ActorModule, IHittable
{
protected Actor _actor;
@ -14,6 +17,8 @@ public partial class DamageReceiverActorModule : ActorModule
[Export] public BulletOwner BulletGroup { get; set; } = BulletOwner.None;
[Export] public Array<DamageResistance> DamageResistances { get; set; } = [];
public override void Init(Actor actor)
{
_actor = actor;
@ -42,14 +47,14 @@ public partial class DamageReceiverActorModule : ActorModule
if (BulletGroup is BulletOwner.None)
{
this.Hit(bullet.Damage);
this.Hit(bullet.Damage, bullet.DamageType);
bullet.RequestCollisionDestruction();
return;
}
if (bullet.BulletInfo.Owner == BulletGroup) return;
this.Hit(bullet.Damage);
this.Hit(bullet.Damage, bullet.DamageType);
bullet.RequestCollisionDestruction();
}
@ -58,7 +63,9 @@ public partial class DamageReceiverActorModule : ActorModule
if (_actor.IsDestroyed) return;
if (Invulnerable) return;
HealthProvider.CurrentResource -= damage;
var dmg = DamageResistances.Aggregate(damage, (current, resistance) => current * resistance.CalculateDamage(current, damageType));
HealthProvider.CurrentResource -= dmg;
}
protected void OnDeath()

View file

@ -0,0 +1,65 @@
using System.Linq;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.Actors;
public partial class GenericDamageReceiver : Area2D, 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; } = [];
private Node2D _parent;
public override void _Ready()
{
_parent = GetParent<Node2D>();
HealthProvider.FillResource();
HealthProvider.ResourceDepleted += OnDeath;
}
private void _on_damage_hitbox_area_entered(Area2D area)
{
if (Invulnerable) return;
if (area is not Bullet bullet) return;
if (BulletGroup is BulletOwner.None)
{
this.Hit(bullet.Damage, bullet.DamageType);
bullet.RequestCollisionDestruction();
return;
}
if (bullet.BulletInfo.Owner == BulletGroup) return;
this.Hit(bullet.Damage, bullet.DamageType);
bullet.RequestCollisionDestruction();
}
public void Hit(float damage, DamageType damageType = DamageType.Neutral)
{
if (Invulnerable) return;
var dmg = DamageResistances.Aggregate(damage, (current, resistance) => current * resistance.CalculateDamage(current, damageType));
HealthProvider.CurrentResource -= dmg;
}
private void OnDeath()
{
if (Debris is not null)
{
_parent.CreateSibling<Node2D>(Debris);
}
_parent.QueueFree();
}
}

View file

@ -0,0 +1 @@
uid://cq3hkweplldbr

View file

@ -0,0 +1,6 @@
namespace Cirno.Scripts.Components.Actors;
public interface IHittable
{
public void Hit(float damage, DamageType damageType = DamageType.Neutral);
}