255 lines
7 KiB
GDScript
255 lines
7 KiB
GDScript
class_name HotbarComponent extends Node
|
|
|
|
var selected_item: int = 0
|
|
var prev_selected_item: int = 0
|
|
var inventory: Array[ItemData] = [null, null]
|
|
|
|
@export var equipped_item_position: Marker3D
|
|
@export var hold_position: Marker3D
|
|
@export var cursor_raycast: RayCast3D
|
|
|
|
signal added_inventory_slot(successful: bool)
|
|
signal removed_inventory_slot(successful: bool)
|
|
|
|
# For UI elements
|
|
signal add_item(index: int, item: ItemData)
|
|
signal remove_item(index: int)
|
|
signal select_item(index: int)
|
|
|
|
var hand_cache: Dictionary = {}
|
|
|
|
|
|
func _select_item(index:int = selected_item) -> void:
|
|
# --- 1. HIDE ALL ITEMS IN CACHE TO BE SAFE ---
|
|
# Instead of relying on prev_selected_item (which can break during inventory swaps),
|
|
# we quickly hide everything in the cache. This guarantees no "ghost" items.
|
|
for key in hand_cache:
|
|
var cached_node = hand_cache[key]
|
|
if is_instance_valid(cached_node):
|
|
cached_node.hide()
|
|
cached_node.process_mode = Node.PROCESS_MODE_DISABLED
|
|
|
|
# Update the index
|
|
selected_item = index
|
|
var current_data = inventory[index]
|
|
|
|
# --- 2. SHOW OR INSTANTIATE THE NEW ITEM ---
|
|
if current_data != null and current_data.scene_path != "":
|
|
var item_path = current_data.scene_path
|
|
|
|
# Did we already instantiate this item type, AND is it still valid (not freed)?
|
|
if hand_cache.has(item_path) and is_instance_valid(hand_cache[item_path]):
|
|
var cached_item = hand_cache[item_path]
|
|
cached_item.show()
|
|
cached_item.process_mode = Node.PROCESS_MODE_INHERIT
|
|
|
|
else:
|
|
# Build it once.
|
|
var item_scene = load(item_path) as PackedScene
|
|
if item_scene:
|
|
var new_item = item_scene.instantiate()
|
|
equipped_item_position.add_child(new_item)
|
|
|
|
if new_item is RigidBody3D:
|
|
new_item.freeze = true
|
|
|
|
new_item.position = Vector3.ZERO
|
|
new_item.rotation = Vector3.ZERO
|
|
|
|
# Save it to the dictionary
|
|
hand_cache[item_path] = new_item
|
|
|
|
select_item.emit(index)
|
|
|
|
|
|
func update_slot(index: int, new_item: ItemData) -> void:
|
|
# 1. Clean up the physical 3D mesh of the old item being replaced/removed
|
|
var old_item = inventory[index]
|
|
if old_item != null and old_item.scene_path != "":
|
|
if hand_cache.has(old_item.scene_path):
|
|
var old_node = hand_cache[old_item.scene_path]
|
|
if is_instance_valid(old_node):
|
|
old_node.queue_free() # Free the 3D node from the equippable marker!
|
|
hand_cache.erase(old_item.scene_path)
|
|
|
|
# 2. Update the array
|
|
inventory[index] = new_item
|
|
|
|
# 3. Tell the in-game HUD that the slot changed
|
|
if new_item != null:
|
|
add_item.emit(index, new_item)
|
|
else:
|
|
remove_item.emit(index)
|
|
|
|
# 4. Instantly refresh the hand visual (whether we added a new item or emptied it)
|
|
_select_item(selected_item)
|
|
|
|
|
|
func _remove_item(index: int):
|
|
# Clean up the physical 3D mesh before clearing the array
|
|
var old_item = inventory[index]
|
|
if old_item != null and old_item.scene_path != "":
|
|
if hand_cache.has(old_item.scene_path):
|
|
var old_node = hand_cache[old_item.scene_path]
|
|
if is_instance_valid(old_node):
|
|
old_node.queue_free() # Free the 3D node!
|
|
hand_cache.erase(old_item.scene_path)
|
|
|
|
remove_item.emit(index)
|
|
inventory[index] = null
|
|
_select_item(selected_item) # Refresh the hand model
|
|
|
|
|
|
func use_active_item() -> void:
|
|
var current_data = inventory[selected_item]
|
|
|
|
if current_data == null:
|
|
return
|
|
|
|
if current_data.item_type == ItemData.ItemType.BACKPACK:
|
|
print("Equip Backpack via Hotbar!")
|
|
return
|
|
elif current_data.item_type == ItemData.ItemType.POCKET:
|
|
print("Equip Pocket via Hotbar!")
|
|
return
|
|
|
|
var item_path = current_data.scene_path
|
|
|
|
if item_path != "" and hand_cache.has(item_path):
|
|
var active_3d_item = hand_cache[item_path]
|
|
|
|
if is_instance_valid(active_3d_item) and active_3d_item is UsableItem:
|
|
active_3d_item.use()
|
|
update_slot(selected_item, null)
|
|
|
|
|
|
func add_inventory_slot() -> bool:
|
|
if len(inventory) < 5:
|
|
inventory.append(null)
|
|
added_inventory_slot.emit(true)
|
|
return true
|
|
added_inventory_slot.emit(false)
|
|
return false
|
|
|
|
|
|
func remove_inventory_slot() -> bool:
|
|
if len(inventory) > 2:
|
|
inventory.remove_at(len(inventory) - 1)
|
|
removed_inventory_slot.emit(true)
|
|
return true
|
|
removed_inventory_slot.emit(false)
|
|
return false
|
|
|
|
|
|
func next_item():
|
|
prev_selected_item = selected_item
|
|
if selected_item + 1 > len(inventory) - 1:
|
|
selected_item = 0
|
|
else:
|
|
selected_item += 1
|
|
_select_item()
|
|
|
|
|
|
func prev_item():
|
|
prev_selected_item = selected_item
|
|
if selected_item - 1 < 0:
|
|
selected_item = len(inventory) - 1
|
|
else:
|
|
selected_item -= 1
|
|
_select_item()
|
|
|
|
|
|
func _add_item(item: PickupableItem):
|
|
var target_index = -1
|
|
|
|
if inventory[selected_item] == null:
|
|
target_index = selected_item
|
|
else:
|
|
for i in range(len(inventory)):
|
|
if inventory[i] == null:
|
|
target_index = i
|
|
break
|
|
|
|
if target_index == -1:
|
|
print("Inventory Full!")
|
|
return
|
|
|
|
var new_item_data: ItemData = ItemData.new()
|
|
new_item_data.name = item.name
|
|
new_item_data.weight = item.weight
|
|
new_item_data.scene_path = item.scene_file_path
|
|
|
|
if item is Backpack:
|
|
new_item_data.item_type = ItemData.ItemType.BACKPACK
|
|
new_item_data.provided_slots = item.inventory_size
|
|
elif item is Pocket:
|
|
new_item_data.item_type = ItemData.ItemType.POCKET
|
|
elif item is UsableItem:
|
|
new_item_data.item_type = ItemData.ItemType.USABLE
|
|
else:
|
|
new_item_data.item_type = ItemData.ItemType.NORMAL
|
|
|
|
if item.mesh:
|
|
new_item_data.hand_mesh = item.mesh.mesh
|
|
|
|
new_item_data.ui_icon = await GrabbableBase.create_icon_from_mesh(item.mesh.mesh)
|
|
|
|
inventory[target_index] = new_item_data
|
|
add_item.emit(target_index, new_item_data)
|
|
|
|
item.queue_free()
|
|
_select_item(selected_item)
|
|
|
|
|
|
func _drop_item(index: int = selected_item):
|
|
var item_data = inventory[index]
|
|
|
|
if item_data != null and item_data.scene_path != "":
|
|
var packed_scene = load(item_data.scene_path) as PackedScene
|
|
if not packed_scene:
|
|
push_error("Could not load scene path: " + item_data.scene_path)
|
|
return
|
|
|
|
var new_object = packed_scene.instantiate() as RigidBody3D
|
|
get_tree().current_scene.add_child(new_object)
|
|
|
|
new_object.global_position = hold_position.global_position
|
|
new_object.global_rotation = Vector3.ZERO
|
|
|
|
_remove_item(index)
|
|
|
|
|
|
func get_selected_item() -> ItemData:
|
|
return inventory[selected_item]
|
|
|
|
|
|
func _ready() -> void:
|
|
get_tree().create_timer(0.1).timeout.connect(func(): _select_item(0))
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if Console.toggled or UiManager.in_game_menu.visible:
|
|
return
|
|
|
|
if Input.is_action_just_pressed("next_item"):
|
|
next_item()
|
|
if Input.is_action_just_pressed("prev_item"):
|
|
prev_item()
|
|
|
|
if Input.is_action_just_pressed("select_item_1"): _select_item(0)
|
|
if Input.is_action_just_pressed("select_item_2"): _select_item(1)
|
|
if Input.is_action_just_pressed("select_item_3"): _select_item(2)
|
|
if Input.is_action_just_pressed("select_item_4"): _select_item(3)
|
|
if Input.is_action_just_pressed("select_item_5"): _select_item(4)
|
|
|
|
if Input.is_action_just_pressed("use"):
|
|
if cursor_raycast.is_colliding():
|
|
var hit_object = cursor_raycast.get_collider()
|
|
if hit_object is PickupableItem:
|
|
_add_item(hit_object)
|
|
else:
|
|
if inventory[selected_item] != null:
|
|
use_active_item()
|
|
|
|
if Input.is_action_just_pressed("drop"):
|
|
_drop_item()
|