From 4510dd22934603d27bd070138f90bc455fada975 Mon Sep 17 00:00:00 2001 From: blujai831 Date: Wed, 9 Apr 2025 18:09:25 -0700 Subject: [PATCH] Reintroduce AutoPathFollow as cleaner base class of MovingPlatform --- props/moving_platform.gd | 22 ++------------------- util/auto_path_follow.gd | 37 ++++++++++++++++++++++++++++++++++++ util/auto_path_follow.gd.uid | 1 + 3 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 util/auto_path_follow.gd create mode 100644 util/auto_path_follow.gd.uid diff --git a/props/moving_platform.gd b/props/moving_platform.gd index db227e2..3f17b04 100644 --- a/props/moving_platform.gd +++ b/props/moving_platform.gd @@ -1,35 +1,17 @@ -class_name MovingPlatform extends PathFollow3D +class_name MovingPlatform extends AutoPathFollow -@export var speed: float = 1.0 -@export var pause_at_ends: float = 1.0 @export var body: AnimatableBody3D @export var area: Area3D -@onready var _prev_position := global_position @onready var _area_bodies: Array[Character] = [] -var direction: float = 1.0 -var pause_timer: float = 0.0 - func _ready() -> void: if area: area.body_entered.connect(_on_area_body_entered) area.body_exited.connect(_on_area_body_exited) func _physics_process(delta: float) -> void: - if pause_timer > 0.0: - pause_timer -= delta - else: - progress += direction*speed*delta - if !loop && ( - (direction > 0.0 && progress_ratio >= 1.0) || - (direction < 0.0 && progress_ratio <= 0.0) - ): - progress_ratio = clamp(progress_ratio, 0.0, 1.0) - direction *= -1.0 - pause_timer = pause_at_ends - var velocity := (global_position - _prev_position).normalized()*speed - _prev_position = global_position + super(delta) if body: body.global_position = global_position body.constant_linear_velocity = velocity diff --git a/util/auto_path_follow.gd b/util/auto_path_follow.gd new file mode 100644 index 0000000..a0b4a63 --- /dev/null +++ b/util/auto_path_follow.gd @@ -0,0 +1,37 @@ +class_name AutoPathFollow extends PathFollow3D + +@export var speed: float = 1.0 +@export var pause_at_ends: float = 1.0 + +@onready var _prev_position := global_position + +var _velocity := Vector3.ZERO + +var direction: float = 1.0 +var pause_timer: float = 0.0 +var velocity: Vector3: + get(): + return _velocity +var paused: bool: + get(): + return pause_timer > 0.0 + set(value): + if value: + pause_timer = INF + else: + pause_timer = 0.0 + +func _physics_process(delta: float) -> void: + if pause_timer > 0.0: + pause_timer -= delta + else: + progress += direction*speed*delta + if !loop && ( + (direction > 0.0 && progress_ratio >= 1.0) || + (direction < 0.0 && progress_ratio <= 0.0) + ): + progress_ratio = clamp(progress_ratio, 0.0, 1.0) + direction *= -1.0 + pause_timer = pause_at_ends + _velocity = (global_position - _prev_position).normalized()*speed + _prev_position = global_position diff --git a/util/auto_path_follow.gd.uid b/util/auto_path_follow.gd.uid new file mode 100644 index 0000000..789b4c9 --- /dev/null +++ b/util/auto_path_follow.gd.uid @@ -0,0 +1 @@ +uid://dbbkfe87bos0n