breadwinner/scenes/ui/inventory/inventory_menu.gd
Hellie-xd c5a907cec2 basic inventory system added
yeah i vibecoded it for now, it will be custom written later.
sooorrryyyy :(
2026-08-13 13:28:13 +02:00

354 lines
12 KiB
GDScript

extends Control
class_name InventoryMenu
@onready var backpack_grid: GridContainer = $PanelContainer/HBoxContainer/VBoxContainer2/BackpackGrid
@onready var hotbar_containers: Array[AspectRatioContainer] = [%Item1Container, %Item2Container, %Item3Container, %Item4Container, %Item5Container]
@onready var equipment_containers: Array[AspectRatioContainer] = [%EquipBackpack, %EquipPocket1, %EquipPocket2, %EquipPocket3]
var current_backpack: Backpack
var player_hotbar: HotbarComponent
var context_menu: PopupMenu
var active_context_slot: InventorySlotUI
# This tracks what is actually in the 4 equipment slots (Index 0 = Backpack, 1-3 = Pockets)
var equipped_items: Array[ItemData] = [null, null, null, null]
func _ready() -> void:
# 1. Create the PopupMenu dynamically so you don't have to place it in the editor
context_menu = PopupMenu.new()
add_child(context_menu)
# 2. Connect its click signal to our logic function
context_menu.id_pressed.connect(_on_context_menu_id_pressed)
func open_inventory(backpack: Backpack, hotbar_component: HotbarComponent) -> void:
current_backpack = backpack
player_hotbar = hotbar_component
update_all_ui()
show()
func update_all_ui() -> void:
update_equipment_ui()
update_hotbar_ui()
update_backpack_ui()
func update_equipment_ui() -> void:
for i in range(equipment_containers.size()):
var container = equipment_containers[i]
if container.get_children():
for child in container.get_children():
child.queue_free()
var slot_ui = _create_slot_ui(i, "equipment")
container.add_child(slot_ui)
func update_hotbar_ui() -> void:
if not player_hotbar: return
for i in range(hotbar_containers.size()):
var container = hotbar_containers[i]
for child in container.get_children():
child.queue_free()
if i < player_hotbar.inventory.size():
container.visible = true
var slot_ui = _create_slot_ui(i, "hotbar")
container.add_child(slot_ui)
else:
container.visible = false
func update_backpack_ui() -> void:
if not current_backpack: return
for child in backpack_grid.get_children():
child.queue_free()
for i in range(current_backpack.inventory_size):
var slot_ui = _create_slot_ui(i, "backpack")
backpack_grid.add_child(slot_ui)
func _create_slot_ui(index: int, type: String) -> Control:
var slot = InventorySlotUI.new()
slot.slot_index = index
slot.slot_type = type
slot.inventory_menu = self
slot.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
slot.mouse_filter = Control.MOUSE_FILTER_STOP
var item: ItemData = null
if type == "backpack": item = current_backpack.inventory[index]
elif type == "hotbar": item = player_hotbar.inventory[index]
elif type == "equipment": item = equipped_items[index]
slot.item_data = item
if item != null:
slot.texture = item.ui_icon
slot.tooltip_text = "Name: " + item.name + "\nWeight: " + str(item.weight) + "kg"
else:
if type == "equipment":
slot.tooltip_text = "Empty Backpack Slot" if index == 0 else "Empty Pocket Slot"
else:
slot.tooltip_text = "Empty Slot"
if type == "hotbar" or type == "equipment":
return slot # These already live inside AspectRatioContainers in your scene
var aspect_rect = AspectRatioContainer.new()
aspect_rect.size_flags_horizontal = Control.SIZE_EXPAND_FILL
aspect_rect.size_flags_vertical = Control.SIZE_EXPAND_FILL
aspect_rect.add_child(slot)
return aspect_rect
func swap_items(source_type: String, source_index: int, target_type: String, target_index: int) -> void:
if source_type == target_type and source_index == target_index:
return
var source_array = _get_array_by_type(source_type)
var target_array = _get_array_by_type(target_type)
var source_item = source_array[source_index]
var target_item = target_array[target_index]
# --- VALIDATION: Can this item be placed in this equipment slot? ---
if target_type == "equipment" and source_item != null:
if target_index == 0 and source_item.item_type != ItemData.ItemType.BACKPACK: return
if target_index > 0 and source_item.item_type != ItemData.ItemType.POCKET: return
if source_type == "equipment" and target_item != null:
if source_index == 0 and target_item.item_type != ItemData.ItemType.BACKPACK: return
if source_index > 0 and target_item.item_type != ItemData.ItemType.POCKET: return
# --- PERFORM SWAP ---
if source_type == "hotbar": player_hotbar.update_slot(source_index, target_item)
else: source_array[source_index] = target_item
if target_type == "hotbar": player_hotbar.update_slot(target_index, source_item)
else: target_array[target_index] = source_item
# --- APPLY DYNAMIC RESIZING ---
_recalculate_equipment_stats()
update_all_ui()
# Helper function to get the correct array
func _get_array_by_type(type: String) -> Array[ItemData]:
if type == "backpack": return current_backpack.inventory
if type == "hotbar": return player_hotbar.inventory
return equipped_items
# This is where the magic happens!
func _recalculate_equipment_stats() -> void:
# --- 1. UPDATE BASE INVENTORY (BACKPACK) ---
var target_inventory_size = 5 # The base size without any bag equipped!
if equipped_items[0] != null:
target_inventory_size += equipped_items[0].provided_slots
# If the inventory is shrinking (we removed a bag), we MUST drop overflow items!
if current_backpack.inventory.size() > target_inventory_size:
# Loop from the new size boundary to the end of the old array
for i in range(target_inventory_size, current_backpack.inventory.size()):
var overflow_item = current_backpack.inventory[i]
if overflow_item != null:
_spawn_item_in_world(overflow_item)
# Apply the new size and resize the actual array
# (.resize() automatically deletes overflow data and pads with nulls if expanding)
current_backpack.inventory_size = target_inventory_size
current_backpack.inventory.resize(target_inventory_size)
# --- 2. UPDATE HOTBAR SIZE (POCKETS) ---
# --- 2. UPDATE HOTBAR SIZE (POCKETS) ---
var target_hotbar_size = 2 # The base hotbar size
# Loop through the 3 pocket slots (indices 1, 2, and 3)
for i in range(1, 4):
if equipped_items[i] != null:
# Automatically increase hotbar size by exactly 1 for each equipped pocket
target_hotbar_size += 1
# Tell the HotbarComponent to grow to match the new size
while player_hotbar.inventory.size() < target_hotbar_size:
player_hotbar.add_inventory_slot()
# Tell the HotbarComponent to shrink if we removed pockets
while player_hotbar.inventory.size() > target_hotbar_size:
# Drop the item in the last slot before deleting the slot
if player_hotbar.inventory.back() != null:
player_hotbar._drop_item(player_hotbar.inventory.size() - 1)
player_hotbar.remove_inventory_slot()
func show_context_menu(slot: InventorySlotUI, mouse_pos: Vector2) -> void:
active_context_slot = slot
context_menu.clear()
var item = slot.item_data
if item == null: return
# Option 1: Equip / Unequip (ID: 0 or 1)
if item.item_type == ItemData.ItemType.BACKPACK or item.item_type == ItemData.ItemType.POCKET:
if slot.slot_type == "equipment":
context_menu.add_item("Unequip", 0)
else:
context_menu.add_item("Equip", 1)
# Option 2: Drop (ID: 2)
context_menu.add_item("Drop", 2)
# Option 3: Info (ID: 3)
context_menu.add_item("Info", 3)
# Show the menu exactly where the mouse is!
context_menu.position = mouse_pos
context_menu.popup()
func _on_context_menu_id_pressed(id: int) -> void:
if not active_context_slot or not active_context_slot.item_data: return
var slot = active_context_slot
match id:
0: _auto_unequip(slot)
1: _auto_equip(slot)
2: _drop_from_menu(slot)
3: _show_info(slot.item_data)
func _auto_equip(slot: InventorySlotUI) -> void:
var item = slot.item_data
var target_index = -1
# Determine which equipment slot to put it in
if item.item_type == ItemData.ItemType.BACKPACK:
target_index = 0
elif item.item_type == ItemData.ItemType.POCKET:
# Find the first empty pocket slot (Indices 1 to 3)
for i in range(1, 4):
if equipped_items[i] == null:
target_index = i
break
# If all pockets are full, just swap with pocket 1
if target_index == -1: target_index = 1
if target_index != -1:
swap_items(slot.slot_type, slot.slot_index, "equipment", target_index)
func _auto_unequip(slot: InventorySlotUI) -> void:
var target_index = -1
# If we are unequipping the Backpack (Equipment Slot 0)
if slot.slot_index == 0:
# Search the Hotbar for an empty spot
for i in range(player_hotbar.inventory.size()):
if player_hotbar.inventory[i] == null:
target_index = i
break
if target_index != -1:
swap_items(slot.slot_type, slot.slot_index, "hotbar", target_index)
else:
print("Hotbar is full! Dropping the Backpack.")
_drop_from_menu(slot)
# If we are unequipping a Pocket (Equipment Slots 1-3)
else:
# Search the Main Inventory for an empty spot
for i in range(current_backpack.inventory_size):
if current_backpack.inventory[i] == null:
target_index = i
break
if target_index != -1:
swap_items(slot.slot_type, slot.slot_index, "backpack", target_index)
else:
print("Inventory is full! Dropping the Pocket.")
_drop_from_menu(slot)
func _drop_from_menu(slot: InventorySlotUI) -> void:
# 1. Spawn the item on the ground
_spawn_item_in_world(slot.item_data)
# 2. Clear the item from whichever array it was sitting in
var array = _get_array_by_type(slot.slot_type)
if slot.slot_type == "hotbar":
player_hotbar.update_slot(slot.slot_index, null)
else:
array[slot.slot_index] = null
# 3. Redraw UI and recalculate sizes
_recalculate_equipment_stats()
update_all_ui()
func _show_info(item: ItemData) -> void:
# Creates a quick native popup window to show item details
var dialog = AcceptDialog.new()
dialog.title = "Item Info"
dialog.dialog_text = "Name: %s\nWeight: %s kg\nType: %s" % [
item.name,
item.weight,
ItemData.ItemType.keys()[item.item_type]
]
add_child(dialog)
dialog.popup_centered()
func _spawn_item_in_world(item: ItemData) -> void:
if item and item.scene_path != "":
var packed_scene = load(item.scene_path) as PackedScene
if packed_scene:
var new_object = packed_scene.instantiate() as RigidBody3D
get_tree().current_scene.add_child(new_object)
# Drop it at the player's hold position
new_object.global_position = player_hotbar.hold_position.global_position
new_object.global_rotation = Vector3.ZERO
func quick_move_item(slot: InventorySlotUI) -> void:
var item = slot.item_data
if item == null: return
# 1. Quick Unequip (If it's already in an equipment slot)
if slot.slot_type == "equipment":
_auto_unequip(slot)
return
# 2. Try Quick Equip for Backpacks
if item.item_type == ItemData.ItemType.BACKPACK:
# Only quick-equip if the backpack slot is empty
if equipped_items[0] == null:
swap_items(slot.slot_type, slot.slot_index, "equipment", 0)
return
# 3. Try Quick Equip for Pockets
if item.item_type == ItemData.ItemType.POCKET:
for i in range(1, 4):
# Find the first empty pocket slot and equip it
if equipped_items[i] == null:
swap_items(slot.slot_type, slot.slot_index, "equipment", i)
return
# 4. REGULAR ITEM MOVE (Or if equipment slots were full)
if slot.slot_type == "hotbar":
# Move Hotbar -> Backpack
var target_index = -1
for i in range(current_backpack.inventory_size):
if current_backpack.inventory[i] == null:
target_index = i
break
if target_index != -1:
swap_items("hotbar", slot.slot_index, "backpack", target_index)
else:
print("Backpack is full! Cannot quick-move.")
elif slot.slot_type == "backpack":
# Move Backpack -> Hotbar
var target_index = -1
for i in range(player_hotbar.inventory.size()):
if player_hotbar.inventory[i] == null:
target_index = i
break
if target_index != -1:
swap_items("backpack", slot.slot_index, "hotbar", target_index)
else:
print("Hotbar is full! Cannot quick-move.")