extends CharacterBody3D
class_name Character

@onready var collision = (%Collision)
@onready var status = (%Status as Status)
@onready var view = (%View as View)
@onready var buff = (%Buff as Buff)
@onready var skill = (%Skill as Skill)
@onready var battle = (%Battle as Battle)
@onready var effect = (%Effect as Effect)

func init(id:int,cfg:CharacterCfg,team:Enum.ETeam):
	status.id = id
	status.team = team
	status.cfg = cfg
	var half_height = Setting.pixel_size * Setting.sprite_scale * cfg.sprite_harf_height
	var width = Setting.pixel_size * Setting.sprite_scale * cfg.sprite_width
	var body_scale = Vector3(width,half_height*2,width)
	collision.position = Vector3(0,half_height,0)
	collision.scale = body_scale
	status.basic_offset = Vector3(0,half_height,0)
	status.ui_offset = Vector3(0,half_height*2.2,0)
	view.scale = Vector3(1,Setting.sprite_scale,1)
	view.init(cfg.sprite_frames)
	skill.init()
	effect.init(cfg.type,body_scale)
	
func init_after():
	var cfg = status.cfg as CharacterCfg
	set_status("hp",cfg.hp_max)
	set_status("hp_max",cfg.hp_max)
	set_status("shield",cfg.shield.shield_max)
	set_status("shield_max",cfg.shield.shield_max)
	set_status("stun",0)
	set_status("stun_max",cfg.stun.stun_max)
	set_status("mp",0)
	set_status("mp_max",cfg.mp.mp_max)
	set_status("mp_sub",0)
	set_status("mp_sub_max",cfg.mp.mp_sub_max)

func set_material(material:ShaderMaterial):
	view.material_override = material
	
#==getter==
func id()->int:return status.id
func cfg()->CharacterCfg:return status.cfg
func team()->Enum.ETeam:return status.team
func pos2D()->Vector2:return Vector2(position.x,position.z)
func pos()->Vector3:return position
func view_pos()->Vector3:return position + view.position
func ui_pos()->Vector3:return position + status.ui_offset

#==interface==
func get_status(status_name:String):return status.get_status(status_name)
func set_status(status_name:String,value):status.set_status(status_name,value)
func set_pos(pos:Vector3):position = pos
func move_to(dir:Vector2):set_status("move_dir",dir)
func move_stop():set_status("move_dir",Vector2.ZERO)
func add_buff(buff_name:String,duration:float,ignore_pause:bool=false):buff.add_buff(buff_name,duration,ignore_pause)
func remove_buff(buff_name:String):buff.remove_buff(buff_name)
func has_buff(buff_name:String)->bool:return buff.has_buff(buff_name)
func set_hit_move(hit_back_dir:Vector2,hit_back_speed:float,hit_up_speed:float):status.hit_back_dir = hit_back_dir;status.hit_back_speed = hit_back_speed;status.hit_up_speed = hit_up_speed
func cancel_skill():skill.cancel_skill()
func set_view_trigger(trigger_name:String):status.set("trigger_%s"%trigger_name,true)
func add_attack(from:int,dir:Vector2,attack:AttackCfg):battle.add_attack(from,dir,attack)
func show_hit_text(value:String):SignalManager.character_hit_text.emit(id(),value)
func show_hit_damage(value:float):SignalManager.character_hit_damage.emit(id(),value)
func add_mp(value:float):battle.add_mp(value)
