-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
429 lines (351 loc) · 14 KB
/
Copy pathscript.js
File metadata and controls
429 lines (351 loc) · 14 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
// config numbers so i can balance this later
const CONFIG = {
TOTAL_TIME: 30,
MAX_STRESS: 100,
// REBALANCED: Dropped idle stress to 0.05 so it matches the healing rate
STRESS_GAIN_IDLE: 0.05,
STRESS_HEAL_ACTIVE: 0.05, // Added this to config for clarity
STRESS_GAIN_HIT: 15, // ouch
STRESS_HEAL_CLICK: 10, // dopamine hit
STRESS_FROM_CLUTTER: 0.05, // how much stress EACH distraction adds per frame
FOCUS_GAIN: 0.2, // slow charge
// REBALANCED: Dropped loss to 0.2 to make it less punishing
FOCUS_LOSS: 0.2,
BRAIN_SPEED: 2.5,
SPAWN_RATE: 1200
};
// global state object to track everything
const STATE = {
running: false,
timeLeft: CONFIG.TOTAL_TIME,
stress: 0,
focusLevel: 0,
mouseX: 0,
mouseY: 0,
brainPos: { x: window.innerWidth/2, y: window.innerHeight/2, vx: 1, vy: 1 },
isHoveringBrain: false,
combo: 0,
distractionsCleared: 0,
// NEW: Stats for the efficiency calculation
totalDistractionsSpawned: 0,
cumulativeFocus: 0,
totalFrames: 0
};
// grab all the dom elements we need. organized by group so i dont lose my mind
const els = {
nav: document.getElementById('main-nav'),
sections: {
intro: document.getElementById('intro-section'),
sim: document.getElementById('simulation-section'),
summary: document.getElementById('summary-section'),
drift: document.getElementById('edu-drift'),
myth: document.getElementById('edu-myth'),
coping: document.getElementById('edu-coping')
},
brain: {
container: document.getElementById('brain-container'),
svg: document.getElementById('brian-the-brain'),
body: document.getElementById('brain-body'),
status: document.getElementById('focus-status'),
tether: document.getElementById('tether-line')
},
hud: {
timer: document.getElementById('timer'),
stressFill: document.getElementById('stress-fill'),
combo: document.getElementById('combo-display'),
comboCount: document.getElementById('combo-count')
},
layer: document.getElementById('distraction-layer'),
glitch: document.getElementById('screen-glitch'),
root: document.documentElement
};
// setup listeners
document.getElementById('start-button').addEventListener('click', startGame);
// make all restart buttons work
document.querySelectorAll('.restart-btn').forEach(btn => {
btn.addEventListener('click', startGame);
});
// navigation buttons - uses data-nav attribute for spa navigation
document.querySelectorAll('[data-nav]').forEach(btn => {
btn.addEventListener('click', (e) => {
const target = e.currentTarget.getAttribute('data-nav');
navigateTo(target);
});
});
// quit game if escape is pressed
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && STATE.running) {
endGame();
}
});
// global mouse tracking for the physics
document.addEventListener('mousemove', (e) => {
STATE.mouseX = e.clientX;
STATE.mouseY = e.clientY;
});
// checking if user is actually focusing on the brain
els.brain.container.addEventListener('mouseenter', () => STATE.isHoveringBrain = true);
els.brain.container.addEventListener('mouseleave', () => STATE.isHoveringBrain = false);
// spa navigation logic. toggles hidden classes.
function navigateTo(targetId) {
// hide everything first
Object.values(els.sections).forEach(sec => {
sec.classList.add('hidden');
sec.classList.remove('fade-active');
});
// show the one we want
const target = document.getElementById(targetId);
if (target) {
target.classList.remove('hidden');
setTimeout(() => target.classList.add('fade-active'), 10);
}
}
// timer references
let gameLoopId, spawnLoopId;
// reset everything and start the chaos
function startGame() {
STATE.running = true;
STATE.timeLeft = CONFIG.TOTAL_TIME;
STATE.stress = 0;
STATE.focusLevel = 50;
STATE.combo = 0;
STATE.distractionsCleared = 0;
// NEW: Reset stats for calculation
STATE.totalDistractionsSpawned = 0;
STATE.cumulativeFocus = 0;
STATE.totalFrames = 0;
// hide nav while playing
els.nav.classList.add('hidden');
// reset brain position to center roughly
STATE.brainPos = {
x: window.innerWidth/2 - 75,
y: window.innerHeight/2 - 75,
vx: Math.random() < 0.5 ? 2 : -2,
vy: Math.random() < 0.5 ? 2 : -2
};
navigateTo('simulation-section');
els.layer.innerHTML = '';
// stop old loops if they exist
if (gameLoopId) cancelAnimationFrame(gameLoopId);
if (spawnLoopId) clearInterval(spawnLoopId);
// go
gameLoopId = requestAnimationFrame(update);
spawnLoopId = setInterval(spawnDistraction, CONFIG.SPAWN_RATE);
}
// main game loop. runs every frame.
function update() {
if (!STATE.running) return;
// time ticks down
STATE.timeLeft -= 0.016;
els.hud.timer.innerText = STATE.timeLeft.toFixed(2);
// NEW: track data for average
STATE.cumulativeFocus += STATE.focusLevel;
STATE.totalFrames++;
// handle movement
moveBrain();
updateTether();
// calculate crowd penalty.
// count how many red distractions exist and add stress for each one.
// this forces the user to clear them or die.
const activeDistractions = els.layer.children.length;
if (activeDistractions > 0) {
STATE.stress += activeDistractions * CONFIG.STRESS_FROM_CLUTTER;
}
// handle mechanics
if (STATE.isHoveringBrain) {
// good: charging focus
STATE.focusLevel = Math.min(100, STATE.focusLevel + CONFIG.FOCUS_GAIN);
// focusing heals stress. now uses the config value (0.05) so it equals the gain
STATE.stress = Math.max(0, STATE.stress - CONFIG.STRESS_HEAL_ACTIVE);
els.brain.status.innerText = "SIGNAL_LOCK";
els.brain.status.style.color = "var(--neon-green)";
} else {
// bad: losing focus
STATE.focusLevel = Math.max(0, STATE.focusLevel - CONFIG.FOCUS_LOSS);
// idle stress gain
STATE.stress = Math.min(100, STATE.stress + CONFIG.STRESS_GAIN_IDLE);
els.brain.status.innerText = "SIGNAL_LOST";
els.brain.status.style.color = "var(--neon-red)";
}
updateVisuals();
// check win/loss state
if (STATE.timeLeft <= 0 || STATE.stress >= 100) {
endGame();
} else {
requestAnimationFrame(update);
}
}
// physics for the brain movement. bouncing dvd logo style basically.
function moveBrain() {
// add some jitter to make it annoying
if (Math.random() < 0.05) STATE.brainPos.vx += (Math.random() - 0.5) * 2;
if (Math.random() < 0.05) STATE.brainPos.vy += (Math.random() - 0.5) * 2;
// cap speed
STATE.brainPos.vx = Math.max(-CONFIG.BRAIN_SPEED, Math.min(CONFIG.BRAIN_SPEED, STATE.brainPos.vx));
STATE.brainPos.vy = Math.max(-CONFIG.BRAIN_SPEED, Math.min(CONFIG.BRAIN_SPEED, STATE.brainPos.vy));
// apply velocity
STATE.brainPos.x += STATE.brainPos.vx;
STATE.brainPos.y += STATE.brainPos.vy;
// wall bouncing
if (STATE.brainPos.x <= 0 || STATE.brainPos.x >= window.innerWidth - 150) {
STATE.brainPos.vx *= -1;
}
if (STATE.brainPos.y <= 0 || STATE.brainPos.y >= window.innerHeight - 150) {
STATE.brainPos.vy *= -1;
}
els.brain.container.style.left = `${STATE.brainPos.x}px`;
els.brain.container.style.top = `${STATE.brainPos.y}px`;
}
// draws the line between mouse and brain
function updateTether() {
const brainCx = STATE.brainPos.x + 75;
const brainCy = STATE.brainPos.y + 75;
els.brain.tether.setAttribute('x1', STATE.mouseX);
els.brain.tether.setAttribute('y1', STATE.mouseY);
els.brain.tether.setAttribute('x2', brainCx);
els.brain.tether.setAttribute('y2', brainCy);
// change line style if connected
if (STATE.isHoveringBrain) {
els.brain.tether.setAttribute('stroke', '#33ff33');
els.brain.tether.setAttribute('stroke-dasharray', '0');
} else {
els.brain.tether.setAttribute('stroke', '#3b82f6');
els.brain.tether.setAttribute('stroke-dasharray', '10,10');
}
}
// updates the hud and visual effects (blur, saturation)
function updateVisuals() {
els.hud.stressFill.style.width = `${STATE.stress}%`;
// make screen blurry and grey when stressed
const blurAmount = (STATE.stress / 100) * 8;
const satAmount = 100 - (STATE.stress / 2);
els.root.style.setProperty('--stress-blur', `${blurAmount}px`);
els.root.style.setProperty('--stress-sat', `${satAmount}%`);
// glitch overlay if you're dying
if (STATE.stress > 80) {
els.glitch.classList.remove('hidden');
} else {
els.glitch.classList.add('hidden');
}
// brain turns red if stressed
const brainColor = STATE.stress > 60 ? '#ff3333' : '#3b82f6';
els.brain.body.setAttribute('fill', brainColor);
}
// distraction thoughts pool
const THOUGHTS = [
"DID_I_LOCK_DOOR?", "CHECK_DISCORD", "HUNGER_LEVEL_LOW",
"LEG_SHAKING", "EARWORM_DETECTED", "TASK_ABORT?",
"TEXT_MSG", "ITCH_DETECTED", "AUDIO_TOO_LOUD",
"BOREDOM", "HOMEWORK_MISSING", "SOCIAL_ANXIETY",
"FOCUS_ERROR", "EXECUTE_TASK"
];
// creates a red box you have to click
function spawnDistraction() {
if (!STATE.running) return;
// prevents crashing if too many pile up
if (els.layer.children.length > 15) return;
// NEW: increment total count for stats
STATE.totalDistractionsSpawned++;
const el = document.createElement('div');
el.className = 'distraction';
el.innerText = THOUGHTS[Math.floor(Math.random() * THOUGHTS.length)];
const x = Math.random() * (window.innerWidth - 150);
const y = Math.random() * (window.innerHeight - 50);
el.style.left = `${x}px`;
el.style.top = `${y}px`;
// random chance to be a "sticky" one
if (Math.random() > 0.8) el.classList.add('sticky');
el.addEventListener('mousedown', (e) => {
e.stopPropagation();
spawnParticles(e.clientX, e.clientY);
el.remove();
// reward for clicking
STATE.stress = Math.max(0, STATE.stress - CONFIG.STRESS_HEAL_CLICK);
STATE.distractionsCleared++;
triggerCombo();
});
els.layer.appendChild(el);
}
// handles the combo counter ui
function triggerCombo() {
STATE.combo++;
els.hud.combo.classList.remove('hidden');
els.hud.comboCount.innerText = STATE.combo;
clearTimeout(STATE.comboTimer);
STATE.comboTimer = setTimeout(() => {
STATE.combo = 0;
els.hud.combo.classList.add('hidden');
}, 1500);
}
// visual pop when clicking a thought
function spawnParticles(x, y) {
for (let i = 0; i < 8; i++) {
const p = document.createElement('div');
p.className = 'particle';
p.style.left = `${x}px`;
p.style.top = `${y}px`;
p.style.backgroundColor = `hsl(${Math.random()*360}, 100%, 50%)`;
document.body.appendChild(p);
const destX = (Math.random() - 0.5) * 100;
const destY = (Math.random() - 0.5) * 100;
const animation = p.animate([
{ transform: 'translate(0,0) scale(1)', opacity: 1 },
{ transform: `translate(${destX}px, ${destY}px) scale(0)`, opacity: 0 }
], {
duration: 500,
easing: 'ease-out'
});
animation.onfinish = () => p.remove();
}
}
// kill everything and show results.
// updated to tell the user WHY they failed/won.
function endGame() {
STATE.running = false;
cancelAnimationFrame(gameLoopId);
clearInterval(spawnLoopId);
els.nav.classList.remove('hidden');
navigateTo('summary-section');
// NEW: complex efficiency calculation
// 1. calculate average focus held throughout the session
let avgFocus = 0;
if (STATE.totalFrames > 0) {
avgFocus = STATE.cumulativeFocus / STATE.totalFrames;
}
// 2. calculate clear rate (avoid division by zero if you died instantly)
let clearRate = 0;
if (STATE.totalDistractionsSpawned > 0) {
clearRate = (STATE.distractionsCleared / STATE.totalDistractionsSpawned) * 100;
} else {
clearRate = 100; // if none spawned, technically you cleared 100% of the threat
}
// 3. weighted score: 70% focus consistency, 30% cleaning up mess
const finalScore = Math.floor((avgFocus * 0.7) + (clearRate * 0.3));
// show stats
document.getElementById('final-focus').innerText = finalScore + "%";
document.getElementById('final-distractions').innerText = STATE.distractionsCleared;
// grab the text elements
const title = document.getElementById('end-title');
const msg = document.getElementById('end-message');
const reason = document.getElementById('end-reason');
// figure out if we died or just ran out of time
if (STATE.stress >= 100) {
// bad ending: stress overload
title.innerText = "SYSTEM_CRASH";
title.style.textShadow = "2px 0 var(--neon-red), -2px 0 var(--neon-pink)";
msg.innerText = "STATUS: SENSORY_OVERLOAD";
msg.style.color = "var(--neon-red)";
reason.innerHTML = "FAILURE_ANALYSIS: Stress levels exceeded 100%. Too many red distraction nodes accumulated in the workspace. The brain was unable to filter the noise, resulting in a total executive shutdown.";
} else {
// good ending: timer ran out
title.innerText = "SESSION_COMPLETE";
title.style.textShadow = "2px 0 var(--neon-green), -2px 0 var(--neon-blue)";
msg.innerText = "STATUS: STABLE";
msg.style.color = "var(--neon-green)";
reason.innerHTML = "SUCCESS_ANALYSIS: Timer reached 0.00 without a system crash. Despite the constant drift and external noise, you successfully regulated dopamine levels and maintained executive control.";
}
// fix the screen filters so we can actually read the text
els.root.style.setProperty('--stress-blur', `0px`);
els.root.style.setProperty('--stress-sat', `100%`);
els.glitch.classList.add('hidden');
}