92 lines
2.4 KiB
GDScript
92 lines
2.4 KiB
GDScript
class_name Player
|
|
extends CharacterBody3D
|
|
|
|
const MOVE_SPEED: float = 10
|
|
const MOVE_ACCELERATION: float = 100
|
|
const MOVE_DECELERATION: float = 50
|
|
|
|
const FALL_SPEED: float = 20
|
|
const FALL_ACCELERATION: float = 25
|
|
|
|
@export var _respawn_height: float = -5
|
|
|
|
var aim_offset: Vector3
|
|
|
|
var _respawn_point: Vector3
|
|
var _floor_height: float = 0
|
|
|
|
|
|
func _ready() -> void:
|
|
_respawn_point = global_position
|
|
Referencer.player = self
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
call_deferred("_mouse_aiming")
|
|
_process_respawning()
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if not is_multiplayer_authority():
|
|
return
|
|
|
|
_lateral_movement(delta)
|
|
_vertical_movement(delta)
|
|
|
|
move_and_slide()
|
|
|
|
|
|
func _mouse_aiming() -> void:
|
|
var player_position := global_position
|
|
if is_on_floor():
|
|
_floor_height = player_position.y
|
|
player_position.y = _floor_height
|
|
var aim_position := _mouse_project(_floor_height)
|
|
aim_offset = aim_position - player_position
|
|
aim_position.y = global_position.y
|
|
look_at(aim_position, Vector3.UP, true)
|
|
|
|
|
|
func _mouse_project(height: float) -> Vector3:
|
|
var mouse_pos := get_viewport().get_mouse_position()
|
|
var camera := Referencer.main_camera
|
|
var from := camera.project_ray_origin(mouse_pos)
|
|
var direction := camera.project_ray_normal(mouse_pos)
|
|
var plane := Plane(Vector3.UP, height)
|
|
var intersection: Variant = plane.intersects_ray(from, direction)
|
|
if not intersection:
|
|
return Vector3.ZERO
|
|
return intersection as Vector3
|
|
|
|
|
|
func _process_respawning() -> void:
|
|
if global_position.y < _respawn_height:
|
|
global_position = _respawn_point
|
|
velocity = Vector3.ZERO
|
|
|
|
|
|
func _lateral_movement(delta: float) -> void:
|
|
var input_dir := Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
|
var has_input := input_dir.length() > 0
|
|
|
|
if has_input:
|
|
var direction := Vector3(input_dir.x, 0, input_dir.y).normalized()
|
|
var new_velocity := (direction * MOVE_SPEED).rotated(
|
|
Vector3.UP, Referencer.main_camera.rotation.y
|
|
)
|
|
new_velocity.y = velocity.y
|
|
velocity = velocity.move_toward(new_velocity, MOVE_ACCELERATION * delta)
|
|
else:
|
|
var new_velocity := Vector3.ZERO
|
|
new_velocity.y = velocity.y
|
|
velocity = velocity.move_toward(new_velocity, MOVE_DECELERATION * delta)
|
|
|
|
|
|
func _vertical_movement(delta: float) -> void:
|
|
if not is_on_floor():
|
|
var new_velocity := velocity
|
|
new_velocity.y = -FALL_SPEED
|
|
velocity = velocity.move_toward(new_velocity, FALL_ACCELERATION * delta)
|
|
else:
|
|
velocity.y = 0
|