extends Node

class_name PlayerMovement

@export var player: Player
@export var player_input: PlayerInput

@export var jump_velocity: float
var disabled: bool

func disable() -> void:
	disabled = true

func process_movement(max_speed: float,acceleration: float,deceleration: float,delta: float) -> void:
	if is_multiplayer_authority() == false:
		return
	if Session.round_state == Session.ROUND_STATES.BUY or disabled or player.passived:
		player.velocity.x = 0
		player.velocity.z = 0
		return
	var input_dir := player_input.direction
	var direction := (player.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		player.velocity.x = clamp(player.velocity.x + direction.x * acceleration * delta,-max_speed*abs(direction.x),max_speed*abs(direction.x))
		player.velocity.z = clamp(player.velocity.z + direction.z * acceleration * delta,-max_speed*abs(direction.z),max_speed*abs(direction.z))
	else:
		player.velocity.x = move_toward(player.velocity.x, 0, deceleration*delta)
		player.velocity.z = move_toward(player.velocity.z, 0, deceleration*delta)

func jump() -> void:
	player.velocity.y = jump_velocity
