Fixed item

This commit is contained in:
2026-01-15 09:35:57 -06:00
parent 5af98a69a2
commit c48de36f2e
8 changed files with 67 additions and 39 deletions

View File

@@ -6,15 +6,15 @@ var items:Array[ItemStack] = []
enum InventorySortType {
ITEM_TYPE,
ITEM_KEY
# ITEM_KEY
}
signal itemAdded(item:Item.ItemId)
signal itemRemoved(item:Item.ItemId)
signal itemQuantityChanged(item:Item.ItemId, quantity:int)
signal itemAdded(item:Item.Id)
signal itemRemoved(item:Item.Id)
signal itemQuantityChanged(item:Item.Id, quantity:int)
signal inventorySorted(sortBy:InventorySortType, reverse:bool)
func setItem(item:Item.ItemId, quantity:int) -> void:
func setItem(item:Item.Id, quantity:int) -> void:
if quantity < 0:
push_error("Cannot set item quantity to negative value, using 0")
quantity = 0
@@ -42,13 +42,13 @@ func setItem(item:Item.ItemId, quantity:int) -> void:
itemAdded.emit(item)
itemQuantityChanged.emit(item, quantity)
func getItemQuantity(item:Item.ItemId) -> int:
func getItemQuantity(item:Item.Id) -> int:
for itemStack in items:
if itemStack.item == item:
return itemStack.quantity
return 0
func addItem(item:Item.ItemId, quantity:int = 1) -> void:
func addItem(item:Item.Id, quantity:int = 1) -> void:
# Add can only take positive quantities, otherwise use set or remove
if quantity <= 0:
push_error("Cannot add non-positive item quantity")
@@ -58,15 +58,15 @@ func addItem(item:Item.ItemId, quantity:int = 1) -> void:
func addStack(stack:ItemStack) -> void:
self.addItem(stack.item, stack.quantity)
func removeItem(item:Item.ItemId) -> void:
func removeItem(item:Item.Id) -> void:
self.setItem(item, 0)
func sort(sortBy:InventorySortType, reverse:bool = false) -> void:
match sortBy:
InventorySortType.ITEM_TYPE:
items.sort_custom(_sortByItemType)
InventorySortType.ITEM_KEY:
items.sort_custom(_sortByItemKey)
# InventorySortType.ITEM_KEY:
# items.sort_custom(_sortByItemKey)
if reverse:
items.reverse()
@@ -75,7 +75,7 @@ func sort(sortBy:InventorySortType, reverse:bool = false) -> void:
# Sorters
func _sortByItemType(a:ItemStack, b:ItemStack) -> int:
return int(Item.getItemById(a.item).itemType) - int(Item.getItemById(b.item).itemType)
return int(Item.getType(a.item)) - int(Item.getType(b.item))
func _sortByItemKey(a:ItemStack, b:ItemStack) -> int:
return Item.getItemById(a.item).key.casecmp_to(Item.getItemById(b.item).key)
# func _sortByItemKey(a:ItemStack, b:ItemStack) -> int:
# return Item.getItemById(a.item).key.casecmp_to(Item.getItemById(b.item).key)