Added debug inventory menu

This commit is contained in:
2025-05-19 17:41:39 -05:00
parent 3b9986b4ee
commit 057ed10851
26 changed files with 309 additions and 24 deletions

View File

@@ -1,6 +1,4 @@
class_name Inventory
const Item = preload("res://scripts/Item/Item.gd");
const ItemStack = preload("res://scripts/Item/ItemStack.gd");
enum ItemSortType {
NAME,
@@ -18,21 +16,22 @@ class ItemStackTypeComparator:
const ITEM_STACK_SIZE_MAX = 99;
var contents:Array[ItemStack] = [];
signal inventoryUpdated()
func isPlayerInventory() -> bool:
return self == ITEM.PLAYER_INVENTORY
func addItem(item:Item.Type, quantity: int = 1) -> void:
if !Item.isStackable(item):
func addItem(type:Item.Type, quantity: int = 1) -> void:
if !Item.isStackable(type):
# Item cannot be stacked, add each item to inv
for i in range(quantity):
contents.append(ItemStack.new(item, 1))
contents.append(ItemStack.new(type, 1))
_contentsUpdated()
return
# Check for existing stacks
for stack in contents:
if stack.item != item or stack.quantity >= ITEM_STACK_SIZE_MAX:
if stack.type != type or stack.quantity >= ITEM_STACK_SIZE_MAX:
continue
var spaceAvailable = ITEM_STACK_SIZE_MAX - stack.quantity
@@ -48,16 +47,16 @@ func addItem(item:Item.Type, quantity: int = 1) -> void:
# Add any remaining inventory as new stack.
while quantity > 0:
var newStackQuantity = min(quantity, ITEM_STACK_SIZE_MAX);
contents.append(ItemStack.new(item, newStackQuantity));
contents.append(ItemStack.new(type, newStackQuantity));
quantity -= newStackQuantity;
_contentsUpdated()
func removeItem(item:Item.Type, quantity:int) -> void:
func removeItem(type:Item.Type, quantity:int) -> void:
var totalQuantity = 0
# Calculate total quantity of the item in the inventory
for stack in contents:
if stack.item != item:
if stack.type != type:
continue
totalQuantity += stack.quantity
@@ -67,7 +66,7 @@ func removeItem(item:Item.Type, quantity:int) -> void:
# Remove the quantity from the stacks
for stack in contents:
if stack.item != item:
if stack.type != type:
continue
if stack.quantity < quantity:
@@ -85,11 +84,11 @@ func removeItem(item:Item.Type, quantity:int) -> void:
func removeStack(stack: ItemStack) -> void:
self.removeItem(stack.item, stack.quantity);
func hasItem(item:Item.Type, quantity: int = 1) -> bool:
func hasItem(type:Item.Type, quantity: int = 1) -> bool:
var totalQuantity = 0
for stack in contents:
if stack.item != item:
if stack.type != type:
continue
totalQuantity += stack.quantity
@@ -109,5 +108,6 @@ func sortBy(by:ItemSortType) -> void:
assert(false, "Invalid sort type: %s" % by)
func _contentsUpdated() -> void:
inventoryUpdated.emit()
if isPlayerInventory():
QUEST.playerInventoryUpdated.emit()