extends LimboState

@onready var root: Normal = %Normal

##TODO:后续把JumpGrace在JumpState也写一下，或者直接写到AirBoneState，目前的JumpGrace由于只能在Fall触发，会卡手

func _setup() -> void:
	self.add_event_handler(&"completed_jump",_handler_completed_jump)

func _enter() -> void:
	_jump_from_land_check()
	
	var _j = agent.locomotion_comp.jump() as bool
	get_root().blackboard.set_var(&"is_jumping",true) #标记jump

func _exit() -> void:
	#这里强制重置一下
	agent.locomotion_comp.stop_jump()


func _update(delta: float) -> void:
	var move_dir = Input.get_axis(&"move_left",&"move_right")
	agent.locomotion_comp.add_movement_input(move_dir)
	
	if agent.velocity.y >= 0:
		get_root().dispatch(self.EVENT_FINISHED)

func _handler_completed_jump() -> bool:
	agent.locomotion_comp.stop_jump()
	return true

func _jump_from_land_check() -> bool:
	var last_state = root.get_previous_active_state() as LimboState
	if last_state and last_state == root.ground_state :
		agent.on_jump()
		return true
	
	return false
