Loot Drops

This commit is contained in:
Marco 2025-03-04 09:43:05 +01:00
commit b7d241bf11
16 changed files with 129 additions and 6 deletions

View 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)
{
}
}

View file

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

View file

@ -0,0 +1,13 @@
using Godot;
namespace Cirno.Scripts.Resources.Loot;
[GlobalClass]
public partial class LootDrop : Resource
{
[Export]
public LootItem Item { get; private set; }
[Export(PropertyHint.None, "suffix:%")]
public float Chance { get; private set; }
}

View file

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

View file

@ -19,7 +19,7 @@ public partial class LootItem : Resource
[Export] public Texture2D InventorySprite;
[Export] public SpriteFrames WorldSprite;
[Export] public PackedScene HudItemScene;
[Export(PropertyHint.File)] public StringName DropScenePath { get; private set; } // Has to be a string path to avoid recursion issues
}
public enum UiItemType