2021-05-19 00:56:18 +02:00
|
|
|
extends KinematicBody3D
|
|
|
|
|
|
|
|
@export var mouse_sensitivity := 0.35
|
2021-05-19 01:59:58 +02:00
|
|
|
#var speed := 15
|
2021-05-19 00:56:18 +02:00
|
|
|
|
|
|
|
@onready var head = $Head
|
2021-05-22 02:10:00 +02:00
|
|
|
@onready var ground_check = $GroundCheck
|
2021-05-19 00:56:18 +02:00
|
|
|
|
2021-05-19 01:59:58 +02:00
|
|
|
var direction := Vector3.ZERO
|
|
|
|
var accel := 0
|
|
|
|
var speed := 0
|
|
|
|
var medium = "ground"
|
|
|
|
var accel_type := {
|
|
|
|
"ground": 12,
|
|
|
|
"air": 1,
|
|
|
|
"water": 4
|
|
|
|
}
|
|
|
|
var speed_type := {
|
|
|
|
"ground": 15,
|
|
|
|
"air": 15,
|
|
|
|
"water": 7.5
|
|
|
|
}
|
2021-05-19 00:56:18 +02:00
|
|
|
var gravity := 28
|
|
|
|
var jump := 14
|
|
|
|
|
2021-05-19 01:59:58 +02:00
|
|
|
var velocity := Vector3.ZERO
|
2021-05-19 00:56:18 +02:00
|
|
|
var movement := Vector3.ZERO
|
|
|
|
var gravity_vec := Vector3.ZERO
|
|
|
|
var slide := Vector3.ZERO
|
2021-05-19 01:59:58 +02:00
|
|
|
var snap := Vector3.ZERO
|
2021-05-19 00:56:18 +02:00
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
|
|
|
|
func aim(event) -> void:
|
|
|
|
var mouse_motion = event as InputEventMouseMotion
|
|
|
|
|
|
|
|
if mouse_motion:
|
|
|
|
rotation_degrees.y -= mouse_motion.relative.x * mouse_sensitivity
|
|
|
|
|
|
|
|
var current_tilt: float = head.rotation_degrees.x
|
|
|
|
current_tilt -= mouse_motion.relative.y * mouse_sensitivity
|
|
|
|
head.rotation_degrees.x = clamp(current_tilt, -90, 90)
|
|
|
|
|
|
|
|
func _input(event) -> void:
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("ui_cancel"):
|
|
|
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
else:
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
|
|
|
|
aim(event)
|
|
|
|
|
|
|
|
func _physics_process(delta):
|
2021-05-19 01:59:58 +02:00
|
|
|
direction = Vector3.ZERO
|
2021-05-19 00:56:18 +02:00
|
|
|
|
2021-05-22 02:10:00 +02:00
|
|
|
if is_on_floor() and ground_check.is_colliding():
|
2021-05-19 01:59:58 +02:00
|
|
|
snap = -get_floor_normal()
|
|
|
|
medium = "ground"
|
|
|
|
gravity_vec = Vector3.ZERO
|
2021-05-19 00:56:18 +02:00
|
|
|
else:
|
2021-05-19 01:59:58 +02:00
|
|
|
snap = Vector3.DOWN
|
|
|
|
medium = "air"
|
2021-05-19 00:56:18 +02:00
|
|
|
gravity_vec += Vector3.DOWN * gravity * delta
|
|
|
|
|
2021-05-19 01:59:58 +02:00
|
|
|
if Input.is_action_just_pressed("move_jump") and is_on_floor():
|
|
|
|
snap = Vector3.ZERO
|
2021-05-19 00:56:18 +02:00
|
|
|
gravity_vec = Vector3.UP * jump
|
|
|
|
|
|
|
|
if Input.is_action_pressed("move_forward"):
|
2021-05-19 01:59:58 +02:00
|
|
|
direction -= transform.basis.z
|
2021-05-19 00:56:18 +02:00
|
|
|
if Input.is_action_pressed("move_backward"):
|
2021-05-19 01:59:58 +02:00
|
|
|
direction += transform.basis.z
|
2021-05-19 00:56:18 +02:00
|
|
|
if Input.is_action_pressed("move_left"):
|
2021-05-19 01:59:58 +02:00
|
|
|
direction -= transform.basis.x
|
2021-05-19 00:56:18 +02:00
|
|
|
if Input.is_action_pressed("move_right"):
|
2021-05-19 01:59:58 +02:00
|
|
|
direction += transform.basis.x
|
|
|
|
|
|
|
|
if direction.length() > 0: # normalized() will return a null
|
|
|
|
direction = direction.normalized()
|
2021-05-19 00:56:18 +02:00
|
|
|
|
2021-05-19 01:59:58 +02:00
|
|
|
speed = speed_type[medium]
|
|
|
|
accel = accel_type[medium]
|
2021-05-19 00:56:18 +02:00
|
|
|
|
2021-05-19 01:59:58 +02:00
|
|
|
velocity = velocity.lerp(direction * speed, accel * delta)
|
2021-05-19 00:56:18 +02:00
|
|
|
|
2021-05-19 01:59:58 +02:00
|
|
|
movement = velocity + gravity_vec
|
2021-05-19 00:56:18 +02:00
|
|
|
|
2021-05-19 01:59:58 +02:00
|
|
|
slide = move_and_slide_with_snap(movement, snap, Vector3.UP)
|
2021-05-19 00:56:18 +02:00
|
|
|
|
2021-05-19 01:59:58 +02:00
|
|
|
if not is_on_floor(): # while in mid-air collisions affect momentum
|
|
|
|
velocity.x = slide.x
|
|
|
|
velocity.z = slide.z
|
2021-05-19 00:56:18 +02:00
|
|
|
gravity_vec.y = slide.y
|