summaryrefslogtreecommitdiffstats
path: root/Player.gd
blob: 99c6abf4a4d8a578ec6990b3261d8b6efd3f0e04 (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
extends KinematicBody

export var speed = 20
export var ang_speed = PI
export var mouse_dpi = 0.2
export var pitch_min = -0.8
export var pitch_max = 0.8

func joystick(neg_x: String,pos_x: String,neg_y: String,pos_y: String):
	var joystick = Input.get_vector(neg_x,pos_x,neg_y,pos_y)
	var joystrength = abs(joystick.length())
	if joystick != Vector2.ZERO:
		joystick = joystick.normalized()
	return Vector3(joystick.x,joystick.y,joystrength)

func _physics_process(delta):
	var d_ang_speed = ang_speed * delta

	var rotation = Transform(self.rotation, Vector3.ZERO)
	var up = rotation * Vector3.UP
	var forward = rotation * Vector3.FORWARD
	var right = rotation * Vector3.RIGHT

	var stick = joystick("move_left", "move_right", "move_back", "move_forward")
	var movement = Vector3.ZERO
	movement += right * stick.x * stick.z * speed
	movement += forward * stick.y * stick.z * speed
	if Input.is_action_pressed("move_up"):
		movement += up * speed
	if Input.is_action_pressed("move_down"):
		movement -= up * speed
	movement = move_and_slide(movement, Vector3.UP)

	stick = joystick("look_right", "look_left", "look_down", "look_up")
	var mouse_movement = MouseInput.get_mouse_movement()
	if stick.z == 0.0:
		stick.z = mouse_movement.length() * mouse_dpi
		mouse_movement = -mouse_movement.normalized()
		stick.x = mouse_movement.x
		stick.y = mouse_movement.y
	var pitch = forward.dot(Vector3.UP)
	if (pitch >= pitch_min and stick.y < 0) or (pitch <= pitch_max and stick.y > 0):
		rotate(right, stick.y * stick.z * d_ang_speed)
	rotate(Vector3.UP, stick.x * stick.z * d_ang_speed)

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _notification(what):
	if what == MainLoop.NOTIFICATION_WM_FOCUS_OUT:
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

func _input(event):
	if event.is_action_pressed("ui_cancel"):
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
		if event.is_action_pressed("click"):
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	else:
		if event is InputEventMouseMotion:
			MouseInput.add_mouse_movement(event.relative)