Add stackable items

This commit is contained in:
2025-03-11 16:00:46 -07:00
parent 192f2eb316
commit f060229b5e
17 changed files with 151 additions and 57 deletions

View File

@@ -4,6 +4,7 @@ using Chickensoft.Serialization;
using Godot;
using System.Collections.Generic;
using System.Linq;
using Zennysoft.Game.Abstractions;
using Zennysoft.Ma.Adapter;
namespace Zennysoft.Game.Ma;
@@ -52,6 +53,16 @@ public partial class Inventory : Node, IInventory
var consumables = listToSort.Where(x => x is ConsumableItem).OrderBy(x => x as ConsumableItem, new ConsumableComparer());
var throwables = listToSort.Where(x => x is ThrowableItem).OrderBy(x => x as ThrowableItem, new ThrowableComparer());
Items = [.. equippedItems, .. weapons, .. armor, .. accessories, .. consumables, .. throwables];
var stackableItems = Items.OfType<IStackable>();
var itemsToStack = stackableItems.GroupBy(x => ((InventoryItem)x).ItemName).Where(x => x.Count() > 1);
foreach (var itemStack in itemsToStack)
{
var firstItem = itemStack.First();
firstItem.SetCount(itemStack.Count());
var itemsToRemove = itemStack.Except([firstItem]).Cast<InventoryItem>();
Items = [.. Items.Except(itemsToRemove)];
}
}
public class WeaponComparer : IComparer<Weapon>