aboutsummaryrefslogtreecommitdiff
path: root/scripts/movement.gd
blob: da4227eaacf635bf18097ba9bf4a56009ebc243c (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
extends KinematicBody

var vel = Vector3()

var camera = Camera

var _mouse = Vector2()
var sensitivity_mouse = 10

var placer : Spatial

var _pivot
var _pivot_dist
var _orbit_timer

func _ready():	
	camera = $camera
	placer = $placer

func look_free (m):
	self.transform.basis = self.transform.basis.rotated(Vector3.UP, m.x)
	var rrrot = self.transform.basis.get_rotation_quat()
	self.transform.basis = self.transform.basis.rotated(rrrot*Vector3.RIGHT, m.y)
	
func look_orbit(m):	
	var diff : Vector3 = self.transform.origin - _pivot
	_pivot_dist = diff.length()
	var orbit : Basis = Transform.looking_at(diff, Vector3.UP).basis
	orbit = orbit.rotated(Vector3.UP, m.x)
	orbit = orbit.rotated(orbit.get_rotation_quat()*Vector3.RIGHT, -m.y)
	
	self.transform.origin = _pivot - orbit.z * _pivot_dist
	
	diff = self.transform.origin - _pivot
	self.transform.basis = self.transform.basis.slerp( \
		Transform.looking_at(-diff, Vector3.UP).basis, _orbit_timer)
	

func _move_input(delta):
	var m = _mouse
	m *= delta
	_mouse = Vector2()

	if Input.is_action_just_pressed("orbit"):
		if placer.colliding:
			_pivot = placer.collision_point
			var diff : Vector3 = self.transform.origin - _pivot
			_pivot_dist = diff.length()
			_orbit_timer = 0
		else: _pivot = null
		
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	
	if Input.is_action_pressed("orbit"):
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		if _pivot != null:
			_orbit_timer = min(1, _orbit_timer+delta * 2)
			look_orbit(m)
		else:
			look_free(m)
	elif Input.is_action_pressed("look"):
		look_free(m)
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

	var input_movement_vector = Vector3()
	if Input.is_action_pressed("movement_forward"):
		input_movement_vector.z -= 1
	if Input.is_action_pressed("movement_backward"):
		input_movement_vector.z += 1
	if Input.is_action_pressed("movement_left"):
		input_movement_vector.x -= 1
	if Input.is_action_pressed("movement_right"):
		input_movement_vector.x += 1
	if Input.is_action_pressed("movement_up"):
		input_movement_vector.y += 1
	if Input.is_action_pressed("movement_down"):
		input_movement_vector.y -= 1
	input_movement_vector = input_movement_vector.normalized()
	
	var dir : Vector3 = self.transform.basis.get_rotation_quat() * input_movement_vector
	
	vel += dir * delta;
	vel *= 0.8
	
	var collision = self.move_and_collide(vel)
	if collision: vel = Vector3(0, 0, 0)

func _physics_process(delta):
	_move_input(delta)

func _input(event):
	if event is InputEventMouseMotion:
		_mouse += Vector2(
			deg2rad(event.relative.x) * -1, 
			deg2rad(event.relative.y) * -1) * sensitivity_mouse