@tool extends Node2D @export var onstart_left := true @export var roll_circles := 4 @export var range_x := 400.0 @export var speed := 100.0 @export var stop_ball_offset_start := 10.0 @export var stop_ball_offset_end := 10.0 @export var animation_run_left := "男孩跑动-左" @export var animation_run_right := "男孩跑动-右" @export var animation_stop_ball := "男孩跑动停球" @onready var boy = $"男孩" as AnimatedSprite2D @onready var ball = $"球" as Sprite2D @export var pause := false: set(val): pause = val if val: if tween: tween.kill() boy.pause() else: _start() var running_left: bool func _ready() -> void: running_left = onstart_left boy.sprite_frames.set_animation_loop(animation_run_left, true) boy.sprite_frames.set_animation_loop(animation_run_right, true) boy.sprite_frames.set_animation_loop(animation_stop_ball, false) _start() $AudioStreamPlayer2D var tween: Tween func _start(): if pause: return ball.visible = true tween = create_tween() var duration = range_x / speed if running_left: boy.position.x = range_x ball.position.x = range_x - stop_ball_offset_end boy.play(animation_run_left) tween.tween_property(boy, "position:x", 0, duration) tween.parallel().tween_property(ball, "rotation", -roll_circles * 2 * PI, duration) var ball_tween = create_tween() ball_tween.tween_property(ball, "position:x", range_x - 35.0, 0.3) ball_tween.tween_property(ball, "position:x", -stop_ball_offset_start + 5.0, duration - 0.5) ball_tween.tween_property(ball, "position:x", -stop_ball_offset_start, 0.2) tween.tween_callback(_turn_around.bind(true)) else: boy.position.x = 0 ball.position.x = stop_ball_offset_end boy.play(animation_run_right) tween.tween_property(boy, "position:x", range_x, duration) tween.parallel().tween_property(ball, "rotation", roll_circles * 2 * PI, duration) var ball_tween = create_tween() ball_tween.tween_property(ball, "position:x", 35.0, 0.3) ball_tween.tween_property( ball, "position:x", range_x + stop_ball_offset_start - 5.0, duration - 0.5 ) ball_tween.tween_property(ball, "position:x", range_x + stop_ball_offset_start, 0.2) tween.tween_callback(_turn_around.bind(false)) func _turn_around(left_to_right: bool): ball.visible = false running_left = not left_to_right if left_to_right: boy.play(animation_stop_ball) else: boy.play_backwards(animation_stop_ball) boy.animation_finished.connect(_start, CONNECT_ONE_SHOT)