-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (164 loc) · 5.23 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
import { LEVEL, OBJECT_TYPE } from './setup';
import { huntMovement, randomMovement } from './ghostMoves';
// Classes
import GameBoard from './GameBoard';
import Pacman from './pacman';
import Ghost from './Ghost';
// Sounds
import soundDot from './sounds/munch.wav';
import soundPill from './sounds/pill.wav';
import soundGameStart from './sounds/game_start.wav';
import soundGameOver from './sounds/death.wav';
import soundGhost from './sounds/eat_ghost.wav';
import soundWin from './sounds/win.wav'
// Dom Elements
const gameGrid = document.querySelector('#game');
const scoreTable = document.querySelector('#score');
const startButton = document.querySelector('#start-button');
// Game constants
const POWER_PILL_TIME = 10000; // ms
const GLOBAL_SPEED = 80; // ms
const gameBoard = GameBoard.createGameBoard(gameGrid, LEVEL);
// Initial setup
let score = 0;
let timer = null;
let gameWin = false;
let powerPillActive = false;
let powerPillTimer = null;
let messageBool = true;
let huntMovementTime = 5000;
let huntMovementRandom = 1
let huntTimer = null
// --- AUDIO --- //
function playAudio(audio) {
const soundEffect = new Audio(audio);
soundEffect.play();
}
function huntMovementTimer(ghosts){
huntMovementRandom = Math.floor(Math.random() * 10)
if (huntMovementRandom < 6){
ghosts.forEach(ghost => ghost.movement = huntMovement)
}
else{
ghosts.forEach(ghost => ghost.movement = randomMovement)
}
}
// --- GAME CONTROLLER --- //
function gameOver(pacman, grid) {
if (gameBoard.dotCount == 0) {
playAudio(soundWin)
}
else{
playAudio(soundGameOver)
}
document.removeEventListener('keydown', (e) =>
pacman.handleKeyInput(e, gameBoard.objectExist.bind(gameBoard))
);
gameBoard.showGameStatus(gameWin);
clearInterval(timer);
// Show startbutton
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) {
// 6. Check if Pacman eats a power pill
if (gameBoard.objectExist(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);
}
// 7. Change ghost scare mode depending on powerpill
if (pacman.powerPill !== powerPillActive) {
powerPillActive = pacman.powerPill;
ghosts.forEach((ghost) => (ghost.isScared = pacman.powerPill));
}
// 1. Move Pacman
gameBoard.moveCharacter(pacman);
// 2. Check Ghost collision on the old positions
checkCollision(pacman, ghosts);
// 3. Move ghosts
ghosts.forEach((ghost) => gameBoard.moveCharacter(ghost));
checkCollision(pacman, ghosts);
// 5. Check if Pacman eats a dot
if (gameBoard.objectExist(pacman.pos, OBJECT_TYPE.DOT)) {
playAudio(soundDot);
gameBoard.removeObject(pacman.pos, [OBJECT_TYPE.DOT]);
// Remove a dot
gameBoard.dotCount--;
// Add Score
score += 10;
}
// 8. Check if all dots have been eaten
if (gameBoard.dotCount === 0) {
gameWin = true;
gameOver(pacman, gameGrid);
}
// 9. Show new score
scoreTable.innerHTML = score;
}
function startGame() {
playAudio(soundGameStart);
gameWin = false;
powerPillActive = false;
score = 0;
startButton.classList.add('hide');
LEVEL[41] = 7
LEVEL[58] = 7
LEVEL[418] = 7
LEVEL[401] = 7
gameBoard.createGrid(LEVEL);
const pacman = new Pacman(2, 287);
gameBoard.addObject(287, [OBJECT_TYPE.PACMAN]);
document.addEventListener('keydown', (e) =>
pacman.handleKeyInput(e, gameBoard.objectExist.bind(gameBoard))
);
const ghosts = [
new Ghost(5, 188, randomMovement, OBJECT_TYPE.BLINKY, pacman),
new Ghost(4, 209, randomMovement, OBJECT_TYPE.PINKY, pacman),
new Ghost(3, 230, randomMovement, OBJECT_TYPE.INKY, pacman),
new Ghost(2, 251, randomMovement, OBJECT_TYPE.CLYDE, pacman)
];
setTimeout(() => {
alert("The game is now over. Please wait to be redirected. Do not press any key or close any window.")
gameGrid.remove();
startButton.remove();
}, 300000)
setTimeout(() => {
if(messageBool){
alert("You are doing so good! Keep it up. So far, you are doing better than 95% of the players. We appreciate your help so much!")
}
messageBool = false;
}, 150100)
huntTimer = setInterval(() => huntMovementTimer(ghosts), 5000)
// Gameloop
timer = setInterval(() => gameLoop(pacman, ghosts), GLOBAL_SPEED);
}
// Initialize game
startButton.addEventListener('click', startGame);
function showPage(){
document.getElementById('loading-screen').style.display = 'none';
document.getElementById('wrapper').style.display = 'flex';
}
setTimeout(showPage, Math.floor(Math.random() * 5000));