2024-07-09 22:33:38 +02:00
|
|
|
class_name Inventory
|
|
|
|
|
2024-09-06 12:34:18 +02:00
|
|
|
var _content:Array[ItemStack] = []
|
|
|
|
const INVENTORY_SIZE:int = 36
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-09-06 12:34:18 +02:00
|
|
|
func _init() -> void:
|
2024-09-08 10:54:39 +02:00
|
|
|
clear()
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-09-06 12:34:18 +02:00
|
|
|
func get_free_item_stack_index() -> int:
|
|
|
|
for i:int in range(_content.size()):
|
2024-09-08 10:54:39 +02:00
|
|
|
if _content[i].count == 0:
|
2024-09-06 12:34:18 +02:00
|
|
|
return i
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
func add_item(item:Item) -> bool:
|
|
|
|
for item_stack:ItemStack in _content:
|
2024-09-08 10:54:39 +02:00
|
|
|
if item_stack.count == 0:
|
2024-09-06 12:34:18 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
if item_stack.item.resource_path != item.resource_path:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if item_stack.count < item_stack.item.max_stack_size:
|
|
|
|
item_stack.count = item_stack.count + 1
|
|
|
|
return true
|
|
|
|
|
|
|
|
var item_stack_index:int = get_free_item_stack_index()
|
|
|
|
if item_stack_index >= 0:
|
2024-09-08 10:54:39 +02:00
|
|
|
_content[item_stack_index].item = item
|
|
|
|
_content[item_stack_index].count = 1
|
2024-09-06 12:34:18 +02:00
|
|
|
return true
|
|
|
|
|
|
|
|
return false
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
func remove_item(item:Item):
|
2024-09-06 12:34:18 +02:00
|
|
|
for i in range(_content.size() - 1, -1, -1):
|
|
|
|
var item_stack:ItemStack = _content[i]
|
2024-09-08 10:54:39 +02:00
|
|
|
if item_stack.count == 0:
|
2024-09-06 12:34:18 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
if item_stack.item.resource_path == item.resource_path:
|
|
|
|
if item_stack.count > 1:
|
|
|
|
item_stack.count = item_stack.count - 1
|
|
|
|
|
|
|
|
return
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
|
2024-09-06 12:34:18 +02:00
|
|
|
func get_item_stacks() -> Array[ItemStack]:
|
2024-07-09 22:33:38 +02:00
|
|
|
return _content
|
|
|
|
|
|
|
|
|
2024-09-08 14:00:02 +02:00
|
|
|
func get_tool_item_stacks() -> Array[ItemStack]:
|
|
|
|
return _content.slice(_content.size() - 9)
|
|
|
|
|
|
|
|
|
2024-08-15 20:24:41 +02:00
|
|
|
func clear() -> void:
|
|
|
|
_content.clear()
|
2024-09-08 10:54:39 +02:00
|
|
|
_content.resize(INVENTORY_SIZE)
|
|
|
|
for i:int in range(_content.size()):
|
|
|
|
if _content[i] == null:
|
|
|
|
_content[i] = ItemStack.new()
|
2024-08-15 20:24:41 +02:00
|
|
|
|
|
|
|
|
2024-07-09 22:33:38 +02:00
|
|
|
func has_all(items:Array[Item]) -> bool:
|
|
|
|
var needed:Array[Item] = items.duplicate()
|
|
|
|
|
2024-09-06 12:34:18 +02:00
|
|
|
for item_stack:ItemStack in _content:
|
2024-09-08 10:54:39 +02:00
|
|
|
if item_stack.count == 0:
|
2024-09-06 12:34:18 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
for i in range (item_stack.count):
|
|
|
|
needed.erase(item_stack.item)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
return needed.is_empty()
|