-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
654 lines (553 loc) · 22.6 KB
/
Copy pathscript.js
File metadata and controls
654 lines (553 loc) · 22.6 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
// Window Manager and Desktop Logic
class WindowManager {
constructor() {
this.windows = document.querySelectorAll('.window');
this.icons = document.querySelectorAll('.desktop-icon');
this.taskbarItems = document.querySelector('.taskbar-items');
this.startBtn = document.querySelector('.start-button');
this.startMenu = document.querySelector('.start-menu');
this.zIndex = 100;
this.setupWindows();
this.setupIcons();
this.setupTaskbar();
this.setupClock();
this.setupStartup();
this.setupApps();
}
setupStartup() {
const startupScreen = document.getElementById('startup-screen');
if (startupScreen) {
// Play sound
const audio = new Audio('win98_startup.mp3');
audio.play().catch(e => console.log('Audio playback failed (interaction needed):', e));
// Hide after 4 seconds (approx length of sound + bar animation)
setTimeout(() => {
startupScreen.style.display = 'none';
}, 4000);
}
}
setupWindows() {
this.windows.forEach(win => {
const titleBar = win.querySelector('.title-bar');
const closeBtn = win.querySelector('.title-bar-controls button[aria-label="Close"]');
const maxBtn = win.querySelector('.title-bar-controls button[aria-label="Maximize"]');
const minBtn = win.querySelector('.title-bar-controls button[aria-label="Minimize"]');
const titleText = win.querySelector('.title-bar-text').textContent;
// Dragging
this.makeDraggable(win, titleBar);
// Bring to front on click
win.addEventListener('mousedown', () => this.bringToFront(win));
// Close button
if (closeBtn) {
closeBtn.addEventListener('click', () => this.closeWindow(win));
}
// Maximize button
if (maxBtn) {
maxBtn.addEventListener('click', () => this.toggleMaximize(win));
}
// Minimize button
if (minBtn) {
minBtn.addEventListener('click', () => this.minimizeWindow(win));
}
// Store title for taskbar
win.dataset.title = titleText;
});
}
setupIcons() {
// Initial positioning for icons (simple grid)
const icons = Array.from(this.icons);
let row = 0;
let col = 0;
const startX = 10;
const startY = 10;
const gapX = 100; // width + gap
const gapY = 100; // height + gap
const maxRows = Math.floor((window.innerHeight - 28) / gapY);
icons.forEach((icon, index) => {
icon.style.left = `${startX + (col * gapX)}px`;
icon.style.top = `${startY + (row * gapY)}px`;
row++;
if (row >= maxRows) {
row = 0;
col++;
}
// Make draggable
this.makeDraggable(icon, icon);
// Double click interaction
icon.addEventListener('dblclick', () => {
const targetId = icon.dataset.target;
const win = document.getElementById(targetId);
if (win) {
this.openWindow(win);
}
});
// Touch support
let lastTap = 0;
icon.addEventListener('touchend', (e) => {
if (icon.classList.contains('dragging')) return;
const currentTime = new Date().getTime();
const tapLength = currentTime - lastTap;
if (tapLength < 500 && tapLength > 0) {
const targetId = icon.dataset.target;
const win = document.getElementById(targetId);
if (win) {
this.openWindow(win);
}
e.preventDefault();
}
lastTap = currentTime;
});
});
}
setupTaskbar() {
// Start Menu Toggle
this.startBtn.addEventListener('click', (e) => {
e.stopPropagation();
this.startBtn.classList.toggle('active');
this.startMenu.style.display = this.startMenu.style.display === 'flex' ? 'none' : 'flex';
});
// Close start menu when clicking elsewhere
document.addEventListener('click', (e) => {
if (!this.startBtn.contains(e.target) && !this.startMenu.contains(e.target)) {
this.startMenu.style.display = 'none';
this.startBtn.classList.remove('active');
}
});
}
setupClock() {
const updateClock = () => {
const now = new Date();
const timeString = new Intl.DateTimeFormat('en-US', {
timeZone: 'America/New_York',
hour: 'numeric',
minute: '2-digit',
hour12: true
}).format(now);
const timeEl = document.querySelector('.time');
if (timeEl) timeEl.textContent = timeString;
};
setInterval(updateClock, 1000);
updateClock();
}
makeDraggable(element, handle) {
let isDragging = false;
let startX, startY, initialLeft, initialTop;
const onMouseDown = (e) => {
if (e.button !== 0) return; // Only left click
if (element.classList.contains('maximized')) return; // No dragging if maximized
isDragging = true;
startX = e.clientX;
startY = e.clientY;
const rect = element.getBoundingClientRect();
// Use offsetLeft/Top for relative positioning logic if parent is positioned
// But getBoundingClientRect is safer for initial delta calculation
initialLeft = element.offsetLeft;
initialTop = element.offsetTop;
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
// Don't prevent default immediately for icons to allow dblclick?
// Actually usually good to prevent default to stop text selection
// e.preventDefault();
};
const onMouseMove = (e) => {
if (!isDragging) return;
const dx = e.clientX - startX;
const dy = e.clientY - startY;
// If moved significantly, mark as dragging (to prevent click events)
if (Math.abs(dx) > 3 || Math.abs(dy) > 3) {
element.classList.add('dragging');
}
element.style.left = `${initialLeft + dx}px`;
element.style.top = `${initialTop + dy}px`;
element.style.transform = 'none';
};
const onMouseUp = () => {
isDragging = false;
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
setTimeout(() => element.classList.remove('dragging'), 100);
};
handle.addEventListener('mousedown', onMouseDown);
// Touch support
const onTouchStart = (e) => {
if (element.classList.contains('maximized')) return;
isDragging = true;
const touch = e.touches[0];
startX = touch.clientX;
startY = touch.clientY;
const rect = element.getBoundingClientRect();
initialLeft = rect.left;
initialTop = rect.top;
document.addEventListener('touchmove', onTouchMove, { passive: false });
document.addEventListener('touchend', onTouchEnd);
};
const onTouchMove = (e) => {
if (!isDragging) return;
const touch = e.touches[0];
const dx = touch.clientX - startX;
const dy = touch.clientY - startY;
e.preventDefault();
element.style.left = `${initialLeft + dx}px`;
element.style.top = `${initialTop + dy}px`;
};
const onTouchEnd = () => {
isDragging = false;
document.removeEventListener('touchmove', onTouchMove);
document.removeEventListener('touchend', onTouchEnd);
};
handle.addEventListener('touchstart', onTouchStart, { passive: false });
}
bringToFront(win) {
this.zIndex++;
win.style.zIndex = this.zIndex;
// Highlight taskbar item
const id = win.id;
document.querySelectorAll('.taskbar-item').forEach(item => {
if (item.dataset.target === id) {
item.classList.add('active');
} else {
item.classList.remove('active');
}
});
}
openWindow(win) {
if (win.style.display === 'block') {
this.bringToFront(win);
// If minimized (visual check logic, though we use display block), restore it
return;
}
win.style.display = 'block';
this.bringToFront(win);
this.addTaskbarItem(win);
// Center if new
if (!win.style.left) {
const desktop = document.querySelector('.desktop');
const rect = win.getBoundingClientRect();
const deskRect = desktop.getBoundingClientRect();
win.style.left = `${(deskRect.width - rect.width) / 2}px`;
win.style.top = `${(deskRect.height - rect.height) / 2}px`;
}
}
closeWindow(win) {
win.style.display = 'none';
this.removeTaskbarItem(win);
}
minimizeWindow(win) {
win.style.display = 'none';
const id = win.id;
const item = document.querySelector(`.taskbar-item[data-target="${id}"]`);
if (item) item.classList.remove('active');
}
toggleMaximize(win) {
if (win.classList.contains('maximized')) {
win.classList.remove('maximized');
win.style.width = win.dataset.prevWidth || '';
win.style.height = win.dataset.prevHeight || '';
win.style.top = win.dataset.prevTop || '';
win.style.left = win.dataset.prevLeft || '';
} else {
const rect = win.getBoundingClientRect();
win.dataset.prevWidth = win.style.width;
win.dataset.prevHeight = win.style.height;
win.dataset.prevTop = win.style.top;
win.dataset.prevLeft = win.style.left;
win.classList.add('maximized');
win.style.width = '100vw';
win.style.height = 'calc(100vh - 28px)';
win.style.top = '0';
win.style.left = '0';
}
}
addTaskbarItem(win) {
if (document.querySelector(`.taskbar-item[data-target="${win.id}"]`)) return;
const item = document.createElement('div');
item.className = 'taskbar-item active';
item.dataset.target = win.id;
// Icon
let iconSrc = 'https://win98icons.alexmeub.com/icons/png/application_hourglass_small-3.png'; // default
// Try to find matching desktop icon
const desktopIcon = document.querySelector(`.desktop-icon[data-target="${win.id}"] img`);
if (desktopIcon) iconSrc = desktopIcon.src;
item.innerHTML = `
<img src="${iconSrc}" alt="icon">
<span>${win.dataset.title}</span>
`;
item.addEventListener('click', () => {
if (win.style.display === 'none') {
win.style.display = 'block';
this.bringToFront(win);
} else {
// Check if it's the top window
if (parseInt(win.style.zIndex) === this.zIndex) {
this.minimizeWindow(win);
} else {
this.bringToFront(win);
}
}
});
this.taskbarItems.appendChild(item);
}
setupApps() {
// Notepad Logic
const notepad = document.getElementById('notepad-content');
if (notepad) {
notepad.value = localStorage.getItem('notepad-content') || '';
notepad.addEventListener('input', () => {
localStorage.setItem('notepad-content', notepad.value);
});
}
// Audio Player Logic
const playBtn = document.getElementById('play-btn');
const stopBtn = document.getElementById('stop-btn');
const visualizer = document.querySelector('.visualizer');
const bars = Array.from(visualizer.getElementsByClassName('bar'));
if (playBtn && stopBtn) {
let audio = new Audio('canyon.mp3');
audio.loop = true;
// Web Audio API Context
let audioContext;
let analyser;
let source;
let isContextSetup = false;
const setupAudioContext = () => {
if (isContextSetup) return;
const AudioContext = window.AudioContext || window.webkitAudioContext;
audioContext = new AudioContext();
analyser = audioContext.createAnalyser();
source = audioContext.createMediaElementSource(audio);
source.connect(analyser);
analyser.connect(audioContext.destination);
analyser.fftSize = 64; // Small size for chunky retro bars
isContextSetup = true;
};
const updateVisualizer = () => {
if (!analyser || audio.paused) return;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
analyser.getByteFrequencyData(dataArray);
// Map frequency data to bars
// We have 7 bars now (from user update), let's distribute them
const step = Math.floor(bufferLength / bars.length);
bars.forEach((bar, index) => {
const dataIndex = index * step;
const value = dataArray[dataIndex];
// Convert 0-255 to percentage height (min 10% max 100%)
const height = Math.max(10, (value / 255) * 100);
bar.style.height = `${height}%`;
// Optional: color change based on intensity
if (value > 200) bar.style.background = 'red';
else if (value > 150) bar.style.background = 'yellow';
else bar.style.background = 'lime';
});
requestAnimationFrame(updateVisualizer);
};
playBtn.addEventListener('click', () => {
setupAudioContext();
// Resume context if suspended (browser autoplay policy)
if (audioContext.state === 'suspended') {
audioContext.resume();
}
audio.play().then(() => {
visualizer.classList.add('playing');
updateVisualizer();
}).catch(e => console.log('Audio error:', e));
});
stopBtn.addEventListener('click', () => {
audio.pause();
audio.currentTime = 0;
bars.forEach(bar => bar.style.height = '10%'); // Reset bars
});
}
// Minesweeper Logic
this.setupMinesweeper();
// BSOD Logic (Konami Code)
let konamiCode = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a'];
let konamiIndex = 0;
document.addEventListener('keydown', (e) => {
if (e.key === konamiCode[konamiIndex]) {
konamiIndex++;
if (konamiIndex === konamiCode.length) {
this.triggerBSOD();
konamiIndex = 0;
}
} else {
konamiIndex = 0;
}
});
// Also trigger BSOD from Shutdown
const shutdownItem = document.querySelector('.start-item[onclick*="shut_down"]');
if (shutdownItem) {
shutdownItem.onclick = null; // Remove old alert
shutdownItem.addEventListener('click', () => {
this.triggerBSOD();
});
}
}
triggerBSOD() {
const bsod = document.getElementById('bsod');
bsod.style.display = 'block';
document.body.style.cursor = 'none';
const closeBSOD = (e) => {
e.preventDefault();
bsod.style.display = 'none';
document.body.style.cursor = 'auto';
document.removeEventListener('keydown', closeBSOD);
document.removeEventListener('click', closeBSOD);
};
// Slight delay before allowing close so user sees it
setTimeout(() => {
document.addEventListener('keydown', closeBSOD);
document.addEventListener('click', closeBSOD);
}, 1000);
}
setupMinesweeper() {
const grid = document.getElementById('minesweeper-grid');
const faceBtn = document.getElementById('face-btn');
const mineCountDisplay = document.getElementById('mine-count');
let width = 9;
let height = 9;
let mineCount = 10;
let cells = [];
let isGameOver = false;
const createBoard = () => {
grid.innerHTML = '';
cells = [];
isGameOver = false;
faceBtn.textContent = '😊';
mineCountDisplay.textContent = mineCount.toString().padStart(3, '0');
// Create cells
const emptyArray = Array(width * height).fill('valid');
const minesArray = Array(mineCount).fill('mine');
const gameArray = emptyArray.concat(minesArray);
// Note: simple randomization, might put mines at end if not sliced correctly.
// Fixed logic:
const totalCells = width * height;
const mines = Array(totalCells).fill(false);
let minesPlaced = 0;
while (minesPlaced < mineCount) {
const idx = Math.floor(Math.random() * totalCells);
if (!mines[idx]) {
mines[idx] = true;
minesPlaced++;
}
}
for (let i = 0; i < totalCells; i++) {
const cell = document.createElement('div');
cell.setAttribute('id', i);
cell.classList.add('mine-cell');
cell.dataset.isMine = mines[i];
grid.appendChild(cell);
cells.push(cell);
// Normal Click
cell.addEventListener('click', () => {
click(cell);
});
// Right Click
cell.addEventListener('contextmenu', (e) => {
e.preventDefault();
addFlag(cell);
});
}
};
const addFlag = (cell) => {
if (isGameOver) return;
if (!cell.classList.contains('revealed')) {
if (!cell.classList.contains('flag')) {
cell.classList.add('flag');
cell.textContent = '🚩';
// Update counter logic could go here
} else {
cell.classList.remove('flag');
cell.textContent = '';
}
}
};
const click = (cell) => {
if (isGameOver) return;
if (cell.classList.contains('revealed') || cell.classList.contains('flag')) return;
if (cell.dataset.isMine === 'true') {
gameOver();
} else {
let total = 0;
const isLeftEdge = (parseInt(cell.id) % width === 0);
const isRightEdge = (parseInt(cell.id) % width === width - 1);
const id = parseInt(cell.id);
// Check neighbors
const check = (idx) => {
if (idx >= 0 && idx < width * height && cells[idx].dataset.isMine === 'true') total++;
};
if (!isLeftEdge) {
check(id - 1); // West
check(id - 1 - width); // North-West
check(id - 1 + width); // South-West
}
if (!isRightEdge) {
check(id + 1); // East
check(id + 1 - width); // North-East
check(id + 1 + width); // South-East
}
check(id - width); // North
check(id + width); // South
if (total !== 0) {
cell.classList.add('revealed');
cell.textContent = total;
cell.style.color = getNumberColor(total);
return;
}
// If 0, reveal neighbors (recursion)
checkSquare(cell, id);
}
// Check for win logic could go here
};
const getNumberColor = (n) => {
switch (n) {
case 1: return 'blue';
case 2: return 'green';
case 3: return 'red';
case 4: return 'darkblue';
default: return 'black';
}
};
const checkSquare = (cell, currentId) => {
const isLeftEdge = (currentId % width === 0);
const isRightEdge = (currentId % width === width - 1);
setTimeout(() => {
if (isGameOver) return;
const check = (id) => {
if (id >= 0 && id < width * height) {
const newCell = cells[id];
click(newCell);
}
};
if (!isLeftEdge) {
check(currentId - 1);
check(currentId - 1 - width);
check(currentId + width - 1);
}
if (!isRightEdge) {
check(currentId + 1);
check(currentId + 1 - width);
check(currentId + width + 1);
}
check(currentId - width);
check(currentId + width);
cell.classList.add('revealed');
}, 10);
};
const gameOver = () => {
isGameOver = true;
faceBtn.textContent = '😵';
cells.forEach(cell => {
if (cell.dataset.isMine === 'true') {
cell.textContent = '💣';
cell.classList.add('revealed', 'mine');
}
});
};
faceBtn.addEventListener('click', createBoard);
createBoard();
}
}
document.addEventListener('DOMContentLoaded', () => {
new WindowManager();
});