39 lines
894 B
GDScript
39 lines
894 B
GDScript
class_name MovementComponent
|
|
extends Node
|
|
|
|
@export var move_speed := 4.5
|
|
@export var jump_velocity := 4.5
|
|
@export var deceleration := 0.75
|
|
|
|
var body: Character
|
|
var direction: Vector3
|
|
var gravity: float
|
|
|
|
func _ready():
|
|
body = owner as Character
|
|
body.controller.direction_changed.connect(on_direction_changed)
|
|
body.controller.jumped.connect(jump)
|
|
gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
func _physics_process(delta):
|
|
if direction:
|
|
body.velocity.x = direction.x * move_speed
|
|
body.velocity.z = direction.z * move_speed
|
|
else:
|
|
body.velocity.x *= deceleration
|
|
body.velocity.z *= deceleration
|
|
|
|
if !body.is_on_floor():
|
|
body.velocity.y -= gravity * delta
|
|
|
|
body.move_and_slide()
|
|
|
|
func on_direction_changed(new_direction: Vector3):
|
|
direction = new_direction
|
|
|
|
func can_jump() -> bool:
|
|
return body.is_on_floor()
|
|
|
|
func jump():
|
|
body.velocity.y = jump_velocity
|