forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTilemapLayer.js
More file actions
460 lines (348 loc) · 11.3 KB
/
Copy pathTilemapLayer.js
File metadata and controls
460 lines (348 loc) · 11.3 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Maybe should extend Sprite?
Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset, tilemap, layer) {
/**
* @property {Phaser.Game} game - Description.
*/
this.game = game;
/**
* @property {Description} canvas - Description.
* @default
*/
this.canvas = Phaser.Canvas.create(renderWidth, renderHeight);
/**
* @property {Description} context - Description.
* @default
*/
this.context = this.canvas.getContext('2d');
/**
* @property {Description} baseTexture - Description.
* @default
*/
this.baseTexture = new PIXI.BaseTexture(this.canvas);
/**
* @property {Description} texture - Description.
* @default
*/
this.texture = new PIXI.Texture(this.baseTexture);
this.frame = new Phaser.Frame(0, 0, 0, renderWidth, renderHeight, 'tilemaplayer', game.rnd.uuid());
/**
* @property {Description} sprite - Description.
* @default
*/
this.sprite = new Phaser.Sprite(this.game, x, y, this.texture, this.frame);
/**
* @property {Description} tileset - Description.
*/
this.tileset = null;
this.tileWidth = 0;
this.tileHeight = 0;
/**
* Read-only variable, do NOT recommend changing after the map is loaded!
* @property {number} widthInPixels
* @default
*/
this.widthInPixels = 0;
/**
* Read-only variable, do NOT recommend changing after the map is loaded!
* @property {number} heightInPixels
* @default
*/
this.heightInPixels = 0;
this.renderWidth = renderWidth;
this.renderHeight = renderHeight;
/**
* @property {number} _ga - Local render loop var to help avoid gc spikes.
* @private
*/
this._ga = 1;
/**
* @property {number} _dx - Local render loop var to help avoid gc spikes.
* @private
*/
this._dx = 0;
/**
* @property {number} _dy - Local render loop var to help avoid gc spikes.
* @private
*/
this._dy = 0;
/**
* @property {number} _dw - Local render loop var to help avoid gc spikes.
* @private
*/
this._dw = 0;
/**
* @property {number} _dh - Local render loop var to help avoid gc spikes.
* @private
*/
this._dh = 0;
/**
* @property {number} _tx - Local render loop var to help avoid gc spikes.
* @private
*/
this._tx = 0;
/**
* @property {number} _ty - Local render loop var to help avoid gc spikes.
* @private
*/
this._ty = 0;
this._results = [];
this._tw = 0;
this._th = 0;
/**
* @property {number} _tl - Local render loop var to help avoid gc spikes.
* @private
*/
this._tl = 0;
/**
* @property {number} _maxX - Local render loop var to help avoid gc spikes.
* @private
*/
this._maxX = 0;
/**
* @property {number} _maxY - Local render loop var to help avoid gc spikes.
* @private
*/
this._maxY = 0;
/**
* @property {number} _startX - Local render loop var to help avoid gc spikes.
* @private
*/
this._startX = 0;
/**
* @property {number} _startY - Local render loop var to help avoid gc spikes.
* @private
*/
this._startY = 0;
this.tilemap = null;
this.layer = null;
this._x = 0;
this._y = 0;
this._prevX = 0;
this._prevY = 0;
this.dirty = true;
if (tileset instanceof Phaser.Tileset || typeof tileset === 'string')
{
this.updateTileset(tileset);
}
if (tilemap instanceof Phaser.Tilemap)
{
this.updateMapData(tilemap, layer);
}
};
Phaser.TilemapLayer.prototype = {
updateTileset: function (tileset) {
if (tileset instanceof Phaser.Tileset)
{
this.tileset = tileset;
}
else if (typeof tileset === 'string')
{
this.tileset = this.game.cache.getTileset('tiles');
}
else
{
return;
}
this.tileWidth = this.tileset.tileWidth;
this.tileHeight = this.tileset.tileHeight;
this.updateMax();
},
updateMapData: function (tilemap, layer) {
if (typeof layer === 'undefined')
{
layer = 0;
}
if (tilemap instanceof Phaser.Tilemap)
{
this.tilemap = tilemap;
this.layer = this.tilemap.layers[layer];
this.updateMax();
}
},
/**
*
* @method getTileOverlaps
* @param {GameObject} object - Tiles you want to get that overlaps this.
* @return {array} Array with tiles informations (each contains x, y, and the tile).
*/
getTiles: function (x, y, width, height, collides) {
if (this.tilemap === null)
{
return;
}
// Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all)
if (typeof collides === 'undefined') { collides = false; }
// Cap the values
if (x < 0)
{
x = 0;
}
if (y < 0)
{
y = 0;
}
if (width > this.widthInPixels)
{
width = this.widthInPixels;
}
if (height > this.heightInPixels)
{
height = this.heightInPixels;
}
// Convert the pixel values into tile coordinates
this._tx = this.game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
this._ty = this.game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
this._tw = (this.game.math.snapToCeil(width, this.tileWidth) + this.tileWidth) / this.tileWidth;
this._th = (this.game.math.snapToCeil(height, this.tileHeight) + this.tileHeight) / this.tileHeight;
this._results.length = 0;
this._results.push( { x: x, y: y, width: width, height: height, tx: this._tx, ty: this._ty, tw: this._tw, th: this._th });
var _index = 0;
var _tile = null;
for (var wy = this._ty; wy < this._ty + this._th; wy++)
{
for (var wx = this._tx; wx < this._tx + this._tw; wx++)
{
if (this.layer.data[wy] && this.layer.data[wy][wx])
{
// Could combine
_index = this.layer.data[wy][wx] - 1;
_tile = this.tileset.getTile(_index);
if (collides == false || (collides && _tile.collideNone == false))
{
this._results.push({ left: wx * _tile.width, right: (wx * _tile.width) + _tile.width, top: wy * _tile.height, bottom: (wy * _tile.height) + _tile.height, width: _tile.width, height: _tile.height, tx: wx, ty: wy, tile: _tile });
}
}
}
}
return this._results;
},
updateMax: function () {
this._maxX = this.game.math.ceil(this.canvas.width / this.tileWidth) + 1;
this._maxY = this.game.math.ceil(this.canvas.height / this.tileHeight) + 1;
if (this.layer)
{
if (this._maxX > this.layer.width)
{
this._maxX = this.layer.width;
}
if (this._maxY > this.layer.height)
{
this._maxY = this.layer.height;
}
this.widthInPixels = this.layer.width * this.tileWidth;
this.heightInPixels = this.layer.height * this.tileHeight;
}
this.dirty = true;
},
render: function () {
if (!this.dirty || !this.tileset || !this.tilemap || !this.sprite.visible)
{
return;
}
this._prevX = this._dx;
this._prevY = this._dy;
this._dx = -(this._x - (this._startX * this.tileWidth));
this._dy = -(this._y - (this._startY * this.tileHeight));
this._tx = this._dx;
this._ty = this._dy;
// First let's just copy over the whole canvas, offset by the scroll difference
// Then we only need fill in the missing strip/s (could be top/bottom/left/right I guess)
// ScrollZone code might be useful here.
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
for (var y = this._startY; y < this._startY + this._maxY; y++)
{
this._column = this.layer.data[y];
for (var x = this._startX; x < this._startX + this._maxX; x++)
{
// only -1 on TILED maps, not CSV
var tile = this.tileset.tiles[this._column[x]-1];
// if (this.tileset.checkTileIndex(tile))
// {
this.context.drawImage(
this.tileset.image,
tile.x,
tile.y,
this.tileWidth,
this.tileHeight,
Math.floor(this._tx),
Math.floor(this._ty),
this.tileWidth,
this.tileHeight
);
// }
this._tx += this.tileWidth;
}
this._tx = this._dx;
this._ty += this.tileHeight;
}
// Only needed if running in WebGL, otherwise this array will never get cleared down I don't think!
if (this.game.renderType == Phaser.WEBGL)
{
PIXI.texturesToUpdate.push(this.baseTexture);
}
this.dirty = false;
return true;
}
};
Phaser.TilemapLayer.prototype.deltaAbsX = function () {
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
}
Phaser.TilemapLayer.prototype.deltaAbsY = function () {
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
}
Phaser.TilemapLayer.prototype.deltaX = function () {
return this._dx - this._prevX;
}
Phaser.TilemapLayer.prototype.deltaY = function () {
return this._dy - this._prevY;
}
Object.defineProperty(Phaser.TilemapLayer.prototype, "x", {
get: function () {
return this._x;
},
set: function (value) {
if (value !== this._x && value >= 0)
{
this._x = value;
if (this._x > (this.widthInPixels - this.renderWidth))
{
this._x = this.widthInPixels - this.renderWidth;
}
this._startX = this.game.math.floor(this._x / this.tileWidth);
if (this._startX < 0)
{
this._startX = 0;
}
if (this._startX + this._maxX > this.layer.width)
{
this._startX = this.layer.width - this._maxX;
}
this.dirty = true;
}
}
});
Object.defineProperty(Phaser.TilemapLayer.prototype, "y", {
get: function () {
return this._y;
},
set: function (value) {
if (value !== this._y && value >= 0)
{
this._y = value;
if (this._y > (this.heightInPixels - this.renderHeight))
{
this._y = this.heightInPixels - this.renderHeight;
}
this._startY = this.game.math.floor(this._y / this.tileHeight);
if (this._startY < 0)
{
this._startY = 0;
}
if (this._startY + this._maxY > this.layer.height)
{
this._startY = this.layer.height - this._maxY;
}
this.dirty = true;
}
}
});