extends KinematicBody

export var Sensitivity_X = 0.02
export var Sensitivity_Y = 0.02
export var Jump_Speed = 10
signal hit

const GRAVITY = .75
var velocity = Vector3(0,0,0)
var Walk_Speed = 10

func _ready():
	set_process(true)
	
func _physics_process(_delta):
	velocity.y -= GRAVITY
	
	#for index in range(get_slide_collision()):
		#var collision = get_slide_collision(index)
		#if collision.collider.is_in_group("mob"):
			
	
	if Input.is_key_pressed(KEY_W) or Input.is_key_pressed(KEY_UP):
		velocity.x = -global_transform.basis.z.x * Walk_Speed
		velocity.z = -global_transform.basis.z.z * Walk_Speed
	if Input.is_key_pressed(KEY_S) or Input.is_key_pressed(KEY_DOWN):
		velocity.x = global_transform.basis.z.x * Walk_Speed
		velocity.z = global_transform.basis.z.z * Walk_Speed
	if Input.is_key_pressed(KEY_LEFT) or Input.is_key_pressed(KEY_A):
		rotate_y(Sensitivity_X)
	if Input.is_key_pressed(KEY_RIGHT) or Input.is_key_pressed(KEY_D):
		rotate_y(-Sensitivity_X)
	if Input.is_action_pressed("look_down"):
		$Spatial/Camera.rotate_x(Sensitivity_Y)
	if Input.is_action_pressed("look_up"):
		$Spatial/Camera.rotate_x(-Sensitivity_Y)
		
	if not(Input.is_key_pressed(KEY_W) or Input.is_key_pressed(KEY_S) or Input.is_key_pressed(KEY_UP) or Input.is_key_pressed(KEY_DOWN)):
		velocity.x = 0
		velocity.z = 0
	
	if is_on_floor():
		if Input.is_action_just_pressed("ui_accept"):
			velocity.y = Jump_Speed
	velocity = move_and_slide(velocity, Vector3(0,1,0))

func _on_Timer_timeout():
	pass # Replace with function body.
	
func die():
	emit_signal("hit")
	get_tree().change_scene("res://Menus/Dead Menu/Dead.tscn")
	
func _on_KillBox_body_entered(_body):
	die()

