class_name Normal extends LimboHSM

@onready var ground_state : LimboHSM = %Grounded
@onready var idle_state: LimboState = %Idle
@onready var move_state: LimboState = %Move

@onready var airbone_state : LimboHSM = %Airbone
@onready var fall_state: LimboState = %Fall
@onready var jump_state: LimboState = %Jump
@onready var wall_jump_state: LimboState = %WallJump

@onready var on_wall_state: LimboHSM = %OnWall
@onready var climb_state: LimboState = %Climb
@onready var climb_jump_state: LimboState = %ClimbJump

@onready var dash_state: LimboState = %Dash

@onready var grap_hook_state: LimboHSM = %GrapHook
@onready var hook_shooting_state: LimboState = %HookShooting
@onready var grapping_state: LimboState = %Grapping

func _setup() -> void:
	self._init_trans()
	self._init_handler()

func _init_handler() -> void:
	self.add_event_handler(&"trigger_dash",_handler_trigger_dash) ##处理Dash输入
	self.add_event_handler(&"trigger_climb",_handler_trigger_climb) ##处理瞬时的climb
	self.add_event_handler(&"trigger_grap_hook",_handler_grap_hook) ##处理grap hook 的輸入

func _init_trans() -> void:
	self.add_transition(ground_state,airbone_state,ground_state.EVENT_FINISHED)
	self.add_transition(airbone_state,ground_state,airbone_state.EVENT_FINISHED)
	self.add_transition(ANYSTATE,dash_state,&"want_to_dash")
	self.add_transition(dash_state,ground_state,&"exit_on_ground")
	self.add_transition(dash_state,airbone_state,&"exit_on_air")
	
	##用於處理貼在墻面的State流轉
	self.add_transition(ANYSTATE,on_wall_state,&"want_to_on_wall")
	self.add_transition(on_wall_state,airbone_state,&"exit_on_ground")
	self.add_transition(on_wall_state,ground_state,&"exit_on_air")
	
	##用於處理鈎爪的State流轉
	self.add_transition(ANYSTATE,grap_hook_state,&"want_to_grap_hook",_has_move_input)
	self.add_transition(grap_hook_state,airbone_state,&"exit_on_ground")
	self.add_transition(grap_hook_state,ground_state,&"exit_on_air")
	self.add_transition(grap_hook_state,airbone_state,&"hook_to_jump")
	

func _update(delta: float) -> void:
	process_direction()
	
	##如果玩家当前在墙上，并且玩家按下了climb
	if agent.wall_detector.is_on_wall and agent.m_climb_press:
		self.dispatch(&"want_to_on_wall")

##处理Dash输入
func _handler_trigger_dash() -> bool:
	#如果当前无法Dash，则返回
	if not agent.locomotion_comp.get_can_dash():
		return false
	
	if get_root().blackboard.get_var(&"is_dashing",false):
		return false
	
	self.dispatch(&"want_to_dash")
	return true

func _handler_trigger_climb() -> bool:
	if not agent.is_on_wall():
		return false
	
	return true

func _handler_grap_hook() -> bool:
	
	self.dispatch(&"want_to_grap_hook")
	return true

func process_direction() -> void:
	var b = get_root().blackboard.get_var(&"lock_direction_changed",false) as bool
	if b: return
	
	##切换方向的逻辑
	var move_direction = Input.get_axis(&"move_left",&"move_right") as float
	if move_direction * Player.get_direction_vector(agent.direction).x <= 0 and move_direction != 0:
		agent.direction = Player.Direction.RIGHT if move_direction> 0 else Player.Direction.LEFT

func _get_dash_exit_enter_state()->LimboState:
	if agent.is_on_floor():
		return ground_state
	else:
		return airbone_state

func _has_move_input() -> bool:
	return agent.get_move_input() != Vector2.ZERO
