|
@ -37,7 +37,7 @@ func _unhandled_input(event: InputEvent) -> void: |
|
|
add_to_inventory.emit("003", 1) |
|
|
add_to_inventory.emit("003", 1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _find_first_stack(item_resource: DBItemResource, item_id: String) -> bool: |
|
|
|
|
|
|
|
|
func _find_first_available_stack(item_resource: DBItemResource, item_id: String) -> bool: |
|
|
return ( |
|
|
return ( |
|
|
item_resource.id == item_id and |
|
|
item_resource.id == item_id and |
|
|
item_resource.amount < item_resource.max_stack_size |
|
|
item_resource.amount < item_resource.max_stack_size |
|
@ -45,33 +45,43 @@ func _find_first_stack(item_resource: DBItemResource, item_id: String) -> bool: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: |
|
|
func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: |
|
|
|
|
|
if not DBItems.data.get(item_id): |
|
|
|
|
|
printerr("Cannot add item, ", item_id, ", to inventory. Could not find item within DBItems.data.") |
|
|
|
|
|
return |
|
|
|
|
|
if amount < 1: |
|
|
|
|
|
printerr("Cannot add item, ", item_id, ", to inventory. Amount to add cannot be less then 1.") |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
var item_resource: DBItemResource = DBItems.data[item_id] |
|
|
var item_resource: DBItemResource = DBItems.data[item_id] |
|
|
|
|
|
|
|
|
# The logic below is a mess and needs to be reworked |
|
|
|
|
|
|
|
|
# This logic seems overly complicated and is a mess. |
|
|
|
|
|
# Should look into fixing/simplifying this in the future |
|
|
var amount_remaining: int = amount |
|
|
var amount_remaining: int = amount |
|
|
while amount_remaining > 0: |
|
|
while amount_remaining > 0: |
|
|
var first_stack_index: int = inventory.find_custom(_find_first_stack.bind(item_id)) |
|
|
|
|
|
|
|
|
var first_stack_index: int = inventory.find_custom(_find_first_available_stack.bind(item_id)) |
|
|
|
|
|
|
|
|
if first_stack_index == -1 and inventory.size() < max_inventory_items: |
|
|
if first_stack_index == -1 and inventory.size() < max_inventory_items: |
|
|
inventory.append(item_resource) |
|
|
|
|
|
|
|
|
var stack_resource: DBItemResource = item_resource.duplicate() |
|
|
|
|
|
inventory.append(stack_resource) |
|
|
|
|
|
|
|
|
if amount_remaining <= item_resource.max_stack_size: |
|
|
|
|
|
|
|
|
if amount_remaining <= stack_resource.max_stack_size: |
|
|
inventory[-1].amount += amount_remaining |
|
|
inventory[-1].amount += amount_remaining |
|
|
amount_remaining = 0 |
|
|
amount_remaining = 0 |
|
|
else: |
|
|
else: |
|
|
inventory[-1].amount = item_resource.max_stack_size |
|
|
|
|
|
amount_remaining -= item_resource.max_stack_size |
|
|
|
|
|
|
|
|
inventory[-1].amount = stack_resource.max_stack_size |
|
|
|
|
|
amount_remaining -= stack_resource.max_stack_size |
|
|
|
|
|
|
|
|
item_added.emit(item_id, amount - amount_remaining) |
|
|
item_added.emit(item_id, amount - amount_remaining) |
|
|
elif first_stack_index > -1: |
|
|
elif first_stack_index > -1: |
|
|
var current_amount: int = inventory[first_stack_index].amount |
|
|
|
|
|
|
|
|
var stack_resource: DBItemResource = inventory[first_stack_index] |
|
|
|
|
|
var current_amount: int = stack_resource.amount |
|
|
var total_amount: int = current_amount + amount_remaining |
|
|
var total_amount: int = current_amount + amount_remaining |
|
|
if item_resource.max_stack_size - total_amount < 0: |
|
|
|
|
|
inventory[first_stack_index].amount += amount_remaining |
|
|
|
|
|
|
|
|
if total_amount < stack_resource.max_stack_size: # Stack not full and can hold |
|
|
|
|
|
inventory[first_stack_index].amount = total_amount |
|
|
amount_remaining = 0 |
|
|
amount_remaining = 0 |
|
|
else: |
|
|
else: |
|
|
inventory[first_stack_index].amount = item_resource.max_stack_size |
|
|
|
|
|
amount_remaining -= item_resource.max_stack_size |
|
|
|
|
|
|
|
|
inventory[first_stack_index].amount = stack_resource.max_stack_size |
|
|
|
|
|
amount_remaining -= stack_resource.max_stack_size |
|
|
|
|
|
|
|
|
item_added.emit(item_id, amount - amount_remaining) |
|
|
item_added.emit(item_id, amount - amount_remaining) |
|
|
|
|
|
|
|
|