mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
Destroyable chests
This commit is contained in:
parent
f1f542efc4
commit
80e0eda977
14 changed files with 253 additions and 27 deletions
|
|
@ -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()
|
||||
|
|
|
|||
65
Scripts/Components/Actors/GenericDamageReceiver.cs
Normal file
65
Scripts/Components/Actors/GenericDamageReceiver.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/Actors/GenericDamageReceiver.cs.uid
Normal file
1
Scripts/Components/Actors/GenericDamageReceiver.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cq3hkweplldbr
|
||||
6
Scripts/Components/Actors/IHittable.cs
Normal file
6
Scripts/Components/Actors/IHittable.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
namespace Cirno.Scripts.Components.Actors;
|
||||
|
||||
public interface IHittable
|
||||
{
|
||||
public void Hit(float damage, DamageType damageType = DamageType.Neutral);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue