-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.gd
254 lines (199 loc) · 7.12 KB
/
Main.gd
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
extends Spatial
onready var gui = $GUI
onready var cube = $Cube
onready var camera = $Camera
var raycast_data = [Vector3(), Vector3()]
var raycast_plane = [Vector3(), Vector3()]
var raycast_event = false
var select_event = false
var selected_face : Face = null
var moving_camera = false
var screen_touches = [Vector2(), Vector2()]
export (float, 0.2, 5) var move_threshold = 0.5
export (float, 0.2, 5) var mouse_sensitivity = 1.0
export (float, 0.2, 5) var touch_sensitivity = 1.0
var ready_to_solve = false
var solving = false
var timer_running = false
var time = 0.0
signal first_move_made
func _ready():
connect("first_move_made", self, "_on_first_move")
cube.connect("solved", self, "_on_cube_solved")
gui.connect("gui_scramble", self, "_on_gui_scramble")
gui.connect("gui_reset", self, "_on_gui_reset")
gui.connect("gui_change_size", self, "_on_gui_change_size")
gui.connect("gui_show_textures", self, "_on_gui_show_textures")
gui.connect("gui_check_orientation", self, "_on_gui_check_orientation")
var cam_offset = camera.global_transform.origin
camera.transform = camera.transform.translated(-cam_offset).rotated(Vector3.RIGHT, -PI / 6
).rotated(Vector3.UP, PI / 6 * 4 / 3).translated(cam_offset)
# Delete QualityLights on mobile to increase performance
if OS.get_name() == "Android":
$QualityLights.queue_free()
else:
$Camera/PerformanceLights.queue_free()
func _process(delta):
if timer_running:
gui.update_time_label(time)
func _physics_process(delta):
if raycast_event:
raycast_event = false
if select_event:
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(raycast_data[0], raycast_data[1])
if result and result.collider is Face:
selected_face = result.collider as Face
highlight_selection(true)
raycast_plane[0] = selected_face.global_transform.basis.z
raycast_plane[1] = result.position
else:
selected_face = null
else:
if selected_face:
# intersect raycast with plane from first raycast
var direction = (raycast_data[1] - raycast_data[0]).normalized()
var n = raycast_plane[0]
var p = raycast_plane[1]
var l = raycast_data[0]
var d = (p - l).dot(n) / direction.dot(n)
var intersection = l + d * direction
var move = intersection - p
if d < 0:
move = -move
highlight_selection(false)
var view = camera.get_viewport().size
var unproj = camera.unproject_position(intersection) - camera.unproject_position(p)
if unproj.length() >= 0.1 * move_threshold * min(view.x, view.y):
process_move(move)
if ready_to_solve:
emit_signal("first_move_made")
selected_face = null
if timer_running:
time += delta
func _input(event):
if not gui.menu_open:
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if event.is_pressed():
select_event = true
else:
select_event = false
cast_ray(event.position)
elif event.button_index == BUTTON_RIGHT:
if event.is_pressed():
moving_camera = true
cancel_selection()
else:
moving_camera = false
elif event.button_index == BUTTON_WHEEL_DOWN:
if camera.global_transform.origin.length() < cube.size * 3:
camera.translate_object_local(Vector3(0, 0, 0.5))
elif event.button_index == BUTTON_WHEEL_UP:
if camera.global_transform.origin.length() > cube.size * 1.5:
camera.translate_object_local(Vector3(0, 0, -0.5))
elif event is InputEventMouseMotion and moving_camera:
var delta = event.relative / 300 * mouse_sensitivity
var pos = camera.transform.origin
camera.transform = camera.transform.translated(-pos).rotated(camera.global_transform.basis.x,
-delta.y).rotated(camera.global_transform.basis.y, -delta.x).translated(pos)
elif event is InputEventScreenTouch:
if event.index == 0:
if event.is_pressed():
screen_touches[0] = event.position
select_event = true
else:
select_event = false
if moving_camera:
moving_camera = false
cast_ray(event.position)
elif event.index == 1:
if event.is_pressed():
screen_touches[1] = event.position
moving_camera = true
cancel_selection()
elif event is InputEventScreenDrag:
if moving_camera and event.index <= 1:
var idx = event.index
var pos = camera.transform.origin
var basis = camera.global_transform.basis
var rot_z = (screen_touches[idx] - screen_touches[idx - 1]).angle_to(event.position - screen_touches[idx - 1])
var rot_x = -(event.position.x - screen_touches[idx].x) / 1000 * touch_sensitivity
var rot_y = -(event.position.y - screen_touches[idx].y) / 1000 * touch_sensitivity
var zoom = -((event.position - screen_touches[idx - 1]).length()
- (screen_touches[idx] - screen_touches[idx - 1]).length()) / 200
if camera.global_transform.origin.length() < cube.size * 1.5 and zoom < 0:
zoom = 0
if camera.global_transform.origin.length() > cube.size * 3 and zoom > 0:
zoom = 0
camera.transform = camera.transform.translated(-pos).rotated(basis.z, rot_z
).rotated(basis.x, rot_y).rotated(basis.y, rot_x).translated(pos).translated(Vector3(0, 0, zoom))
screen_touches[idx] = event.position
func cast_ray(screen_pos : Vector2):
raycast_event = true
var distance = camera.global_transform.origin.length() * 2
raycast_data[0] = camera.project_ray_origin(screen_pos)
raycast_data[1] = raycast_data[0] + camera.project_ray_normal(screen_pos) * distance
func cancel_selection():
if select_event:
select_event = false
if selected_face:
highlight_selection(false)
selected_face = null
func highlight_selection(enabled : bool):
selected_face.mat.emission_enabled = enabled
func process_move(vec : Vector3):
var face_basis = selected_face.global_transform.basis
var cube_basis = cube.global_transform.basis
var dot = face_basis.z.dot(cube_basis.x)
if abs(dot) > 0.9:
cube.move_from_raycast(selected_face, Vector3(1, 0, 0) * sign(dot), vec)
else:
dot = face_basis.z.dot(cube_basis.y)
if abs(dot) > 0.9:
cube.move_from_raycast(selected_face, Vector3(0, 1, 0) * sign(dot), vec)
else:
dot = face_basis.z.dot(cube_basis.z)
cube.move_from_raycast(selected_face, Vector3(0, 0, 1) * sign(dot), vec)
func _on_gui_scramble():
cube.scramble_cube()
solving = false
ready_to_solve = true
time = 0.0
timer_running = false
gui.update_time_label(time)
func _on_gui_reset():
cube.reset_cube()
solving = false
time = 0.0
timer_running = false
gui.update_time_label(time)
func _on_gui_change_size(size : int):
cube.set_size(size)
solving = false
time = 0.0
timer_running = false
gui.update_time_label(time)
func _on_gui_show_textures(show_textures : bool):
cube.show_textures = show_textures
cube.update_colors()
func _on_gui_check_orientation(check_orientation : bool):
cube.check_orientation = check_orientation
func _on_first_move():
ready_to_solve = false
solving = true
reset_timer()
start_timer()
func _on_cube_solved():
if solving:
stop_timer()
gui.update_time_label(time)
solving = false
print("Solved!")
func start_timer():
timer_running = true
func stop_timer():
timer_running = false
func reset_timer():
time = 0.0
gui.update_time_label(time)