2026-07-31 02:38:02 +02:00
|
|
|
extends Node
|
2026-07-29 00:05:40 +02:00
|
|
|
|
2026-07-31 02:38:02 +02:00
|
|
|
var stamina: float = 100.0
|
|
|
|
|
var flashlight_power: float = 100.0
|
|
|
|
|
var selected_item: int = 0
|
2026-08-05 23:37:19 +02:00
|
|
|
var prev_selected_item: int = 0
|
2026-07-31 02:38:02 +02:00
|
|
|
var stamina_recharge: bool = true
|
2026-08-05 23:37:19 +02:00
|
|
|
var inventory: Array[RigidBody3D] = [null,null]
|
2026-07-31 02:38:02 +02:00
|
|
|
|
|
|
|
|
@onready var ui: Control = %PlayerUi
|
|
|
|
|
@onready var flashlight: SpotLight3D = %Flashlight
|
2026-08-07 12:02:32 +02:00
|
|
|
@onready var equipped_item_mesh: MeshInstance3D = %EquippedItemMesh
|
2026-08-07 12:29:40 +02:00
|
|
|
@onready var hold_position: Marker3D = %HoldPosition
|
2026-08-05 23:37:19 +02:00
|
|
|
|
|
|
|
|
var selected_item_object: RigidBody3D = null
|
2026-07-31 02:38:02 +02:00
|
|
|
|
|
|
|
|
func next_item():
|
2026-08-05 23:37:19 +02:00
|
|
|
prev_selected_item = selected_item
|
2026-07-31 02:38:02 +02:00
|
|
|
if selected_item + 1 > len(inventory) - 1:
|
|
|
|
|
selected_item = 0
|
|
|
|
|
else:
|
|
|
|
|
selected_item += 1
|
2026-08-05 23:37:19 +02:00
|
|
|
select_item()
|
2026-07-31 02:38:02 +02:00
|
|
|
|
|
|
|
|
func prev_item():
|
2026-08-05 23:37:19 +02:00
|
|
|
prev_selected_item = selected_item
|
2026-07-31 02:38:02 +02:00
|
|
|
if selected_item - 1 < 0:
|
|
|
|
|
selected_item = len(inventory) - 1
|
|
|
|
|
else:
|
|
|
|
|
selected_item -= 1
|
2026-08-05 23:37:19 +02:00
|
|
|
select_item()
|
|
|
|
|
|
|
|
|
|
func select_item(index:int = selected_item):
|
|
|
|
|
if index >= 0 and index < len(inventory):
|
|
|
|
|
ui.select_item(index)
|
|
|
|
|
|
|
|
|
|
if inventory[index] == null:
|
2026-08-07 12:02:32 +02:00
|
|
|
#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
|
|
|
|
|
|
2026-08-05 23:37:19 +02:00
|
|
|
|
2026-08-07 12:02:32 +02:00
|
|
|
#selecting empty slot makes all inventory items invisible
|
|
|
|
|
#if inventory[index] == null:
|
|
|
|
|
#for c in inventory:
|
|
|
|
|
#c.visible = false
|
|
|
|
|
#return
|
|
|
|
|
#
|
|
|
|
|
#else:
|
|
|
|
|
##deselect item --> hide currently equipped item object
|
|
|
|
|
##show newly selected item, since its not null because of the else statement it shouldnt crash
|
|
|
|
|
##if called from outside, sets the selected item to the index called from the argument
|
|
|
|
|
#if index != selected_item:
|
|
|
|
|
#prev_selected_item = selected_item
|
|
|
|
|
#if inventory[prev_selected_item] != null:
|
|
|
|
|
#inventory[prev_selected_item].visible = false
|
|
|
|
|
#else:
|
|
|
|
|
#if inventory[prev_selected_item] != null:
|
|
|
|
|
#inventory[prev_selected_item].visible = false
|
|
|
|
|
#selected_item = index
|
|
|
|
|
#inventory[index].visible = true
|
2026-08-05 23:37:19 +02:00
|
|
|
|
|
|
|
|
func add_item(item: RigidBody3D):
|
|
|
|
|
#add item to current equipped slot if empty
|
|
|
|
|
if inventory[selected_item] == null:
|
|
|
|
|
inventory[selected_item] = item
|
|
|
|
|
ui.add_item(selected_item, item)
|
2026-08-07 12:02:32 +02:00
|
|
|
item.reparent(get_parent().get_parent())
|
2026-08-05 23:37:19 +02:00
|
|
|
item.collision_layer = 0
|
2026-08-07 12:02:32 +02:00
|
|
|
item.visible = false
|
|
|
|
|
select_item(selected_item)
|
2026-08-05 23:37:19 +02:00
|
|
|
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
|
|
|
|
|
ui.add_item(i, item)
|
2026-08-07 12:02:32 +02:00
|
|
|
item.reparent(get_parent().get_parent())
|
2026-08-05 23:37:19 +02:00
|
|
|
item.collision_layer = 0
|
|
|
|
|
item.visible = false
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
#dont equip item if inventory full
|
2026-07-31 02:38:02 +02:00
|
|
|
|
2026-08-06 01:00:09 +02:00
|
|
|
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)
|
|
|
|
|
|
2026-08-06 18:32:01 +02:00
|
|
|
object.collision_layer = 17
|
2026-08-06 01:00:09 +02:00
|
|
|
object.add_to_group("equippable")
|
2026-08-07 12:29:40 +02:00
|
|
|
object.global_position = hold_position.global_position
|
|
|
|
|
object.visible = true
|
2026-08-06 01:00:09 +02:00
|
|
|
|
|
|
|
|
for child in object.get_children():
|
|
|
|
|
if child is CollisionObject3D:
|
|
|
|
|
child.add_to_group("equippable")
|
|
|
|
|
|
|
|
|
|
remove_item(index)
|
2026-07-31 02:38:02 +02:00
|
|
|
|
2026-08-05 23:37:19 +02:00
|
|
|
func remove_item(index: int):
|
|
|
|
|
inventory[index] = null
|
2026-08-07 12:29:40 +02:00
|
|
|
select_item(index)
|
2026-08-05 23:37:19 +02:00
|
|
|
ui.remove_item(index)
|
|
|
|
|
|
|
|
|
|
func get_selected_item() -> RigidBody3D:
|
2026-07-31 02:38:02 +02:00
|
|
|
return inventory[selected_item]
|
|
|
|
|
|
|
|
|
|
func stamina_recharge_off():
|
|
|
|
|
stamina_recharge = false
|
|
|
|
|
|
|
|
|
|
func _on_stamina_reset_timeout() -> void:
|
|
|
|
|
stamina_recharge = true
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2026-08-02 16:19:24 +02:00
|
|
|
get_tree().create_timer(0.1).timeout.connect(func (): select_item(0))
|
2026-07-31 02:38:02 +02:00
|
|
|
|
2026-08-05 23:37:19 +02:00
|
|
|
#func _physics_process(_delta: float) -> void:
|
2026-08-07 12:02:32 +02:00
|
|
|
##handle equipped object
|
2026-08-05 23:37:19 +02:00
|
|
|
#if selected_item_object != null:
|
|
|
|
|
#var target_pos = selected_item_position.global_position
|
|
|
|
|
#var current_pos = selected_item_object.global_position
|
|
|
|
|
#var direction = target_pos - current_pos
|
|
|
|
|
#
|
|
|
|
|
#selected_item_object.linear_velocity = direction * PlayerConsts.GRAB_POWER
|
|
|
|
|
#
|
|
|
|
|
#var current_quat = selected_item_object.global_transform.basis.get_rotation_quaternion()
|
|
|
|
|
#var target_quat = selected_item_position.global_transform.basis.get_rotation_quaternion()
|
|
|
|
|
#
|
|
|
|
|
#var delta_quat = target_quat * current_quat.inverse()
|
|
|
|
|
#var angle = delta_quat.get_angle()
|
|
|
|
|
#var axis = delta_quat.get_axis()
|
|
|
|
|
#
|
|
|
|
|
#if angle > PI:
|
|
|
|
|
#angle -= TAU
|
|
|
|
|
#
|
|
|
|
|
#selected_item_object.angular_velocity = axis * angle * PlayerConsts.GRAB_ROTATION_POWER
|
|
|
|
|
|
2026-08-02 16:19:24 +02:00
|
|
|
func _process(_delta: float) -> void:
|
2026-07-31 02:38:02 +02:00
|
|
|
if stamina_recharge:
|
|
|
|
|
stamina = move_toward(stamina, 100, .5)
|
|
|
|
|
if flashlight.visible:
|
|
|
|
|
flashlight_power = move_toward(flashlight_power, 0, .2)
|