|
extends Node
|
|
|
|
signal next_quick_slot
|
|
signal previous_quick_slot
|
|
signal select_quick_slot(slot_index: int)
|
|
signal quick_slot_item_changed(item_id: String)
|
|
signal item_picked_up(item: DBItemResource)
|
|
signal item_dropped(item: DBItemResource)
|
|
signal inventory_opened
|
|
signal inventory_closed
|
|
|
|
signal add_to_inventory(item_id: String, amount: int)
|
|
signal remove_from_inventory(item_id: String, amount: int)
|
|
signal item_added(item_id: String, amount: int)
|
|
signal item_removed(item_id: String, amount: int)
|
|
|
|
var quick_slot_item_id: String = "001"
|
|
|
|
var max_inventory_items: int = 40 # 4 rows of 10
|
|
|
|
var inventory: Array[DBItemResource] = []
|
|
|
|
|
|
func _ready() -> void:
|
|
self.quick_slot_item_changed.connect(_on_quick_slot_item_changed)
|
|
self.item_picked_up.connect(_on_item_picked_up)
|
|
self.item_dropped.connect(_on_item_dropped)
|
|
|
|
self.add_to_inventory.connect(_on_add_to_inventory)
|
|
self.remove_from_inventory.connect(_on_remove_from_inventory)
|
|
|
|
|
|
|
|
# TODO: REMOVE ME
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("ui_cancel"):
|
|
add_to_inventory.emit("003", 1)
|
|
|
|
|
|
func _find_first_stack(item_resource: DBItemResource, item_id: String) -> bool:
|
|
return (
|
|
item_resource.id == item_id and
|
|
item_resource.amount < item_resource.max_stack_size
|
|
)
|
|
|
|
|
|
func _on_add_to_inventory(item_id: String, amount: int = 1) -> void:
|
|
var item_resource: DBItemResource = DBItems.data[item_id]
|
|
|
|
# The logic below is a mess and needs to be reworked
|
|
var amount_remaining: int = amount
|
|
while amount_remaining > 0:
|
|
var first_stack_index: int = inventory.find_custom(_find_first_stack.bind(item_id))
|
|
|
|
if first_stack_index == -1 and inventory.size() < max_inventory_items:
|
|
inventory.append(item_resource)
|
|
|
|
if amount_remaining <= item_resource.max_stack_size:
|
|
inventory[-1].amount += amount_remaining
|
|
amount_remaining = 0
|
|
else:
|
|
inventory[-1].amount = item_resource.max_stack_size
|
|
amount_remaining -= item_resource.max_stack_size
|
|
|
|
item_added.emit(item_id, amount - amount_remaining)
|
|
elif first_stack_index > -1:
|
|
var current_amount: int = inventory[first_stack_index].amount
|
|
var total_amount: int = current_amount + amount_remaining
|
|
if item_resource.max_stack_size - total_amount < 0:
|
|
inventory[first_stack_index].amount += amount_remaining
|
|
amount_remaining = 0
|
|
else:
|
|
inventory[first_stack_index].amount = item_resource.max_stack_size
|
|
amount_remaining -= item_resource.max_stack_size
|
|
|
|
item_added.emit(item_id, amount - amount_remaining)
|
|
|
|
func _on_remove_from_inventory(item_id: String, amount: int = 1) -> void:
|
|
if not inventory.has(item_id): return
|
|
|
|
#if inventory[item_id].amount > 0:
|
|
#inventory[item_id].amount -= amount
|
|
#item_removed.emit(item_id, max(0, amount))
|
|
|
|
func _on_item_dropped(item_id: String) -> void:
|
|
_on_remove_from_inventory(item_id, 1)
|
|
|
|
func _on_item_picked_up(item_id: String) -> void:
|
|
_on_add_to_inventory(item_id, 1)
|
|
|
|
func _on_quick_slot_item_changed(item_id: String) -> void:
|
|
quick_slot_item_id = item_id
|