|
|
@ -5,10 +5,10 @@ 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 items: Array[Node] = [] |
|
|
|
var selected_item: int = 0 |
|
|
|
|
|
|
|
|
|
|
@ -17,16 +17,12 @@ func _input(event: InputEvent) -> void: |
|
|
|
toggle_inventory() |
|
|
|
|
|
|
|
func _ready() -> void: |
|
|
|
InventoryManager.item_picked_up.connect(add_item) |
|
|
|
InventoryManager.item_dropped.connect(subtract_item) |
|
|
|
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) |
|
|
|
|
|
|
|
for item: InventoryItemRect in grid_container.get_children(): |
|
|
|
item.queue_free() |
|
|
|
|
|
|
|
for item_resource: DBItemResource in inventory_resource.inventory: |
|
|
|
add_item(item_resource) |
|
|
|
|
|
|
|
refresh_items() |
|
|
|
generate_inventory_grid() |
|
|
|
|
|
|
|
|
|
|
|
func add_item(item_resource: DBItemResource, amount: int = 1) -> void: |
|
|
@ -52,22 +48,35 @@ func find_item_rect(item_resource: DBItemResource) -> InventoryItemRect: |
|
|
|
|
|
|
|
return rect |
|
|
|
|
|
|
|
func subtract_item(item_resource: DBItemResource) -> void: |
|
|
|
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 -= item_resource.amount |
|
|
|
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(item_resource.amount) + " from nonexistent inventory item (" + str(item_resource.name) + ")") |
|
|
|
|
|
|
|
func refresh_items() -> void: |
|
|
|
items = grid_container.get_children() |
|
|
|
push_error("Attempting to subtract amount (" + str(amount) + " from nonexistent inventory item (" + str(item_resource.name) + ")") |
|
|
|
|
|
|
|
func toggle_inventory() -> void: |
|
|
|
visible = not visible |
|
|
@ -80,3 +89,16 @@ func toggle_inventory() -> void: |
|
|
|
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) |