-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (117 loc) · 4.01 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
import {LEVEL, OBJECT_TYPE} from './setup.js';
import { randomMovement } from './GhostMoves.js';
import Gameboard from './Gameboard.js';
import Pacman from './Pacman.js';
import Ghost from './Ghost.js';
import soundDot from './static/sounds/munch.mp3';
import soundPill from './static/sounds/pill.mp3';
import soundGameStart from './static/sounds/game_start.mp3';
import soundGameOver from './static/sounds/death.mp3';
import soundGhost from './static/sounds/eat_ghost.mp3';
//DOM
const gameGrid = document.querySelector('#game');
const scoreTable = document.querySelector('#score');
const startButton = document.querySelector('#start-button');
//Constants
const POWER_PILL_TIME = 10000;
const GLOBAL_SPEED = 80;
const gameboard = Gameboard.createGameboard(gameGrid, LEVEL);
//Initialize
let score = 0;
let timer = null;
let gameWin = false;
let powerPillActive = false;
let powerPillTimer = null;
//AUDIO
function playAudio(audio) {
const soundEffect = new Audio(audio);
soundEffect.play();
}
//GAME CONTROLLER
function gameOver(pacman) {
playAudio(soundGameOver);
document.removeEventListener('keydown', (e) =>
pacman.handleKeyInput(e, gameboard.objectExists.bind(gameboard))
);
gameboard.showGameStatus(gameWin);
clearInterval(timer);
startButton.classList.remove('hide');
};
function checkCollision(pacman, ghosts) {
const collidedGhost = ghosts.find((ghost) => pacman.pos === ghost.pos);
if (collidedGhost) {
if (pacman.powerPill) {
playAudio(soundGhost);
gameboard.removeObject(collidedGhost.pos, [
OBJECT_TYPE.GHOST,
OBJECT_TYPE.SCARED,
collidedGhost.name
]);
collidedGhost.pos = collidedGhost.startPos;
score += 100;
} else {
gameboard.removeObject(pacman.pos, [OBJECT_TYPE.PACMAN]);
gameboard.rotateDiv(pacman.pos,0);
gameOver(pacman,gameGrid);
}
}
}
function gameLoop(pacman, ghosts) {
gameboard.moveCharacter(pacman);
checkCollision(pacman, ghosts);
ghosts.forEach((ghost) => gameboard.moveCharacter(ghost));
checkCollision(pacman,ghosts);
//check if pacman eats a dot
if (gameboard.objectExists(pacman.pos, OBJECT_TYPE.DOT)) {
playAudio(soundDot);
gameboard.removeObject(pacman.pos, [OBJECT_TYPE.DOT]);
gameboard.dotCount--;
score += 10;
}
//check if pacman eats a powerPill
if (gameboard.objectExists(pacman.pos, OBJECT_TYPE.PILL)) {
playAudio(soundPill);
gameboard.removeObject(pacman.pos, [OBJECT_TYPE.PILL]);
pacman.powerPill = true;
score += 50;
clearTimeout(powerPillTimer);
powerPillTimer = setTimeout(
() => (pacman.powerPill = false),
POWER_PILL_TIME
);
}
//ghost scare mode (depending on powerPill)
if (pacman.powerPill !== powerPillActive) {
powerPillActive = pacman.powerPill;
ghosts.forEach((ghost) => (ghost.isScared = pacman.powerPill));
}
//check if all dots are eaten
if(gameboard.dotCount === 0) {
gameWin = true;
gameOver(pacman, gameGrid);
}
//show new score
scoreTable.innerHTML = score;
}
function startGame() {
playAudio(soundGameStart);
gameWin =false;
powerPillActive = false;
score= 0;
startButton.classList.add('hide');
gameboard.createGrid(LEVEL);
const pacman = new Pacman(2, 287);
gameboard.addObject(287, [OBJECT_TYPE.PACMAN]);
document.addEventListener('keydown', (e) =>
pacman.handleKeyInput(e, gameboard.objectExists.bind(gameboard))
);
const ghosts = [
new Ghost(5, 188, randomMovement, OBJECT_TYPE.BLINKY),
new Ghost(4, 209, randomMovement, OBJECT_TYPE.PINKY),
new Ghost(3, 230, randomMovement, OBJECT_TYPE.INKY),
new Ghost(2, 251, randomMovement, OBJECT_TYPE.CLYDE)
];
timer = setInterval(() => gameLoop(pacman, ghosts), GLOBAL_SPEED);
}
//Start the game
startButton.addEventListener('click', startGame);