128 lines
4.8 KiB
GDScript
128 lines
4.8 KiB
GDScript
extends RigidBody
|
|
|
|
export (bool) var active: bool = true setget set_active
|
|
export (float, 0, 10) var max_speed: float = 3
|
|
export (float, 0, 10) var max_acceleration: float = 5
|
|
export (float, 0, 20) var damp: float = 10
|
|
export (float, 0.1, 1) var mouse_jerk:float = 0.5
|
|
export (float,10, 100) var gamepad_response = 50
|
|
export (float, 50, 500) var mouse_jerk_rejection = 200
|
|
export (float) var max_angle = 75
|
|
|
|
export (float) var camera_jitter_speed:float = 3
|
|
export (Vector3) var angular_jitter:Vector3 = Vector3(0.1, 0, 0.05)
|
|
export (Vector3) var angular_jitter_speed:Vector3 = Vector3(2, 1, 1)
|
|
export (Vector3) var location_jitter:Vector3 = Vector3(0.1, 0.1, 0.1)
|
|
export (Vector3) var location_jitter_speed:Vector3 = Vector3(3, 0.3, 1)
|
|
|
|
var loc_noise_spot: Vector3 = Vector3.ZERO
|
|
var rot_noise_spot: Vector3 = Vector3.ZERO
|
|
|
|
var noise:OpenSimplexNoise = OpenSimplexNoise.new()
|
|
var rotation_speed: Vector2 = Vector2.ZERO
|
|
var current_mouse_rotation: Vector2 = Vector2.ZERO
|
|
var mouse_sensitivity: Vector2 = Vector2(3, 3)
|
|
|
|
var crouched:bool = false
|
|
|
|
onready var yaw:Spatial = $Yaw
|
|
onready var pitch:Spatial = $Yaw/Pitch
|
|
onready var mount:Spatial = $Yaw/Pitch/Mount
|
|
onready var camera:Camera = $Yaw/Pitch/Mount/Camera
|
|
|
|
|
|
func set_active(activate):
|
|
if !is_inside_tree(): return
|
|
if activate:
|
|
camera.make_current()
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
sleeping = active
|
|
|
|
func _ready():
|
|
|
|
if active:
|
|
set_active(active)
|
|
|
|
func _physics_process(delta:float):
|
|
_handle_movement(delta)
|
|
_handle_rotation(delta)
|
|
_handle_jitter(delta)
|
|
|
|
func _handle_movement(delta:float):
|
|
var input:Vector2 = Vector2(Input.get_action_strength("player_right") - Input.get_action_strength("player_left"),
|
|
Input.get_action_strength("player_backwards")*0.8 - Input.get_action_strength("player_forwards"))
|
|
|
|
if input.length()>1:
|
|
input = input.normalized()
|
|
|
|
var direction: Vector3 = Vector3(input.x, 0, input.y)
|
|
|
|
direction = yaw.global_transform.basis.x * direction.x + transform.basis.y * direction.y + yaw.global_transform.basis.z * direction.z
|
|
|
|
if linear_velocity.length() > (linear_velocity + (direction*max_speed - linear_velocity)).length():
|
|
direction = Vector3.ZERO
|
|
else:
|
|
direction *= (direction*max_speed - linear_velocity).length()*max_acceleration
|
|
|
|
linear_damp = damp * max(0.5, 1 - input.length())
|
|
|
|
apply_central_impulse(direction*delta)
|
|
|
|
func _handle_rotation(delta:float):
|
|
var smoothness = 60/Engine.get_frames_per_second()
|
|
|
|
var input_speed = Vector2( Input.get_action_strength("look_right")-Input.get_action_strength("look_left"), Input.get_action_strength("look_down")-Input.get_action_strength("look_up")) * gamepad_response
|
|
|
|
if current_mouse_rotation.length()>0:
|
|
input_speed = current_mouse_rotation
|
|
current_mouse_rotation = Vector2.ZERO
|
|
|
|
rotation_speed = rotation_speed * (1-mouse_jerk*smoothness) + input_speed * mouse_jerk * smoothness
|
|
|
|
if rotation_speed.y > 0 and pitch.rotation_degrees.x < 0:
|
|
rotation_speed.y *= 1-pow(pitch.rotation_degrees.x/-max_angle, 4)
|
|
elif rotation_speed.y < 0 and pitch.rotation_degrees.x > 0 :
|
|
rotation_speed.y *= 1-pow(pitch.rotation_degrees.x/max_angle, 4)
|
|
|
|
yaw.rotate_y(deg2rad(-rotation_speed.x * delta * mouse_sensitivity.x))
|
|
pitch.rotate_x(deg2rad(-rotation_speed.y * delta * mouse_sensitivity.y))
|
|
|
|
func _handle_jitter(delta):
|
|
loc_noise_spot += Vector3(delta * camera_jitter_speed * location_jitter_speed)
|
|
rot_noise_spot += Vector3(delta * camera_jitter_speed * angular_jitter_speed)
|
|
pitch.translation = Vector3(
|
|
noise.get_noise_1d(loc_noise_spot.x),
|
|
noise.get_noise_1d(loc_noise_spot.y),
|
|
noise.get_noise_1d(loc_noise_spot.z)
|
|
) * location_jitter
|
|
|
|
mount.rotation = Vector3(
|
|
noise.get_noise_1d(rot_noise_spot.x),
|
|
noise.get_noise_1d(rot_noise_spot.y),
|
|
noise.get_noise_1d(rot_noise_spot.z)
|
|
) * angular_jitter
|
|
|
|
|
|
func _handle_mouse_input(event:InputEventMouseMotion):
|
|
if event.relative.length() < mouse_jerk_rejection:
|
|
current_mouse_rotation = event.relative
|
|
|
|
func _unhandled_input(event:InputEvent):
|
|
if active:
|
|
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
|
_handle_mouse_input(event)
|
|
|
|
|
|
func _on_bed_enter(body):
|
|
if not crouched:
|
|
$PlayerAnimationPlayer.queue("crouch")
|
|
max_speed *= .5
|
|
crouched = true
|
|
|
|
func _on_bed_exited(body):
|
|
if crouched:
|
|
crouched = false
|
|
$PlayerAnimationPlayer.queue("stand_up")
|
|
yield($PlayerAnimationPlayer, "animation_finished")
|
|
max_speed *= 2
|