summaryrefslogtreecommitdiffstats
path: root/character/fps_controller/fps_controller.gd
blob: c522e672b0cf4dff2f7dacda04d5f74f1b126eb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
extends KinematicBody

onready var camera = $Camera
onready var ray = $Camera/RayCast

export var walk_speed : int = 10
export var accel : float = 0.2
export var gravity : int = -1
export var term_velocity : int = -35
export var jump_strength : int = 20

var velocity : Vector3

var state = WALKING

enum {
	WALKING,
	SPRINTING,
}


func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	
func _process(_delta):
	get_input()
	
func _physics_process(_delta):
	handle_movement()
	handle_jumping()
	
func get_input():
	var dir : Vector3 = Vector3.ZERO
	
	#Get base movement input
	
	if Input.is_action_pressed("forward"):
		dir += -global_transform.basis.z
	if Input.is_action_pressed("backward"):
		dir += global_transform.basis.z
	if Input.is_action_pressed("left"):
		dir += -global_transform.basis.x
	if Input.is_action_pressed("right"):
		dir += global_transform.basis.x
		
	dir = dir.normalized()
	
	#Pull X and Z values from directional input. Velocity.y will be handled in physics loop
	
	velocity.x = lerp(velocity.x, dir.x * walk_speed, accel)
	velocity.z = lerp(velocity.z, dir.z * walk_speed, accel)

func handle_movement():
	#Apply gravity
	velocity.y += gravity
	if velocity.y < term_velocity:
		velocity.y = term_velocity
		
	velocity = move_and_slide(velocity, Vector3.UP, true, 4, PI/4, false)

func _input(event):
	#Handle mouse movement for camera
	if event is InputEventMouseMotion:
		self.rotate_y(-event.relative.x * Settings.mouse_sens)
		camera.rotate_x(-event.relative.y * Settings.mouse_sens)
		camera.rotation.x = clamp(camera.rotation.x, -1.2, 1.2)

func handle_jumping():
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_strength