class_name Inventory extends Control @export var inventory_resource: InventoryResource @export var item_rect_scene: PackedScene @export var grid_container: GridContainer @export var max_items: int = 40 # 4 rows of 10 @export var highlight_theme: Resource var selected_item: int = 0 func _input(event: InputEvent) -> void: if event.is_action_pressed("open_inventory"): toggle_inventory() func _ready() -> void: InventoryManager.item_picked_up.connect(_on_item_picked_up) InventoryManager.item_dropped.connect(_on_item_dropped) InventoryManager.add_to_inventory.connect(_on_add_to_inventory) InventoryManager.remove_from_inventory.connect(_on_remove_from_inventory) generate_inventory_grid() func add_item(item_resource: DBItemResource, amount: int = 1) -> void: var item_rect: InventoryItemRect = find_item_rect(item_resource) if item_rect != null: if item_rect.item_resource.amount + item_resource.amount >= inventory_resource.max_stack_size: item_rect.on_stack_full(true) return item_rect.item_resource.amount += item_resource.amount item_rect.update_rect() else: item_rect = item_rect_scene.instantiate() grid_container.add_child(item_rect) item_rect.init(item_resource, highlight_theme) func find_item_rect(item_resource: DBItemResource) -> InventoryItemRect: var rect: InventoryItemRect = null for container: InventoryItemRect in grid_container.get_children(): if container.item_resource.id == item_resource.id: rect = container break return rect func generate_inventory_grid() -> void: for item: InventoryItemRect in grid_container.get_children(): item.queue_free() # Add any items from the existing inventory resource for item_resource: DBItemResource in inventory_resource.inventory: InventoryManager.add_to_inventory.emit(item_resource.id, item_resource.amount) var empty_cells: int = max_items - inventory_resource.inventory.size() # Add empty item cells for _i: int in range(empty_cells): var item_rect: InventoryItemRect = item_rect_scene.instantiate() grid_container.add_child(item_rect) item_rect.clear_rect() func subtract_item(item_resource: DBItemResource, amount: int =1) -> void: var item_rect: InventoryItemRect = find_item_rect(item_resource) if item_rect != null: if item_rect.item_resource.amount >= inventory_resource.max_stack_size: item_rect.on_stack_full(false) item_rect.item_resource.amount -= amount item_rect.update_rect() if item_rect.item_resource.amount < 1: # Empty stack item_rect.queue_free() else: push_error("Attempting to subtract amount (" + str(amount) + " from nonexistent inventory item (" + str(item_resource.name) + ")") func toggle_inventory() -> void: visible = not visible if visible: Input.mouse_mode = Input.MOUSE_MODE_VISIBLE InventoryManager.inventory_opened.emit() get_tree().paused = true else: Input.mouse_mode = Input.MOUSE_MODE_CAPTURED InventoryManager.inventory_closed.emit() get_tree().paused = false func _on_add_to_inventory(item_id: String, amount: int) -> void: add_item(DBItems.data[item_id], amount) func _on_remove_from_inventory(item_id: String, amount: int) -> void: subtract_item(DBItems.data[item_id], amount) func _on_item_picked_up(item_resource: DBItemResource, amount: int) -> void: add_item(item_resource, amount) func _on_item_dropped(item_resource: DBItemResource) -> void: subtract_item(item_resource, 1)