89 lines
3.0 KiB
GDScript
89 lines
3.0 KiB
GDScript
extends RigidBody3D
|
|
|
|
|
|
var airtime = 0.0
|
|
var mouse_input = Vector2(0,0)
|
|
var raw_mouse_input = Vector2(0,0)
|
|
var alpha = 10
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
|
|
# 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
|
|
$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")
|
|
|
|
# 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()):
|
|
|
|
# Makes airtime negative (coyote frames)
|
|
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) * 300.0 * delta))
|
|
|
|
# Is the break key pressed?
|
|
if Input.get_action_strength("alternate") > 0.0 && move_input == Vector3(0,0,0):
|
|
# Velocity becomes up/down
|
|
linear_velocity = Globals.global_down * Globals.global_down.dot(linear_velocity)
|
|
|
|
# Updates the air time
|
|
airtime += delta
|
|
# Pressed jump and can jump?
|
|
if (Input.get_action_strength("up") > 0.0 && airtime < 0.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 * -20)
|
|
|
|
# Applies the new velocity
|
|
set_linear_velocity(new_velocity)
|
|
|