2026-07-29 00:05:40 +02:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
|
|
@export_category("Grabbing")
|
|
|
|
|
@export var GRAB_POWER: float = 15.0 ##how fast the grabbed object snaps to the hold position
|
|
|
|
|
@export var GRAB_ROTATION_POWER: float = 15.0 ##how fast the object rotates towards the player
|
|
|
|
|
@export var GRAB_PANEL: PackedScene ##panel that goes over the grabbable object to show its grabbable
|
|
|
|
|
|
|
|
|
|
@onready var ui_handler: Node = %UiHandler
|
|
|
|
|
@onready var player_state: Node = %PlayerState
|
|
|
|
|
@onready var flashlight: SpotLight3D = %Flashlight
|
|
|
|
|
@onready var sound_controller: Node = %SoundController
|
|
|
|
|
@onready var cursor_raycast: RayCast3D = %CursorRaycast
|
|
|
|
|
@onready var popup_panels: Control = %PopupPanels
|
|
|
|
|
@onready var stamina_reset: Timer = %StaminaReset
|
|
|
|
|
@onready var player: PlayerCharacter = $"../.."
|
|
|
|
|
@onready var procedural_camera_mover: Node = %ProceduralCameraMover
|
|
|
|
|
@onready var camera: Camera3D = %PlayerCamera
|
|
|
|
|
@onready var hold_position: Marker3D = %HoldPosition
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var sensitivity: float = 0.002
|
|
|
|
|
|
2026-07-30 01:44:27 +02:00
|
|
|
var mouse_motion: Vector2 = Vector2.ZERO
|
|
|
|
|
var mouse_motion_delta: Vector2 = Vector2.ZERO
|
2026-07-29 00:05:40 +02:00
|
|
|
|
|
|
|
|
var held_object: RigidBody3D = null
|
|
|
|
|
|
|
|
|
|
func toggle_flashlight():
|
|
|
|
|
flashlight.visible = !flashlight.visible
|
|
|
|
|
sound_controller.play_flashlight_sound()
|
|
|
|
|
|
|
|
|
|
func _input(event):
|
|
|
|
|
if Input.is_action_just_pressed("open_menu"):
|
|
|
|
|
ui_handler.toggle_menu()
|
|
|
|
|
|
|
|
|
|
if not player_state.in_menu:
|
|
|
|
|
if event is InputEventMouseMotion:
|
|
|
|
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
2026-07-30 01:44:27 +02:00
|
|
|
mouse_motion += event.relative
|
|
|
|
|
mouse_motion_delta = event.relative
|
2026-07-29 00:05:40 +02:00
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("flashlight"):
|
|
|
|
|
toggle_flashlight()
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_released("jump") or Input.is_action_just_released("sprint"):
|
|
|
|
|
stamina_reset.start()
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("grab"):
|
|
|
|
|
if cursor_raycast.is_colliding():
|
|
|
|
|
var hit_object = cursor_raycast.get_collider()
|
|
|
|
|
|
|
|
|
|
if hit_object is RigidBody3D and hit_object.is_in_group("grabbable"):
|
|
|
|
|
held_object = hit_object
|
|
|
|
|
held_object.gravity_scale = 0.0
|
|
|
|
|
held_object.angular_damp = 5.0
|
|
|
|
|
popup_panels.visible = false
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_released("grab") and held_object != null:
|
|
|
|
|
held_object.gravity_scale = 1.0
|
|
|
|
|
held_object.angular_damp = 0.0
|
|
|
|
|
held_object = null
|
|
|
|
|
popup_panels.visible = true
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
|
#handle grabbed object
|
|
|
|
|
if held_object != null:
|
|
|
|
|
var target_pos = hold_position.global_position
|
|
|
|
|
var current_pos = held_object.global_position
|
|
|
|
|
var direction = target_pos - current_pos
|
|
|
|
|
|
|
|
|
|
held_object.linear_velocity = direction * GRAB_POWER
|
|
|
|
|
|
|
|
|
|
var current_quat = held_object.global_transform.basis.get_rotation_quaternion()
|
|
|
|
|
var target_quat = hold_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
|
|
|
|
|
|
|
|
|
|
held_object.angular_velocity = axis * angle * GRAB_ROTATION_POWER
|
|
|
|
|
|
|
|
|
|
func _process(delta: float) -> void:
|
2026-07-30 01:44:27 +02:00
|
|
|
mouse_motion.y = clamp(mouse_motion.y, -1560, 1560)
|
|
|
|
|
player.transform.basis = Basis.from_euler(Vector3(0, mouse_motion.x * -0.001 * procedural_camera_mover.LOOK_SENSITIVITY, 0))
|
|
|
|
|
camera.transform.basis = lerp(camera.transform.basis, Basis.from_euler(Vector3(mouse_motion.y * -0.001 * procedural_camera_mover.LOOK_SENSITIVITY, 0, 0)), delta * 10)
|
|
|
|
|
|
2026-07-29 00:05:40 +02:00
|
|
|
#func _unhandled_input(event: InputEvent) -> void:
|
|
|
|
|
#if event is InputEventMouseMotion:
|
|
|
|
|
#player_camera.rotate_y(-event.relative.x * sensitivity)
|
|
|
|
|
#player_camera.rotate_x(-event.relative.y * sensitivity)
|
|
|
|
|
#player_camera.rotation.x = clamp(player_camera.rotation.x, deg_to_rad(-90), deg_to_rad(90))
|
|
|
|
|
#player_camera.rotation.z = 0
|