extends CharacterBody3D

const SPEED = 5.0
const JUMP_VELOCITY = 4.5

const MOUSE_SENSETIVITY = 0.1 	#ADDED CODE

# ADDED FUNCTION
func _ready() -> void:
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

# ADDED FUNCTION
func _process(delta: float) -> void:
	if Input.is_action_just_pressed("pause"):
		if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
			Input.mouse_mode =Input.MOUSE_MODE_VISIBLE
		else:
			Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

# ADDED FUNCTION
func _input(event: InputEvent) -> void:
	if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
		if event is InputEventMouseMotion:
			rotation_degrees.x -= event.relative.y * MOUSE_SENSETIVITY
			rotation_degrees.y -= event.relative.x * MOUSE_SENSETIVITY
			rotation_degrees.x = clamp(rotation_degrees.x, -90.0, 90.0)

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_pressed("player_jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("player_left", "player_right", "player_forward", "player_back")
	var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if is_on_floor():	# ADDED IF STATEMENT FOR AIR CONTROLS
		if direction:
			velocity.x = direction.x * SPEED
			velocity.z = direction.z * SPEED
		else:
			# CHANGED CODE HERE FOR ACTUAL WORKING SLOWING (because godot template was scuffed) (ADDED CODE)
			velocity.x = lerp(velocity.x, direction.x * SPEED, delta * 4.0)
			velocity.z = lerp(velocity.z, direction.z * SPEED, delta * 4.0)
	# ADDED CODE
	else:	
		velocity.x = lerp(velocity.x, direction.x * SPEED, delta * 3.0)
		velocity.z = lerp(velocity.z, direction.z * SPEED, delta * 3.0)

	move_and_slide()
