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
146 lines
5 KiB
GDScript
146 lines
5 KiB
GDScript
extends Node
|
|
|
|
@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: CharacterBody3D = $"../.."
|
|
@onready var procedural_camera_mover: Node = %ProceduralCameraMover
|
|
@onready var camera: Camera3D = %PlayerCamera
|
|
@onready var hold_position: Marker3D = %HoldPosition
|
|
@onready var player_manager: Node = %PlayerManager
|
|
@onready var ui: Control = %PlayerUi
|
|
|
|
|
|
var sensitivity: float = 0.002
|
|
|
|
var mouse_motion: Vector2 = Vector2.ZERO
|
|
var mouse_motion_delta: Vector2 = Vector2.ZERO
|
|
|
|
var held_object: RigidBody3D = null
|
|
|
|
func toggle_flashlight():
|
|
flashlight.visible = !flashlight.visible
|
|
sound_controller.play_flashlight_sound()
|
|
|
|
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 * PlayerConsts.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 * PlayerConsts.GRAB_ROTATION_POWER
|
|
|
|
func handle_joypad_movement(delta: float):
|
|
var controller_look_vector = Input.get_vector("look_left", "look_right", "look_up", "look_down") * 50
|
|
|
|
if controller_look_vector.length() > 0:
|
|
mouse_motion += controller_look_vector
|
|
mouse_motion_delta = controller_look_vector
|
|
|
|
func equip_object(object: RigidBody3D):
|
|
player_manager.add_item(object)
|
|
|
|
func check_raycast(raycast):
|
|
if raycast.is_colliding():
|
|
var hit_object = raycast.get_collider()
|
|
|
|
# THIS WILL REVEAL THE BUG:
|
|
print("Raycast hit: ", hit_object.name)
|
|
print("Is hit object equippable? ", hit_object.is_in_group("equippable"))
|
|
print("Is hit object's parent equippable? ", hit_object.get_parent().is_in_group("equippable"))
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if not player_state.in_menu:
|
|
if event is InputEventMouseMotion:
|
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
|
mouse_motion += event.relative
|
|
mouse_motion_delta = event.relative
|
|
|
|
func _process(delta: float) -> void:
|
|
if !Console.toggled:
|
|
handle_joypad_movement(delta)
|
|
|
|
mouse_motion.y = clamp(mouse_motion.y, -1560, 1560)
|
|
player.transform.basis = Basis.from_euler(Vector3(0, mouse_motion.x * -0.001 * PlayerConsts.LOOK_SENSITIVITY, 0))
|
|
camera.transform.basis = lerp(camera.transform.basis, Basis.from_euler(Vector3(mouse_motion.y * -0.001 * PlayerConsts.LOOK_SENSITIVITY, 0, 0)), delta * 10)
|
|
|
|
if not Console.toggled:
|
|
if Input.is_action_just_pressed("next_item"):
|
|
player_manager.next_item()
|
|
|
|
if Input.is_action_just_pressed("prev_item"):
|
|
player_manager.prev_item()
|
|
|
|
if Input.is_action_just_pressed("open_menu"):
|
|
ui_handler.toggle_menu()
|
|
|
|
if Input.is_action_just_pressed("select_item_1"):
|
|
player_manager.select_item(0)
|
|
|
|
if Input.is_action_just_pressed("select_item_2"):
|
|
player_manager.select_item(1)
|
|
|
|
if Input.is_action_just_pressed("select_item_3"):
|
|
player_manager.select_item(2)
|
|
|
|
if Input.is_action_just_pressed("select_item_4"):
|
|
player_manager.select_item(3)
|
|
|
|
if Input.is_action_just_pressed("select_item_5"):
|
|
player_manager.select_item(4)
|
|
|
|
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") or hit_object.is_in_group("equippable"):
|
|
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
|
|
|
|
if Input.is_action_just_pressed("equip"):
|
|
if cursor_raycast.is_colliding():
|
|
var hit_object = cursor_raycast.get_collider()
|
|
|
|
if hit_object is RigidBody3D and hit_object.is_in_group("equippable"):
|
|
equip_object(hit_object)
|
|
|
|
if Input.is_action_just_pressed("drop"):
|
|
player_manager.drop_item()
|
|
|
|
#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
|