class_name Player extends CharacterBody2D


const DEADZONE = 0.1

@export var max_speed: float = 300.0

@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var state_machine: StateMachine = $StateMachine

var h_press_tick: int = 0  ## last time horizontal axis was pressed
var v_press_tick: int = 0  ## last time vertical axis was pressed

var speed_mult: float = 1.0


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("move_left") or event.is_action_pressed("move_right"):
		h_press_tick = Time.get_ticks_msec()
	if event.is_action_pressed("move_up") or event.is_action_pressed("move_down"):
		v_press_tick = Time.get_ticks_msec()


func get_movement_vector() -> Vector2:
	var x_strength: float = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	var y_strength: float = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
	
	if absf(x_strength) < DEADZONE and absf(y_strength) < DEADZONE:
		return Vector2.ZERO
	
	if absf(x_strength) < DEADZONE:
		return Vector2(0, signf(y_strength))
	if absf(y_strength) < DEADZONE:
		return Vector2(signf(x_strength), 0)
	
	# use most recent axis press
	if h_press_tick > v_press_tick:
		return Vector2(signf(x_strength), 0)
	else:
		return Vector2(0, signf(y_strength))
