Loot drops provider

This commit is contained in:
Marco 2025-03-21 16:03:44 +01:00
commit cf4b11d157
9 changed files with 117 additions and 19 deletions

View file

@ -9,6 +9,9 @@ public partial class Dead : EnemyStateBase
[Export]
public EnemyStorageModule StorageModule { get; private set; }
[Export]
public EnemyDropsProvider DropsProvider { get; private set; }
// public override void Init(IStateMachine<EnemyState, CharacterBody2D> machine)
// {
@ -27,7 +30,8 @@ public partial class Dead : EnemyStateBase
activatable.Activate(StorageModule.Root.ActivationType);
}
StorageModule.Root.QueueFree();
DropsProvider.DropLoot();
StorageModule.Root.QueueFree();
}
}

View 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}");
}
}
}

View file

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

View file

@ -11,8 +11,8 @@ public partial class EnemyFSMProxy : CharacterBody2D
[Export] public EnemyStateMachine EnemyFSM { get; private set; }
[Export] public EnemyResource EnemyResource { get; private set; }
[Export] public Array<LootDrop> ExtraLoot { get; private set; }
[Export] public Array<LootDrop> ExtraLoot { get; private set; } = [];
[Export]
public AiState StartingAiState { get; private set; }

View file

@ -1,4 +1,8 @@
using Godot;
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Resources.Loot;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.FSM.Enemy;
@ -11,8 +15,8 @@ public partial class EnemyStorageModule : Node2D
public Vector2 FacingDirection { get; set; }
public float MovementSpeed => Root.EnemyResource.MovementSpeed;
public IEnumerable<LootDrop> LootDrops => Root.EnemyResource.LootDrops.Concat(Root.ExtraLoot);