cirnogodot/Scripts/Interactables/ItemPickup.cs
2025-03-28 15:38:55 +01:00

74 lines
No EOL
1.9 KiB
C#

using System.Linq;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Interactables;
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)
{
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();
}
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();
}
}
}
}