mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
Loot Drops
This commit is contained in:
parent
9d0b399052
commit
b7d241bf11
16 changed files with 129 additions and 6 deletions
66
Scripts/Components/Actors/EnemyDropModule.cs
Normal file
66
Scripts/Components/Actors/EnemyDropModule.cs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue