2026-08-09 03:24:28 +02:00
|
|
|
class_name InventoryComponent extends Node
|
|
|
|
|
|
|
|
|
|
var selected_item: int = 0
|
|
|
|
|
var prev_selected_item: int = 0
|
|
|
|
|
var selected_item_object: EquippableItem = null
|
|
|
|
|
var inventory: Array[EquippableItem] = [null,null]
|
|
|
|
|
|
|
|
|
|
@export var equipped_item_mesh: MeshInstance3D
|
|
|
|
|
@export var hold_position: Marker3D
|
|
|
|
|
@export var cursor_raycast: RayCast3D
|
|
|
|
|
|
2026-08-12 01:47:15 +02:00
|
|
|
signal added_inventory_slot(successful:bool)
|
|
|
|
|
signal removed_inventory_slot(successful:bool)
|
2026-08-09 03:24:28 +02:00
|
|
|
signal select_item(index: int)
|
|
|
|
|
|
|
|
|
|
#for ui elements
|
|
|
|
|
signal add_item(index: int, item: EquippableItem)
|
2026-08-09 23:18:13 +02:00
|
|
|
signal remove_item(index: int)
|
2026-08-09 03:24:28 +02:00
|
|
|
|
2026-08-12 01:47:15 +02:00
|
|
|
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
|
|
|
|
|
added_inventory_slot.emit(false)
|
|
|
|
|
return false
|
|
|
|
|
|
2026-08-09 03:24:28 +02:00
|
|
|
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 _select_item(index:int = selected_item):
|
|
|
|
|
if index >= 0 and index < len(inventory):
|
|
|
|
|
select_item.emit(index)
|
|
|
|
|
|
|
|
|
|
if inventory[index] == null:
|
|
|
|
|
#if current index is empty in the inventory, replace held object with placeholder (empty mesh)
|
|
|
|
|
equipped_item_mesh.mesh = PlaceholderMesh.new()
|
|
|
|
|
else:
|
|
|
|
|
var mesh = null
|
|
|
|
|
for i in inventory[index].get_children():
|
|
|
|
|
if i is MeshInstance3D:
|
|
|
|
|
equipped_item_mesh.mesh = i.mesh
|
|
|
|
|
|
|
|
|
|
func _add_item(item: EquippableItem):
|
|
|
|
|
#add item to current equipped slot if empty
|
|
|
|
|
if inventory[selected_item] == null:
|
|
|
|
|
inventory[selected_item] = item
|
|
|
|
|
|
|
|
|
|
add_item.emit(selected_item, item)
|
|
|
|
|
|
|
|
|
|
#ui.add_item(selected_item, item)
|
|
|
|
|
item.reparent(get_parent().get_parent())
|
|
|
|
|
item.collision_layer = 0
|
|
|
|
|
item.visible = false
|
|
|
|
|
_select_item(selected_item)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
#add item to first empty slot if currently equipped slot is full
|
|
|
|
|
for i in range(0, len(inventory)):
|
|
|
|
|
if inventory[i] == null:
|
|
|
|
|
inventory[i] = item
|
|
|
|
|
|
|
|
|
|
add_item.emit(i, item)
|
|
|
|
|
#ui.add_item(i, item)
|
|
|
|
|
item.reparent(get_parent().get_parent())
|
|
|
|
|
item.collision_layer = 0
|
|
|
|
|
item.visible = false
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
#dont equip item if inventory full
|
|
|
|
|
|
|
|
|
|
func _drop_item(index: int = selected_item):
|
|
|
|
|
if inventory[index] != null:
|
|
|
|
|
var object = inventory[index]
|
|
|
|
|
|
|
|
|
|
object.scale = Vector3.ONE
|
|
|
|
|
|
|
|
|
|
if object.is_inside_tree():
|
|
|
|
|
object.reparent(get_tree().current_scene, true)
|
|
|
|
|
|
|
|
|
|
object.collision_layer = 17
|
|
|
|
|
object.global_position = hold_position.global_position
|
|
|
|
|
object.visible = true
|
|
|
|
|
object.global_rotation = Vector3.ZERO
|
|
|
|
|
|
|
|
|
|
_remove_item(index)
|
|
|
|
|
|
|
|
|
|
func _remove_item(index: int):
|
|
|
|
|
#ui.remove_item(index)
|
|
|
|
|
remove_item.emit(index)
|
|
|
|
|
inventory[index] = null
|
|
|
|
|
_select_item(index)
|
|
|
|
|
|
|
|
|
|
func get_selected_item() -> EquippableItem:
|
|
|
|
|
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 not Console.toggled and not UiManager.in_game_menu.visible:
|
|
|
|
|
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("equip"):
|
|
|
|
|
if cursor_raycast.is_colliding():
|
|
|
|
|
var hit_object = cursor_raycast.get_collider()
|
|
|
|
|
|
|
|
|
|
if hit_object is EquippableItem:
|
|
|
|
|
_add_item(hit_object)
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("drop"):
|
|
|
|
|
_drop_item()
|