-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level.js
270 lines (239 loc) · 6.71 KB
/
Level.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
function Level(stage, game, width, height, tileSize) {
this.stage = stage;
this.game = game;
this.width = width;
this.height = height;
this.tileSize = tileSize;
this.tileWidth = this.width / this.tileSize;
this.tileHeight = this.height / this.tileSize;
this.map = new LevelMap(this.tileWidth, this.tileHeight);
this.container = new PIXI.Container();
this.stage.addChild(this.container);
this.loadTextures();
this.regenerate();
}
/**
* Regenerate the level, generating the map, creating the tiles for the map,
* and creating the ice block
*/
Level.prototype.regenerate = function() {
this.generateMap();
this.createTiles();
this.createBlock();
};
/**
* Get tile location corresponding to tile coordinates (indices)
*
* @param {Vec2} loc
* @return {Vec2}
*/
Level.prototype.getTileLoc = function(loc) {
return new Vec2(
loc.x * this.tileSize,
loc.y * this.tileSize);
};
/**
* Get tile location corresponding to pixel location
*
* @param {Vec2} loc
* @return {Vec2}
*/
Level.prototype.getTileCoords = function(loc) {
return new Vec2(
Math.floor(loc.x / this.tileSize),
Math.floor(loc.y / this.tileSize));
};
/**
* Get tile location corresponding to pixel location using Math.round
*
* @param {Vec2} loc
* @return {Vec2}
*/
Level.prototype.getTileCoordsRounded = function(loc) {
return new Vec2(
Math.round(loc.x / this.tileSize),
Math.round(loc.y / this.tileSize));
};
/**
* Get the closest tile center to the given coordinates
*
* @param {Vec2} loc
* @return {Object}
*/
Level.prototype.getCenter = function(loc) {
return this.getTileLoc(this.getTileCoords(loc));
};
/**
* Detect the block (or lack thereof) collided with moving from loc in dir.
*
* @param {Vec2} loc
* @param {Vec2} dir
* @return {String} Block type collided with
*/
Level.prototype.getCollision = function(loc, dir) {
if (dir.x === 0 && dir.y === 0) {
return BlockType.EMPTY;
}
var newLoc = loc.add(dir);
var tileCoords = this.getTileCoords(newLoc);
if (dir.x > 0) {
tileCoords.x += 1;
}
if (dir.y > 0) {
tileCoords.y += 1;
}
var tileLoc = this.getTileLoc(tileCoords);
var xCollideBlock = this.map.tileMap[tileCoords.x][tileCoords.y];
if (newLoc.y < tileLoc.y) {
// If we overlap in the negative y direction throw the block in that
// direction into the y collide calculation
xCollideBlock = BlockType.orCollision(
xCollideBlock,
this.map.tileMap[tileCoords.x][tileCoords.y - 1]);
} else if (newLoc.y > tileLoc.y) {
xCollideBlock = BlockType.orCollision(
xCollideBlock,
this.map.tileMap[tileCoords.x][tileCoords.y + 1]);
}
var yCollideBlock = this.map.tileMap[tileCoords.x][tileCoords.y];
if (newLoc.x < tileLoc.x) {
// If we overlap in the negative x direction throw the block in that
// direction into the y collide calculation
yCollideBlock = BlockType.orCollision(
yCollideBlock,
this.map.tileMap[tileCoords.x - 1][tileCoords.y]);
} else if (newLoc.x > tileLoc.x) {
yCollideBlock = BlockType.orCollision(
yCollideBlock,
this.map.tileMap[tileCoords.x + 1][tileCoords.y]);
}
if (BlockType.isCollision(xCollideBlock)) {
// If both collide and y is the movement direction, prefer returning y
if (BlockType.isCollision(yCollideBlock)) {
if (Math.abs(dir.x) < Math.abs(dir.y)) {
return yCollideBlock;
}
}
return xCollideBlock;
}
return yCollideBlock;
};
/**
* Detect the collision from loc proceeding in dir direction. If collision,
* return the furthest the player can travel from loc in dir.
*
* @param {Vec2} loc
* @param {Vec2} dir
* @return {Vec2} new location to move to
*/
Level.prototype.getMovementWithCollision = function(loc, dir) {
if (dir.x === 0 && dir.y === 0) {
return loc;
}
var newDir = new Vec2(dir.x, dir.y);
var xCollide = BlockType.isCollision(
this.getCollision(loc, new Vec2(dir.x, 0)));
var yCollide = BlockType.isCollision(
this.getCollision(loc, new Vec2(0, dir.y)));
var xyCollide = BlockType.isCollision(
this.getCollision(loc, dir));
var collideDirX = Math.round(loc.x / this.tileSize) * this.tileSize - loc.x;
var collideDirY = Math.round(loc.y / this.tileSize) * this.tileSize - loc.y;
if (xyCollide && !xCollide && !yCollide) {
newDir.x = collideDirX;
newDir.y = collideDirY;
}
if (xCollide) {
newDir.x = collideDirX;
}
if (yCollide) {
newDir.y = collideDirY;
}
return loc.add(newDir);
};
/**
* Load in the tile textures required
*/
Level.prototype.loadTextures = function() {
this.wallTexture = PIXI.Texture.fromImage('assets/wall.png');
this.goalTexture = PIXI.Texture.fromImage('assets/goal.png');
this.emptyTexture = PIXI.Texture.fromImage('assets/empty.png');
};
/**
* Generate a new map of tiles
*/
Level.prototype.generateMap = function() {
this.map.generate();
};
/**
* Create the tiles corresponding to the map
*/
Level.prototype.createTiles = function() {
this.container.removeChildren();
for (var tileX = 0; tileX < this.map.width; tileX++) {
for (var tileY = 0; tileY < this.map.height; tileY++) {
var x = tileX * this.tileSize;
var y = tileY * this.tileSize;
var type = null;
switch (this.map.tileMap[tileX][tileY]) {
case BlockType.EMPTY:
case BlockType.BLOCK:
tile = new PIXI.Sprite(this.emptyTexture);
break;
case BlockType.WALL:
tile = new PIXI.Sprite(this.wallTexture);
break;
case BlockType.GOAL:
tile = new PIXI.Sprite(this.goalTexture);
break;
default:
throw new Error('Unknown type found');
}
tile.position.x = x;
tile.position.y = y;
tile.width = this.tileSize;
tile.height = this.tileSize;
this.container.addChild(tile);
}
}
};
/**
* Create the ice block
*/
Level.prototype.createBlock = function() {
if (this.block) {
this.block.remove();
}
for (var x = 0; x < this.map.width; x++) {
for (var y = 0; y < this.map.height; y++) {
if (this.map.tileMap[x][y] !== BlockType.BLOCK) {
continue;
}
this.block = new Block(this.stage, this.game, this,
new Vec2(x, y), this.tileSize);
return;
}
}
};
/**
* @return {Vec2} a random empty location
*/
Level.prototype.getRandomEmptyLocation = function() {
var possibilities = [];
for (var x = 0; x < this.map.width; x++) {
for (var y = 0; y < this.map.height; y++) {
if (this.map.tileMap[x][y] === BlockType.EMPTY) {
possibilities.push(new Vec2(x * this.tileSize, y * this.tileSize));
}
}
}
return Utils.randElement(possibilities);
};
/**
* Update for a time step
*
* @param {number} dt
*/
Level.prototype.update = function(dt) {
this.block.update(dt);
};