42 lines
1.0 KiB
GDScript3
Raw Normal View History

2024-02-07 22:04:19 +00:00
class_name MovementComponent
extends Node
@export var move_speed := 4.5
@export var jump_velocity := 4.5
2024-02-23 19:13:46 +00:00
@export var deceleration_speed := 20
2024-02-07 22:04:19 +00:00
2024-02-15 15:28:27 +00:00
var body: Character
2024-02-07 22:04:19 +00:00
var direction: Vector3
var gravity: float
func _ready():
gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
2024-02-24 14:30:41 +00:00
body = owner as Character
body.controlled.connect(on_controlled)
func on_controlled(controller: Controller):
controller.direction_changed.connect(on_direction_changed)
controller.jumped.connect(jump)
2024-02-07 22:04:19 +00:00
func _physics_process(delta):
if direction:
body.velocity.x = direction.x * move_speed
body.velocity.z = direction.z * move_speed
else:
2024-02-23 19:13:46 +00:00
body.velocity.x = Math.dampf(body.velocity.x, 0, deceleration_speed * delta)
body.velocity.z = Math.dampf(body.velocity.z, 0, deceleration_speed * delta)
2024-02-07 22:04:19 +00:00
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