|
@ -17,66 +17,39 @@ func _input(event: InputEvent) -> void: |
|
|
toggle_inventory() |
|
|
toggle_inventory() |
|
|
|
|
|
|
|
|
func _ready() -> void: |
|
|
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) |
|
|
|
|
|
|
|
|
update_inventory_with_resource() |
|
|
|
|
|
refresh_inventory_grid() |
|
|
|
|
|
|
|
|
|
|
|
InventoryManager.item_added.connect(_on_item_added) # Should be added after update_inventory_with_resource() |
|
|
|
|
|
InventoryManager.item_removed.connect(_on_item_removed) # Should be added after update_inventory_with_resource() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func create_item_cell(item_resource: DBItemResource) -> void: |
|
|
|
|
|
var item_rect: InventoryItemRect = 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: |
|
|
func find_item_rect(item_resource: DBItemResource) -> InventoryItemRect: |
|
|
var rect: InventoryItemRect = null |
|
|
var rect: InventoryItemRect = null |
|
|
|
|
|
|
|
|
for container: InventoryItemRect in grid_container.get_children(): |
|
|
|
|
|
if container.item_resource.id == item_resource.id: |
|
|
|
|
|
rect = container |
|
|
|
|
|
|
|
|
for item_rect: InventoryItemRect in grid_container.get_children(): |
|
|
|
|
|
if item_rect.item_resource == null: continue |
|
|
|
|
|
if item_rect.item_resource.id == item_resource.id: |
|
|
|
|
|
rect = item_rect |
|
|
break |
|
|
break |
|
|
|
|
|
|
|
|
return rect |
|
|
return rect |
|
|
|
|
|
|
|
|
func generate_inventory_grid() -> void: |
|
|
|
|
|
|
|
|
func refresh_inventory_grid() -> void: |
|
|
for item: InventoryItemRect in grid_container.get_children(): |
|
|
for item: InventoryItemRect in grid_container.get_children(): |
|
|
item.queue_free() |
|
|
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() |
|
|
|
|
|
|
|
|
for item: DBItemResource in InventoryManager.inventory: |
|
|
|
|
|
create_item_cell(item) |
|
|
|
|
|
|
|
|
# Add empty item cells |
|
|
|
|
|
|
|
|
var empty_cells: int = InventoryManager.max_inventory_items - inventory_resource.inventory.size() |
|
|
for _i: int in range(empty_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) + ")") |
|
|
|
|
|
|
|
|
create_item_cell(null) |
|
|
|
|
|
|
|
|
func toggle_inventory() -> void: |
|
|
func toggle_inventory() -> void: |
|
|
visible = not visible |
|
|
visible = not visible |
|
@ -90,15 +63,14 @@ func toggle_inventory() -> void: |
|
|
InventoryManager.inventory_closed.emit() |
|
|
InventoryManager.inventory_closed.emit() |
|
|
get_tree().paused = false |
|
|
get_tree().paused = false |
|
|
|
|
|
|
|
|
|
|
|
func update_inventory_with_resource() -> void: |
|
|
|
|
|
# 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) |
|
|
|
|
|
|
|
|
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_added(_item_id: String, _amount: int) -> void: |
|
|
|
|
|
refresh_inventory_grid() |
|
|
|
|
|
|
|
|
func _on_item_dropped(item_resource: DBItemResource) -> void: |
|
|
|
|
|
subtract_item(item_resource, 1) |
|
|
|
|
|
|
|
|
func _on_item_removed(_item_id: String, _amount: int) -> void: |
|
|
|
|
|
refresh_inventory_grid() |