@tool
extends AnimationSelectCfg

class_name SkillCfg

@export var name : String
@export var skill_animation : Animation
@export var attack_list : Array[AttackCfg]
@export var attack_particle : PackedScene
@export var free_lock : bool

@export var refresh_animation : bool :
	get:return false
	set(value):if Engine.is_editor_hint():check_animation()

func check_animation():
	var res_name = Util.get_resource_name(self)
	var skill_animation_path = "res://resource/skill_animation/%s.tres" %res_name
	if !ResourceLoader.exists(skill_animation_path):
		if !create_animation(res_name,skill_animation_path):
			return
	skill_animation = load(skill_animation_path)

func create_animation(res_name,path) -> bool:
	if sprite_frams == null:
		print("未设置技能动画资源")
		return false
	if animation_name == "":
		print("未设置技能动画名")
		return false
	print(animation_name)
	if !sprite_frams.has_animation(animation_name):
		print("技能动画名不存在")
		return false
	var animation = Animation.new()
	animation.resource_name = res_name
	
	var track_sprite_frames = animation.add_track(Animation.TYPE_VALUE)
	animation.track_set_path(track_sprite_frames,"View:sprite_frames")
	animation.track_insert_key(track_sprite_frames,0,sprite_frams)
	
	var track_animation = animation.add_track(Animation.TYPE_VALUE)
	animation.track_set_path(track_animation,"View:animation")
	animation.track_insert_key(track_animation,0,animation_name)
	
	var track_frame = animation.add_track(Animation.TYPE_VALUE)
	animation.track_set_path(track_frame,"View:frame")
	var animation_speed = 0.1
	var animation_frame_count = sprite_frams.get_frame_count(animation_name)
	animation.length = animation_speed*animation_frame_count
	for i in range(0,animation_frame_count):
		var time = i * animation_speed
		animation.track_insert_key(track_frame,time,i)
		
	ResourceSaver.save(animation,path)
	Util.refresh_animation_lib()
	return true
	
	
