extends LimboHSM

@onready var root: Normal = %Normal

func _setup() -> void:
	self.add_event_handler(&"trigger_jump",_handler_trigger_jump)
	self.add_event_handler(root.climb_state.EVENT_FINISHED,_handler_climb_state_finished)
	self.add_event_handler(root.climb_jump_state.EVENT_FINISHED,_handler_climb_state_finished)
	
	self.add_transition(root.climb_state,root.climb_jump_state,&"want_to_climb_jump")

func _enter() -> void:
	if agent.is_on_wall():##如果已经在Wall上，则什么都不做
		return
	
	##这里是给予玩家一个补偿的上墙力，因为我们存在一个小区间运行玩家可以不贴着墙壁，所以我们给予一个很大的力让玩家的位置修正到墙壁
	var compensaton_velocity = Player.get_direction_vector(agent.direction) * 1000
	agent.velocity = compensaton_velocity
	agent.move_and_slide()

func _handler_climb_state_finished() -> bool:
	if agent.is_on_floor():
		self.dispatch(&"exit_on_ground")
	else:
		self.dispatch(&"exit_on_air")
	
	return true

func _handler_trigger_jump() -> bool:
	if self.get_active_state() == root.climb_state:
		var input_x: float = agent.m_input_intent_direction.x
		var wall_dir: float = agent.get_wall_normal().x
		if sign(input_x) * sign(wall_dir) > 0:
			root._trigger_climb_cd(.12)
			self.get_root().blackboard.set_var(&"want_to_jump",true)
			_handler_climb_state_finished()
			return true
		
		self.dispatch(&"want_to_climb_jump")
	return true
