mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-19 07:13:47 +00:00
Loot drops provider
This commit is contained in:
parent
cc85cd4b54
commit
cf4b11d157
9 changed files with 117 additions and 19 deletions
55
Scripts/Components/FSM/Enemy/EnemyDropsProvider.cs
Normal file
55
Scripts/Components/FSM/Enemy/EnemyDropsProvider.cs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy;
|
||||
|
||||
public partial class EnemyDropsProvider : Node2D
|
||||
{
|
||||
private RandomNumberGenerator _rng = new();
|
||||
|
||||
[Export] public float DropRadius { get; private set; } = 8f;
|
||||
|
||||
[Export]
|
||||
public EnemyStorageModule StorageModule { get; private set; }
|
||||
|
||||
public void DropLoot()
|
||||
{
|
||||
foreach (var loot in StorageModule.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);
|
||||
var droppedItem = StorageModule.Root.CreateSibling<Node2D>(scene);
|
||||
|
||||
// Generate random point within DropRadius
|
||||
float angle = _rng.RandfRange(0, Mathf.Tau); // Random angle (0 to 2π)
|
||||
float radius = _rng.RandfRange(0, DropRadius); // Random radius within range
|
||||
|
||||
// Convert polar to cartesian coordinates
|
||||
float xOffset = radius * Mathf.Cos(angle);
|
||||
float yOffset = radius * Mathf.Sin(angle);
|
||||
|
||||
Vector2 spawnPosition = StorageModule.Root.GlobalPosition + new Vector2(xOffset, yOffset);
|
||||
|
||||
droppedItem.GlobalPosition = spawnPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print($"Skipping Item with missing path: {item.ItemName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue