71 lines
2.3 KiB
GDScript
71 lines
2.3 KiB
GDScript
# Based on Godot Engine Third Person Shooter Demo, used under MIT License
|
|
# Copyright (c) 2018-2021 Juan Linietsky, Godot Engine contributors
|
|
# https://github.com/godotengine/tps-demo/blob/master/player/player_input.gd
|
|
|
|
extends Node3D
|
|
|
|
const CAMERA_MOUSE_ROTATION_SPEED := 0.001
|
|
const CAMERA_X_ROT_MIN := deg_to_rad(-85)
|
|
const CAMERA_X_ROT_MAX := deg_to_rad(70)
|
|
|
|
@export var use_mouse: bool = true
|
|
@export var pov_move_speed = 0.05
|
|
|
|
var active: bool = false
|
|
var initial_rotation: Vector3
|
|
var initial_pov_position: Vector3
|
|
|
|
@onready var camera_base = $"."
|
|
@onready var camera = $FallbackCamera
|
|
|
|
func _ready() -> void:
|
|
set_process(false)
|
|
set_process_input(false)
|
|
initial_rotation = camera.rotation
|
|
initial_pov_position = position
|
|
|
|
func _on_aircraft_use_fallback(active_in: bool) -> void:
|
|
if active_in:
|
|
set_process(true)
|
|
set_process_input(true)
|
|
camera.make_current()
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
else:
|
|
set_process(false)
|
|
set_process_input(false)
|
|
camera.current = false
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) # default value
|
|
|
|
# Reset the rotation
|
|
camera_base.rotation = Vector3.ZERO
|
|
camera.rotation = initial_rotation
|
|
|
|
active = active_in
|
|
|
|
|
|
func _input(event):
|
|
# Make mouse aiming speed resolution-independent
|
|
# (required when using the `canvas_items` stretch mode).
|
|
var scale_factor: float = min(
|
|
(float(get_viewport().size.x) / get_viewport().get_visible_rect().size.x),
|
|
(float(get_viewport().size.y) / get_viewport().get_visible_rect().size.y)
|
|
)
|
|
|
|
if use_mouse and event is InputEventMouseMotion:
|
|
var camera_speed_this_frame = CAMERA_MOUSE_ROTATION_SPEED
|
|
rotate_camera(event.relative * camera_speed_this_frame * scale_factor)
|
|
|
|
if event.is_action_pressed("pov_reset"):
|
|
position = initial_pov_position
|
|
|
|
func _process(delta: float):
|
|
var horizontal = Input.get_vector("pov_left", "pov_right", "pov_forward", "pov_back")
|
|
var vertical = Input.get_axis("pov_down", "pov_up")
|
|
# Currently PilotEyes has Yaw 180, so horizontal axes need to be inverted
|
|
position += delta * pov_move_speed * Vector3(-horizontal.x, vertical, -horizontal.y)
|
|
|
|
func rotate_camera(move):
|
|
camera_base.rotate_y(-move.x)
|
|
# After relative transforms, camera needs to be renormalized.
|
|
camera_base.orthonormalize()
|
|
camera.rotation.x = clamp(camera.rotation.x - move.y, CAMERA_X_ROT_MIN, CAMERA_X_ROT_MAX)
|