122 lines
4.4 KiB
GDScript
122 lines
4.4 KiB
GDScript
extends RigidBody3D
|
|
|
|
|
|
var airtime = -1.0
|
|
var mouse_input = Vector2(0,0)
|
|
var raw_mouse_input = Vector2(0,0)
|
|
var alpha = 10
|
|
@export var allow_jump = true
|
|
@export var allow_jump_hold = true
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
#Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
pass
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta):
|
|
|
|
# Makes sure everything is sane
|
|
$grounded.global_position = global_position
|
|
$grounded.target_position = Globals.global_down * 1.1
|
|
$grounded_override.global_position = global_position
|
|
$grounded_override.target_position = Globals.global_down * 2
|
|
$level.position = global_position
|
|
|
|
|
|
# Gravity!
|
|
apply_central_force(Globals.global_down * 9.8 * 100 * delta)
|
|
|
|
# Sets up the input variables
|
|
# Cursor input
|
|
raw_mouse_input = Input.get_last_mouse_velocity() * delta * Globals.sensitivity
|
|
# Zoom in/out
|
|
var scroll_input = 0.5 * int(Input.is_action_just_released("out")) - int(Input.is_action_just_released("in"))
|
|
# WASD
|
|
var move_input := Vector3.ZERO
|
|
|
|
# Filters the mouse input using exponential interpolation
|
|
mouse_input.x = mouse_input.x * exp(-alpha * delta) + raw_mouse_input.x * (1.0 - exp(-alpha * delta))
|
|
mouse_input.y = mouse_input.y * exp(-alpha * delta * 3) + raw_mouse_input.y * (1.0 - exp(-alpha * delta * 3))
|
|
|
|
# Assigns the controls input
|
|
move_input.x = Input.get_axis("forward", "backwards")
|
|
move_input.y = -Input.get_axis("right", "left")
|
|
move_input = move_input.normalized()
|
|
|
|
# Funky FOV thing
|
|
if (!Globals.next && !Globals.restart):
|
|
# Not doing anything special? Track FOV
|
|
$level/gimbal_y/gimbal_x/stick/camera.fov = $level/gimbal_y/gimbal_x/stick/camera.fov * exp(-0.5 * delta) + Globals.fov * (1.0 - exp(-0.5 * delta))
|
|
else:
|
|
# Either skipping / restarting : explode FOV
|
|
$level/gimbal_y/gimbal_x/stick/camera.fov = $level/gimbal_y/gimbal_x/stick/camera.fov * exp(-7 * delta) + 179 * (1.0 - exp(-7 * delta))
|
|
# Finished animation?
|
|
if $level/gimbal_y/gimbal_x/stick/camera.fov > 175:
|
|
# Was it triggered by a restart or by a next?
|
|
if Globals.restart:
|
|
# Reset vars and load active scene
|
|
Globals.restart = false
|
|
Globals.next = false
|
|
LevelLoader.redo()
|
|
else:
|
|
# Reset vars and load next scene
|
|
Globals.restart = false
|
|
Globals.next = false
|
|
LevelLoader.next()
|
|
|
|
# Rotates the camera
|
|
$level/gimbal_y.rotate_z(-mouse_input.x)
|
|
$level/gimbal_y/gimbal_x.rotate_x(-mouse_input.y)
|
|
# Clamps the up/down
|
|
$level/gimbal_y/gimbal_x.rotation_degrees.x = clamp ($level/gimbal_y/gimbal_x.rotation_degrees.x, 5, 175)
|
|
|
|
# Zooms in/out
|
|
$level/gimbal_y/gimbal_x/stick/camera.translate(Vector3(0,0,scroll_input))
|
|
$level/gimbal_y/gimbal_x/stick/camera.position.z = clamp($level/gimbal_y/gimbal_x/stick/camera.position.z, 0, 50)
|
|
# Default size is 0.85 = 1m
|
|
$player_model.scale = Vector3(1,1,1) * clamp($level/gimbal_y/gimbal_x/stick/camera.position.z/2, 0, 0.85)
|
|
|
|
# Is the player on the ground?
|
|
if ($grounded.is_colliding() || $grounded_override.is_colliding()):
|
|
airtime = -0.3
|
|
|
|
# Moves via rotation
|
|
apply_torque(Quaternion.from_euler($level/gimbal_y.global_rotation) * (move_input * 2000.0 * delta))
|
|
|
|
else:
|
|
#Moves via translation
|
|
apply_central_force(Quaternion.from_euler($level/gimbal_y.global_rotation) * (Vector3(move_input.y, -move_input.x, 0) * 350.0 * delta))
|
|
|
|
# Is the break key pressed?
|
|
if Input.get_action_strength("alternate") > 0.0 && airtime < 0.0:
|
|
linear_damp = 10
|
|
else:
|
|
linear_damp = 0.3
|
|
|
|
# Updates the air time
|
|
airtime += delta
|
|
# Pressed jump and can jump?
|
|
if (allow_jump && ((Input.get_action_strength("up") && allow_jump_hold) || (Input.is_action_just_pressed("up") && !allow_jump_hold)) && airtime < 0.0):
|
|
|
|
# Prevents sneaky double-jumping!
|
|
airtime = 0
|
|
|
|
# Plays the animation
|
|
$player_model/AnimationPlayer.play("ArmatureAction")
|
|
|
|
# Find the velocity along the down vector
|
|
var velocity_along_normal = Globals.global_down * Globals.global_down.dot(linear_velocity)
|
|
var new_velocity = (linear_velocity - velocity_along_normal) + (Globals.global_down * -29)
|
|
|
|
# Applies the new velocity
|
|
set_linear_velocity(new_velocity)
|
|
|
|
if (allow_jump && Input.is_action_just_released("up")):
|
|
# Find the velocity along the down vector
|
|
var velocity_along_normal = Globals.global_down * Globals.global_down.dot(linear_velocity)
|
|
var new_velocity = (linear_velocity - velocity_along_normal/3) + (Globals.global_down * 5)
|
|
|
|
# Applies the new velocity
|
|
set_linear_velocity(new_velocity)
|