mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-10 08:05:54 +00:00
Dropping and picking up
This commit is contained in:
parent
488d02ef81
commit
bcd007fa1e
14 changed files with 294 additions and 62 deletions
75
Scripts/Components/FSM/Player/DeathItemDropper.cs
Normal file
75
Scripts/Components/FSM/Player/DeathItemDropper.cs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Interactables;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Player;
|
||||
|
||||
public partial class DeathItemDropper : Node2D
|
||||
{
|
||||
[Export] public PlayerStorageModule Storage { get; private set; }
|
||||
[Export] public ActorResourceProvider HealthProvider { get; private set; }
|
||||
|
||||
[Export] public Array<LootItem> ItemsToDrop { get; private set; } = [];
|
||||
|
||||
[Export(PropertyHint.None, "suffix:%")] public float PercentageToDrop { get; private set; } = 50f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:%")] public float PercentageToDelete { get; private set; } = 50f;
|
||||
|
||||
private readonly StringName _dropPhysicsWrapperScene = "uid://d3hds3dbosfcm";
|
||||
|
||||
[Export] public float DropRadius { get; private set; } = 8f;
|
||||
|
||||
[Export] public float DropSpeed { get; set; } = 40f;
|
||||
|
||||
private PackedScene _loadedDropScene;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
|
||||
|
||||
_loadedDropScene = GD.Load<PackedScene>(_dropPhysicsWrapperScene);
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;
|
||||
}
|
||||
|
||||
private void HealthProviderOnResourceDepleted()
|
||||
{
|
||||
foreach (var item in ItemsToDrop)
|
||||
{
|
||||
if (InventoryManager.Instance.TryGetItem(item.ItemKey, out var itm))
|
||||
{
|
||||
var amountToDrop = Mathf.RoundToInt(itm.Count * PercentageToDrop / 100f);
|
||||
|
||||
var removedAmount = InventoryManager.Instance.RemoveItem(item.ItemKey, amountToDrop);
|
||||
|
||||
var toDelete = Mathf.RoundToInt(removedAmount * PercentageToDelete / 100f);
|
||||
|
||||
DropItem(item, removedAmount - toDelete);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DropItem(LootItem item, int amountToDrop)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(item.DropScenePath))
|
||||
{
|
||||
for (int i = 0; i < amountToDrop; i++)
|
||||
{
|
||||
GD.Print($"Dropped item: {item.ItemName}");
|
||||
|
||||
var dropInstance = Storage.Root.CreateSibling<ItemDrop>(_loadedDropScene);
|
||||
dropInstance.ItemToDrop = item;
|
||||
dropInstance.StartingSpeed = DropSpeed;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print($"Skipping Item with missing path: {item.ItemName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue