-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
307 lines (257 loc) · 7.43 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
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
var canvas = document.getElementById("gameCanvas");
var context = canvas.getContext("2d");
var startFrameMillis = Date.now();
var endFrameMillis = Date.now();
// This function will return the time in seconds since the function
// was last called
// You should only call this function once per frame
function getDeltaTime()
{
endFrameMillis = startFrameMillis;
startFrameMillis = Date.now();
// Find the delta time (dt) - the change in time since the last drawFrame
// We need to modify the delta time to something we can use.
// We want 1 to represent 1 second, so if the delta is in milliseconds
// we divide it by 1000 (or multiply by 0.001). This will make our
// animations appear at the right speed, though we may need to use
// some large values to get objects movement and rotation correct
var deltaTime = (startFrameMillis - endFrameMillis) * 0.001;
// validate that the delta is within range
if(deltaTime > 1)
deltaTime = 1;
return deltaTime;
}
//-------------------- Don't modify anything above here
var SCREEN_WIDTH = canvas.width;
var SCREEN_HEIGHT = canvas.height;
// some variables to calculate the Frames Per Second (FPS - this tells use
// how fast our game is running, and allows us to make the game run at a
// constant speed)
var fps = 0;
var fpsCount = 0;
var fpsTime = 0;
var LAYER_COUNT = 2;
var LAYER_PLATFORMS = 0;
var LAYER_LADDERS = 1;
var MAP = { tw: 60, th: 15 };
var TILE = 35;
// images are twice as big as our map's grid, so we have to
// multiply to get a useable size
var TILESET_TILE = TILE * 2;
var TILESET_PADDING = 2;
var TILESET_SPACING = 2;
var TILESET_COUNT_X = 14;
var TILESET_COUNT_Y = 14;
var tileset = document.createElement("img");
tileset.src = "tileset.png";
function cellAtPixelCoord(layer, x,y)
{
if(x<0 || x>SCREEN_WIDTH || y<0)
return 1;
// let the player drop of the bottom of the screen (this means death)
if(y>SCREEN_HEIGHT)
return 0;
return cellAtTileCoord(layer, p2t(x), p2t(y));
};
function cellAtTileCoord(layer, tx, ty)
{
if(tx<0 || tx>=MAP.tw || ty<0)
return 1;
// let the player drop of the bottom of the screen (this means death)
if(ty>=MAP.th)
return 0;
return cells[layer][ty][tx];
};
function tileToPixel(tile)
{
return tile * TILE;
};
function pixelToTile(pixel)
{
return Math.floor(pixel/TILE);
};
function bound(value, min, max)
{
if(value < min)
return min;
if(value > max)
return max;
return value;
}
var worldOffsetX = 0;
function drawMap()
{
var startX = -1;
var maxTiles = Math.floor(SCREEN_WIDTH / TILE) + 2;
var tileX = pixelToTile(player.position.x);
var offsetX = TILE + Math.floor(player.position.x % TILE);
startX = tileX - Math.floor(maxTiles / 2);
if (startX < -1)
{
startX = 0;
offsetX = 0;
}
if (startX > MAP.tw - maxTiles)
{
startX = MAP.tw - maxTiles + 1;
offsetX = TILE;
}
worldOffsetX = startX * TILE + offsetX;
for(var layerIdx=0; layerIdx<LAYER_COUNT; layerIdx++)
{
for( var y = 0; y < level1.layers[layerIdx].height; y++ )
{
var idx = y * level1.layers[layerIdx].width + startX;
for( var x = startX; x < level1.layers[layerIdx].width; x++ )
{
if( level1.layers[layerIdx].data[idx] != 0 )
{
// the tiles in the Tiled map are base 1 (meaning a value of 0 means no tile), so subtract one from the tileset id to get the
// correct tile
var tileIndex = level1.layers[layerIdx].data[idx] - 1;
var sx = TILESET_PADDING + (tileIndex % TILESET_COUNT_X) * (TILESET_TILE + TILESET_SPACING);
var sy = TILESET_PADDING + (Math.floor(tileIndex / TILESET_COUNT_X)) * (TILESET_TILE + TILESET_SPACING);
context.drawImage(tileset, sx, sy, TILESET_TILE, TILESET_TILE, (x-startX)*TILE - offsetX, (y-1)*TILE, TILESET_TILE, TILESET_TILE);
}
idx++;
}
}
}
}
var musicBackground;
var sfxFire;
var cells = []; // the array that holds our simplified collision data
function initialize() {
for(var layerIdx = 0; layerIdx < LAYER_COUNT; layerIdx++) { // initialize the collision map
cells[layerIdx] = [];
var idx = 0;
for(var y = 0; y < level1.layers[layerIdx].height; y++) {
cells[layerIdx][y] = [];
for(var x = 0; x < level1.layers[layerIdx].width; x++) {
if(level1.layers[layerIdx].data[idx] != 0) {
// for each tile we find in the layer data, we need to create 4 collisions
// (because our collision squares are 35x35 but the tile in the
// level are 70x70)
cells[layerIdx][y][x] = 1;
cells[layerIdx][y-1][x] = 1;
cells[layerIdx][y-1][x+1] = 1;
cells[layerIdx][y][x+1] = 1;
}
else if(cells[layerIdx][y][x] != 1) {
// if we haven't set this cell's value, then set it to 0 now
cells[layerIdx][y][x] = 0;
}
idx++;
}
}
}
musicBackground = new Howl(
{
urls: ["background.ogg"],
loop: true,
buffer: true,
volume: 0.5
});
musicBackground.play();
sfxFire = new Howl(
{
urls: ["fireEffect.ogg"],
buffer: true,
volume: 1
});
}
var player = new Player();
var keyboard = new Keyboard();
// abitrary choice for 1m
var METER = TILE;
// very exaggerated gravity (6x)
var GRAVITY = METER * 9.8 * 6;
// max horizontal speed (10 tiles per second)
var MAXDX = METER * 10;
// max vertical speed (15 tiles per second)
var MAXDY = METER * 15;
// horizontal acceleration - take 1/2 second to reach maxdx
var ACCEL = MAXDX * 2;
// horizontal friction - take 1/6 second to stop from maxdx
var FRICTION = MAXDX * 6;
// (a large) instantaneous jump impulse
var JUMP = METER * 1500;
var heartImg = document.createElement("img");
heartImg.src = "heart.png";
var heartWidth = 20;
var heartHeight = 20;
var bIsPaused = false;
var bWasPDown = false;
function run()
{
context.fillStyle = "#ccc";
context.fillRect(0, 0, canvas.width, canvas.height);
var deltaTime = getDeltaTime();
if (keyboard.isKeyDown(keyboard.KEY_P) == true)
{
if (!bWasPDown)
{
console.log("Paused");
bIsPaused = !bIsPaused;
bWasPDown = true;
}
}
else
{
bWasPDown = false;
}
for (var i = 0; i < player.lives; ++i)
{
//context.drawImage(heartImg, (canvas.width - 100) + ((heartWidth + 2) * i), 10, heartWidth, heartHeight);
context.drawImage(heartImg, player.position.x - worldOffsetX - 5 + ((heartWidth + 2) * i), player.position.y - 80, heartWidth, heartHeight);
}
drawMap();
player.draw();
if (!bIsPaused)
{
player.update(deltaTime);
}
else
{
context.fillStyle = "white";
context.font = "32px Arial";
context.fillText("PAUSED", SCREEN_WIDTH * 0.5 - 55, SCREEN_HEIGHT * 0.5);
}
// update the frame counter
fpsTime += deltaTime;
fpsCount++;
if(fpsTime >= 1)
{
fpsTime -= 1;
fps = fpsCount;
fpsCount = 0;
}
// draw the FPS
context.fillStyle = "#f00";
context.font="14px Arial";
context.fillText("FPS: " + fps, 5, 20, 100);
}
initialize();
//-------------------- Don't modify anything below here
// This code will set up the framework so that the 'run' function is called 60 times per second.
// We have a some options to fall back on in case the browser doesn't support our preferred method.
(function() {
var onEachFrame;
if (window.requestAnimationFrame) {
onEachFrame = function(cb) {
var _cb = function() { cb(); window.requestAnimationFrame(_cb); }
_cb();
};
} else if (window.mozRequestAnimationFrame) {
onEachFrame = function(cb) {
var _cb = function() { cb(); window.mozRequestAnimationFrame(_cb); }
_cb();
};
} else {
onEachFrame = function(cb) {
setInterval(cb, 1000 / 60);
}
}
window.onEachFrame = onEachFrame;
})();
window.onEachFrame(run);