2024-02-07 22:04:19 +00:00
|
|
|
class_name RotationComponent
|
2024-02-27 20:05:55 +00:00
|
|
|
extends CharacterComponent
|
2024-02-07 22:04:19 +00:00
|
|
|
|
|
|
|
@export var root: Node3D
|
2024-02-23 19:13:46 +00:00
|
|
|
@export var rotation_speed: float = 15.0
|
2024-02-07 22:04:19 +00:00
|
|
|
|
|
|
|
var direction: Vector3
|
|
|
|
var angle: float
|
|
|
|
|
|
|
|
func _ready():
|
2024-02-27 20:05:55 +00:00
|
|
|
assert(root, "rotation root needs to be set")
|
|
|
|
assert(rotation_speed > 0, "rotation speed must be greater than zero")
|
|
|
|
character.controlled.connect(on_controlled)
|
2024-02-07 22:04:19 +00:00
|
|
|
|
2024-02-27 22:21:44 +00:00
|
|
|
func _process(delta: float):
|
2024-02-26 22:50:56 +00:00
|
|
|
if absf(angle_difference(root.rotation.y, angle)) < 0.001:
|
|
|
|
return
|
|
|
|
|
2024-02-16 11:51:05 +00:00
|
|
|
root.rotation.y = Math.damp_angle(root.rotation.y, angle, rotation_speed * delta)
|
2024-02-07 22:04:19 +00:00
|
|
|
|
2024-02-24 14:30:41 +00:00
|
|
|
func on_controlled(controller: Controller):
|
|
|
|
controller.direction_changed.connect(on_direction_changed)
|
|
|
|
|
2024-02-07 22:04:19 +00:00
|
|
|
func on_direction_changed(new_direction: Vector3):
|
2024-02-15 15:28:27 +00:00
|
|
|
direction = new_direction
|
|
|
|
|
|
|
|
if direction != Vector3.ZERO:
|
|
|
|
angle = atan2(direction.x, direction.z)
|