FantasyRun/character/character.gd

50 lines
1.2 KiB
GDScript

extends Area3D
class_name Character
signal turn(direction)
@export var path: Path3D:
set = set_path
@export var speed: float = 10.0
var follow: PathFollow3D
var marker: Marker3D
var total_distance: float = 0.0
func _ready() -> void:
if path == null or path is not Path3D:
printerr("path is not set to a Path3D")
return
follow = Utils.find_first_node_of_type(path, PathFollow3D) as PathFollow3D
marker = Utils.find_first_node_of_type(path, Marker3D) as Marker3D
func _process(delta: float):
if path == null:
return
global_position = marker.global_position
global_rotation = marker.global_rotation
if follow.progress_ratio >= 1:
return
var travel: float = speed * delta
follow.progress += travel
total_distance += travel
func set_path(new_path: Path3D):
if new_path is not Path3D:
printerr("path must be a Path3D")
return
path = new_path
follow = Utils.find_first_node_of_type(path, PathFollow3D) as PathFollow3D
if follow == null:
follow = PathFollow3D.new()
path.add_child(follow)
marker = Marker3D.new()
follow.add_child(marker)
return
marker = Utils.find_first_node_of_type(follow, Marker3D) as Marker3D
if marker == null:
marker = Marker3D.new()
follow.add_child(marker)