extends CharacterBody3D

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var speed = 4.0  # movement speed
var jump_speed = 6.0  # determines jump height
var mouse_sensitivity = 0.002  # turning speed
var moving = false


func get_input():
	var input = Input.get_vector("strafe_left", "strafe_right", "move_forward", "move_back")
	var movement_dir = transform.basis * Vector3(input.x, 0, input.y)
	velocity.x = movement_dir.x * speed
	velocity.z = movement_dir.z * speed

func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _physics_process(delta):
	velocity.y += -gravity * delta
	get_input()
	if abs(velocity.x) + abs(velocity.z) > 0 && !moving:
		get_node("PlayerModel/AnimationPlayer").play("Walk")
		moving = true
	elif abs(velocity.x) + abs(velocity.z) == 0 && moving:
		get_node("PlayerModel/AnimationPlayer").play("Idle")
		moving = false
	move_and_slide()

#mouse input stuff
func _unhandled_input(event):
	if event is InputEventMouseMotion:
		rotate_y(-event.relative.x * mouse_sensitivity)
		get_node("SpringArm3D").rotate_x(-event.relative.y * mouse_sensitivity)
	if event.is_action_pressed("jump") and is_on_floor():
		velocity.y = jump_speed
