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();
}
}
}
}

View file

@ -52,6 +52,11 @@ public partial class InventoryManager : Node2D
return _itemsDict.TryGetValue(key, out item);
}
public LootItem FindItemInDb(string key)
{
return ItemsDatabase.GetLootItem(key);
}
public bool HasItems(IList<string> itemKeys)
{
return itemKeys.Aggregate(false, (current, item) => current || GetItemCount(item) > 0);
@ -87,6 +92,19 @@ public partial class InventoryManager : Node2D
}
public bool CanAddItem(string itemKey)
{
var found = TryGetItem(itemKey, out var itm);
if (found)
{
return (itm.Count < itm.Item.Max);
}
var dbItem = FindItemInDb(itemKey);
return dbItem != null;
}
public bool AddItem(LootItem item)
{
//var item = new LootItem() { Item = type, Amount = amount };

View file

@ -19,6 +19,7 @@ public partial class LootItem : Resource
[Export] public bool ConsumeOnUse;
[Export] public UiItemType UiType;
[Export] public bool Selectable;
[Export] public bool AutoPickup { get; private set; } = false;
[Export] public Texture2D InventorySprite;
//[Export] public SpriteFrames WorldSprite;
//[Export] public PackedScene HudItemScene;