-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
259 lines (235 loc) · 8.28 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
var Level = function(levelInfo) {
levelInfo.generateLevel();
var plan = levelInfo.level;
this.levelInfo = levelInfo;
this.width = plan[0].length;
this.height = plan.length;
this.grid = [];
this.actors = [];
this.islandGrid = [];
var ch;
Game.currentLevel = this;
for (y = 0; y < this.height; y++) {
var line = [];
for (x = 0; x < this.width; x++) {
line.push(null);
}
}
for (y = 0; y < this.height; y++) {
var line = plan[y];
var gridLine = [];
var islandLine = [];
for (x = 0; x < this.width; x++) {
var ch = line[x]; // Get the char.
var fieldType = null;
var islandFieldType = null;
var Actor = levelInfo.actorChars[ch]; // Is it an dynamic item. Get the constructor for same.
if (Actor) {
this.actors.push(new Actor(new Vector(x, y), ch)); // Instantate the actor. Needs the vector,ch
if (levelInfo.islandChars && levelInfo.islandChars[ch]) {
islandFieldType = levelInfo.actorChars[ch];
}
} else {
fieldType = levelInfo.backgroundChars[ch];
}
gridLine.push(fieldType);
islandLine.push(islandFieldType);
}
this.grid.push(gridLine); // Push the field type.
this.islandGrid.push(islandLine);
}
this.player = this.actors.filter(function(actor) { // Get the player instance separately.
return actor.type == "player";
})[0];
this.status = this.finishDelay = null;
};
// Returns true if game is finished.
Level.prototype.isFinished = function() {
return ((this.status != null) && (this.finishDelay < 0));
};
// Check if there is any object in the static layer at the given bounding box of pos and size.
Level.prototype.collisionWith = function(pos, size, type) {
var xStart = Math.floor(pos.x);
var xEnd = Math.ceil(pos.x + size.x);
var yStart = Math.floor(pos.y);
var yEnd = Math.ceil(pos.y + size.y);
var grid;
if (type == "obstacle") {
// Check if beyond boundaries
if (xStart < 0 || xEnd > this.width || yStart < 0) return "wall";
else if (yEnd > this.height) return "lava";
grid = this.grid;
} else if (type == "island") grid = this.islandGrid;
// Is there any obstacle overlapping the players bounding box.
for (var y = yStart; y < yEnd; y++) {
for (var x = xStart; x < xEnd; x++) {
var fieldType = grid[y][x];
if (fieldType) {
return fieldType
}
}
}
}
// Collision with any other dynamic object.
Level.prototype.actorAt = function(actor) {
var other;
for (var i = 0; i < this.actors.length; i++) {
other = this.actors[i];
// Check if the current actor falls within the bounding box of the other.
if (other != actor && actor.pos.x + actor.size.x > other.pos.x &&
actor.pos.x < other.pos.x + other.size.x &&
actor.pos.y + actor.size.y > other.pos.y &&
actor.pos.y < other.pos.y + other.size.y && !other.collisionNotRequired
) {
return other;
}
}
}
// Get the minimal step size. Call the actor.act for each of the dynamic objects.
Level.prototype.animate = function(step, keys) {
var maxStep = 0.05;
if (this.status != null) { // Keep reducing the finishDelay
this.finishDelay = this.finishDelay - step;
}
while (step > 0) {
var thisStep = Math.min(step, maxStep);
this.actors.forEach(function(actor) {
actor.act(thisStep, this, keys);
}, this);
step -= thisStep; // Reduce the step by step size taken.
}
};
Level.prototype.playerTouched = function(type, actor) {
var levelStatus = this.levelInfo.playerTouched(type, actor, this);
if (levelStatus == "lost" || levelStatus == "won") {
this.status = levelStatus;
this.finishDelay = 2;
}
};
Level.prototype.getPlayerProgress = function() {
if (this.levelInfo.platformerType == "horizontal")
return ~~(this.player.pos.x);
else if (this.levelInfo.platformerType == "vertical")
return ~~(this.height - this.player.pos.y);
}
var keyCodes = {
37: "left",
38: "up",
39: "right",
40: "down",
70: "fly",
71: "revokeFly",
80: "pause",
27: "skip",
13: "enter"
};
// Create the key handler func & register for the keydown and keyup.
var trackKeys = function(codes) {
var pressed = {};
var keyReleased = {};
function handler(event) {
if (codes.hasOwnProperty(event.keyCode)) {
var down = event.type == "keydown";
var up = event.type == "keyup";
pressed[codes[event.keyCode]] = down;
keyReleased[codes[event.keyCode]] = up;
event.preventDefault();
}
}
addEventListener("keydown", handler);
addEventListener("keyup", handler);
return { "down": pressed, "up": keyReleased };
}
var pauseIsDown = false;
// Toggle the gamePaused variable whenever p key is pressed and released.
function pauseKeyHandler() {
if (keys.pause) {
pauseIsDown = true;
}
if (pauseIsDown && !keys.pause) {
Game.gamePaused = !Game.gamePaused;
pauseIsDown = false;
Game.hud.clearScreen();
}
}
var runAnimation = function(frameFunc) { // frameFunc anonymous func.
var lastTime = null;
function frame(time) {
pauseKeyHandler();
var stop = false;
if (lastTime != null) {
var timeStep = Math.min(time - lastTime, 100) / 1000;
if (!Game.gamePaused) {
stop = frameFunc(timeStep) == false;
} else {
Game.inInteraction = false;
Game.hud.drawMenuScreen();
}
if (Game.GameFirstStart || Game.GameOver) {
Game.gamePaused = true;
Game.GameFirstStart = false;
}
}
lastTime = time;
if (!stop) {
requestAnimationFrame(frame); // Calls frame with the time in millisecs
}
}
requestAnimationFrame(frame);
};
var keyUpDown = trackKeys(keyCodes);
var keys = keyUpDown["down"];
var keyPressed = keyUpDown["up"];
var resetKeyPressed = function() {
keyPressed.up = false;
keyPressed.down = false;
keyPressed.enter = false;
}
// Called from runGame.
// Calls runAnimation function with an anonymous func as parameter
function runLevel(level, Display, andThen) {
var display = new Display(document.body, level); // Clear display for each level.
var hud = new InGameHUD(document.body);
hud.setHealthBar(level.player, "getHealth", 100);
if (level.levelInfo.type == LEVEL_TYPE.PLATFORMER) {
if (level.levelInfo.platformerType == "horizontal")
hud.setPlayerProgress(level, "getPlayerProgress", level.width);
else if (level.levelInfo.platformerType == "vertical")
hud.setPlayerProgress(level, "getPlayerProgress", level.height);
}
Game.hud = hud;
runAnimation(function(step) {
level.animate(step, keys);
display.drawFrame(step);
hud.draw();
if (level.isFinished()) {
display.clear();
hud.clear();
if (andThen) {
andThen(level.status);
}
return false;
}
});
}
// runGame starting point of game.
// Starts with level 0.
// Recursive
// if lost re-call same level else recursively call next level till final level
function runGame(startingLevel) {
function startLevel(levelinfo) {
Game.inInteraction = false;
Game.nextLevel = null;
runLevel(new Level(levelinfo), startingLevel.display, function(status) {
if (status == "lost")
startLevel(levelinfo);
else if (Game.nextLevel)
startLevel(Game.nextLevel);
else {
console.log("You win!");
startLevel(startingLevel);
}
});
}
startLevel(startingLevel);
}