add a Player with basic movement
This commit is contained in:
98
scripts/player.gd
Normal file
98
scripts/player.gd
Normal file
@@ -0,0 +1,98 @@
|
||||
class_name Player
|
||||
extends CharacterBody3D
|
||||
|
||||
const MOVE_SPEED: float = 5
|
||||
const MOVE_ACCELERATION: float = 25
|
||||
const MOVE_DECELERATION: float = 25
|
||||
|
||||
const JUMP_FORCE: float = 10
|
||||
const FALL_SPEED: float = 20
|
||||
const FALL_ACCELERATION: float = 25
|
||||
|
||||
@export var _respawn_height: float = -100
|
||||
|
||||
var _respawn_point: Vector3
|
||||
|
||||
@onready var _camera: Camera3D = $Camera3D
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_respawn_point = global_position
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if global_position.y < _respawn_height:
|
||||
global_position = _respawn_point
|
||||
velocity = Vector3.ZERO
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
_lateral_movement(delta)
|
||||
_vertical_movement(delta)
|
||||
|
||||
_jumping(delta)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseMotion:
|
||||
var mouse_event := event as InputEventMouseMotion
|
||||
rotate_y(-mouse_event.screen_relative.x * 0.005)
|
||||
_camera.rotate_x(-mouse_event.screen_relative.y * 0.005)
|
||||
_camera.rotation.x = clamp(_camera.rotation.x, -PI / 2, PI / 2)
|
||||
|
||||
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
||||
if event is InputEventMouseMotion:
|
||||
var mouse_event := event as InputEventMouseMotion
|
||||
var sensitivity := deg_to_rad(Settings.mouse_sensitivity)
|
||||
|
||||
rotate_y(
|
||||
(
|
||||
-mouse_event.screen_relative.x
|
||||
* sensitivity
|
||||
* (-1 if Settings.invert_mouse_horizontal else 1)
|
||||
)
|
||||
)
|
||||
|
||||
_camera.rotate_x(
|
||||
(
|
||||
-mouse_event.screen_relative.y
|
||||
* sensitivity
|
||||
* (-1 if Settings.invert_mouse_vertical else 1)
|
||||
)
|
||||
)
|
||||
_camera.rotation.x = clamp(_camera.rotation.x, -PI / 2, PI / 2)
|
||||
|
||||
|
||||
func _lateral_movement(delta: float) -> void:
|
||||
var input_dir := Input.get_vector(
|
||||
"move_left", "move_right", "move_forward", "move_back"
|
||||
)
|
||||
var has_input := input_dir.length() > 0
|
||||
|
||||
if has_input:
|
||||
var direction := (
|
||||
(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||
)
|
||||
var new_velocity := direction * MOVE_SPEED
|
||||
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
|
||||
|
||||
|
||||
func _jumping(_delta: float) -> void:
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_FORCE
|
||||
Reference in New Issue
Block a user