mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
Damage actor handler
This commit is contained in:
parent
335f4d5430
commit
4fd31d7988
7 changed files with 100 additions and 4 deletions
66
Scripts/Components/Actors/DamageReceiverActorModule.cs
Normal file
66
Scripts/Components/Actors/DamageReceiverActorModule.cs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.Actors;
|
||||
|
||||
public partial class DamageReceiverActorModule : ActorModule
|
||||
{
|
||||
protected Actor _actor;
|
||||
|
||||
[Export]
|
||||
public ActorResourceProvider HealthProvider { get; private set; }
|
||||
|
||||
[Export]
|
||||
public bool Invulnerable { get; private set; } = false;
|
||||
|
||||
[Export] protected BulletOwner BulletGroup { get; set; } = BulletOwner.None;
|
||||
|
||||
public override void Init(Actor actor)
|
||||
{
|
||||
_actor = actor;
|
||||
|
||||
HealthProvider.ResourceDepleted += OnDeath;
|
||||
}
|
||||
|
||||
public override void Update(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsUpdate(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void _on_damage_hitbox_area_entered(Area2D area)
|
||||
{
|
||||
if (_actor.IsDestroyed) return;
|
||||
if (Invulnerable) return;
|
||||
if (area is not Bullet bullet) return;
|
||||
|
||||
if (BulletGroup is BulletOwner.None)
|
||||
{
|
||||
this.Hit(bullet.Damage);
|
||||
bullet.RequestCollisionDestruction();
|
||||
return;
|
||||
}
|
||||
|
||||
if (bullet.BulletInfo.Owner == BulletGroup) return;
|
||||
|
||||
this.Hit(bullet.Damage);
|
||||
bullet.RequestCollisionDestruction();
|
||||
}
|
||||
|
||||
public void Hit(float damage, DamageType damageType = DamageType.Neutral)
|
||||
{
|
||||
if (_actor.IsDestroyed) return;
|
||||
if (Invulnerable) return;
|
||||
|
||||
HealthProvider.CurrentResource -= damage;
|
||||
}
|
||||
|
||||
protected void OnDeath()
|
||||
{
|
||||
_actor.IsDestroyed = true;
|
||||
GD.Print("Actor dead");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue