-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
257 lines (226 loc) · 9.08 KB
/
main.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
/*global $, Crafty*/
var levelBitMap = [];
(function () {
"use strict";
//define a sprite size
var spriteSize = 20,
speed = 1.6;
function initializeGame(width, height) {
//initialize our game. We give it width, height, and the html element to pput the game in
Crafty.init(width * spriteSize, height * spriteSize, document.getElementById('game'));
//set the background color to black
Crafty.background("#000000");
//load our sprites
Crafty.sprite(spriteSize, "imgs/pacman20.png", {
pacman: [12, 0],
ghost: [0, 0],
pellet: [14, 1],
powerUp: [15, 0],
blinky: [0, 0],
inky: [6, 0],
pinky: [4, 0],
clyde: [2, 0],
lives: [16, 0],
cherry: [16, 0],
strawberry: [16, 1],
orange: [16, 2],
banana: [16,3]
});
//load walls
Crafty.sprite(spriteSize, "imgs/walls+gate.png", {
wall: [14, 0], //self contained wall
gate: [15, 0], //gate
blCr: [12, 0], //bottom left corner
regT: [4, 0], //a T intersection
brCr: [13, 0], //bottom right corner
lftT: [6, 0], //90 degree clockwise rotation
ritT: [7, 0], //270 degree clockwise rotation
tlCr: [10, 0], //top left corner
invT: [5, 0], //180 degree clockwise rotation
trCr: [11, 0], // top right corner
bCap: [3, 0], //bottom cap
tCap: [2, 0], //top cap
horz: [9, 0], //horizontal
late: [8, 0], //lateral
lCap: [1, 0], //left cap
rCap: [0, 0] //right cap
});
//load audio files
Crafty.audio.add({
munch: ['sounds/munch.wav'],
life: ['sounds/life.wav'],
begin: ['sounds/begin.wav'],
fruit: ['sounds/fruit.wav'],
death: ['sounds/death.wav'],
ghost: ['sounds/ghost.wav'],
extralife: ['sounds/extralife.wav'],
siren: ['sounds/siren.wav'],
laser: ['sounds/shortLaser.mp3']
});
//play the intro tune when game starts
Crafty.audio.play('begin');
// setTimeout(function(){Crafty.unpause();}, 1000);
}
function makeFruit() {
setTimeout(function () {
Crafty.e("Fruit").create(9 * spriteSize, 12 * spriteSize);
makeFruit();
}, 20000);
}
function loadMap(levelMap, currentScore, currentLives, level) {
console.log("called");
//split file into lines
var lines = levelMap.split("\n");
//call initializeGame
initializeGame(lines[0].length, lines.length);
//loop over each line
$.each(lines, function (y, line) {
var characters = line.split(""),
bitmap = $.map(characters, function (char) {
if (['W', '1', '2', '3', '4', '6', '7', '8', '9', 'v', '^', '<', '>', '|', '-'].indexOf(char) > -1) {
return 1;
} else if (char === 'G') {
return 2;
}
return 0;
});
levelBitMap.push(bitmap);
//split each line into characters and loop over each character
$.each(characters, function (x, char) {
//set the x and y coordinates for the current item
var xCoord = x * spriteSize,
yCoord = y * spriteSize;
//match and create the appropriate entity
if (char === 'W') {
Crafty.e("Wall").create(xCoord, yCoord, "wall");
} else if (char === 'Z') {
Crafty.e("Score");
Crafty("Score").setPoints(currentScore);
} else if (char === 'X') {
Crafty.e("Lives");
Crafty("Lives").setLives(currentLives);
} else if (char === 'V') {
Crafty.e("Levels");
Crafty("Levels").setLevel(level);
} else if (char === 'p') {
Crafty.e("Pellet").create(xCoord, yCoord);
} else if (char === 'S') {
Crafty.e("PowerUp").create(xCoord, yCoord);
} else if (char === 'M') {
Crafty.e("Pacman").create(xCoord, yCoord);
} else if (char === 'B') {
Crafty.e("Ghost").create(xCoord, yCoord, speed).setAI("Blinky");
} else if (char === 'I') {
Crafty.e("Ghost").create(xCoord, yCoord, speed).setAI("Inky");
} else if (char === 'P') {
Crafty.e("Ghost").create(xCoord, yCoord, speed).setAI("Pinky");
} else if (char === 'C') {
Crafty.e("Ghost").create(xCoord, yCoord, speed).setAI("Clyde");
} else if (char === 'G') { //gate
Crafty.e("Gate").create(xCoord, yCoord, "gate");
} else if (char === '1') { //bottom left corner
Crafty.e("Wall").create(xCoord, yCoord, "blCr");
} else if (char === '2') { //T
Crafty.e("Wall").create(xCoord, yCoord, "regT");
} else if (char === '3') { //bottom right corner
Crafty.e("Wall").create(xCoord, yCoord, "brCr");
} else if (char === '4') { //left T
Crafty.e("Wall").create(xCoord, yCoord, "lftT");
} else if (char === '6') { //right T
Crafty.e("Wall").create(xCoord, yCoord, "ritT");
} else if (char === '7') { //top left corner
Crafty.e("Wall").create(xCoord, yCoord, "tlCr");
} else if (char === '8') { //inverted T
Crafty.e("Wall").create(xCoord, yCoord, "invT");
} else if (char === '9') { //top right corner
Crafty.e("Wall").create(xCoord, yCoord, "trCr");
} else if (char === 'v') { //bottom cap
Crafty.e("Wall").create(xCoord, yCoord, "bCap");
} else if (char === '^') { //top cap
Crafty.e("Wall").create(xCoord, yCoord, "tCap");
} else if (char === '-') { //horizontal
Crafty.e("Wall").create(xCoord, yCoord, "horz");
} else if (char === '|') { //lateral
Crafty.e("Wall").create(xCoord, yCoord, "late");
} else if (char === '<') { //left cap
Crafty.e("Wall").create(xCoord, yCoord, "lCap");
} else if (char === '>') { //right cap
Crafty.e("Wall").create(xCoord, yCoord, "rCap");
}
});
});
makeFruit();
}
Crafty.defineScene("startScreen", function () {
Crafty.init('385', '440', document.getElementById('game'));
Crafty.background('#000000 url(imgs/startscreen.png) no-repeat center center');
Crafty.e("2D, DOM, Text, Mouse")
.attr({
w: 300,
h: 50,
x: 40,
y: 325
})
.text("START GAME")
.css({
"text-align": "center",
'cursor': 'pointer'
})
.textFont({
size: '30px',
weight: 'bold'
})
.textColor("#FFFFFF")
.bind('Click', function () {
console.log("over");
Crafty.enterScene("game", "maps/level+.map");
});
});
//define our game scene
Crafty.defineScene("game", function (mapFile) {
//get the level map file
$.get(mapFile, function (levelMap) {
//load our level map
loadMap(levelMap, 0, 3, 1);
});
});
//define our new level scene
Crafty.defineScene("level", function (levelObject) {
//get the level map file
$.get(levelObject.mapFile, function (levelMap) {
//load our level map
loadMap(levelMap, levelObject.currentScore, levelObject.currentLives, levelObject.level);
});
});
// Start the start screen
Crafty.scene("startScreen");
}());
function postScore(name, score) {
$.get(
"http://104.236.6.144/new_score/" + name + "/" + score,
function (highScores) {
updateHighScores(highScores.scores.splice(0, 10))
}
);
}
function getScores() {
$.get(
"http://104.236.6.144/highscores/" + 10,
function (highScores) {
updateHighScores(highScores.scores)
}
);
}
function updateHighScores(highScores) {
var html = "<tr>l<th>#</th><th>Name</th><th>Score</th></tr>";
for (var i = 0; i < highScores.length; i++) {
html += "<tr>" +
"<td>" + (i+1) + "</td>" +
"<td>" + highScores[i].name + "</td>" +
"<td>" + highScores[i].score + "</td>" +
"</tr>";
}
$( "#scoreContainer" ).html(html);
console.log(highScores);
}
getScores();