aboutsummaryrefslogtreecommitdiff
path: root/scripts/movement.gd
blob: 587b338d67ac5e4c72a943d02c911abf3337c25c (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
extends KinematicBody

var in_editor : bool = false

var vel = Vector3()

var camera = Camera

var _mouse = Vector2()
var sensitivity_mouse = 10

var placer : Spatial

var _pivot
var _pivot_dist
var _orbit_timer

var _world : VoxelMesh

var _gui : Control

var _turret_holder : Node
var _attach_point_holder : Node
var path_holder : Node
var load_turrets : Node
var ptr : Node

var _enemies : Node

var sel = {
	"type": "", 
	"name": ""
}

func _ready():	
	camera = $camera
	placer = $placer
	
	var root = get_tree().root.get_child(0)
	
	path_holder = root.get_node("path")
	_gui = root.get_node("gui")
	_enemies = root.get_node("enemies")
	ptr = root.find_node("pointer");
	
	var saveload = root.get_node("saveload")
	load_turrets = saveload.get_node("load_turrets")
	
	_gui.refresh(in_editor)
	
	refresh_path()

func refresh_gui():
	_gui.refresh(in_editor)

func gui_editor_toggle_event ():
	in_editor = !in_editor
	_gui.refresh(in_editor)
	refresh_path()
	
func gui_start_wave_event ():
	refresh_path()
	_enemies.spawn()
	_gui.refresh(in_editor)
	
func refresh_path():
	if in_editor:
		path_holder.show()
	else:
		var _res = path_holder.load_nodes()
		path_holder.hide()
	
func selected_event (sel_name, sel_type):
	sel.name = sel_name
	sel.type = sel_type
	_gui.refresh(in_editor)
	
	if sel_type == "turrets":
		var info = load_turrets.info[sel.name]
		var model = load_turrets.models[info.model_name]
		var instance_model = model.instance()
		instance_model.name = "preview"
		ptr.add_child(instance_model)
	elif sel_type != "":
		ptr.get_node("base").visible = true

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