-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
454 lines (396 loc) · 17.5 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
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
// Variables to track the game state
let gameOver = true;
let gameOverMessage;
let restartButton;
let subTitleText;
let titleText;
let samImage;
let instructionText;
let collidingSprites = []; // Array to store colliding sprite objects
let score = 0;
let scoreDisplay;
let isMovingUp = false; // control character movement
let isMovingDown = false;
let spriteGenerationFrequency;
let characterSprite;
let characterTexture;
// Set up the scene, camera, and renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 20);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
document.body.style.touchAction = 'manipulation'; // prevents double tapping to zoom while playing
document.body.style.backgroundColor = 'black'; // prevents double tapping to zoom while playing
// document.body.style.overflow = "hidden";
const textureLoader = new THREE.TextureLoader();
const heartTexture = textureLoader.load('heart.png'); // image textures
const logoTexture = textureLoader.load('logo.png');
characterTexture = textureLoader.load('sam.png', () => { // Load the character texture
loadCharacter();
});
setupBackground();
// Event listeners
window.addEventListener('keydown', handleCharacterMovement);
window.addEventListener('keyup', handleCharacterMovement);
window.addEventListener('touchstart', handleTouchStart);
window.addEventListener('touchend', handleTouchEnd);
window.addEventListener('resize', onWindowResize);
function loadCharacter() {
const characterMaterial = new THREE.SpriteMaterial({ map: characterTexture });
characterSprite = new THREE.Sprite(characterMaterial);
// Calculate the aspect ratio of the loaded character image
const aspectRatio = characterTexture.image.width / characterTexture.image.height;
// Adjust the scale based on the aspect ratio to maintain proportions
const desiredHeight = 4; // Set the desired height of the character sprite
const desiredWidth = aspectRatio * desiredHeight;
characterSprite.scale.set(desiredWidth, desiredHeight, 1);
// Create the character sprite
scene.add(characterSprite);
// Set the position of the character sprite
characterSprite.position.set(-(window.innerWidth / window.innerHeight) * 10, 0, -15);
}
function setupScoreDisplay() {
scoreDisplay = document.createElement('div');
scoreDisplay.style.position = 'absolute';
scoreDisplay.style.top = '10px';
scoreDisplay.style.right = '10px';
scoreDisplay.style.fontSize = '2rem';
scoreDisplay.style.color = '#ffffff';
scoreDisplay.innerHTML = `Score: ${score}`;
document.body.appendChild(scoreDisplay);
}
// Function to create difficulty selection buttons
function createDifficultyButtons() {
// Add an image alongside the difficulty buttons
samImage = document.createElement('img');
samImage.src = 'sam.png';
samImage.style.position = 'absolute';
samImage.style.top = '20%';
samImage.style.left = '85%';
samImage.style.transform = 'translateX(-50%)';
samImage.style.width = '50rem'; // Adjust the width as needed
document.body.appendChild(samImage);
titleText = document.createElement('div');
titleText.style.position = 'absolute';
titleText.style.top = '15%';
titleText.style.left = '50%';
titleText.style.transform = 'translateX(-50%)';
titleText.style.color = '#ffffff';
titleText.style.fontSize = '6rem';
titleText.style.fontFamily = 'Impact';
titleText.style.font = "bold"
titleText.style.textAlign = "center";
titleText.style.fontWeight = 'bold';
titleText.textContent = "Altman's Adventures";
document.body.appendChild(titleText);
subTitleText = document.createElement('div');
subTitleText.style.position = 'absolute';
subTitleText.style.top = '39%';
subTitleText.style.left = '50%';
subTitleText.style.textAlign = "center";
subTitleText.style.padding = "2rem";
subTitleText.style.transform = 'translateX(-50%)';
subTitleText.style.color = '#ffffff';
subTitleText.style.fontSize = '2rem';
subTitleText.textContent = 'Gather love from your employees and avoid the Board!';
document.body.appendChild(subTitleText);
instructionText = document.createElement('div');
instructionText.style.position = 'absolute';
instructionText.style.top = '97%';
instructionText.style.left = '50%';
instructionText.style.transform = 'translateX(-50%)';
instructionText.style.color = '#ffffff';
instructionText.style.fontSize = '1rem';
instructionText.style.textAlign = "center";
instructionText.textContent = "Use UP and DOWN keys or press to MOVE.";
document.body.appendChild(instructionText);
// Function to handle difficulty selection
function selectDifficulty(difficulty) {
removeDifficultyButtons(); // Remove difficulty buttons once selected
startGame(difficulty); // Start the game with the selected difficulty
}
// Create buttons for different difficulty levels
const difficultyButtons = ['Easy', 'Medium', 'Satya Nadella'];
for (const difficulty of difficultyButtons) {
const button = document.createElement('button');
button.style.position = 'absolute';
button.style.top = `${59 + (difficultyButtons.indexOf(difficulty) * 12)}%`;
button.style.left = '50%';
button.style.color = '#098ed6';
button.style.textAlign = 'center'
button.style.border = '2px solid #098ed6'; // Border style
button.style.borderRadius = '10px'; // Rounded corners
button.style.boxShadow = '2px 2px 4px rgba(0, 0, 0, 0.3)'; // Shadow
button.style.padding = '10px 10px'; // Padding for button
button.style.transform = 'translateX(-50%)';
button.style.fontSize = '3rem';
button.textContent = difficulty;
button.onclick = () => selectDifficulty(difficulty);
document.body.appendChild(button);
}
}
// Function to remove difficulty selection buttons and display score
function removeDifficultyButtons() {
const buttons = document.querySelectorAll('button');
buttons.forEach(button => button.remove());
document.body.removeChild(titleText);
document.body.removeChild(subTitleText);
document.body.removeChild(samImage);
document.body.removeChild(instructionText);
setupScoreDisplay();
}
function setupBackground() {
const canvasGradient = document.createElement('canvas');
canvasGradient.width = window.innerWidth;
canvasGradient.height = window.innerHeight;
const canvasContext = canvasGradient.getContext('2d');
let gradient = canvasContext.createLinearGradient(0, 0, 0, window.innerHeight);
gradient.addColorStop(0, '#098ed6'); // Light blue
canvasContext.fillStyle = gradient;
canvasContext.fillRect(0, 0, window.innerWidth, window.innerHeight);
const backgroundTexture = new THREE.CanvasTexture(canvasGradient);
// Create a plane geometry with the same aspect ratio as the window
const aspect = window.innerWidth / window.innerHeight;
const backgroundGeometry = new THREE.PlaneGeometry(2 * aspect, 2, 1, 1);
// Use the background texture in a material
const backgroundMaterial = new THREE.MeshBasicMaterial({ map: backgroundTexture });
const background = new THREE.Mesh(backgroundGeometry, backgroundMaterial);
// Setting the z-position
background.position.z = -19; // Adjust the value based on your scene setup
// Scale the background to cover the viewport
background.scale.set(window.innerWidth, window.innerHeight, 1);
scene.add(background);
}
// Function to start the game based on selected difficulty
function startGame(difficulty) {
removeGameOverElements();
gameOver = false;
score = 0;
collidingSprites = []; // Clear collidingSprites array
// Adjust the frequency of generated colliding sprites based on difficulty
switch (difficulty) {
case 'Easy':
spriteGenerationFrequency = 0.01; // Adjust as needed
break;
case 'Medium':
spriteGenerationFrequency = 0.05; // Adjust as needed
break;
case 'Satya Nadella':
spriteGenerationFrequency = 0.1; // Adjust as needed
break;
default:
spriteGenerationFrequency = 0.01; // Default to easy
break;
}
gameLoop();
}
// Function to update the character placement in window
function onWindowResize() {
characterSprite.position.set(-(window.innerWidth / window.innerHeight) * 10, 0, -15);
}
// Function to create colliding sprites based on selected frequency
function createSpriteBasedOnDifficulty() {
if (!gameOver && Math.random() < spriteGenerationFrequency) {
createMovingSprite();
}
}
// Function to create a heart or logo sprite
function createMovingSprite() {
let textureToUse;
// Randomly choose between heartTexture and boardTexture
if (Math.random() < 0.5) {
textureToUse = heartTexture;
} else {
textureToUse = logoTexture;
}
const collidingMaterial = new THREE.SpriteMaterial({ map: textureToUse });
const collidingSprite = new THREE.Sprite(collidingMaterial);
// Set initial position randomly along the right side of the screen
const posX = (window.innerWidth / window.innerHeight) * 12;
const posY = Math.random() * 20 - 10; // Y range from -10 to 10
const posZ = -15; // Adjust the Z position if needed
collidingSprite.position.set(posX, posY, posZ);
// Calculate the aspect ratio of the loaded character image
const aspectRatio = textureToUse.image.width / textureToUse.image.height;
// Adjust the scale based on the aspect ratio to maintain proportions
const desiredHeight = 1.5; // Set the desired height of the texture sprite
const desiredWidth = aspectRatio * desiredHeight;
collidingSprite.scale.set(desiredWidth, desiredHeight, 1);
// Add the heart to the scene
scene.add(collidingSprite);
// Add the heart to the collidingSprites array
collidingSprites.push(collidingSprite);
}
// Function to move collidingSprites towards the character
function movecollidingSprites() {
for (let i = 0; i < collidingSprites.length; i++) {
const incoming_sprite = collidingSprites[i];
// Move collidingSprites towards the character along the X-axis
incoming_sprite.position.x -= 0.05; // Adjust the speed as needed
// Check if the incoming_sprite is out of view
if (incoming_sprite.position.x < -20) {
// Remove the incoming_sprite from the scene
scene.remove(incoming_sprite);
collidingSprites.splice(i, 1); // Remove from the collidingSprites array
}
}
}
// Function to handle character movement
function handleCharacterMovement(event) {
if (event.type === 'keydown') {
if (event.key === 'ArrowUp') {
isMovingUp = true;
} else if (event.key === 'ArrowDown') {
isMovingDown = true;
}
} else if (event.type === 'keyup') {
if (event.key === 'ArrowUp') {
isMovingUp = false;
} else if (event.key === 'ArrowDown') {
isMovingDown = false;
}
}
}
// Event listeners for touch events
window.addEventListener('touchstart', handleTouchStart);
window.addEventListener('touchend', handleTouchEnd);
// Function to handle touchstart event
function handleTouchStart(event) {
event.preventDefault();
const touchY = event.touches[0].clientY;
const screenHeight = window.innerHeight;
if (touchY < screenHeight / 2) {
isMovingUp = true;
isMovingDown = false;
} else {
isMovingDown = true;
isMovingUp = false;
}
}
// Function to handle touchend event
function handleTouchEnd() {
isMovingUp = false;
isMovingDown = false;
}
function getCharacterBoundingBox(characterOldBox) {
// Get the character's bounding box
// Get the dimensions of the bounding box
const boxSize = new THREE.Vector3();
characterOldBox.getSize(boxSize);
// Reduce the size of the bounding box along the x-dimension
const reducedSize = 0.2; // Adjust the reduction amount as needed
const newXSize = boxSize.x * reducedSize;
const newYSize = boxSize.y * reducedSize;
// Calculate the center of the character's bounding box
const boxCenter = new THREE.Vector3();
characterOldBox.getCenter(boxCenter);
// Calculate the new minimum and maximum points for the bounding box
const newMin = new THREE.Vector3(boxCenter.x - newXSize / 2, boxCenter.y - newYSize / 2, boxCenter.z - boxSize.z / 2);
const newMax = new THREE.Vector3(boxCenter.x + newXSize / 2, boxCenter.y + newYSize / 2, boxCenter.z + boxSize.z / 2);
// Set the new size for the character's bounding box
return new THREE.Box3(newMin, newMax);
}
// Function to remove game over elements
function removeGameOverElements() {
if (document.body.contains(gameOverMessage)) {
document.body.removeChild(gameOverMessage);
}
if (document.body.contains(restartButton)) {
document.body.removeChild(restartButton);
}
}
// Collision detection function between character and collidingSprites
function checkCollision() {
const characterOldBox = new THREE.Box3().setFromObject(characterSprite);
const newCharacterBox = getCharacterBoundingBox(characterOldBox);
for (let i = 0; i < collidingSprites.length; i++) {
const current_sprite = collidingSprites[i];
const spriteBox = new THREE.Box3().setFromObject(current_sprite);
// Define a buffer distance for collision detection (adjust as needed)
const bufferDistance = 0.1; // This represents half the size of the bounding box
// Expand the character bounding box by the buffer distance
const expandedCharacterBox = characterOldBox.clone();
expandedCharacterBox.expandByScalar(bufferDistance);
if (newCharacterBox.intersectsBox(spriteBox)) {
// Collision detected with a current_sprite
const collidingTexture = current_sprite.material.map;
if (collidingTexture === heartTexture) {
score++;
scene.remove(current_sprite);
collidingSprites.splice(i, 1); // Remove from the collidingSprites array
}
else if (collidingTexture === logoTexture) {
gameOver = true;
// Display game over message and restart button
gameOverMessage = document.createElement('div');
gameOverMessage.style.position = 'absolute';
gameOverMessage.style.top = '50%';
gameOverMessage.style.left = '50%';
gameOverMessage.style.transform = 'translate(-50%, -50%)';
gameOverMessage.style.color = '#ffffff';
gameOverMessage.style.fontSize = '2rem';
gameOverMessage.style.textAlign = 'center'
gameOverMessage.textContent = '❌ You were not consistently candid in your communications with the board!';
// Display game over message and restart button
restartButton = document.createElement('div');
restartButton.style.position = 'absolute';
restartButton.style.top = '65%';
restartButton.style.left = '50%';
restartButton.style.transform = 'translate(-50%, -50%)';
restartButton.style.color = '#FF0000';
restartButton.style.fontSize = '2rem';
restartButton.style.textAlign = 'center'
restartButton.style.border = '2px solid #FF0000'; // Border style
restartButton.style.borderRadius = '10px'; // Rounded corners
restartButton.style.boxShadow = '2px 2px 4px rgba(0, 0, 0, 0.3)'; // Shadow
restartButton.style.padding = '10px 10px'; // Padding for button
restartButton.style.cursor = 'pointer'; // Change cursor on hover
restartButton.textContent = 'Bring back Sam and Greg! ❤️';
document.body.appendChild(gameOverMessage);
document.body.appendChild(restartButton);
restartButton.onclick = function () {
removeGameOverElements();
gameOver = false;
score = 0;
// for each colliding delete it and set colliidng array to empty
for (let i = 0; i < collidingSprites.length; i++) {
scene.remove(collidingSprites[i]);
}
collidingSprites = [];
}
}
}
}
}
// main movement of characters and sprites
function animate() {
requestAnimationFrame(animate);
if (!gameOver) {
// Character movement
if (isMovingUp) {
if (characterSprite.position.y < 10) {
characterSprite.position.y += 0.2;
}
} if (isMovingDown) {
if (characterSprite.position.y > -10) {
characterSprite.position.y -= 0.2;
}
}
// Create a new sprite occasionally
createSpriteBasedOnDifficulty();
movecollidingSprites();
// Check collision between character and collidingSprites
checkCollision();
// Update score display
scoreDisplay.innerHTML = `Score: ${score}`;
renderer.render(scene, camera);
}
}
function gameLoop() {
animate();
}
// main execution
// Initialize the game by creating difficulty selection buttons
// this later calls the gameloop
createDifficultyButtons();