-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.js
More file actions
739 lines (687 loc) · 28.7 KB
/
actions.js
File metadata and controls
739 lines (687 loc) · 28.7 KB
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import * as THREE from 'three';
import { scene, camera, renderer, controls, bixieCube, cubieSize, gap, numPerAxis, getOffset } from './init.js';
// Global flags, history, and queues for rotations.
let isRotating = false;
const rotationQueue = [];
let undoQueue = []; // <-- new undo queue
let moveHistory = [];
let isUndoing = false;
let turningLayers = 1;
const eps = 0.001;
let difficulty = 5;
function rotateFace(face, angle, layersCount = 1, sound = true) {
if (isRotating) {
rotationQueue.push({ face, angle, layersCount });
return;
}
isRotating = true;
// Play twist_sound.
if (sound) {
const audio = document.getElementById('twist_sound');
try {
audio.play();
} catch (error) {
// console.error('Error playing audio:', error);
}
}
const rotatingCubies = [];
let boundary;
switch (face) {
case 'front':
boundary = getOffset() - ((layersCount - 1) * (cubieSize + gap));
bixieCube.children.forEach(cubie => {
if (cubie.position.z > boundary - eps) { rotatingCubies.push(cubie); }
});
break;
case 'back':
boundary = -getOffset() + ((layersCount - 1) * (cubieSize + gap));
bixieCube.children.forEach(cubie => {
if (cubie.position.z < boundary + eps) { rotatingCubies.push(cubie); }
});
break;
case 'right':
boundary = getOffset() - ((layersCount - 1) * (cubieSize + gap));
bixieCube.children.forEach(cubie => {
if (cubie.position.x > boundary - eps) { rotatingCubies.push(cubie); }
});
break;
case 'left':
boundary = -getOffset() + ((layersCount - 1) * (cubieSize + gap));
bixieCube.children.forEach(cubie => {
if (cubie.position.x < boundary + eps) { rotatingCubies.push(cubie); }
});
break;
case 'top':
boundary = getOffset() - ((layersCount - 1) * (cubieSize + gap));
bixieCube.children.forEach(cubie => {
if (cubie.position.y > boundary - eps) { rotatingCubies.push(cubie); }
});
break;
case 'bottom':
boundary = -getOffset() + ((layersCount - 1) * (cubieSize + gap));
bixieCube.children.forEach(cubie => {
if (cubie.position.y < boundary + eps) { rotatingCubies.push(cubie); }
});
break;
}
const tempGroup = new THREE.Group();
scene.add(tempGroup);
rotatingCubies.forEach(cubie => {
bixieCube.remove(cubie);
tempGroup.add(cubie);
});
let rotationAxis = new THREE.Vector3();
if (face === 'front' || face === 'back') {
rotationAxis.set(0, 0, 1);
} else if (face === 'left' || face === 'right') {
rotationAxis.set(1, 0, 0);
} else if (face === 'top' || face === 'bottom') {
rotationAxis.set(0, 1, 0);
}
const duration = 200; // duration in milliseconds
const startTime = performance.now();
const initialQuat = tempGroup.quaternion.clone();
const deltaQuat = new THREE.Quaternion().setFromAxisAngle(rotationAxis, angle);
const targetQuat = deltaQuat.multiply(initialQuat);
function animateRotation(now) {
const elapsed = now - startTime;
const t = Math.min(elapsed / duration, 1);
tempGroup.quaternion.copy(initialQuat);
tempGroup.quaternion.slerp(targetQuat, t);
if (t < 1) {
requestAnimationFrame(animateRotation);
} else {
tempGroup.updateMatrixWorld();
rotatingCubies.forEach(cubie => {
tempGroup.remove(cubie);
cubie.applyMatrix4(tempGroup.matrixWorld);
// Recalculate grid indices based on the new position
const currentOffset = getOffset();
cubie.userData.i = Math.round((cubie.position.x + currentOffset) / (cubieSize + gap));
cubie.userData.j = Math.round((cubie.position.y + currentOffset) / (cubieSize + gap));
cubie.userData.k = Math.round((cubie.position.z + currentOffset) / (cubieSize + gap));
bixieCube.add(cubie);
});
scene.remove(tempGroup);
isRotating = false;
if (!isUndoing) {
// if finalizing a twist, store the whole move
if (Math.abs(angle) === twist * 3) {
angle = twist * 4 * Math.sign(angle);
}
// but don't store preview twists
if (Math.abs(angle) !== twist) {
moveHistory.push({ face, angle, layersCount });
if (checkCubeSolved() && startGameBtn.textContent != 'Start Game') {
console.log("Cube solved!");
celebrateWin(); // <-- Trigger the celebration.
stopGame();
startGameBtn.textContent = 'Start Game';
return;
}
}
}
if (undoQueue.length > 0) {
const nextUndo = undoQueue.shift();
rotateFace(nextUndo.face, nextUndo.angle, nextUndo.layersCount);
} else if (rotationQueue.length > 0) {
const next = rotationQueue.shift();
rotateFace(next.face, next.angle, next.layersCount);
} else {
// End the undo chain only when no more undo moves are queued.
isUndoing = false;
}
// turningLayers = 1;
}
}
requestAnimationFrame(animateRotation);
}
// helper to check if the cube is solved.
function checkCubeSolved() {
const faceColors = {
front: new Set(),
back: new Set(),
left: new Set(),
right: new Set(),
top: new Set(),
bottom: new Set(),
};
const checkFaces = [
{ test: cubie => Math.abs(cubie.position.z - getOffset()) < eps, face: 'front', materialIndex: 4 },
{ test: cubie => Math.abs(cubie.position.z + getOffset()) < eps, face: 'back', materialIndex: 5 },
{ test: cubie => Math.abs(cubie.position.x + getOffset()) < eps, face: 'left', materialIndex: 1 },
{ test: cubie => Math.abs(cubie.position.x - getOffset()) < eps, face: 'right', materialIndex: 0 },
{ test: cubie => Math.abs(cubie.position.y - getOffset()) < eps, face: 'top', materialIndex: 2 },
{ test: cubie => Math.abs(cubie.position.y + getOffset()) < eps, face: 'bottom', materialIndex: 3 }
];
// Tally up the material colors of each face
bixieCube.children.forEach(cubie => {
checkFaces.forEach(({ test, face, materialIndex }) => {
if (test(cubie)) {
faceColors[face].add(cubie.material[materialIndex].color.getHex());
}
});
});
// If faceColors set.size === 1, every set of faces is one, uniform color
return Object.values(faceColors).every(set => set.size === 1);
}
// Find (t)opmost, (l)eftmost, (r)ightmost, or (b)ottommost face.
function findFace(direction = 't') {
// 1. Define the center points of each face in world space.
const faceCenters = {
front: new THREE.Vector3(0, 0, getOffset()),
back: new THREE.Vector3(0, 0, -getOffset()),
left: new THREE.Vector3(-getOffset(), 0, 0),
right: new THREE.Vector3(getOffset(), 0, 0),
top: new THREE.Vector3(0, getOffset(), 0),
bottom: new THREE.Vector3(0, -getOffset(), 0),
};
// 2. Project each face center to screen space.
const screenPositions = {};
for (const face in faceCenters) {
const center = faceCenters[face].clone();
center.project(camera);
screenPositions[face] = {
x: (center.x + 1) / 2 * renderer.domElement.clientWidth,
y: (-center.y + 1) / 2 * renderer.domElement.clientHeight,
z: center.z
};
}
// 3. Find the face closest to direction.
let bestFace = null;
let bestValue = "bd".includes(direction) ? -Infinity : Infinity; // Initialize for bottommost or topmost
if (direction === 'r') {
bestValue = -Infinity;
}
if (direction === 'k') {
bestValue = -Infinity;
}
for (const face in screenPositions) {
let currentValue;
//FIXME: It can't find the frontmost face because screenPositions is two dimensional
switch (direction) {
case 'f':
currentValue = screenPositions[face].z;
if (currentValue < bestValue) {
bestValue = currentValue;
bestFace = face;
}
break;
case 'u':
case 't':
currentValue = screenPositions[face].y;
if (currentValue < bestValue) {
bestValue = currentValue;
bestFace = face;
}
break;
case 'b':
case 'd':
currentValue = screenPositions[face].y;
if (currentValue > bestValue) {
bestValue = currentValue;
bestFace = face;
}
break;
case 'l':
currentValue = screenPositions[face].x;
if (currentValue < bestValue) {
bestValue = currentValue;
bestFace = face;
}
break;
case 'r':
currentValue = screenPositions[face].x;
if (currentValue > bestValue) {
bestValue = currentValue;
bestFace = face;
}
break;
case 'k':
currentValue = screenPositions[face].z;
if (currentValue > bestValue) {
bestValue = currentValue;
bestFace = face;
}
break;
default:
console.error("Invalid direction:", direction);
return null;
}
}
return bestFace;
}
// Key event handler for hotkeys
window.addEventListener('keydown', e => {
// If a number key 2-9 is pressed, set the layer mode.
if (e.key >= '1' && e.key <= '9') {
const n = parseInt(e.key, 10);
turningLayers = Math.max(1, Math.min(n, Math.floor(numPerAxis / 2)));
console.log('Rotate layers set to:', turningLayers);
document.getElementById("layersInput").value = turningLayers;
return;
}
// Hotkey for undo (last move)
if (e.key === 'z') {
if (moveHistory.length > 0) {
const lastMove = moveHistory.pop();
const undoMove = { face: lastMove.face, angle: -lastMove.angle, layersCount: lastMove.layersCount };
// If rotation is underway or queued then push it to the undo queue.
if (isRotating || rotationQueue.length > 0 || undoQueue.length > 0) {
undoQueue.push(undoMove);
} else {
isUndoing = true;
rotateFace(undoMove.face, undoMove.angle, undoMove.layersCount);
}
}
return;
}
// Process face rotation "fublr" keys.
let angle = -Math.PI / 2;
const theKey = e.key.toLowerCase();
if ("dfutblrk".includes(theKey)){
const face = findFace(theKey);
if (['front', 'top', 'right'].includes(face)) angle = -angle;
if ("DFUTBLRK".includes(e.key)) {
angle = -angle;
}
rotateFace(face, angle, turningLayers);
}
});
// Global variables for face dragging.
let dragStart = null;
let currentFace = null;
let dragCubie = null;
let faceCenterScreen = null;
const raycaster = new THREE.Raycaster();
// Face dragging handles
let selectedFace = null;
let dragDirection = 0;
const twist = Math.PI / 8;
function projectToScreen(pos3D) {
const pos = pos3D.clone().project(camera);
return {
x: (pos.x + 1) / 2 * renderer.domElement.clientWidth,
y: (-pos.y + 1) / 2 * renderer.domElement.clientHeight
};
}
function getFaceCandidates(intersection, box) {
const diffRight = Math.abs(intersection.point.x - box.max.x);
const diffLeft = Math.abs(intersection.point.x - box.min.x);
const diffTop = Math.abs(intersection.point.y - box.max.y);
const diffBottom = Math.abs(intersection.point.y - box.min.y);
const diffFront = Math.abs(intersection.point.z - box.max.z);
const diffBack = Math.abs(intersection.point.z - box.min.z);
const faceCandidates = [
{ face: 'right', diff: diffRight },
{ face: 'left', diff: diffLeft },
{ face: 'top', diff: diffTop },
{ face: 'bottom',diff: diffBottom },
{ face: 'front', diff: diffFront },
{ face: 'back', diff: diffBack }
];
faceCandidates.sort((a, b) => a.diff - b.diff);
selectedFace = faceCandidates[0].face;
return faceCandidates;
}
function computeFaceCenterScreen(box, face, camera, renderer) {
const centerX = (box.min.x + box.max.x) / 2;
const centerY = (box.min.y + box.max.y) / 2;
const centerZ = (box.min.z + box.max.z) / 2;
const faceCenter3D = new THREE.Vector3();
switch (face) {
case 'right': faceCenter3D.set(box.max.x, centerY, centerZ); break;
case 'left': faceCenter3D.set(box.min.x, centerY, centerZ); break;
case 'top': faceCenter3D.set(centerX, box.max.y, centerZ); break;
case 'bottom': faceCenter3D.set(centerX, box.min.y, centerZ); break;
case 'front': faceCenter3D.set(centerX, centerY, box.max.z); break;
case 'back': faceCenter3D.set(centerX, centerY, box.min.z); break;
}
const pos = faceCenter3D.clone().project(camera);
return {
x: (pos.x + 1) / 2 * renderer.domElement.clientWidth,
y: (-pos.y + 1) / 2 * renderer.domElement.clientHeight
};
}
function getCanvasRelativePosition(event) {
const canvasRect = renderer.domElement.getBoundingClientRect();
const devicePixelRatio = window.devicePixelRatio || 1
if (event.changedTouches){
event = event.changedTouches[0];
}
let clientX = event.clientX;
let clientY = event.clientY;
// Adjust for canvas offset and device pixel ratio
const x = clientX - canvasRect.left / devicePixelRatio;
const y = clientY - canvasRect.top / devicePixelRatio;
return { x, y };
}
function pressDown(event) {
event.preventDefault();
const devicePixelRatio = window.devicePixelRatio || 1
const { x, y } = getCanvasRelativePosition(event);
const mouse = new THREE.Vector2(
(x / renderer.domElement.clientWidth) * 2 - 1,
-(y / renderer.domElement.clientHeight) * 2 + 1
);
raycaster.setFromCamera(mouse, camera);
// Intersect with the cube's children to get a hit point on the cube.
const intersects = raycaster.intersectObjects(bixieCube.children, true);
if (intersects.length > 0) {
const intersection = intersects[0];
// Create a bounding box around the entire cube.
const box = new THREE.Box3().setFromObject(bixieCube);
// Compare the intersection point to each face of the box.
const faceCandidates = getFaceCandidates(intersection, box);
selectedFace = faceCandidates[0].face;
// Compute the 3D center of the selected face.
faceCenterScreen = computeFaceCenterScreen(box, selectedFace, camera, renderer);
dragStart = { x, y };
controls.enabled = false;
}
}
renderer.domElement.addEventListener('mousedown', pressDown);
renderer.domElement.addEventListener('touchstart', pressDown);
function turnDirection(start, end){
const dx = end.x - start.x;
const dy = end.y - start.y;
const distance = Math.sqrt(dx * dx + dy * dy);
// Only proceed if the drag is significant.
if (distance > 100) {
// Compute vectors from the face center (screen coords) to the drag start and end.
const v1 = { x: start.x - faceCenterScreen.x, y: start.y - faceCenterScreen.y };
const v2 = { x: end.x - faceCenterScreen.x, y: end.y - faceCenterScreen.y };
// use z-component of the cross product to get direction.
const cross = v1.x * v2.y - v1.y * v2.x;
let angle = cross > 0 ? -1: 1;
if (selectedFace === 'bottom' || selectedFace === 'left' || selectedFace === 'back') {
angle = -angle;
}
return angle;
}
return null;
}
function release(event) {
event.preventDefault();
const { x, y } = getCanvasRelativePosition(event);
if (selectedFace && faceCenterScreen && dragStart) {
const dragEnd = { x, y };
const angle = 3 * twist * dragDirection;
if (angle) {
rotateFace(selectedFace, angle, turningLayers);
}
}
// Reset state and re-enable OrbitControls.
selectedFace = null;
faceCenterScreen = null;
dragStart = null;
dragDirection = 0;
controls.enabled = true;
}
renderer.domElement.addEventListener('mouseup', release);
renderer.domElement.addEventListener('touchend', release);
// renderer.domElement.addEventListener('mouseleave', release);
// renderer.domElement.addEventListener('touchcancel', release);
function dragFace(event){
if (isRotating || !dragStart) return;
const { x, y } = getCanvasRelativePosition(event);
if (selectedFace && faceCenterScreen) {
const dragCurrent = { x, y };
let direction = turnDirection(dragStart, dragCurrent, false);
if (direction === dragDirection) return; // ignore subsequent dragging
// dragDirection has changed, so we restore face rotation
if (dragDirection) {
rotateFace(selectedFace, (-twist) * dragDirection, turningLayers, false);
dragDirection = 0;
return;
}
if (direction) {
dragDirection = direction;
rotateFace(selectedFace, twist * dragDirection, turningLayers, false);
}
}
}
// Provide visual feedback with a small twist in dragDirection
renderer.domElement.addEventListener('mousemove', dragFace);
renderer.domElement.addEventListener('touchmove', dragFace);
// Add drop event handlers and helper function to paint a face with a dropped image
renderer.domElement.addEventListener('dragover', (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener('drop', (event) => {
event.preventDefault();
if (event.dataTransfer.files.length === 0) return;
const file = event.dataTransfer.files[0];
if (!file.type.startsWith('image/')) return;
const reader = new FileReader();
reader.onload = function(e) {
const imgUrl = e.target.result;
const textureLoader = new THREE.TextureLoader();
textureLoader.load(imgUrl, (texture) => {
const mouse = new THREE.Vector2(
(event.clientX / renderer.domElement.clientWidth) * 2 - 1,
-(event.clientY / renderer.domElement.clientHeight) * 2 + 1
);
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(bixieCube.children, true);
if (intersects.length > 0) {
const intersection = intersects[0];
const box = new THREE.Box3().setFromObject(bixieCube);
const faceCandidates = getFaceCandidates(intersection, box);
const targetFace = faceCandidates[0].face;
paintFacesWithImage(targetFace, texture);
} else {
// Drop occurred outside the cube's bounding box: paint the body.
paintBody(imgUrl);
}
});
};
reader.readAsDataURL(file);
});
function paintFacesWithImage(face, texture) {
bixieCube.children.forEach(cubie => {
const currentOffset = getOffset();
let qualifies = false;
let i, j;
// Use cubieSize as a tolerance threshold.
switch (face) {
case 'front':
qualifies = Math.abs(cubie.position.z - currentOffset) < cubieSize / 2;
if (qualifies) {
i = cubie.userData.i !== undefined ? cubie.userData.i :
Math.round((cubie.position.x + currentOffset) / (cubieSize + gap));
j = cubie.userData.j !== undefined ? cubie.userData.j :
Math.round((cubie.position.y + currentOffset) / (cubieSize + gap));
applyTextureToCubie(cubie, face, texture, i, j);
}
break;
case 'back':
qualifies = Math.abs(cubie.position.z + currentOffset) < cubieSize / 2;
if (qualifies) {
i = cubie.userData.i !== undefined ? cubie.userData.i :
Math.round((cubie.position.x + currentOffset) / (cubieSize + gap));
j = cubie.userData.j !== undefined ? cubie.userData.j :
Math.round((cubie.position.y + currentOffset) / (cubieSize + gap));
applyTextureToCubie(cubie, face, texture, numPerAxis - i - 1, j);
}
break;
case 'right':
qualifies = Math.abs(cubie.position.x - currentOffset) < cubieSize / 2;
if (qualifies) {
j = cubie.userData.k !== undefined ? cubie.userData.k :
Math.round((cubie.position.z + currentOffset) / (cubieSize + gap));
i = cubie.userData.j !== undefined ? cubie.userData.j :
Math.round((cubie.position.y + currentOffset) / (cubieSize + gap));
applyTextureToCubie(cubie, face, texture, numPerAxis - j - 1, i);
}
break;
case 'left':
qualifies = Math.abs(cubie.position.x + currentOffset) < cubieSize / 2;
if (qualifies) {
j = cubie.userData.k !== undefined ? cubie.userData.k :
Math.round((cubie.position.z + currentOffset) / (cubieSize + gap));
i = cubie.userData.j !== undefined ? cubie.userData.j :
Math.round((cubie.position.y + currentOffset) / (cubieSize + gap));
applyTextureToCubie(cubie, face, texture, j, i);
}
break;
case 'top':
qualifies = Math.abs(cubie.position.y - currentOffset) < cubieSize / 2;
if (qualifies) {
i = cubie.userData.i !== undefined ? cubie.userData.i :
Math.round((cubie.position.x + currentOffset) / (cubieSize + gap));
j = cubie.userData.k !== undefined ? cubie.userData.k :
Math.round((cubie.position.z + currentOffset) / (cubieSize + gap));
applyTextureToCubie(cubie, face, texture, i, numPerAxis - j - 1);
}
break;
case 'bottom':
qualifies = Math.abs(cubie.position.y + currentOffset) < cubieSize / 2;
if (qualifies) {
i = cubie.userData.i !== undefined ? cubie.userData.i :
Math.round((cubie.position.x + currentOffset) / (cubieSize + gap));
j = cubie.userData.k !== undefined ? cubie.userData.k :
Math.round((cubie.position.z + currentOffset) / (cubieSize + gap));
applyTextureToCubie(cubie, face, texture, i, j);
}
break;
}
});
}
function applyTextureToCubie(cubie, face, texture, i, j) {
// Clone the texture so each cubie gets its own instance.
const cubieTexture = texture.clone();
// Force texture to use sRGB encoding to fix brightness.
cubieTexture.encoding = THREE.sRGBEncoding;
cubieTexture.minFilter = THREE.NearestFilter;
cubieTexture.magFilter = THREE.NearestFilter;
cubieTexture.wrapS = THREE.ClampToEdgeWrapping;
cubieTexture.wrapT = THREE.ClampToEdgeWrapping;
// Mapping: right:0, left:1, top:2, bottom:3, front:4, back:5
const faceMaterialIndex = { right: 0, left: 1, top: 2, bottom: 3, front: 4, back: 5 }[face];
const currentOffset = getOffset();
const totalCubies = numPerAxis; // number of cubies per face
// Set the texture repeat and offset so that each cubie only shows its sub-area.
cubieTexture.repeat.set(1 / totalCubies, 1 / totalCubies);
cubieTexture.offset.set(i / totalCubies, j / totalCubies);
cubieTexture.needsUpdate = true;
const oldMat = cubie.material[faceMaterialIndex];
const newMat = oldMat.clone();
newMat.map = cubieTexture;
cubie.material[faceMaterialIndex] = newMat;
}
function paintBody(imageUrl) {
document.body.style.backgroundImage = `url(${imageUrl})`;
document.body.style.backgroundSize = 'cover';
}
export function scrambleCube() {
const faces = ['front', 'back', 'left', 'right', 'top', 'bottom'];
for (let i = 0; i < difficulty; i++) {
const randomFace = faces[Math.floor(Math.random() * faces.length)];
const randomAngle = Math.random() < 0.5 ? Math.PI / 2 : -Math.PI / 2;
rotateFace(randomFace, randomAngle, Math.random() * Math.floor(numPerAxis / 2) + 1);
}
}
// Format seconds as MM:SS
function formatTime(seconds) {
const mins = String(Math.floor(seconds / 60)).padStart(2, '0');
const secs = String(seconds % 60).padStart(2, '0');
return `${mins}:${secs}`;
}
let gameTime = 60; // game time in seconds (1 minute)
let timerInterval = null;
// Start game button click starts timer and shows popover
startGameBtn.addEventListener('click', (e) => {
timerBox.style.display = 'block';
if (e.target.textContent === 'Reset') {
e.target.textContent = 'Start Game';
stopGame();
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
return;
}
// resetCube.click();
e.target.textContent = 'Reset';
scrambleCube();
setTimeout(() => {
moveHistory.length = 0; // <-- Reset the undo queue when starting a new game.
}, 2000);
let gameTime = 0;
timerBox.style.display = 'block';
document.getElementById('timerDisplay').textContent = formatTime(gameTime);
if (timerInterval) clearInterval(timerInterval);
timerInterval = setInterval(() => {
gameTime++
document.getElementById('timerDisplay').textContent = formatTime(gameTime);
}, 1000);
});
// Hide timer popover (and stop timer) when game is over
function stopGame() {
// timerBox.style.display = 'none';
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
// Clear all pending actions
undoQueue.length = 0;
rotationQueue.length = 0;
moveHistory.length = 0;
isRotating = false;
isUndoing = false;
dragStart = null;
selectedFace = null;
dragDirection = 0;
controls.enabled = true;
turningLayers = 1; // Turn one layer by default
document.getElementById('layersInput').value = 1;
}
}
document.getElementById('numPerAxisInput').addEventListener('change', (event) => {
stopGame();
timerBox.style.display = 'none';
// timerBox.textContent = '00:00';
startGameBtn.textContent = 'Start Game';
});
document.getElementById('resetCube').addEventListener('click', () => {
stopGame();
// timerBox.style.display = 'none';
// timerBox.textContent = '00:00';
startGameBtn.textContent = 'Start Game';
});
// New function to celebrate winning.
function celebrateWin() {
// Play fireworks sound.
const audio = document.getElementById('fireworks_sound');
audio.play();
const wt = document.querySelector(".wavetext");
const pyro = document.querySelector(".pyro");
wt.style.display = "block";
pyro.style.display = "block";
setTimeout(function(){
pyro.style.display = "none";
wt.style.display = "none";
}, 8000);
}
// Respond to difficulty slider event
document.getElementById('difficulty').addEventListener('input', (e) => {
// Get the current difficulty value.
difficulty = e.target.value;
// Do something with the difficulty value (e.g., update the game logic).
// This is a placeholder; replace with your actual game logic.
console.log("Difficulty changed to:", difficulty);
});
document.getElementById('increment').addEventListener('click',
(e)=>{
const input = document.getElementById('layersInput');
const val = parseInt(input.value);
turningLayers = val;
turningLayers = Math.max(1, Math.min(val, Math.floor(numPerAxis / 2)));
input.value = turningLayers;
});
document.getElementById('decrement').addEventListener('click',
(e)=>{
const input = document.getElementById('layersInput');
const val = parseInt(input.value);
turningLayers = val;
});