extends Node3D

@export var player: Player
@export var speed_multiplier: float = 3.0

@onready var timer: Timer = $Timer
@onready var particles: GPUParticles3D = $GPUParticles3D

var direction: Vector3 = Vector3.ZERO
var dash_duration: float = 0.1
var time_remaining: float = 0.0

func _physics_process(delta: float) -> void:
	if direction.is_zero_approx():
		return

	player.velocity = direction * player.stats.speed.get_modifier() * speed_multiplier
	time_remaining -= delta
	if time_remaining <= 0:
		direction = Vector3.ZERO
		particles.emitting = false

func _unhandled_input(event: InputEvent) -> void:
	if not player.is_physics_processing():
		return

	if not timer.is_stopped():
		return

	if event.is_action_pressed("dash"):
		direction = player.get_movement_direction()
		if not direction.is_zero_approx():
			player.rig.playback.travel("Dash")
			particles.emitting = true
			timer.start(player.stats.get_dash_cooldown())
			time_remaining = dash_duration
