xiandie/scene/entity/general/autoplay_animated_sprite.gd

85 lines
2.3 KiB
GDScript

@tool
extends AnimatedSprite2D
@export var autostart := true
@export var state_configs := [] as Array[StateActionConfigResource]
var auto_checkout_dict = {
# intro -> {state_config instance}
}
func _ready() -> void:
_load_config()
if Engine.is_editor_hint():
return
if autostart and animation:
# 制造一点错差
frame = randi() % sprite_frames.get_frame_count(animation)
play()
animation_finished.connect(_on_animation_finished)
func _load_config():
for i in range(state_configs.size()):
var state_config = state_configs[i]
if sprite_frames and sprite_frames.has_animation(state_config.animation_intro):
# intro 的 animation 关闭循环
sprite_frames.set_animation_loop(state_config.animation_intro, false)
auto_checkout_dict[state_config.animation_intro] = {
"animation_next": state_config.animation_next,
"animation_wait_time": state_config.animation_wait_time,
"intro_loop": max(1, state_config.intro_loop)
}
func _on_animation_finished() -> void:
if auto_checkout_dict.has(animation):
auto_checkout_dict[animation].intro_loop -= 1
if auto_checkout_dict[animation].intro_loop <= 0:
var wait_time = auto_checkout_dict[animation].animation_wait_time
var next = auto_checkout_dict[animation].animation_next
if wait_time > 0:
pause()
get_tree().create_timer(wait_time).timeout.connect(play.bind(next))
else:
play(next)
# const gif_root_dir = "res://asset/art/gif/"
# var gif_dir:
# set(val):
# gif_dir = val
# # 只在编辑器模式下加载配置
# if Engine.is_editor_hint() and gif_dir:
# sprite_frames = load(gif_root_dir + gif_dir + "/frames.tres")
# func _get(property: StringName) -> Variant:
# if property == "gif_dir":
# return gif_dir
# return null
# func _set(property: StringName, value: Variant) -> bool:
# if property == "gif_dir":
# gif_dir = value
# return true
# return false
# func _get_property_list() -> Array[Dictionary]:
# # 只在编辑器模式下加载配置
# var hint_str = ""
# if Engine.is_editor_hint():
# var dir_access = DirAccess.open(gif_root_dir) as DirAccess
# var dirs = dir_access.get_directories()
# hint_str = ",".join(dirs)
# return [
# {
# "name": "gif_dir",
# "type": TYPE_STRING,
# "hint": PROPERTY_HINT_ENUM_SUGGESTION,
# "hint_string": hint_str
# }
# ]