Auto Pickup

This commit is contained in:
Marco 2025-03-28 15:38:55 +01:00
commit 488d02ef81
24 changed files with 148 additions and 19 deletions

View file

@ -1,4 +1,5 @@
using Cirno.Scripts.Resources;
using System.Linq;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
@ -8,19 +9,66 @@ public partial class ItemPickup : Interactable
{
[Export] public Array<LootItem> LootTable = [];
private bool _autoPickup = false;
public override void _Ready()
{
_autoPickup = LootTable.Any(x => x.AutoPickup);
// if (LootTable.Any(x => x.AutoPickup))
// {
// _autoPickup = true;
// }
//this.AreaEntered += _on_area_entered;
}
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
{
GD.Print("Attempting to Pickup Item");
if (!MeetsRequirements()) return false;
AddItemsToInventory();
return true;
}
private void AddItemsToInventory()
{
var failedItems = new Array<LootItem>();
foreach (var item in LootTable)
{
InventoryManager.Instance.AddItem(item);
if (!InventoryManager.Instance.AddItem(item))
{
failedItems.Add(item);
}
}
if (failedItems.Count > 0)
{
foreach (var failedItem in failedItems)
{
var dup = this.Duplicate() as ItemPickup;
this.AddSibling(dup);
dup.LootTable = [failedItem];
}
}
// Delet This
QueueFree();
return true;
}
private void _on_area_entered(Area2D area)
{
if (!_autoPickup) return;
if (area is InteractionController interactionController)
{
//Check if items are not maxed to avoid a looping autopickup situation
var canAdd = LootTable.Aggregate(false, (current, item) => current || InventoryManager.Instance.CanAddItem(item.ItemKey));
if (canAdd)
{
AddItemsToInventory();
}
}
}
}