cirnogodot/Scripts/Components/Actors/EnemyDropModule.cs

66 lines
1.5 KiB
C#
Raw Normal View History

2025-03-04 09:43:05 +01:00
using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.Loot;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.Actors;
public partial class EnemyDropModule : ActorModule
{
[Export] public Array<LootDrop> LootDrops { get; private set; } = [];
private RandomNumberGenerator _rng = new ();
private Actor _actor;
public override void Init(Actor actor)
{
_actor = actor;
actor.OnDeath += ActorOnDeath;
}
private void DropLoot()
{
foreach (var loot in LootDrops)
{
if (loot is { Item: not null })
{
float roll = _rng.RandfRange(0f, 100f); // Generate a number between 0 and 100
if (roll <= loot.Chance) // Compare with drop chance
{
DropItem(loot.Item);
}
}
}
}
private void DropItem(LootItem item)
{
if (!string.IsNullOrWhiteSpace(item.DropScenePath))
{
GD.Print($"Dropped item: {item.ItemName}");
var scene = GD.Load<PackedScene>(item.DropScenePath);
_actor.CreateSibling<Node2D>(scene);
}
else
{
GD.Print($"Skipping Item with missing path: {item.ItemName}");
}
}
private void ActorOnDeath()
{
DropLoot();
_actor.OnDeath -= ActorOnDeath;
}
public override void Update(double delta)
{
}
public override void PhysicsUpdate(double delta)
{
}
}