-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
789 lines (711 loc) · 35 KB
/
index.html
File metadata and controls
789 lines (711 loc) · 35 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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Rubik's Master 3D - Damian Kita</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<style>
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&family=Roboto:wght@300;500&display=swap');
body {
margin: 0;
overflow: hidden;
background-color: #1a1a1a;
font-family: 'Roboto', sans-serif;
color: white;
touch-action: none;
}
#canvas-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
#ui-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;
pointer-events: none;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.pointer-events-auto {
pointer-events: auto;
}
.glass-panel {
background: rgba(0, 0, 0, 0.75);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}
.neon-text {
font-family: 'Orbitron', sans-serif;
text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
}
#controls-window {
position: absolute;
width: 280px;
bottom: 80px;
left: 50%;
transform: translateX(-50%);
z-index: 50;
display: flex;
flex-direction: column;
overflow: hidden;
transition: opacity 0.2s;
}
#controls-window.hidden-panel {
display: none;
}
#controls-header {
background: rgba(255, 255, 255, 0.1);
padding: 8px 12px;
cursor: move;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.controls-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
padding: 10px;
}
.control-btn {
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
color: white;
font-weight: bold;
border-radius: 6px;
padding: 10px 0;
cursor: pointer;
transition: all 0.1s;
font-family: 'Orbitron', sans-serif;
font-size: 0.9rem;
}
.control-btn:active {
background: rgba(0, 255, 255, 0.3);
transform: scale(0.95);
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.85);
z-index: 100;
justify-content: center;
align-items: center;
pointer-events: auto;
}
.modal.active {
display: flex;
}
.modal-content {
background: #222;
padding: 2rem;
border-radius: 16px;
max-width: 600px;
width: 90%;
max-height: 90vh;
overflow-y: auto;
border: 1px solid #444;
}
#move-log {
font-family: 'Orbitron', monospace;
font-size: 0.8rem;
display: flex;
flex-wrap: wrap;
gap: 5px;
max-height: 60px;
overflow-y: auto;
scrollbar-width: thin;
}
.move-chip {
background: rgba(0,255,255,0.1);
padding: 2px 6px;
border-radius: 4px;
border: 1px solid rgba(0,255,255,0.2);
}
.timer-running {
color: #00ffcc;
text-shadow: 0 0 10px rgba(0, 255, 204, 0.4);
}
</style>
</head>
<body>
<div id="canvas-container"></div>
<div id="ui-layer" class="p-4">
<div class="glass-panel p-3 flex justify-between items-center pointer-events-auto relative z-30">
<div>
<h1 class="text-xl md:text-2xl font-bold neon-text text-cyan-400">Cube Master</h1>
<div class="text-xs text-gray-400">by Damian Kita</div>
</div>
<div class="flex items-center gap-2 md:gap-3">
<div id="timer" class="text-xl md:text-3xl font-mono font-bold text-gray-300 mr-2">00:00.00</div>
<button id="controls-toggle-btn" class="p-2 text-cyan-400 hover:text-white transition bg-gray-800 rounded-lg border border-gray-600 hidden" title="Pokaż sterowanie"><i class="fas fa-gamepad"></i></button>
<button id="music-btn" class="p-2 text-gray-400 hover:text-white transition"><i class="fas fa-music"></i></button>
<button id="help-btn" class="p-2 text-cyan-400 hover:text-white transition"><i class="fas fa-question-circle"></i></button>
</div>
</div>
<div class="absolute top-20 left-4 w-48 hidden md:block pointer-events-auto z-20">
<div class="glass-panel p-3 mb-2">
<label class="text-xs text-gray-400 uppercase">Rozmiar Kostki</label>
<select id="size-select" class="w-full bg-gray-800 border border-gray-600 rounded mt-1 p-1 text-sm text-white focus:outline-none">
<option value="2">2x2 Pocket</option>
<option value="3" selected>3x3 Standard</option>
<option value="4">4x4 Revenge</option>
<option value="5">5x5 Professor</option>
</select>
</div>
<div class="glass-panel p-3">
<div class="flex justify-between items-center mb-2">
<label class="text-xs text-gray-400 uppercase">Historia</label>
<button id="undo-btn" class="text-xs bg-gray-700 px-2 py-1 rounded hover:bg-gray-600"><i class="fas fa-undo"></i> Cofnij</button>
</div>
<div id="move-log">
<span class="text-gray-500 text-xs italic">Brak ruchów</span>
</div>
</div>
</div>
<div class="md:hidden absolute top-20 left-4 pointer-events-auto z-20">
<select id="size-select-mobile" class="glass-panel p-2 text-sm text-white focus:outline-none">
<option value="2">2x2</option>
<option value="3" selected>3x3</option>
<option value="4">4x4</option>
<option value="5">5x5</option>
</select>
</div>
<div id="controls-window" class="glass-panel pointer-events-auto">
<div id="controls-header">
<span class="text-xs font-bold text-gray-300 uppercase tracking-wider"><i class="fas fa-arrows-alt text-gray-500 mr-1"></i> Sterowanie</span>
<button id="close-controls-btn" class="text-gray-400 hover:text-red-400 px-1"><i class="fas fa-times"></i></button>
</div>
<div class="controls-grid">
<button class="control-btn" data-move="L">L</button>
<button class="control-btn" data-move="U">U</button>
<button class="control-btn" data-move="R">R</button>
<button class="control-btn" data-move="F">F</button>
<button class="control-btn" data-move="D">D</button>
<button class="control-btn" data-move="B">B</button>
</div>
<div class="px-3 pb-3 flex justify-center">
<label class="flex items-center text-sm gap-2 cursor-pointer bg-gray-800 px-3 py-1 rounded-full border border-gray-600">
<input type="checkbox" id="prime-toggle" class="form-checkbox h-4 w-4 text-cyan-500 rounded bg-gray-700 border-gray-600">
<span class="text-cyan-300 text-xs font-bold">Ruch Odwrotny ( ' )</span>
</label>
</div>
</div>
<div class="absolute bottom-4 left-0 w-full pointer-events-none flex justify-center z-40">
<div class="pointer-events-auto flex gap-2 bg-black/50 p-2 rounded-xl backdrop-blur-md border border-white/10">
<button id="scramble-btn" class="bg-yellow-600 hover:bg-yellow-500 text-white px-4 py-2 rounded font-bold shadow-lg transition text-xs md:text-sm"><i class="fas fa-random"></i> Pomieszaj</button>
<button id="reset-btn" class="bg-red-600 hover:bg-red-500 text-white px-4 py-2 rounded font-bold shadow-lg transition text-xs md:text-sm"><i class="fas fa-redo"></i> Reset</button>
<button id="undo-btn-mobile" class="bg-gray-700 hover:bg-gray-600 text-white px-4 py-2 rounded font-bold shadow-lg transition text-xs md:text-sm md:hidden"><i class="fas fa-undo"></i></button>
</div>
</div>
<div class="absolute bottom-1 right-2 text-xs text-gray-600 pointer-events-auto z-10 hidden md:block">
<a href="https://linktr.ee/kitadamian" target="_blank" class="hover:text-cyan-400 transition">Damian Kita</a>
</div>
</div>
<div id="tutorial-modal" class="modal">
<div class="modal-content">
<div class="flex justify-between items-center mb-4">
<h2 class="text-2xl font-bold text-cyan-400">Jak układać?</h2>
<button class="close-modal text-gray-400 hover:text-white text-xl"><i class="fas fa-times"></i></button>
</div>
<div class="space-y-4 text-gray-300 text-sm md:text-base">
<p>Celem jest ułożenie kostki tak, aby każda ściana miała jeden kolor.</p>
<h3 class="font-bold text-white mt-4 border-b border-gray-600 pb-1">Sterowanie Oknem:</h3>
<ul class="list-disc pl-5 space-y-1">
<li>Panel <strong>"Sterowanie"</strong> możesz przesuwać chwytając za nagłówek.</li>
<li>Możesz go zamknąć (X) i otworzyć ponownie ikoną pada (🎮) na górnym pasku.</li>
</ul>
<h3 class="font-bold text-white mt-4 border-b border-gray-600 pb-1">Notacja Ruchów:</h3>
<ul class="list-disc pl-5 space-y-1">
<li><strong class="text-yellow-400">U / D:</strong> Góra / Dół</li>
<li><strong class="text-yellow-400">L / R:</strong> Lewa / Prawa</li>
<li><strong class="text-yellow-400">F / B:</strong> Przód / Tył</li>
</ul>
<button class="close-modal mt-6 w-full bg-cyan-600 hover:bg-cyan-500 text-white font-bold py-2 rounded">Zrozumiałem!</button>
</div>
</div>
</div>
<div id="win-modal" class="modal">
<div class="modal-content text-center">
<h2 class="text-4xl font-bold text-yellow-400 mb-2">GRATULACJE!</h2>
<p class="text-xl text-white mb-4">Ułożyłeś kostkę!</p>
<div class="bg-gray-800 p-4 rounded mb-6">
<div class="text-gray-400 text-sm">Twój czas:</div>
<div id="final-time" class="text-3xl font-mono text-cyan-400 font-bold">00:00.00</div>
<div class="text-gray-400 text-sm mt-2">Liczba ruchów: <span id="final-moves" class="text-white font-bold">0</span></div>
</div>
<button id="play-again-btn" class="bg-green-600 hover:bg-green-500 text-white font-bold py-3 px-8 rounded-full text-lg shadow-lg transform transition hover:scale-105">
Zagraj ponownie
</button>
</div>
</div>
<script>
/**
* AUDIO ENGINE
*/
class SoundEngine {
constructor() {
this.ctx = new (window.AudioContext || window.webkitAudioContext)();
this.masterGain = this.ctx.createGain();
this.masterGain.connect(this.ctx.destination);
this.masterGain.gain.value = 0.3;
this.muted = true;
this.bgmOscillators = [];
this.isPlayingMusic = false;
}
toggleMute() {
if (this.ctx.state === 'suspended') this.ctx.resume();
this.muted = !this.muted;
if(this.muted) {
this.stopMusic();
return false;
} else {
this.startMusic();
return true;
}
}
playClick() {
if (this.muted) return;
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(400 + Math.random() * 200, this.ctx.currentTime);
osc.frequency.exponentialRampToValueAtTime(100, this.ctx.currentTime + 0.1);
gain.gain.setValueAtTime(0.5, this.ctx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.01, this.ctx.currentTime + 0.1);
osc.connect(gain);
gain.connect(this.masterGain);
osc.start();
osc.stop(this.ctx.currentTime + 0.1);
}
playWin() {
// Wygrana gra dźwięk nawet jeśli wyciszone (opcjonalnie, tu sprawdzam mute)
if (this.muted) return;
const notes = [523.25, 659.25, 783.99, 1046.50];
notes.forEach((freq, i) => {
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'triangle';
osc.frequency.value = freq;
const startTime = this.ctx.currentTime + (i * 0.1);
gain.gain.setValueAtTime(0, startTime);
gain.gain.linearRampToValueAtTime(0.3, startTime + 0.05);
gain.gain.exponentialRampToValueAtTime(0.001, startTime + 0.5);
osc.connect(gain);
gain.connect(this.masterGain);
osc.start(startTime);
osc.stop(startTime + 0.6);
});
}
startMusic() {
if (this.isPlayingMusic || this.muted) return;
this.isPlayingMusic = true;
const freqs = [130.81, 196.00, 261.63];
freqs.forEach(f => {
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'sine';
osc.frequency.value = f;
gain.gain.value = 0.05;
osc.connect(gain);
gain.connect(this.masterGain);
osc.start();
this.bgmOscillators.push({osc, gain});
});
}
stopMusic() {
this.bgmOscillators.forEach(o => {
o.osc.stop();
o.osc.disconnect();
});
this.bgmOscillators = [];
this.isPlayingMusic = false;
}
}
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x1a1b1e);
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.set(6, 4, 8);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.getElementById('canvas-container').appendChild(renderer.domElement);
function handleResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
if (width < 768 || width < height) {
const shiftY = height * 0.1;
camera.setViewOffset(width, height, 0, shiftY, width, height);
} else {
camera.clearViewOffset();
}
}
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(10, 20, 10);
scene.add(dirLight);
const COLORS = {
U: 0xFFFFFF, D: 0xFFD500, F: 0x009E60, B: 0x0051BA, R: 0xC41E3A, L: 0xFF5800, CORE: 0x111111
};
let cubeSize = 3;
let allCubies = [];
let isAnimating = false;
let moveHistory = [];
let startTime = 0;
let timerInterval = null;
let isTimerRunning = false;
let soundEngine = new SoundEngine();
const pivot = new THREE.Object3D();
scene.add(pivot);
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
const cameraGroup = new THREE.Group();
cameraGroup.add(camera);
scene.add(cameraGroup);
function initControls() {
const el = renderer.domElement;
const onDown = (e) => {
isDragging = true;
previousMousePosition = {
x: e.touches ? e.touches[0].clientX : e.clientX,
y: e.touches ? e.touches[0].clientY : e.clientY
};
};
const onMove = (e) => {
if (!isDragging) return;
const x = e.touches ? e.touches[0].clientX : e.clientX;
const y = e.touches ? e.touches[0].clientY : e.clientY;
const deltaMove = { x: x - previousMousePosition.x, y: y - previousMousePosition.y };
cameraGroup.rotation.y -= deltaMove.x * 0.005;
previousMousePosition = { x, y };
};
const onUp = () => isDragging = false;
el.addEventListener('mousedown', onDown);
el.addEventListener('mousemove', onMove);
el.addEventListener('mouseup', onUp);
el.addEventListener('touchstart', onDown, {passive: false});
el.addEventListener('touchmove', (e) => { e.preventDefault(); onMove(e); }, {passive: false});
el.addEventListener('touchend', onUp);
}
initControls();
function initDraggableWindow() {
const win = document.getElementById('controls-window');
const header = document.getElementById('controls-header');
const closeBtn = document.getElementById('close-controls-btn');
const toggleBtn = document.getElementById('controls-toggle-btn');
let isDraggingWin = false;
let startX, startY, initialLeft, initialTop;
closeBtn.addEventListener('click', () => {
win.classList.add('hidden-panel');
toggleBtn.classList.remove('hidden');
});
toggleBtn.addEventListener('click', () => {
win.classList.remove('hidden-panel');
toggleBtn.classList.add('hidden');
});
function dragStart(e) {
if (e.target.closest('button')) return;
isDraggingWin = true;
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const clientY = e.touches ? e.touches[0].clientY : e.clientY;
startX = clientX;
startY = clientY;
const rect = win.getBoundingClientRect();
if (window.getComputedStyle(win).transform !== 'none') {
win.style.left = rect.left + 'px';
win.style.top = rect.top + 'px';
win.style.bottom = 'auto';
win.style.transform = 'none';
}
initialLeft = parseInt(win.style.left || rect.left);
initialTop = parseInt(win.style.top || rect.top);
}
function dragMove(e) {
if (!isDraggingWin) return;
e.preventDefault();
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const clientY = e.touches ? e.touches[0].clientY : e.clientY;
const dx = clientX - startX;
const dy = clientY - startY;
win.style.left = (initialLeft + dx) + 'px';
win.style.top = (initialTop + dy) + 'px';
}
function dragEnd() { isDraggingWin = false; }
header.addEventListener('mousedown', dragStart);
document.addEventListener('mousemove', dragMove);
document.addEventListener('mouseup', dragEnd);
header.addEventListener('touchstart', dragStart, {passive: false});
document.addEventListener('touchmove', dragMove, {passive: false});
document.addEventListener('touchend', dragEnd);
}
initDraggableWindow();
function createCube(n) {
allCubies.forEach(c => scene.remove(c));
allCubies = [];
moveHistory = [];
updateHistoryUI();
resetTimer();
cubeSize = n;
const offset = (n - 1) / 2;
const geometry = new THREE.BoxGeometry(0.95, 0.95, 0.95);
for (let x = 0; x < n; x++) {
for (let y = 0; y < n; y++) {
for (let z = 0; z < n; z++) {
const materials = [
new THREE.MeshStandardMaterial({ color: x === n-1 ? COLORS.R : COLORS.CORE, roughness: 0.2 }),
new THREE.MeshStandardMaterial({ color: x === 0 ? COLORS.L : COLORS.CORE, roughness: 0.2 }),
new THREE.MeshStandardMaterial({ color: y === n-1 ? COLORS.U : COLORS.CORE, roughness: 0.2 }),
new THREE.MeshStandardMaterial({ color: y === 0 ? COLORS.D : COLORS.CORE, roughness: 0.2 }),
new THREE.MeshStandardMaterial({ color: z === n-1 ? COLORS.F : COLORS.CORE, roughness: 0.2 }),
new THREE.MeshStandardMaterial({ color: z === 0 ? COLORS.B : COLORS.CORE, roughness: 0.2 }),
];
const mesh = new THREE.Mesh(geometry, materials);
mesh.position.set(x - offset, y - offset, z - offset);
// Zachowujemy referencję do macierzy bazowej dla checkWin
mesh.userData.originalPos = mesh.position.clone();
scene.add(mesh);
allCubies.push(mesh);
}
}
}
}
function getCubiesInLayer(axis, val) {
const epsilon = 0.1;
return allCubies.filter(mesh => {
const worldPos = new THREE.Vector3();
mesh.getWorldPosition(worldPos);
return Math.abs(worldPos[axis] - val) < epsilon;
});
}
function animateRotation(axis, layerVal, direction, speed = 500, onComplete) {
if (isAnimating) return;
isAnimating = true;
const activeCubies = getCubiesInLayer(axis, layerVal);
pivot.rotation.set(0, 0, 0);
pivot.position.set(0, 0, 0);
scene.add(pivot);
activeCubies.forEach(mesh => pivot.attach(mesh));
const targetRot = (Math.PI / 2) * direction;
const startRot = 0;
let start = null;
function step(time) {
if (!start) start = time;
const progress = (time - start) / speed;
if (progress < 1) {
pivot.rotation[axis] = startRot + (targetRot * easeInOutCubic(progress));
requestAnimationFrame(step);
} else {
pivot.rotation[axis] = targetRot;
pivot.updateMatrixWorld();
activeCubies.forEach(mesh => {
scene.attach(mesh);
mesh.position.x = Math.round(mesh.position.x * 100) / 100;
mesh.position.y = Math.round(mesh.position.y * 100) / 100;
mesh.position.z = Math.round(mesh.position.z * 100) / 100;
mesh.rotation.x = Math.round(mesh.rotation.x / (Math.PI/2)) * (Math.PI/2);
mesh.rotation.y = Math.round(mesh.rotation.y / (Math.PI/2)) * (Math.PI/2);
mesh.rotation.z = Math.round(mesh.rotation.z / (Math.PI/2)) * (Math.PI/2);
});
isAnimating = false;
if (onComplete) onComplete();
}
}
requestAnimationFrame(step);
}
function easeInOutCubic(x) { return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2; }
function executeMove(moveStr, isUndo = false, record = true) {
if (isAnimating) return;
let base = moveStr.charAt(0);
let isPrime = moveStr.includes("'");
let axis, layerVal, dir;
const offset = (cubeSize - 1) / 2;
switch(base) {
case 'R': axis='x'; layerVal=offset; dir=-1; break;
case 'L': axis='x'; layerVal=-offset; dir=1; break;
case 'U': axis='y'; layerVal=offset; dir=-1; break;
case 'D': axis='y'; layerVal=-offset; dir=1; break;
case 'F': axis='z'; layerVal=offset; dir=-1; break;
case 'B': axis='z'; layerVal=-offset; dir=1; break;
}
if (isPrime) dir *= -1;
startTimer();
soundEngine.playClick();
if (record) { moveHistory.push(moveStr); updateHistoryUI(); }
animateRotation(axis, layerVal, dir, 250, () => { checkWin(); });
}
function undoLastMove() {
if (moveHistory.length === 0 || isAnimating) return;
let lastMove = moveHistory.pop();
updateHistoryUI();
let isPrime = lastMove.includes("'");
let base = lastMove.charAt(0);
let invertedMove = base + (isPrime ? "" : "'");
executeMove(invertedMove, true, false);
}
async function scramble() {
if (isAnimating) return;
resetTimer();
moveHistory = [];
updateHistoryUI();
const moves = ['U', "U'", 'D', "D'", 'L', "L'", 'R', "R'", 'F', "F'", 'B', "B'"];
const count = 20;
for(let i=0; i<count; i++) {
await new Promise(resolve => {
const rMove = moves[Math.floor(Math.random() * moves.length)];
let base = rMove.charAt(0);
let isPrime = rMove.includes("'");
let axis, layerVal, dir;
const offset = (cubeSize - 1) / 2;
switch(base) {
case 'R': axis='x'; layerVal=offset; dir=-1; break;
case 'L': axis='x'; layerVal=-offset; dir=1; break;
case 'U': axis='y'; layerVal=offset; dir=-1; break;
case 'D': axis='y'; layerVal=-offset; dir=1; break;
case 'F': axis='z'; layerVal=offset; dir=-1; break;
case 'B': axis='z'; layerVal=-offset; dir=1; break;
}
if (isPrime) dir *= -1;
const activeCubies = getCubiesInLayer(axis, layerVal);
pivot.rotation.set(0,0,0); pivot.position.set(0,0,0); scene.add(pivot);
activeCubies.forEach(m => pivot.attach(m));
pivot.rotation[axis] = (Math.PI/2) * dir;
pivot.updateMatrixWorld();
activeCubies.forEach(m => {
scene.attach(m);
m.position.x = Math.round(m.position.x * 100) / 100;
m.position.y = Math.round(m.position.y * 100) / 100;
m.position.z = Math.round(m.position.z * 100) / 100;
m.rotation.x = Math.round(m.rotation.x / (Math.PI/2)) * (Math.PI/2);
m.rotation.y = Math.round(m.rotation.y / (Math.PI/2)) * (Math.PI/2);
m.rotation.z = Math.round(m.rotation.z / (Math.PI/2)) * (Math.PI/2);
});
resolve();
});
}
soundEngine.playClick();
}
function startTimer() {
if (!isTimerRunning) {
isTimerRunning = true;
startTime = Date.now();
document.getElementById('timer').classList.add('timer-running');
timerInterval = setInterval(() => {
const delta = Date.now() - startTime;
const min = Math.floor(delta / 60000);
const sec = Math.floor((delta % 60000) / 1000);
const ms = Math.floor((delta % 1000) / 10);
document.getElementById('timer').innerText =
`${min.toString().padStart(2, '0')}:${sec.toString().padStart(2, '0')}.${ms.toString().padStart(2, '0')}`;
}, 10);
}
}
function stopTimer() {
isTimerRunning = false;
clearInterval(timerInterval);
document.getElementById('timer').classList.remove('timer-running');
}
function resetTimer() {
stopTimer();
document.getElementById('timer').innerText = "00:00.00";
}
function updateHistoryUI() {
const container = document.getElementById('move-log');
if (moveHistory.length === 0) {
container.innerHTML = '<span class="text-gray-500 text-xs italic">Brak ruchów</span>';
} else {
container.innerHTML = moveHistory.map(m => `<div class="move-chip">${m}</div>`).join('');
container.scrollTop = container.scrollHeight;
}
}
function checkWin() {
if (!isTimerRunning || moveHistory.length < 5) return; // Mniej niż 5 ruchów to zazwyczaj scramble
let isSolved = true;
const epsilon = 0.01;
for (let cubie of allCubies) {
// Sprawdzamy czy rotacja jest wielokrotnością 360 stopni (względnie 0)
// Używamy macierzy świata, aby upewnić się, że ściany kolory są na miejscu
const rot = new THREE.Euler().setFromQuaternion(cubie.quaternion);
const rx = Math.abs(rot.x % (Math.PI * 2));
const ry = Math.abs(rot.y % (Math.PI * 2));
const rz = Math.abs(rot.z % (Math.PI * 2));
const isAligned = (val) => val < epsilon || Math.abs(val - Math.PI * 2) < epsilon;
if (!isAligned(rx) || !isAligned(ry) || !isAligned(rz)) {
isSolved = false;
break;
}
}
if (isSolved) {
triggerWin();
}
}
function triggerWin() {
stopTimer();
soundEngine.playWin();
document.getElementById('final-time').innerText = document.getElementById('timer').innerText;
document.getElementById('final-moves').innerText = moveHistory.length;
document.getElementById('win-modal').classList.add('active');
}
window.addEventListener('resize', handleResize);
document.querySelectorAll('.control-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const move = btn.dataset.move;
const isPrime = document.getElementById('prime-toggle').checked;
executeMove(move + (isPrime ? "'" : ""));
});
});
document.getElementById('size-select').addEventListener('change', (e) => createCube(parseInt(e.target.value)));
document.getElementById('size-select-mobile').addEventListener('change', (e) => createCube(parseInt(e.target.value)));
document.getElementById('scramble-btn').addEventListener('click', scramble);
document.getElementById('reset-btn').addEventListener('click', () => createCube(cubeSize));
document.getElementById('undo-btn').addEventListener('click', undoLastMove);
document.getElementById('undo-btn-mobile').addEventListener('click', undoLastMove);
const helpModal = document.getElementById('tutorial-modal');
const winModal = document.getElementById('win-modal');
document.getElementById('help-btn').addEventListener('click', () => helpModal.classList.add('active'));
document.querySelectorAll('.close-modal').forEach(btn => {
btn.addEventListener('click', () => { helpModal.classList.remove('active'); winModal.classList.remove('active'); });
});
document.getElementById('play-again-btn').addEventListener('click', () => {
winModal.classList.remove('active');
createCube(cubeSize);
});
const musicBtn = document.getElementById('music-btn');
musicBtn.addEventListener('click', () => {
const isPlaying = soundEngine.toggleMute();
musicBtn.innerHTML = isPlaying ? '<i class="fas fa-volume-up text-cyan-400"></i>' : '<i class="fas fa-volume-mute text-red-400"></i>';
});
window.addEventListener('keydown', (e) => {
if(helpModal.classList.contains('active') || winModal.classList.contains('active')) return;
const key = e.key.toUpperCase();
const validKeys = ['U', 'D', 'L', 'R', 'F', 'B'];
if (validKeys.includes(key)) executeMove(key + (e.shiftKey ? "'" : ""));
});
function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); }
createCube(3);
handleResize();
animate();
</script>
</body>
</html>