extends Reference

const NAME = "default"
const EPSILON = 0.0001

var ctx = null
var anim_tree: AnimationTree
var move_playback

func _init():
	pass

func enter_from(state):
	anim_tree = ctx.get_node("model_transform/DungMan/AnimationTree")
	move_playback = anim_tree["parameters/Move/playback"]

func exit_to(state):
	pass

func physics_process(delta):
	var input = Vector3(Input.get_axis("left", "right"), 0.0, Input.get_axis("up", "down"))
	var motion = Plane.PLANE_XZ.project(ctx.get_viewport().get_camera().global_transform.basis.xform(input)).normalized() * ctx.SPEED
	if motion.length_squared() > EPSILON:
		ctx.get_node("model_transform").look_at(ctx.global_transform.origin - motion, Vector3.UP)
		ctx.move_playback.travel("run")
	else:
		ctx.move_playback.travel("idle")
	ctx.move_and_slide(motion)

func unhandled_input(event):
	if event.is_action_pressed("action"):
		ctx.get_tree().set_input_as_handled()
		ctx.change_state(ctx.StateInteracting.new())

func on_sleep(made_it_to_bed: bool, hop_position: Spatial):
	ctx.change_state(ctx.StateSleeping.new(made_it_to_bed, hop_position))
