-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
266 lines (232 loc) · 9.04 KB
/
app.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
// function imports
import { addPoints } from './js/addPoints.js';
import { movePlayer } from './js/movePlayer.js';
import { playerDied } from './js/playerDied.js';
import { createClouds } from './js/createClouds.js';
import { createTriangles } from './js/createTriangles.js';
import { generatePlatforms } from './js/generatePlatforms.js';
// rendering imports
import { drawScore } from './js/drawScore.js';
import { drawClouds } from './js/drawClouds.js';
import { drawTriangles } from './js/drawTriangles.js';
import { drawPlayerLives } from './js/drawPlayerLives.js';
// collision detection imports
import { isPlayerOnTheGround } from './js/collisionDetection/isPlayerOnTheGround.js';
import { isPlayerAtEndOfLevel } from './js/collisionDetection/isPlayerAtEndOfLevel.js';
import { keepPlayerOnTheScreen } from './js/collisionDetection/keepPlayerOnTheScreen.js';
// level exit imports
import { findLastPlatformY } from './js/findLastPlatformY.js';
// class imports
import { Menu } from './js/classes/menu.js';
import { Player } from './js/classes/player.js';
import { LevelExit } from './js/classes/levelExit.js';
let trianglesCurrentPosition = 0;
let trianglePeakMaxHeight = innerHeight/2;
let trianglePeakMinHeight = innerHeight/1.5;
let levelExit
let triangles = [];
let clouds = [];
let level = 1
// canvas setup
const canvas = document.querySelector('canvas');
const context = canvas.getContext("2d");
canvas.width = innerWidth;
canvas.height = innerHeight;
// menu
let isMenuDisplayed = true;
let displayMenuImage = true;
const menuImage = document.getElementById('image');
const menuNavToOptions = document.getElementById('nav-to-options');
const menuNavToCredits = document.getElementById('nav-to-credits');
const menuNavToLevelSelect = document.getElementById('nav-to-level-select');
const menuNavToInstructions = document.getElementById('nav-to-instructions');
const menuNavToMain = document.getElementsByClassName('navigate-to-main-menu');
const menuNavToTechnicalInfo = document.getElementById('nav-to-technical-info');
const startButton = document.getElementById('start-button');
const finalScoreText = document.getElementById('final-score');
const restartButton = document.getElementById('restart-button');
// player variables
let player;
let playerLanded = false;
let playerStartingXPosition;
let playerStartingLives = 3;
let playerLives = playerStartingLives;
// score variables
const scorePositionX = canvas.width / 2;
const scorePositionY = 50;
let scoreTotal = 0;
let scoreThisLife = 0;
// level variables
let platforms;
let levelWidth = 10000;
let scoredPlatforms = new Set();
let endPortalX = levelWidth;
let endPortalWidth = 40;
let endPortalHeight = 100;
// Sounds
const musicMenu = new Audio("sounds/Funk'e'Tony_-_Funkafe.mp3");
const soundPlayerLanding = new Audio('sounds/332661__reitanna__big-thud.wav');
const soundGameOver = new Audio('sounds/362204__taranp__horn_fail_wahwah_3.wav');
const soundPlayerJumping = new Audio('sounds/399095__plasterbrain__8bit-jump.wav');
const backgroundMusic = new Audio('sounds/Kirill_Kharchenko_-_Background_Hip-Hop_Funk.mp3');
const menuMain = new Menu('start-menu');
const menuWelcome = new Menu('welcome-menu');
const menuOptions = new Menu('options-menu');
const menuCredits = new Menu('credits-menu');
const menuGameOver = new Menu('game-over-menu');
const menuContainer = new Menu('menu-container');
const menuLevelSelect = new Menu('level-select-menu');
const menuInstructions = new Menu('instructions-menu');
const menuTechnicalInfo = new Menu('technical-info-menu');
const allMenus = [menuMain, menuWelcome, menuOptions, menuCredits, menuGameOver, menuLevelSelect, menuInstructions, menuTechnicalInfo];
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
menuNavToOptions.addEventListener('click', () => showMenu(menuOptions));
menuNavToCredits.addEventListener('click', () => showMenu(menuCredits));
menuNavToLevelSelect.addEventListener('click', () => showMenu(menuLevelSelect));
menuNavToInstructions.addEventListener('click', () => showMenu(menuInstructions));
menuNavToTechnicalInfo.addEventListener('click', () => showMenu(menuTechnicalInfo));
menuWelcome.show();
// Physics Variables
let speed = 5;
let friction = .7;
let gravity = 1.3;
let keys = {
right: {
pressed: false
},
left: {
pressed: false
}
}
// INPUT EVENT LISTENERS--------------------------------------------------------------
onload = function() {
for(var i = 0; i < menuNavToMain.length; i++) {
let eachOne = menuNavToMain[i];
eachOne.onclick = function() {
menuCredits.hide();
menuInstructions.hide();
menuLevelSelect.hide();
menuOptions.hide();
menuTechnicalInfo.hide();
menuGameOver.hide();
if (displayMenuImage) {
menuImage.style.display = 'flex'
}
menuMain.show();
}
}
}
import { setupKeyboardEvents } from './js/eventListeners/keyboardEvents.js';
// GAME LOOP -------------------------------------------------------------------------
const loop = function() {
// clear screen
context.clearRect(0, 0, canvas.width, canvas.height);
// render elemnts
drawPlayerLives(playerLives, context);
drawClouds(clouds, player, context, levelWidth, innerWidth);
drawScore(context, scoreTotal, scoreThisLife, scorePositionX, scorePositionY);
drawTriangles(triangles, context, player, levelWidth, innerWidth, innerHeight);
// update elements
player.update(gravity, context);
levelExit.update(context, player, levelWidth, innerWidth);
platforms.forEach(platform => {
platform.update(context, player, levelWidth);
})
// collision detection
isPlayerOnAPlatform();
isPlayerOnTheGround(player, canvas) ? playerDied(playerLives) ? startLevel() : gameOver() : null;
isPlayerAtEndOfLevel(player, levelExit) ? gameOver() : null;
// move player
player = movePlayer(player, keys, speed, friction)
player = keepPlayerOnTheScreen(player, levelWidth, playerStartingXPosition);
// check if menu is displayed
if (isMenuDisplayed) {
return;
}
// request next frame
requestAnimationFrame(loop);
}
// FUNCTIONS --------------------------------------------------------------------------
function startLevel() {
if (playerLives === 0) {
gameOver()
} else {
playerLives--;
player.position.x = playerStartingXPosition;
player.position.y = 20;
player.velocity.y = 0;
player.velocity.x = 0;
player.jumping = false;
player.inLevelXPosition.x = playerStartingXPosition;
levelExit.position.x = levelExit.initialPosition.x
scoreThisLife=0
platforms = generatePlatforms(level, innerHeight, levelWidth);
backgroundMusic.play()
}
}
function gameOver() {
backgroundMusic.pause()
soundGameOver.play()
menuContainer.show();
showMenu(menuGameOver);
isMenuDisplayed = true
musicMenu.play()
if (displayMenuImage) {
menuImage.style.display = 'flex'
}
finalScoreText.textContent = `${scoreTotal}`
isMenuDisplayed = true
}
function startGame() {
createGameAssets()
menuContainer.hide();
isMenuDisplayed = false
displayMenuImage = false
musicMenu.pause()
scoreThisLife = 0;
scoreTotal = 0
playerLives = playerStartingLives
backgroundMusic.play();
window.requestAnimationFrame(loop)
}
function isPlayerOnAPlatform() {
for (let platform of platforms) {
if (
player.position.y >= platform.position.y - player.height &&
player.position.y <= platform.position.y &&
player.position.x + player.width >= platform.position.x &&
player.position.x <= platform.position.x + platform.width
) {
player.jumping = false;
if (!playerLanded) {
soundPlayerLanding.play()
playerLanded = true;
}
player.position.y = platform.position.y - player.height;
player.velocity.y = 0
let addPointsResults = addPoints(platform, scoredPlatforms, scoreTotal, scoreThisLife);
scoredPlatforms = addPointsResults.scoredPlatforms;
scoreTotal = addPointsResults.scoreTotal;
scoreThisLife = addPointsResults.scoreThisLife;
return true;
}
}
return false;
}
function createGameAssets() {
player = new Player
playerStartingXPosition = player.position.x
platforms = generatePlatforms(level, innerHeight, levelWidth);
let endPortalY = findLastPlatformY(platforms, endPortalHeight)
levelExit = new LevelExit(endPortalX, endPortalY, endPortalWidth, endPortalHeight);
triangles = createTriangles(levelWidth, trianglePeakMinHeight, trianglePeakMaxHeight, trianglesCurrentPosition);
clouds = createClouds(levelWidth, clouds)
player.create(context)
// create event listeners
setupKeyboardEvents(player, keys, soundPlayerJumping, isPlayerOnAPlatform)
}
function showMenu(requestedMenu) {
allMenus.forEach(menu => menu.hide());
requestedMenu.show();
}