-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tilemap.js
317 lines (267 loc) · 10.2 KB
/
Tilemap.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
308
309
310
311
312
313
314
315
316
317
'use strict';
/* I wanted to make a more sophisticated tiling system so that I could separate
* visual from logical tiles. I also wanted to make use of my own tile sets for
* visuals - instead of manually picking what image goes into what tile, I
* wanted to make that an automatic process. Finally, I wanted to tag advantage
* of the original positioning of the tilemap to optimize collision search. */
class Tilemap {
/* Takes a map of tiles and a tilesize (the size of each tile in pixels).
* This only works with square tiles. */
constructor(map, tilesize) {
this.map = map;
this.tilesize = tilesize;
/* Easier to type. */
this.rows = map.length;
this.cols = map[0].length;
/* Easier to create a camera for each tilemap (tilemaps are really just
* levels or the representation of the game world)
*/
this.camera = new Camera(this.rows, this.cols, this.tilesize);
/* Initialize the visual map by collecting references to the
* original sprite sheet. Depending on neigboring tiles, pick a
* different image from the sprite sheet. */
this.visualMap = new Array(this.rows * this.cols);
this.visualMap.fill(0)
/* Raster order array of logical blocks */
this.logicalMap = new Array(this.rows * this.cols);
this.logicalMap.fill(null);
this.totalCollectables = 0;
this.enemies = [];
this.hiddenTriggers = [];
/* Reference to a player object */
this.player = undefined;
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
let actual = createVector(col * this.tilesize, row * this.tilesize);
let ridx = this.rasterIdx(row, col);
/* Parse the tiles */
switch (this.map[row][col]) {
/* Walls */
case 'w':
this.logicalMap[ridx] = new Entity(actual.x, actual.y, this.tilesize);
this.visualMap[ridx] = this.getTileNumber(row, col);
break;
/* Thorns */
case 't':
this.logicalMap[ridx] = new Thorns(actual.x, actual.y);
break;
/* Collectables */
case 'c':
this.logicalMap[ridx] = new Collectable(actual.x, actual.y);
this.totalCollectables += 1;
break;
/* Simple enemies */
case 's':
this.enemies.push(new SimpleEnemy(actual.x, actual.y, this.tilesize));
break;
/* Trigger for activating final boss */
case 'h':
this.logicalMap[ridx] = new HiddenTrigger(actual.x, actual.y);
this.hiddenTriggers.push(this.logicalMap[ridx]);
break;
/* Queen bee boss */
case 'B':
this.finalBoss = new Boss(actual.x, actual.y);
this.enemies.push(this.finalBoss);
break;
/* Player character */
case 'p':
this.player = new Player(actual.x, actual.y, this.tilesize);
this.camera.setPos(actual.x, actual.y);
break;
}
}
}
}
/* Return a list of objects from the logical grid where the rectangular
* object overlaps */
getSpanningLogicalTiles(x, y, w, h) {
let gridStart = this.getGridIdx(x, y);
let gridEnd = this.getGridIdx(x + w, y + h);
let filteredTiles = [];
for (let col = gridStart.col; col <= gridEnd.col; col++) {
for (let row = gridStart.row; row <= gridEnd.row; row++) {
let ltile = this.logicalMap[this.rasterIdx(row, col)];
if (ltile !== null) {
filteredTiles.push(ltile);
}
}
}
return filteredTiles;
}
rasterIdx(row, col) {
return col + (this.cols * row);
}
/* explanation of what I'm doing for auto-tiling:
* http://www.angryfishstudios.com/2011/04/adventures-in-bitmasking/ */
getTileNumber(row, col) {
let sum = 0;
let rm = max(0, row - 1);
let rp = min(this.rows - 1, row + 1);
let cm = max(0, col - 1);
let cp = min(this.cols - 1, col + 1);
/* TOP */
if (this.map[rm][col] === 'w') {
sum += 16;
}
/* LEFT */
if (this.map[row][cm] === 'w') {
sum += 128;
}
/* RIGHT */
if (this.map[row][cp] === 'w') {
sum += 32;
}
/* BOTTOM */
if (this.map[rp][col] === 'w') {
sum += 64;
}
/* TOP LEFT */
if (this.map[rm][cm] === 'w') {
sum += 8;
}
/* TOP RIGHT */
if (this.map[rm][cp] === 'w') {
sum += 1;
}
/* BOTTOM LEFT */
if (this.map[rp][cm] === 'w') {
sum += 4;
}
/* BOTTOM RIGHT */
if (this.map[rp][cp] === 'w') {
sum += 2;
}
return sum;
}
/* Convert x and y values to a grid position for use with the 2d map
* array */
getGridIdx(x, y) {
let col = floor(x / this.tilesize);
let row = floor(y / this.tilesize);
return {
row: constrain(row, 0, this.rows - 1),
col: constrain(col, 0, this.cols - 1)
};
}
/* Floor an x, y position to a grid position (i.e. 12, 3 might clamp to 1, 0
* for tilesize = 10) */
getTilePos(x, y) {
let tileX = floor(x / this.tilesize) * this.tilesize;
let tileY = floor(y / this.tilesize) * this.tilesize;
return createVector(tileX, tileY);
}
/* Grabs a snippet from the tileset and draws it. The actual snippet depends
* on the number, n. */
drawTilesetImg(x, y, n) {
let sx = 0;
let sy = 0;
/* Diagonals, 4th row of my tileset */
if (n === 254) {
sx = 0 * this.tilesize;
sy = 4 * this.tilesize;
} else if (n === 253) {
sx = 1 * this.tilesize;
sy = 4 * this.tilesize;
} else if (n === 251) {
sx = 2 * this.tilesize;
sy = 4 * this.tilesize;
} else if (n === 247) {
sx = 3 * this.tilesize;
sy = 4 * this.tilesize;
} else {
/* Only looking at a few corner cases, so for the most part, we look at
* NSEW. */
let nsew = n >> 4;
sx = (nsew % 4) * this.tilesize;
sy = (floor(nsew / 4)) * this.tilesize;
}
image(assets.getImage("grassy_tileset"), x, y, this.tilesize, this.tilesize, sx, sy, this.tilesize, this.tilesize);
}
drawUI() {
fill(0, 0, 0, 80);
noStroke();
rect(4, 4, 172, 62, 5, 5);
/* Draw the player's health bar */
fill(0, 0, 0);
stroke(0, 0, 0);
strokeWeight(8);
rect(10, 10, 160, 20, 5, 40, 5, 40);
noStroke();
fill(255, 0, 0);
/* Draw player health, then overlay health added from collectables */
let playerHealth = 160 * (this.player.health / this.player.baseHealth);
let pollenHealth = 160 * (this.player.collectablesFound / this.totalCollectables);
rect(10, 10, playerHealth, 20, 5, 40, 5, 40);
if (pollenHealth !== 0) {
fill(255, 255, 0);
rect(10, 10, pollenHealth, 20, 5, 40, 5, 40);
}
/* Signal that the player is invincible */
if (this.player.invincibleTimer.active) {
stroke(211, 211, 211);
strokeWeight(8);
noFill();
rect(10, 10, 160, 20, 5, 40, 5, 40);
}
/* Pollen collected */
noStroke();
assets.drawImage("pollen_small", 40, 40, 16, 16);
fill(255, 255, 255);
textFont(assets.getFont("options_font"));
textAlign(LEFT, BASELINE);
textSize(16);
text(this.player.collectablesFound, 64, 40 + 16);
this.player.shootCooldown.draw(20, 48);
}
drawBackground() {
let b1 = color(175, 238, 238);
let b2 = color(245, 245, 245);
utils.setGradient(0, 0, width, height, b1, b2, 1);
}
/* Render the tilemap. Only draw tiles inside the camera */
draw() {
this.drawBackground();
/* Get the range of rows and columns to draw */
let gridStart = this.getGridIdx(this.camera.x, this.camera.y);
let gridEnd = {
row: min(this.rows - 1, gridStart.row + (height / this.tilesize) + 1),
col: min(this.cols - 1, gridStart.col + (width / this.tilesize))
};
/* only draw tiles the camera can see */
for (let col = gridStart.col; col <= gridEnd.col; col++) {
for (let row = gridStart.row; row <= gridEnd.row; row++) {
let tile = this.map[row][col];
let ridx = this.rasterIdx(row, col);
let vtile = this.visualMap[ridx];
let ltile = this.logicalMap[ridx];
let x = (col * this.tilesize) - this.camera.x;
let y = (row * this.tilesize) - this.camera.y;
if ("w".includes(tile)) {
fill(30, 30, 30);
noStroke();
this.drawTilesetImg(x, y, vtile);
} else if (tile === "c" && !ltile.collected) {
push();
translate(-this.camera.x, -this.camera.y);
ltile.draw(this.camera);
pop();
} else if (tile === "t") {
push();
translate(-this.camera.x, -this.camera.y);
ltile.draw(this.camera);
pop();
}
}
}
push();
translate(-this.camera.x, -this.camera.y);
this.enemies.forEach((enemy) => {
enemy.draw(this.camera);
});
this.player.draw(this.camera);
pop();
/* UI elements on top */
this.drawUI();
}
}