title says all. next up: fixing the bugs of the texturerects in the inventory after equipping an item, it gets bigger because the created image is 128x128, but should be dynamic dependant on the viewport window size
140 lines
4 KiB
GDScript
140 lines
4 KiB
GDScript
extends Node
|
|
|
|
var stamina: float = 100.0
|
|
var flashlight_power: float = 100.0
|
|
var selected_item: int = 0
|
|
var prev_selected_item: int = 0
|
|
var stamina_recharge: bool = true
|
|
var inventory: Array[RigidBody3D] = [null,null]
|
|
|
|
@onready var ui: Control = %PlayerUi
|
|
@onready var flashlight: SpotLight3D = %Flashlight
|
|
@onready var selected_item_position: Marker3D = %SelectedItemPosition
|
|
|
|
var selected_item_object: RigidBody3D = null
|
|
|
|
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):
|
|
ui.select_item(index)
|
|
|
|
#selecting empty slot makes all inventory items invisible
|
|
if inventory[index] == null:
|
|
for c in selected_item_position.get_children():
|
|
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
|
|
|
|
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)
|
|
item.reparent(selected_item_position)
|
|
item.position = Vector3.ZERO
|
|
item.scale = Vector3(0.2, 0.2, 0.2)
|
|
item.collision_layer = 0
|
|
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)
|
|
item.reparent(selected_item_position)
|
|
item.position = Vector3.ZERO
|
|
item.scale = Vector3(0.2, 0.2, 0.2)
|
|
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 = 1
|
|
object.add_to_group("equippable")
|
|
|
|
for child in object.get_children():
|
|
if child is CollisionObject3D:
|
|
child.add_to_group("equippable")
|
|
|
|
remove_item(index)
|
|
|
|
func remove_item(index: int):
|
|
inventory[index] = null
|
|
ui.remove_item(index)
|
|
|
|
func get_selected_item() -> RigidBody3D:
|
|
return inventory[selected_item]
|
|
|
|
func stamina_recharge_off():
|
|
stamina_recharge = false
|
|
|
|
func _on_stamina_reset_timeout() -> void:
|
|
stamina_recharge = true
|
|
|
|
func _ready() -> void:
|
|
get_tree().create_timer(0.1).timeout.connect(func (): select_item(0))
|
|
|
|
#func _physics_process(_delta: float) -> void:
|
|
#handle equipped object
|
|
#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
|
|
|
|
func _process(_delta: float) -> void:
|
|
if stamina_recharge:
|
|
stamina = move_toward(stamina, 100, .5)
|
|
if flashlight.visible:
|
|
flashlight_power = move_toward(flashlight_power, 0, .2)
|