Add stackable items
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user