stick-the-quick/objects/MovingPlatform/MovingPlatform.gd

43 lines
1.2 KiB
GDScript3
Raw Normal View History

class_name MovingPlatform extends Platform
@export var path: Curve3D
@export var cyclic := false
@export var speed: float = 1.0
@export var pauses: Array[float] = [0.0, 1.0]
@export var pause_timeout: float = 1.0
@onready var _locus_travel_distance: float = (
path.get_closest_offset(Vector3.ZERO)
)
@onready var _direction: float = 1.0
@onready var _timer: float = -1.0
@onready var _next_pause: int = 0
func _locus() -> Vector3:
return super() + path.sample_baked(_locus_travel_distance)
func _physics_process(delta: float) -> void:
var delta_distance: float = speed*delta
if _timer > 0.0:
_timer -= delta
elif (
(not pauses.is_empty()) and
abs(_locus_travel_distance - pauses[_next_pause]) < delta_distance/2.0
):
_next_pause = (_next_pause + 1) % pauses.size()
_timer = pause_timeout
else:
_locus_travel_distance += _direction*delta_distance
var length: float = path.get_baked_length()
if _locus_travel_distance >= length or _locus_travel_distance < 0.0:
if cyclic:
_locus_travel_distance = fposmod(
_locus_travel_distance, length
)
else:
_direction = -_direction
_locus_travel_distance = clampf(
_locus_travel_distance,
0.0, length
)