class_name ServerControlledPlayer
extends CharacterBody3D
const JUMP_VELOCITY = 4.5

func _ready() -> void:
	pass

@export_category("ServerControlledPlayer")
@onready var placeholder: Node3D = $'..'

var input_direction := Vector2()
var input_rotation_y 

@rpc("call_local", "any_peer", "unreliable_ordered")
func set_input_direction(new_input_direction: Vector2):
	input_direction = new_input_direction

@rpc("call_local", "any_peer", "unreliable_ordered")
func set_rotation_y(new_rotation_y: float):
	rotation.y = new_rotation_y
	
@rpc("call_local", "any_peer", "unreliable_ordered")
func set_rotation_x(new_rotation_x: float):
	rotation.x = new_rotation_x

@rpc("call_local", "any_peer", "unreliable_ordered")
func jump(): 
	jumping = true

var jumping := false
func _process(delta: float) -> void:
	if not is_on_floor():
		velocity += get_gravity() * delta
	if is_on_floor() && jumping:
		velocity.y = JUMP_VELOCITY
	jumping = false
	if is_on_floor():
		var direction := (transform.basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
		#if is_on_floor():
		if direction:
			velocity.x = direction.x * placeholder.character_speed
			velocity.z = direction.z * placeholder.character_speed
		else:
			velocity.x = move_toward(velocity.x, 0, placeholder.character_speed)
			velocity.z = move_toward(velocity.z, 0, placeholder.character_speed)

func _physics_process(delta: float) -> void:
	move_and_slide()