-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube.gd
528 lines (459 loc) · 12.2 KB
/
Cube.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#tool
extends Spatial
class_name Cube
onready var tween = $Tween
export (int, 2, 7) var size = 3 setget set_size
export var colors = [Color(1, 1, 1), Color(1, 1, 0), Color(0.15, 0.9, 0.15),
Color(0.15, 0.15, 0.9), Color(1, 0.5, 0), Color(0.9, 0.15, 0.15)]
var textures = [load("res://Assets/Textures/Godot_White.png"), load("res://Assets/Textures/Godot_Yellow.png"),
load("res://Assets/Textures/Godot_Green.png"), load("res://Assets/Textures/Godot_Blue.png"),
load("res://Assets/Textures/Godot_Orange.png"), load("res://Assets/Textures/Godot_Red.png")]
var show_textures = false
var cells = []
var rotating_cells = []
var rotating = false
var rotation_axis = Vector3()
var rotation_angle = 0.0
var t = 0.0
export (float, 0.1, 1.0) var rotation_duration = 0.25
export (float, 0.0, 0.1) var scramble_time = 0.001
var animation_time = 0.0
var move_queue = []
var moves = []
var move_idx = 0
export (bool) var check_orientation = false
var cell_scene = preload("res://Cell.tscn")
signal solved
func _ready():
init_cube()
func _input(event):
if event is InputEventKey and event.is_pressed() and not event.echo:
var move = ""
match event.scancode:
KEY_F:
move = "F"
KEY_B:
move = "B"
KEY_L:
move = "L"
KEY_R:
move = "R"
KEY_U:
move = "U"
KEY_D:
move = "D"
KEY_M:
move = "M"
KEY_E:
move = "E"
KEY_S:
move = "S"
KEY_X:
move = "X"
KEY_Y:
move = "Y"
KEY_Z:
move = "Z"
KEY_PAGEUP:
undo_move()
return
KEY_PAGEDOWN:
redo_move()
return
_:
return
if Input.is_key_pressed(KEY_ALT):
move = move.to_lower()
var modifier = ""
if Input.is_key_pressed(KEY_SHIFT):
modifier = "'"
elif Input.is_key_pressed(KEY_CONTROL):
modifier = "2"
move = move + modifier
add_move_from_notation(move)
func _process(delta):
if rotating:
while animation_time <= 0 and not move_queue.empty():
rotate_cells(rotation_angle)
play_next_move()
if t < 1:
rotate_cells(rotation_angle, t)
else:
tween.remove(self)
rotating = false
t = 0
rotate_cells(rotation_angle)
is_solved(check_orientation)
else:
play_next_move()
func play_move(axis : Vector3, pos1 : float, pos2 : float, angle : int = 1, time : float = rotation_duration):
move_queue.append([axis, pos1, pos2, angle, time])
func add_move(axis : Vector3, pos1 : float, pos2 : float, angle : int = 1, time : float = rotation_duration):
play_move(axis, pos1, pos2, angle, time)
write_move(axis, pos1, pos2, angle)
func play_move_from_notation(move : String):
var move_full = move
var axis = Vector3.ZERO
var pos1 = 0.5 - size / 2.0
var pos2 = pos1
var angle = 1
var invert = 1
var time = rotation_duration
if move.ends_with("2"):
angle *= 2
time *= 2
move = move.left(move.length() - 1)
if move.ends_with("'"):
invert = -1
move = move.left(move.length() - 1)
match move:
"F", "f":
axis = Vector3(0, 0, 1)
pos1 = 0.5 - size / 2.0 + size - 1
if move == "F":
pos2 = pos1
else:
pos2 = pos1 - 1
angle *= -1
"B", "b":
axis = Vector3(0, 0, 1)
if move == "b":
pos2 = pos1 + 1
"L", "l":
axis = Vector3(1, 0, 0)
if move == "l":
pos2 = pos1 + 1
"R", "r":
axis = Vector3(1, 0, 0)
pos1 = 0.5 - size / 2.0 + size - 1
if move == "R":
pos2 = pos1
else:
pos2 = pos1 - 1
angle *= -1
"U", "u":
axis = Vector3(0, 1, 0)
pos1 = 0.5 - size / 2.0 + size - 1
if move == "U":
pos2 = pos1
else:
pos2 = pos1 - 1
angle *= -1
"D", "d":
axis = Vector3(0, 1, 0)
if move == "d":
pos2 = pos1 + 1
"M":
if size != 3:
OS.alert("Invalid move for size %s: \"%s\"" % [str(size), move_full])
return
axis = Vector3(1, 0, 0)
pos1 += 1
pos2 = pos1
"E":
if size != 3:
OS.alert("Invalid move for size %s: \"%s\"" % [str(size), move_full])
return
axis = Vector3(0, 1, 0)
pos1 += 1
pos2 = pos1
angle *= -1
"S":
if size != 3:
OS.alert("Invalid move for size %s: \"%s\"" % [str(size), move_full])
return
axis = Vector3(0, 0, 1)
pos1 += 1
pos2 = pos1
angle *= -1
"X", "x":
axis = Vector3(1, 0, 0)
pos1 = 0.5 - size / 2.0 + size - 1
angle *= -1
"Y", "y":
axis = Vector3(0, 1, 0)
pos1 = 0.5 - size / 2.0 + size - 1
angle *= -1
"Z", "z":
axis = Vector3(0, 0, 1)
pos1 = 0.5 - size / 2.0 + size - 1
angle *= -1
if axis == Vector3.ZERO:
OS.alert("Invalid move: %s" % [move_full])
return
angle *= invert
play_move(axis, pos1, pos2, angle, time)
func add_move_from_notation(move : String):
play_move_from_notation(move)
write_move_from_notation(move)
func write_move(axis : Vector3, pos1 : float, pos2 : float, angle : int):
var move = ""
match axis:
Vector3(1, 0, 0):
if pos1 == 0.5 - size / 2.0:
if pos2 == pos1:
move = "L"
elif pos2 == pos1 + 1:
move = "l"
elif pos2 == pos1 + size - 1:
move = "X"
elif pos1 == 0.5 - size / 2.0 + size - 1:
if pos2 == pos1:
move = "R'"
elif pos2 == pos1 - 1:
move = "r'"
elif pos2 == 0.5 - size / 2.0:
move = "X'"
elif pos1 == 0.5 - size / 2.0 + 1:
if pos2 == pos1:
move = "M"
Vector3(0, 1, 0):
if pos1 == 0.5 - size / 2.0:
if pos2 == pos1:
move = "D"
elif pos2 == pos1 + 1:
move = "d"
elif pos2 == pos1 + size - 1:
move = "Y"
elif pos1 == 0.5 - size / 2.0 + size - 1:
if pos2 == pos1:
move = "U'"
elif pos2 == pos1 - 1:
move = "u'"
elif pos2 == 0.5 - size / 2.0:
move = "Y'"
elif pos1 == 0.5 - size / 2.0 + 1:
if pos2 == pos1:
move = "E'"
Vector3(0, 0, 1):
if pos1 == 0.5 - size / 2.0:
if pos2 == pos1:
move = "B"
elif pos2 == pos1 + 1:
move = "b"
elif pos2 == pos1 + size - 1:
move = "Z"
elif pos1 == 0.5 - size / 2.0 + size - 1:
if pos2 == pos1:
move = "F'"
elif pos2 == pos1 - 1:
move = "f'"
elif pos2 == 0.5 - size / 2.0:
move = "Z'"
elif pos1 == 0.5 - size / 2.0 + 1:
if pos2 == pos1:
move = "S'"
if move == "":
print("Error: could not write move")
return
if angle % 2 == 0:
move += "2"
if angle < 0:
if "'" in move:
move = move.replace("'", "")
else:
move += "'"
write_move_from_notation(move)
func write_move_from_notation(move : String):
if move_idx < moves.size():
moves.resize(move_idx)
moves.append(move)
move_idx += 1
print(move)
func undo_move():
if moves.empty() or move_idx <= 0:
return
move_idx -= 1
var move = moves[move_idx]
if "'" in move:
move = move.replace("'", "")
else:
if move.ends_with("2"):
move = move.replace("2", "'2")
else:
move += "'"
play_move_from_notation(move)
func redo_move():
if moves.empty() or move_idx >= moves.size():
return
play_move_from_notation(moves[move_idx])
move_idx += 1
func play_next_move():
if not move_queue.empty():
var move = move_queue[0]
rotate_slice(move[0], move[1], move[2], move[3], move[4])
move_queue.remove(0)
func rotate_cells(angle : int = 1, weight : float = 1.0):
for cell in rotating_cells:
cell.transform = cell.rotated_around_origin(rotation_axis, angle * weight)
if weight == 1.0:
cell.round_transform()
func rotate_slice(axis : Vector3, pos1 : float, pos2 : float, angle : int = 1, time : float = rotation_duration):
rotating_cells = []
var num = abs(pos2 - pos1)
var start = min(pos1, pos2)
var targets = []
for i in range(num + 1):
targets.append((start + i) * axis)
for cell in cells:
if axis * cell.transform.origin in targets:
rotating_cells.append(cell)
cell.set_target_rotation(axis, angle)
rotation_axis = axis
rotation_angle = angle
rotating = true
animation_time = time
tween.interpolate_method(self, "update_tween", 0, 1, animation_time, tween.TRANS_SINE, Tween.EASE_IN_OUT)
tween.start()
func update_tween(x : float):
t = x
func scramble_cube():
randomize()
var num = 20 + randi() % 20
for i in range(num):
var axis = randi() % 3
match axis:
0:
axis = Vector3(1, 0, 0)
1:
axis = Vector3(0, 1, 0)
2:
axis = Vector3(0, 0, 1)
var angle_sign = 1 - 2 * (randi() % 2)
var pos = 0.5 - size / 2.0 + randi() % size
var angle = (1 + randi() % 2) * angle_sign
add_move(axis, pos, pos, angle, scramble_time)
func is_solved(check_face_orientation = false):
for i in range(6):
var normal = Vector3.ZERO
var tangent = Vector3.ZERO
for cell in cells:
for face in cell.faces:
if face.side != i:
continue
else:
if normal == Vector3.ZERO:
normal = (cell.transform * face.transform).basis.z
else:
if (cell.transform * face.transform).basis.z != normal:
return false
if check_face_orientation:
if tangent == Vector3.ZERO:
tangent = (cell.transform * face.transform).basis.x
else:
if (cell.transform * face.transform).basis.x != tangent:
return false
emit_signal("solved")
return true
func move_from_raycast(face : Face, axis : Vector3, vec : Vector3, angle : int = 1):
var pos = face.get_parent().transform.origin
match axis:
Vector3(1, 0, 0), Vector3(-1, 0, 0):
if abs(vec.dot(Vector3(0, 1, 0))) >= abs(vec.dot(Vector3(0, 0, 1))):
vec = Vector3(0, 1, 0) * sign(vec.y)
else:
vec = Vector3(0, 0, 1) * sign(vec.z)
Vector3(0, 1, 0), Vector3(0, -1, 0):
if abs(vec.dot(Vector3(1, 0, 0))) >= abs(vec.dot(Vector3(0, 0, 1))):
vec = Vector3(1, 0, 0) * sign(vec.x)
else:
vec = Vector3(0, 0, 1) * sign(vec.z)
Vector3(0, 0, 1), Vector3(0, 0, -1):
if abs(vec.dot(Vector3(1, 0, 0))) >= abs(vec.dot(Vector3(0, 1, 0))):
vec = Vector3(1, 0, 0) * sign(vec.x)
else:
vec = Vector3(0, 1, 0) * sign(vec.y)
var rot_axis = axis.cross(vec)
pos = (pos * rot_axis).dot(rot_axis)
match rot_axis:
Vector3(-1, 0, 0), Vector3(0, -1, 0), Vector3(0, 0, -1):
rot_axis = -rot_axis
angle = -angle
add_move(rot_axis, pos, pos, angle)
func set_size(s : int):
size = s
delete_cube()
init_cube()
func delete_cube():
for cell in cells:
cell.queue_free()
cells = []
func init_cube():
var type = 0
for j in range(size):
for i in range(size):
for k in range(size):
if j > 0 and j < size - 1:
if i > 0 and i < size - 1:
if k > 0 and k < size - 1:
continue
else:
type = Cell.CENTER
else:
if k > 0 and k < size - 1:
type = Cell.CENTER
else:
type = Cell.EDGE
else:
if i > 0 and i < size - 1:
if k > 0 and k < size - 1:
type = Cell.CENTER
else:
type = Cell.EDGE
else:
if k > 0 and k < size - 1:
type = Cell.EDGE
else:
type = Cell.CORNER
var cell = cell_scene.instance()
cells.append(cell)
add_child(cell)
cell.translate(Vector3(i, j, k) - (size - 1) / 2.0 * Vector3.ONE)
# Normalized cell coordinates
var a = (i - size / 2.0 + 0.5) * 2 / (size - 1)
var b = (j - size / 2.0 + 0.5) * 2 / (size - 1)
var c = (k - size / 2.0 + 0.5) * 2 / (size - 1)
cell.init_cell(type, a, b, c)
update_faces_uv()
func reset_cube():
rotating = false
t = 0
move_queue.clear()
moves.clear()
move_idx = 0
for cell in cells:
cell.reset()
func update_faces_uv(tile = false):
if tile:
for cell in cells:
for face in cell.faces:
face.set_face_uv(Vector3.ONE, Vector3(0, 0, 0))
else:
var uv_size = Vector3.ONE / size
for cell in cells:
# Transform cell pos into offset-normalized cell coordinates
var pos = cell.transform.origin
pos = Vector3((pos.x - size / 2.0 - 0.5) / size + 1,
(pos.y - size / 2.0 - 0.5) / size + 1,
(pos.z - size / 2.0 - 0.5) / size + 1)
for face in cell.faces:
var uv_offset = Vector3.ZERO
var face_normal = (cell.transform * face.transform).basis.z
match face_normal:
Vector3(1, 0, 0):
uv_offset = Vector3(1 - 1.0 / size - pos.z, 1 - 1.0 / size - pos.y, 0)
Vector3(-1, 0, 0):
uv_offset = Vector3(pos.z, 1 - 1.0 / size - pos.y, 0)
Vector3(0, 1, 0):
uv_offset = Vector3(pos.x, pos.z, 0)
Vector3(0, -1, 0):
uv_offset = Vector3(pos.x, 1 - 1.0 / size - pos.z, 0)
Vector3(0, 0, 1):
uv_offset = Vector3(pos.x, 1 - 1.0 / size - pos.y, 0)
Vector3(0, 0, -1):
uv_offset = Vector3(1 - 1.0 / size - pos.x, 1 - 1.0 / size - pos.y, 0)
face.set_face_uv(uv_size, uv_offset)
func update_colors():
for cell in cells:
for face in cell.faces:
face.color_face()