-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
387 lines (319 loc) · 13.6 KB
/
main.js
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
document.addEventListener("DOMContentLoaded", function () {
const spinSound = document.getElementById("spin-sound");
const bangSound = document.getElementById("bang-sound");
const cylinder = document.getElementById("cylinder");
const cylinderWrapper = cylinder.parentElement;
const message = document.getElementById("message");
const spinButton = document.getElementById("spin-button");
const fireButton = document.getElementById("fire-button");
const counter = document.getElementById("counter");
const redOverlay = document.getElementById("red-overlay");
const heartbeatPulses = document.querySelectorAll(".heartbeat-pulse");
// Apple devices are kinda dumb
const clickSound1 = document.getElementById("click-sound");
const clickSound2 = document.getElementById("click-sound1");
const clickSound3 = document.getElementById("click-sound2");
const clickSound4 = document.getElementById("click-sound3");
const clickSound5 = document.getElementById("click-sound4");
const clickSound6 = document.getElementById("click-sound5");
const chambersNodeList = document.querySelectorAll(".chamber");
const chambers = Array.from(chambersNodeList).sort((a, b) => {
// Extract the chamber number from the class name
const getNumber = (el) => {
const classes = el.className.split(" ");
const numberClass = classes.find((c) => ["one", "two", "three", "four", "five", "six"].includes(c));
const numbers = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
};
return numbers[numberClass];
};
return getNumber(a) - getNumber(b);
});
redOverlay.style.opacity = "1";
redOverlay.style.visibility = "visible";
let isSpinning = false;
let loadedChamber = null;
let currentChamber = 0;
let attempts = 0;
let gameStarted = false;
let isSpun = false;
let totalRotation = 0;
// Reset sounds function (in case of quick successive plays)
function resetSound(sound) {
sound.pause();
sound.currentTime = 0;
sound.volume = 0.4;
}
chambers.forEach((chamber, index) => {
chamber.addEventListener("click", function () {
if (gameStarted || isSpinning) return;
// Load the bullet in clicked chamber
const bullet = this.querySelector(".bullet");
bullet.classList.add("loaded");
loadedChamber = index;
gameStarted = true;
// Show spin button
spinButton.style.visibility = "visible";
message.textContent = "Spin the cylinder";
});
});
/* Spin Button */
spinButton.addEventListener("click", function () {
if (isSpinning || isSpun) return;
isSpinning = true;
cylinderWrapper.classList.add("spinning");
spinButton.style.visibility = "hidden";
message.textContent = "Spinning...";
resetSound(spinSound);
spinSound.play();
// Add question marks to all chambers
chambers.forEach((chamber) => {
chamber.classList.add("unknown");
chamber.querySelector(".bullet").classList.add("unknown");
chamber.querySelector(".bullet").classList.remove("loaded");
});
setTimeout(() => {
cylinderWrapper.classList.remove("spinning");
isSpinning = false;
isSpun = true;
// Use weighted spin function
const targetChamber = getWeightedSpinResult(loadedChamber);
const spins = (targetChamber - loadedChamber + 6) % 6;
loadedChamber = targetChamber;
currentChamber = 0;
fireButton.style.visibility = "visible";
message.textContent = "Pull the trigger";
counter.textContent = "0/6";
}, 2000);
});
/* Fire Button */
let currentClickSound = 1;
fireButton.addEventListener("click", function () {
message.textContent = " ";
if (isSpinning) return;
isSpinning = true;
totalRotation += 60;
cylinderWrapper.style.transform = `scale(var(--cylinder-scale)) rotate(${totalRotation}deg)`;
attempts++;
counter.textContent = `${attempts}/6`;
// Remove the final-bullet class from all chambers first
chambers.forEach((chamber) => {
chamber.classList.remove("final-bullet");
});
setTimeout(() => {
if (currentChamber === loadedChamber) {
// Remove bullets
chambers.forEach((chamber) => {
chamber.classList.remove("unknown");
chamber.querySelector(".bullet").classList.remove("unknown");
chamber.querySelector(".bullet").classList.remove("loaded");
});
// Bang!
resetSound(bangSound);
bangSound.play();
createBloodSplatters();
heartbeatPulses.forEach((pulse) => {
pulse.classList.remove("active");
});
message.textContent = "BANG!";
cylinderWrapper.classList.add("game-over");
fireButton.style.visibility = "hidden";
redOverlay.classList.add("show");
} else {
const clickSoundElement = document.getElementById(`click-sound${currentClickSound}`);
resetSound(clickSoundElement);
clickSoundElement.play();
// Increment the click sound counter (1 through 6)
currentClickSound = (currentClickSound % 6) + 1;
// Remove bullet class from current chamber
chambers[currentChamber].querySelector(".bullet").classList.remove("loaded");
chambers[currentChamber].querySelector(".bullet").classList.remove("unknown");
chambers[currentChamber].classList.remove("unknown");
message.textContent = "*click*";
currentChamber = (currentChamber + 1) % 6;
isSpinning = false;
}
}, 500);
// Check if this is the second-to-last attempt (5th shot)
if (attempts === 5 && loadedChamber === 5) {
// We know the next chamber is the final one, so mark it
const nextChamber = chambers[(currentChamber + 1) % 6];
nextChamber.classList.add("final-bullet");
heartbeatPulses.forEach((pulse) => {
pulse.classList.add("active");
});
} else if (attempts < 5) {
// Make sure to remove the effect if we're not on the final chamber
heartbeatPulses.forEach((pulse) => {
pulse.classList.remove("active");
});
}
});
});
function getWeightedSpinResult(initialChamber) {
// Define weights for each chamber (1-6)
// The weights are relative probabilities
const weights = {
1: 0.1, // Least likely
2: 0.2,
3: 0.2,
4: 0.2,
5: 0.15,
6: 0.15,
};
// Add a slight bias based on initial chamber position
// This creates a subtle tendency to land closer to the starting position
const biasedWeights = { ...weights };
const maxBias = 0.05; // Maximum bias amount
// Apply distance-based bias
Object.keys(biasedWeights).forEach((chamber) => {
const distance = Math.abs(initialChamber - parseInt(chamber));
const adjustedDistance = Math.min(distance, 6 - distance); // Account for circular nature
const bias = maxBias * (1 - adjustedDistance / 3); // Linear falloff
biasedWeights[chamber] += bias;
});
// Normalize weights to ensure they sum to 1
const totalWeight = Object.values(biasedWeights).reduce((sum, weight) => sum + weight, 0);
Object.keys(biasedWeights).forEach((chamber) => {
biasedWeights[chamber] /= totalWeight;
});
// Convert weights to cumulative probabilities
let cumulative = 0;
const cumulativeWeights = {};
Object.keys(biasedWeights).forEach((chamber) => {
cumulative += biasedWeights[chamber];
cumulativeWeights[chamber] = cumulative;
});
// Generate random number and find corresponding chamber
const random = Math.random();
for (const [chamber, weight] of Object.entries(cumulativeWeights)) {
if (random <= weight) {
return parseInt(chamber) - 1; // Convert to 0-based index
}
}
return 5; // Fallback to last chamber (should never reach here)
}
function createBloodSplatters() {
let container = document.getElementById('blood-splatter-container');
if (!container) {
container = document.createElement('div');
container.id = 'blood-splatter-container';
document.body.appendChild(container);
}
const numSplatters = Math.floor(Math.random() * 5) + 6;
let largeSplatCount = 0;
let splat1Count = 0;
let largeSplat1Exists = false;
// Sectors for better distribution
const sectors = [
{ x: [0, 50], y: [0, 50], used: 0 }, // Top Left
{ x: [50, 100], y: [0, 50], used: 0 }, // Top Right
{ x: [0, 50], y: [50, 100], used: 0 }, // Bottom Left
{ x: [50, 100], y: [50, 100], used: 0 } // Bottom Right
];
// Helper function to get random position within a sector
function getPositionInSector(sector, size) {
const sizePercentage = (size / window.innerWidth) * 100;
const padding = sizePercentage / 2;
// Adjust ranges to account for splatter size
const xMin = Math.max(sector.x[0] + padding, sector.x[0]);
const xMax = Math.min(sector.x[1] - padding, sector.x[1]);
const yMin = Math.max(sector.y[0] + padding, sector.y[0]);
const yMax = Math.min(sector.y[1] - padding, sector.y[1]);
return {
x: xMin + (Math.random() * (xMax - xMin)),
y: yMin + (Math.random() * (yMax - yMin))
};
}
// Helper function to select a sector
function selectSector(size) {
const isLarge = size > 200;
if (isLarge) {
// For large splatters, prefer less used sectors
const availableSectors = sectors
.filter(s => s.used < 3) // Increased from 2 to 3 since we have fewer sectors
.sort((a, b) => a.used - b.used);
if (availableSectors.length > 0) {
const sector = availableSectors[0];
sector.used++;
return sector;
}
}
// For smaller splatters or if no preferred sectors available
// Weighted random selection favoring less used sectors
const totalWeight = sectors.reduce((sum, sector) => sum + (4 - sector.used), 0); // Changed from 3 to 4
let random = Math.random() * totalWeight;
for (const sector of sectors) {
const weight = 4 - sector.used; // Changed from 3 to 4
if (random <= weight) {
sector.used++;
return sector;
}
random -= weight;
}
return sectors[Math.floor(Math.random() * sectors.length)];
}
for (let i = 0; i < numSplatters; i++) {
const splatter = document.createElement('img');
// Size logic
let size;
const sizeRoll = Math.random();
if (largeSplatCount < 3 && sizeRoll > 0.6) {
size = 200 + Math.random() * 150;
largeSplatCount++;
} else {
size = largeSplatCount >= 2 ?
(30 + Math.random() * 100) :
(50 + Math.random() * 130);
}
// Splatter type selection with constraints
let splatterNum;
if (size > 200) {
if (splat1Count === 0 && Math.random() > 0.5) {
splatterNum = 1;
splat1Count++;
largeSplat1Exists = true;
} else {
splatterNum = Math.random() < 0.5 ? 2 : 3;
}
} else {
if (splat1Count < 2 && !largeSplat1Exists && Math.random() > 0.7) {
splatterNum = 1;
splat1Count++;
} else if (splat1Count < 1 && !largeSplat1Exists && Math.random() > 0.5) {
splatterNum = 1;
splat1Count++;
} else {
splatterNum = Math.random() < 0.5 ? 2 : 3;
}
}
splatter.src = `img/splat${splatterNum}.svg`;
// Position using sector system
const sector = selectSector(size);
const position = getPositionInSector(sector, size);
splatter.style.position = 'fixed';
splatter.style.left = `${position.x}%`;
splatter.style.top = `${position.y}%`;
splatter.style.width = `${size}px`;
splatter.style.height = `${size}px`;
let rotation;
if (splatterNum === 1) {
rotation = (Math.random() * 40) - 20;
} else {
rotation = Math.random() * 360;
}
splatter.style.transform = `translate(-50%, -50%) rotate(${rotation}deg)`;
const baseOpacity = 0.3 + Math.random() * 0.4;
splatter.style.opacity = size > 200 ?
Math.min(baseOpacity * 1.2, 0.85) :
baseOpacity;
splatter.style.pointerEvents = 'none';
splatter.style.filter = 'brightness(0.8)';
container.appendChild(splatter);
}
}