-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
287 lines (243 loc) · 8.2 KB
/
index.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
import Player from "./Player.js";
import Ground from "./Ground.js";
import RockController from "./RockController.js";
import Score from "./Score.js";
// GAME SETTINGS
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const GAME_WIDTH = 800;
const GAME_HEIGHT = 200;
let scaleRatio = null;
let previousTime = null;
const GAME_SPEED_START = 0.75;
const GAME_SPEED_INCREMENT = 0.00001;
// ROCK SETTINGS
const ROCK_CONFIG = [
{ width: 20 * 3, height: 20 * 3, image: 'images/rock1.png' },
{ width: 32 * 1.25, height: 32 * 1.25, image: 'images/rock2.png' },
{ width: 64, height: 64, image: 'images/rock3.png' }
];
// PLAYER SETTINGS
const PLAYER_WIDTH = 104 / 1.5; // 58
const PLAYER_HEIGHT = 48 / 1.5; // 62
// JUMP SETTINGS
const MAX_JUMP_HEIGHT = GAME_HEIGHT;
const MIN_JUMP_HEIGHT = 150;
// GROUND SETTINGS
const GROUND_WIDTH = 576;
const GROUND_HEIGHT = 64;
const GROUND_AND_ROCK_SPEED = 0.5;
// GAME OBJECTS
let player = null;
let ground = null;
let gameSpeed = GAME_SPEED_START;
let rockController = null;
let gameOver = false;
let pixelFontLoaded = false;
let hasAddedEventListenersForRestart = false;
let waitingToStart = true;
let score = null;
let explosionCompleted = false;
function createSprites() {
const playerWidthInGame = PLAYER_WIDTH * scaleRatio;
const playerHeightInGame = PLAYER_HEIGHT * scaleRatio;
const minJumpHeightInGame = MIN_JUMP_HEIGHT * scaleRatio;
const maxJumpHeightInGame = MAX_JUMP_HEIGHT * scaleRatio;
const groundWidthInGame = GROUND_WIDTH * scaleRatio;
const groundHeightInGame = GROUND_HEIGHT * scaleRatio;
if (!player) {
player = new Player(ctx, playerWidthInGame, playerHeightInGame, minJumpHeightInGame, maxJumpHeightInGame, scaleRatio);
console.log('Player instance created');
} else {
player.width = playerWidthInGame;
player.height = playerHeightInGame;
player.minJumpHeight = minJumpHeightInGame;
player.maxJumpHeight = maxJumpHeightInGame;
player.scaleRatio = scaleRatio;
player.x = 10 * scaleRatio;
player.y = canvas.height - player.height - (65 * scaleRatio);
console.log('Player instance updated:', player);
}
if (!ground) {
ground = new Ground(ctx, groundWidthInGame, groundHeightInGame, GROUND_AND_ROCK_SPEED, scaleRatio);
console.log('Ground instance created');
} else {
ground.width = groundWidthInGame;
ground.height = groundHeightInGame;
ground.scaleRatio = scaleRatio;
console.log('Ground instance updated:', ground);
}
const rockImages = ROCK_CONFIG.map(rock => {
const image = new Image();
image.src = rock.image;
return {
image: image,
width: rock.width * scaleRatio,
height: rock.height * scaleRatio
};
});
rockController = new RockController(ctx, rockImages, scaleRatio, GROUND_AND_ROCK_SPEED, ground.y);
score = new Score(ctx, scaleRatio);
}
function setScreen() {
scaleRatio = getScaleRatio();
canvas.width = GAME_WIDTH * scaleRatio;
canvas.height = GAME_HEIGHT * scaleRatio;
createSprites();
console.log('Screen set with scale ratio:', scaleRatio);
}
function getScaleRatio() {
const screenHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
const screenWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
if (screenWidth / screenHeight < GAME_WIDTH / GAME_HEIGHT) {
return screenWidth / GAME_WIDTH;
} else {
return screenHeight / GAME_HEIGHT;
}
}
function loadFont(name, url) {
const font = new FontFace(name, `url(${url})`);
return font.load().then(function(loadedFont) {
document.fonts.add(loadedFont);
return loadedFont;
});
}
function showGameOver() {
const fontSize = 70 * scaleRatio;
ctx.font = `${fontSize}px PixelFont`; // Use the pixel font
ctx.fillStyle = 'white';
// Add shadow properties
ctx.shadowColor = 'black';
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
const x = canvas.width / 3;
const y = canvas.height / 3;
ctx.fillText("GAME OVER", x, y);
// Reset shadow properties for other drawings
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
}
function setUpGameResetButton() {
if (!hasAddedEventListenersForRestart) {
hasAddedEventListenersForRestart = true;
setTimeout(() => {
window.addEventListener("keyup", reset, { once: true });
window.addEventListener("touchstart", reset, { once: true });
}, 1000);
}
}
function reset() {
hasAddedEventListenersForRestart = false;
gameOver = false;
explosionCompleted = false;
ground.reset();
rockController.reset();
player.reset(); // Reset player image
gameSpeed = GAME_SPEED_START;
previousTime = null; // Reset previousTime to restart the game loop timing
waitingToStart = false; // Reset to waiting state
score.reset(); // Reset score
}
function showStartGameText() {
const fontSize = 40 * scaleRatio;
ctx.font = `${fontSize}px PixelFont`; // Use the pixel font
ctx.fillStyle = 'white';
// Add shadow properties
ctx.shadowColor = 'black';
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
const x = canvas.width / 3; // Adjusted to fit text properly
const y = canvas.height / 3;
ctx.fillText("Press Space To Start", x, y);
// Reset shadow properties for other drawings
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
}
function startGame() {
waitingToStart = false;
gameOver = false;
gameSpeed = GAME_SPEED_START;
previousTime = null;
// Reset any other game states if necessary
}
function updateGameSpeed(frameTimeDelta){
gameSpeed += frameTimeDelta * GAME_SPEED_INCREMENT
}
function clearScreen() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
console.log('Screen cleared');
}
function gameLoop(currentTime) {
if (previousTime == null) {
previousTime = currentTime;
requestAnimationFrame(gameLoop);
return;
}
const frameTimeDelta = currentTime - previousTime;
previousTime = currentTime;
clearScreen();
if (!gameOver && !waitingToStart) {
ground.update(gameSpeed, frameTimeDelta);
player.update(gameSpeed, frameTimeDelta);
rockController.update(gameSpeed, frameTimeDelta);
updateGameSpeed(frameTimeDelta);
score.update(frameTimeDelta);
}
if (!gameOver && rockController.collideWith(player)) {
player.triggerExplosion();
gameOver = true;
setUpGameResetButton();
score.setHighScore();
}
player.draw();
ground.draw();
rockController.draw();
score.draw();
if (gameOver) {
if (player.explosionInProgress) {
player.update(gameSpeed, frameTimeDelta); // Continue updating player to show explosion animation
} else if (!explosionCompleted) {
showGameOver();
explosionCompleted = true;
} else {
showGameOver(); // Ensure game over text remains on screen
}
}
if (waitingToStart) {
showStartGameText();
}
requestAnimationFrame(gameLoop);
}
function initializeGame() {
setScreen();
window.addEventListener('resize', () => setTimeout(setScreen, 100));
if (screen.orientation) {
screen.orientation.addEventListener('change', setScreen);
}
loadFont('PixelFont', 'fonts/pixel-font.ttf').then(() => {
pixelFontLoaded = true;
console.log('PixelFont loaded');
}).catch(error => {
console.error('Failed to load PixelFont:', error);
});
window.addEventListener("keyup", (e) => {
if (waitingToStart && e.code === "Space") {
startGame();
}
});
window.addEventListener("touchstart", () => {
if (waitingToStart) {
startGame();
}
});
requestAnimationFrame(gameLoop);
window.addEventListener("keyup", reset, { once: true });
window.addEventListener("touchstart", reset, { once: true });
}
initializeGame();