-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
629 lines (524 loc) · 20.1 KB
/
game.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
'use strict';
let gameScale = 25;
let worldSize = vec2(200, 32);
let currentLevel = 0;
let activePlayerIndex = 0;
let currentPlayer = null;
let background;
// Add game state tracking
let gameState = 'start'; // 'start', 'credits', 'playing', 'paused', 'completed', 'gameover'
let menuSelection = 0; // 0: Start, 1: Credits
let pauseMenuSelection = 0; // 0: Resume, 1: Main Menu
// Add start screen background
let startScreenBackground = new Image();
startScreenBackground.src = '../assets/startscreen.png';
// Add at the top with other image loads
let textLogo = new Image();
textLogo.src = '../assets/text-logo.png';
// Add at the top with other variables
let timeScale = 1; // Replace engineUpdateEnabled with timeScale
let unlockedCharacters = [Bear, Bunny]; // Starting with Bear and Bunny unlocked
let lockedCharacters = [Cat, Dog, Parrot, Turtle]; // Other characters are locked
let currentCharacterIndex = 0;
let gameUI;
let levelManager;
let transition;
// Sprite sheets konfigürasyonunu kaldır
const spriteSheets = []; // Boş array bırakalım, çünkü engineInit bir array bekliyor
// Global değişken olarak ekleyin
let customCursor;
// DOMContentLoaded event listener ekleyin
document.addEventListener('DOMContentLoaded', () => {
customCursor = document.getElementById('customCursor');
if (customCursor) { // customCursor'un bulunduğundan emin ol
document.addEventListener('mousemove', (e) => {
if (gameState !== 'playing' && customCursor) {
customCursor.style.display = 'block';
customCursor.style.left = (e.clientX - 16) + 'px';
customCursor.style.top = (e.clientY - 16) + 'px';
} else if (customCursor) {
customCursor.style.display = 'none';
}
});
}
});
function gameInit() {
cameraScale = gameScale;
gravity = -0.02;
// Piksel-perfect rendering için canvas ayarları
mainContext.imageSmoothingEnabled = false;
overlayContext.imageSmoothingEnabled = false;
background = new ParallaxBackground(mainCanvas, {
get x() { return cameraPos.x },
get y() { return cameraPos.y }
}, currentLevel);
gameUI = new GameUI();
levelManager = new LevelManager();
transition = new Transition();
}
function startGame() {
gameState = 'playing';
levelManager.loadLevel(0);
}
function loadNextLevel() {
if (levelManager.currentLevelIndex < levelManager.levels.length - 1) {
// Fade out
transition.start('spiral', 0.7, () => {
levelManager.nextLevel();
// Unlock the next character after skipping a level
if (lockedCharacters.length > 0) {
const unlockedCharacter = lockedCharacters.shift(); // Get the next locked character
unlockedCharacters.push(unlockedCharacter); // Add to unlocked list
console.log(`${unlockedCharacter.name} has been unlocked!`);
}
currentPlayer.switchesLeft += 5;
console.log("Added 5 switches to the player")
background = new ParallaxBackground(mainCanvas, {
get x() { return cameraPos.x },
get y() { return cameraPos.y }
}, levelManager.currentLevelIndex);
// Fade in
transition.start('spiral', 0.7, null, -1);
}, 1);
} else {
gameState = 'completed';
engineObjects.forEach(o => {
if (o !== background) {
o.destroy();
}
});
}
}
function drawStartScreen() {
// Draw the background image
if (startScreenBackground.complete) {
const scale = Math.max(
mainCanvas.width / startScreenBackground.width,
mainCanvas.height / startScreenBackground.height
);
const scaledWidth = startScreenBackground.width * scale;
const scaledHeight = startScreenBackground.height * scale;
const x = (mainCanvas.width - scaledWidth) / 2;
const y = (mainCanvas.height - scaledHeight) / 2;
overlayContext.drawImage(
startScreenBackground,
x, y,
scaledWidth,
scaledHeight
);
} else {
overlayContext.fillStyle = '#1a1a33';
overlayContext.fillRect(0, 0, mainCanvas.width, mainCanvas.height);
}
// Add semi-transparent overlay for better text readability
overlayContext.fillStyle = 'rgba(0, 0, 0, 0.5)';
overlayContext.fillRect(0, 0, mainCanvas.width, mainCanvas.height);
if (textLogo.complete) {
const logoWidth = mainCanvas.width * 0.4; // Adjust this value to change logo size
const logoHeight = (logoWidth / textLogo.width) * textLogo.height;
const logoX = (mainCanvas.width - logoWidth) / 2;
const logoY = mainCanvas.height * 0.15; // Adjust this value to change vertical position
overlayContext.drawImage(
textLogo,
logoX, logoY,
logoWidth, logoHeight
);
}
// Menu items
overlayContext.textAlign = 'center';
overlayContext.textBaseline = 'middle';
overlayContext.fillStyle = '#fff';
overlayContext.font = '24px Arial';
const menuItems = ['Start Game', 'Credits', 'Help'];
const menuY = mainCanvas.height/2;
const menuSpacing = 40;
menuItems.forEach((item, index) => {
if (index === menuSelection) {
overlayContext.fillStyle = '#ffff00';
overlayContext.fillText('> ' + item + ' <', mainCanvas.width/2, menuY + index * menuSpacing);
} else {
overlayContext.fillStyle = '#fff';
overlayContext.fillText(item, mainCanvas.width/2, menuY + index * menuSpacing);
}
});
}
function drawMenuBackground() {
// Reuse background drawing code
if (startScreenBackground.complete) {
const scale = Math.max(
mainCanvas.width / startScreenBackground.width,
mainCanvas.height / startScreenBackground.height
);
const scaledWidth = startScreenBackground.width * scale;
const scaledHeight = startScreenBackground.height * scale;
const x = (mainCanvas.width - scaledWidth) / 2;
const y = (mainCanvas.height - scaledHeight) / 2;
overlayContext.drawImage(
startScreenBackground,
x, y,
scaledWidth,
scaledHeight
);
}
// Add overlay
overlayContext.fillStyle = 'rgba(0, 0, 0, 0.7)';
overlayContext.fillRect(0, 0, mainCanvas.width, mainCanvas.height);
}
function switchPlayer() {
if (!currentPlayer) return;
if (currentPlayer.switchesLeft == 0) {
console.error("No switch left")
return
}
const oldSwitchesLeft = currentPlayer.switchesLeft;
const pos = currentPlayer.pos;
const damageTaken = currentPlayer.maxHealth - currentPlayer.health;
// Destroy the current player
currentPlayer.destroy();
// Switch to the next character (from the unlocked list)
currentCharacterIndex = (currentCharacterIndex + 1) % unlockedCharacters.length;
const CharacterClass = unlockedCharacters[currentCharacterIndex];
// Slightly raise the new character's position for a "switching in air" effect
const airborneOffset = vec2(0, 0.5); // Adjust the Y offset as needed
const newPos = pos.add(airborneOffset);
currentPlayer = new CharacterClass(newPos);
currentPlayer.switchesLeft = oldSwitchesLeft - 1
console.log("Switches left: ", currentPlayer.switchesLeft)
// Carry over health and activate the new player
const newHealth = currentPlayer.maxHealth - damageTaken;
if (newHealth <= 0) {
currentPlayer.health = 30;
} else {
currentPlayer.health = newHealth;
}
currentPlayer.isActive = true;
console.log('Transformed to:', CharacterClass.name);
}
function gameUpdate() {
// Önce customCursor'un var olup olmadığını kontrol et
if (customCursor) {
if (gameState === 'playing') {
document.body.style.cursor = 'none';
mainCanvas.style.cursor = 'none';
customCursor.style.display = 'none';
} else {
document.body.style.cursor = 'none'; // Her zaman varsayılan cursor'u gizle
mainCanvas.style.cursor = 'none';
customCursor.style.display = 'block';
}
}
transition.update();
if (gameState === 'start') {
// Menu navigation
if (keyWasPressed('ArrowUp')) {
menuSelection = (menuSelection - 1 + 3) % 3;
}
if (keyWasPressed('ArrowDown')) {
menuSelection = (menuSelection + 1) % 3;
}
if (keyWasPressed('Enter') || keyWasPressed('Space')) {
switch(menuSelection) {
case 0: // Start Game
startGame();
break;
case 1: // Credits
gameState = 'credits';
break;
case 2: // Credits
gameState = 'help';
break;
}
}
} else if (gameState === 'credits') {
if (keyWasPressed('Escape')) {
gameState = 'start';
}
} else if (gameState === 'help') {
if (keyWasPressed('Escape')) {
gameState = 'start';
}
} else if (gameState === 'playing') {
updateGameLogic();
} else if (gameState === 'completed') {
// Completed ekranından ana menüye dönüş
if (keyWasPressed('Space')) {
resetGame();
}
}
}
// Add new function to handle game logic updates
function updateGameLogic() {
// Move all game logic here
if (keyWasPressed('Digit7')) {
switchPlayer();
}
if (currentPlayer) {
cameraPos = cameraPos.lerp(currentPlayer.pos, .1);
}
}
function gameRender() {
switch(gameState) {
case 'start':
drawStartScreen();
break;
case 'credits':
drawCredits();
break;
case 'help':
drawHelp();
break;
case 'completed':
drawGameComplete();
break;
case 'playing':
case 'paused':
background.render();
// Draw current level platforms
if (levelManager && levelManager.levels[levelManager.currentLevelIndex]) {
const currentLevel = levelManager.levels[levelManager.currentLevelIndex];
currentLevel.platforms.forEach(p => {
drawRect(
vec2(p.pos.x + p.size.x/2, p.pos.y + 0.5),
vec2(p.size.x, 1),
p.color || new Color(.5, .3, .2)
);
});
}
// Draw game UI
gameUI.render();
if (gameState === 'paused') {
drawPauseMenu();
}
break;
}
// Always render transition last
transition.render();
}
function gameUpdatePost() {
// Handle pause state changes
if (gameState === 'playing' && keyWasPressed('Escape')) {
gameState = 'paused';
pauseMenuSelection = 0;
} else if (gameState === 'paused') {
// Pause menu controls
if (keyWasPressed('ArrowUp')) {
pauseMenuSelection = (pauseMenuSelection - 1 + 3) % 3;
}
if (keyWasPressed('ArrowDown')) {
pauseMenuSelection = (pauseMenuSelection + 1) % 3;
}
if (keyWasPressed('Enter') || keyWasPressed('Space')) {
switch(pauseMenuSelection) {
case 0: // Resume
gameState = 'playing';
break;
case 1: // Main Menu
resetGame();
break;
case 2: // Main Menu
gameState = 'help';
break;
}
}
// Quick resume with ESC
if (keyWasPressed('Escape')) {
gameState = 'playing';
}
}
// Use the engine's pause system
setPaused(gameState === 'paused');
if (gameState === 'paused') {
engineObjects.forEach(object => {
if (object !== background) {
// Stop all movement
object.velocity = vec2(0, 0);
object.acceleration = vec2(0, 0);
object.gravity = 0;
}
});
}
}
// Add this new function to handle game reset
function resetGame() {
// Destroy all game objects
engineObjects.forEach(o => {
if (o !== background) {
o.destroy();
}
});
// Reset game state
gameState = 'start';
menuSelection = 0;
currentPlayer = null;
currentLevel = 0;
activePlayerIndex = 0;
// Reset camera
cameraPos = vec2(0, 0);
// Clear tile collision data
initTileCollision(worldSize);
// Reset background to the first level
background = new ParallaxBackground(mainCanvas, {
get x() { return cameraPos.x },
get y() { return cameraPos.y }
}, 0); // Use 0 for the first level's background
}
function gameRenderPost() {}
function drawPauseMenu() {
// Add dark overlay to show game is paused
overlayContext.fillStyle = 'rgba(0, 0, 0, 0.7)';
overlayContext.fillRect(0, 0, mainCanvas.width, mainCanvas.height);
// Draw pause menu text
overlayContext.textAlign = 'center';
overlayContext.textBaseline = 'middle';
// Pause title
overlayContext.font = '36px Arial';
overlayContext.fillStyle = '#fff';
overlayContext.fillText('PAUSED', mainCanvas.width/2, mainCanvas.height/3);
// Menu items
overlayContext.font = '24px Arial';
const pauseItems = ['Resume', 'Main Menu', 'Help'];
const menuY = mainCanvas.height/2;
const menuSpacing = 40;
pauseItems.forEach((item, index) => {
if (index === pauseMenuSelection) {
overlayContext.fillStyle = '#ffff00';
overlayContext.fillText('> ' + item + ' <', mainCanvas.width/2, menuY + index * menuSpacing);
} else {
overlayContext.fillStyle = '#fff';
overlayContext.fillText(item, mainCanvas.width/2, menuY + index * menuSpacing);
}
});
}
function drawCredits() {
drawMenuBackground();
// Set the context properties for centering the text
overlayContext.textAlign = 'center'; // Horizontally center the text
overlayContext.textBaseline = 'middle'; // Vertically center the text
// Draw the credits title
overlayContext.font = '36px Arial';
overlayContext.fillStyle = '#fff';
overlayContext.fillText('Credits', mainCanvas.width / 2, mainCanvas.height / 4);
// Draw the creators' names
overlayContext.font = '24px Arial';
overlayContext.fillStyle = '#fff';
overlayContext.fillText('Ömer Duran & Furkan Ünsalan', mainCanvas.width / 2, mainCanvas.height / 2);
// Add the return instruction with a glowing effect
overlayContext.font = '18px Arial';
overlayContext.fillStyle = 'rgba(255, 255, 255, 0.8)';
overlayContext.fillText('Press ESC to return', mainCanvas.width / 2, mainCanvas.height * 0.95);
// Optional: create some glow or subtle effect for the links
applyGlowEffect(mainCanvas.width / 2, mainCanvas.height * 0.65, 'github.com/omerdduran/PixelPaws');
applyGlowEffect(mainCanvas.width / 2, mainCanvas.height * 0.75, 'github.com/furkanunsalan');
applyGlowEffect(mainCanvas.width / 2, mainCanvas.height * 0.85, 'github.com/omerdduran');
}
// Helper function for glow effect on links
function applyGlowEffect(x, y, text) {
overlayContext.shadowColor = '#ffffff';
overlayContext.shadowBlur = 10;
overlayContext.fillText(text, x, y);
overlayContext.shadowColor = 'transparent'; // Reset shadow after drawing
}
function drawHelp() {
drawMenuBackground();
// Set the base context properties
overlayContext.textAlign = 'center';
overlayContext.textBaseline = 'middle';
overlayContext.fillStyle = '#fff';
// Draw the Help Screen title with a larger font and shadow
overlayContext.font = '48px Arial';
overlayContext.shadowColor = '#000';
overlayContext.shadowBlur = 4;
overlayContext.fillStyle = '#FFD700'; // Golden color for the title
overlayContext.fillText('Help Screen', mainCanvas.width / 2, mainCanvas.height / 10);
// Reset shadow for the rest of the text
overlayContext.shadowBlur = 0;
overlayContext.fillStyle = '#fff';
// Layout positions
const column1X = mainCanvas.width / 4; // Controls and power-ups column
const column2X = (mainCanvas.width * 3) / 4; // Character abilities column
const startY = mainCanvas.height / 5;
const lineHeight = 30;
const sectionSpacing = 50; // Extra space between sections
// Define the content
const controls = [
'← → - Move',
'W - Jump',
'Q - Special Ability',
'SPACE - Attack',
'7 - Switch Character',
];
const abilities = {
bear: 'Berserker',
bunny: 'Double Jump',
cat: 'Climb Walls',
dog: 'Bark and Freeze Enemies',
parrot: 'Limited Time Flying',
turtle: 'Shield',
};
const powerUps = [
'Double Damage - Increases attack power',
'Health Pack - Restores 25 Health',
'Invincibility - Temporary Immunity',
'Jump Boost - Enhanced Jump Power',
'Magnetic Field - Attracts Nearby Coins',
'Speed Boost - Double Movement Speed',
];
const allAbilities = Object.entries(abilities).map(
([character, ability]) =>
`${character.charAt(0).toUpperCase() + character.slice(1)}: ${ability}`
);
// Draw Controls Section
overlayContext.textAlign = 'center';
overlayContext.font = '30px Arial';
overlayContext.fillStyle = '#FF4500'; // Orange for headings
overlayContext.fillText('Controls', column1X, startY);
overlayContext.font = '22px Arial';
overlayContext.fillStyle = '#fff';
controls.forEach((control, index) => {
overlayContext.fillText(control, column1X, startY + lineHeight * (index + 1));
});
// Draw Power-Ups Section
const powerUpsStartY = startY + lineHeight * (controls.length + 2) + sectionSpacing;
overlayContext.font = '30px Arial';
overlayContext.fillStyle = '#FF4500';
overlayContext.fillText('Power-Ups', column1X, powerUpsStartY);
overlayContext.font = '22px Arial';
overlayContext.fillStyle = '#fff';
powerUps.forEach((powerUp, index) => {
overlayContext.fillText(powerUp, column1X, powerUpsStartY + lineHeight * (index + 1));
});
// Draw Character Abilities Section
overlayContext.textAlign = 'center';
const abilitiesStartY = startY;
overlayContext.font = '30px Arial';
overlayContext.fillStyle = '#FF4500';
overlayContext.fillText('Character Abilities', column2X, abilitiesStartY);
overlayContext.font = '22px Arial';
overlayContext.fillStyle = '#fff';
allAbilities.forEach((ability, index) => {
overlayContext.fillText(ability, column2X, abilitiesStartY + lineHeight * (index + 1));
});
// Draw Footer Instructions
overlayContext.textAlign = 'center';
overlayContext.font = '24px Arial';
overlayContext.fillStyle = '#ADFF2F'; // Light green for footer text
overlayContext.fillText('Press ESC to return', mainCanvas.width / 2, mainCanvas.height - 50);
}
// Oyun tamamlama ekranı için yeni fonksiyon
function drawGameComplete() {
// Arka plan ve overlay
drawMenuBackground();
// Başlık
overlayContext.textAlign = 'center';
overlayContext.textBaseline = 'middle';
overlayContext.fillStyle = '#fff';
overlayContext.font = '48px Arial';
overlayContext.fillText('Congratulations!', mainCanvas.width/2, mainCanvas.height/3);
// Alt başlık
overlayContext.font = '24px Arial';
overlayContext.fillText('You have completed all levels!', mainCanvas.width/2, mainCanvas.height/2);
// Yönlendirme metni
overlayContext.font = '20px Arial';
overlayContext.fillText('Press SPACE to return to main menu', mainCanvas.width/2, mainCanvas.height * 0.7);
}
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);