summaryrefslogtreecommitdiff
path: root/src/camera_controller/PlayerCam.gd
blob: 382ec06727f6444a3e598a745c8e3b28dca94e13 (plain)
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
71
72
73
74
75
76
77
78
79
extends ClippedCamera

var _modes: PoolStringArray = ["STATIC", "FIRSTPERSON", "THIRDPERSON", "ARM", "FREECAM"]
var mode: String = "STATIC" #STATIC, FIRSTPERSON, THIRDPERSON, ARM, FREECAM

#first/third person variables
var head: Spatial = null
var neck: Spatial = null
var player: RigidBody = null

#third person/arm variables
var arm: SpringArm = null

var mouse_axis: Vector2
var mouse_sensitivity: float = 12.0

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	current = true

func _input(event: InputEvent):
	if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
		mouse_axis = event.relative
		match mode:
			"FIRSTPERSON":
				mouse_firstperson()
			"THIRDPERSON":
				mouse_thirdperson()
			"STATIC":
				pass
			"ARM":
				mouse_arm()
			"FREECAM":
				mouse_freecam()
			_:
				pass

func attach(new_parent: Node, c_mode: String, extra_path: String = "."):
	if get_parent():
		get_parent().remove_child(self)
	if c_mode in _modes:
		mode = c_mode
		if mode == "FIRSTPERSON":
			head = new_parent.head; neck = new_parent.neck;
			arm = null;
		elif mode == "THIRDPERSON":
			head = new_parent.head; neck = new_parent.neck;
			arm = new_parent.arm
		elif mode == "ARM":
			head = null; neck = null;
			arm = new_parent.arm
		else:
			head = null; neck = null; arm = null;
	new_parent.get_node(extra_path).add_child(self)
	transform = Transform.IDENTITY

func mouse_firstperson() -> void:
	if mouse_axis.length_squared() > 0:
		var horizontal: float = -mouse_axis.x * (mouse_sensitivity / 100)
		var vertical: float = -mouse_axis.y * (mouse_sensitivity / 100)
		
		neck.rotate_y(deg2rad(horizontal))
		head.rotate_x(deg2rad(vertical))
		
		#vertical clamp
		head.rotation_degrees.x = clamp(head.rotation_degrees.x, -90, 90)

func mouse_thirdperson() -> void:
	arm.rotation_degrees.x = clamp(rotation_degrees.x-mouse_axis.y*(mouse_sensitivity / 100),-90,90)
	arm.rotation_degrees.y -= mouse_axis.x*(mouse_sensitivity / 100)
	head.rotation_degrees.x = arm.rotation_degrees.x
	neck.rotation_degrees.y = arm.rotation_degrees.y

func mouse_arm() -> void:
	arm.rotation_degrees.x = clamp(rotation_degrees.x-mouse_axis.y*(mouse_sensitivity / 100),-70,70)
	arm.rotation_degrees.y -= mouse_axis.x*(mouse_sensitivity / 100)

func mouse_freecam() -> void:
	pass