forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.js
More file actions
511 lines (451 loc) · 15.1 KB
/
Copy pathTile.js
File metadata and controls
511 lines (451 loc) · 15.1 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
var Class = require('../../utils/Class');
var Components = require('../components');
var Tile = new Class({
Mixins: [
Components.Alpha,
Components.Flip,
Components.Visible
],
initialize:
/**
* A Tile is a representation of a single tile within the Tilemap. This is a lightweight data
* representation, so it's position information is stored without factoring in scroll, layer
* scale or layer position.
*
* @class Tile
* @constructor
*
* @param {LayerData} layer - The LayerData object in the Tilemap that this tile belongs to.
* @param {integer} index - The unique index of this tile within the map.
* @param {integer} x - The x coordinate of this tile in tile coordinates.
* @param {integer} y - The y coordinate of this tile in tile coordinates.
* @param {integer} width - Width of the tile in pixels.
* @param {integer} height - Height of the tile in pixels.
* @param {integer} baseWidth - The base width a tile in the map (in pixels). Tiled maps support
* multiple tileset sizes within one map, but they are still placed at intervals of the base
* tile width.
* @param {integer} baseHeight - The base height of the tile in pixels (in pixels). Tiled maps
* support multiple tileset sizes within one map, but they are still placed at intervals of the
* base tile height.
*/
function Tile (layer, index, x, y, width, height, baseWidth, baseHeight)
{
/**
* The LayerData in the Tilemap data that this tile belongs to.
* @property {LayerData} layer
*/
this.layer = layer;
/**
* The index of this tile within the map data corresponding to the tileset, or -1 if this
* represents a blank tile.
* @property {integer} index
*/
this.index = index;
/**
* The x map coordinate of this tile in tile units.
* @property {integer} x
*/
this.x = x;
/**
* The y map coordinate of this tile in tile units.
* @property {integer} y
*/
this.y = y;
/**
* The width of the tile in pixels.
* @property {integer} width
*/
this.width = width;
/**
* The height of the tile in pixels.
* @property {integer} height
*/
this.height = height;
/**
* The map's base width of a tile in pixels. Tiled maps support multiple tileset sizes
* within one map, but they are still placed at intervals of the base tile size.
* @property {integer} baseWidth
*/
this.baseWidth = (baseWidth !== undefined) ? baseWidth : width;
/**
* The map's base height of a tile in pixels. Tiled maps support multiple tileset sizes
* within one map, but they are still placed at intervals of the base tile size.
* @property {integer} baseHeight
*/
this.baseHeight = (baseHeight !== undefined) ? baseHeight : height;
/**
* The x coordinate of the top left of this tile in pixels. This is relative to the top left
* of the layer this tile is being rendered within. This property does NOT factor in camera
* scroll, layer scale or layer position.
* @property {number} pixelX
*/
this.pixelX = 0;
/**
* The y coordinate of the top left of this tile in pixels. This is relative to the top left
* of the layer this tile is being rendered within. This property does NOT factor in camera
* scroll, layer scale or layer position.
* @property {number} pixelY
*/
this.pixelY = 0;
this.updatePixelXY();
/**
* Tile specific properties. These usually come from Tiled.
* @property {object} properties
*/
this.properties = {};
/**
* The rotation angle of this tile.
* @property {number} rotation
*/
this.rotation = 0;
/**
* Whether the tile should collide with any object on the left side.
* @property {boolean} collideLeft
*/
this.collideLeft = false;
/**
* Whether the tile should collide with any object on the right side.
* @property {boolean} collideRight
*/
this.collideRight = false;
/**
* Whether the tile should collide with any object on the top side.
* @property {boolean} collideUp
*/
this.collideUp = false;
/**
* Whether the tile should collide with any object on the bottom side.
* @property {boolean} collideDown
*/
this.collideDown = false;
/**
* Whether the tile's left edge is interesting for collisions.
* @property {boolean} faceLeft
*/
this.faceLeft = false;
/**
* Whether the tile's right edge is interesting for collisions.
* @property {boolean} faceRight
*/
this.faceRight = false;
/**
* Whether the tile's top edge is interesting for collisions.
* @property {boolean} faceTop
*/
this.faceTop = false;
/**
* Whether the tile's bottom edge is interesting for collisions.
* @property {boolean} faceBottom
*/
this.faceBottom = false;
/**
* Tile collision callback.
* @property {function} collisionCallback
*/
this.collisionCallback = null;
/**
* The context in which the collision callback will be called.
* @property {object} collisionCallbackContext
*/
this.collisionCallbackContext = this;
/**
* The tint to apply to this tile. Note: tint is currently a single color value instead of
* the 4 corner tint component on other GameObjects.
* @property {number} Tint
* @default
*/
this.tint = 0xffffff;
},
/**
* Check if the given x and y world coordinates are within this Tile. This does not factor in
* camera scroll, layer scale or layer position.
*
* @param {number} x - The x coordinate to test.
* @param {number} y - The y coordinate to test.
* @return {boolean} True if the coordinates are within this Tile, otherwise false.
*/
containsPoint: function (x, y)
{
return !(x < this.pixelX || y < this.pixelY || x > this.right || y > this.bottom);
},
/**
* Copies the tile data & properties from the given tile to this tile. This copies everything
* except for position and interesting faces.
*
* @param {Tile} tile - The tile to copy from.
* @returns {this}
*/
copy: function (tile)
{
this.index = tile.index;
this.alpha = tile.alpha;
this.properties = tile.properties;
this.visible = tile.visible;
this.setFlip(tile.flipX, tile.flipY);
this.tint = tile.tint;
this.rotation = tile.rotation;
this.collideUp = tile.collideUp;
this.collideDown = tile.collideDown;
this.collideLeft = tile.collideLeft;
this.collideRight = tile.collideRight;
this.collisionCallback = tile.collisionCallback;
this.collisionCallbackContext = tile.collisionCallbackContext;
return this;
},
/**
* Clean up memory.
*/
destroy: function ()
{
this.collisionCallback = undefined;
this.collisionCallbackContext = undefined;
this.properties = undefined;
},
/**
* Check for intersection with this tile. This does not factor in camera scroll, layer scale or
* layer position.
*
* @param {number} x - The x axis in pixels.
* @param {number} y - The y axis in pixels.
* @param {number} right - The right point.
* @param {number} bottom - The bottom point.
* @return {boolean}
*/
intersects: function (x, y, right, bottom)
{
return !(
right <= this.pixelX || bottom <= this.pixelY ||
x >= this.right || y >= this.bottom
);
},
/**
* Checks if the tile is interesting.
*
* @param {boolean} collides - If true, will consider the tile interesting if it collides on any
* side.
* @param {boolean} faces - If true, will consider the tile interesting if it has an interesting
* face.
* @returns {boolean} True if the Tile is interesting, otherwise false.
*/
isInteresting: function (collides, faces)
{
if (collides && faces) { return (this.canCollide || this.hasInterestingFace); }
else if (collides) { return this.collides; }
else if (faces) { return this.hasInterestingFace; }
return false;
},
/**
* Reset collision status flags.
*
* @returns {this}
*/
resetCollision: function ()
{
this.collideLeft = false;
this.collideRight = false;
this.collideUp = false;
this.collideDown = false;
this.faceTop = false;
this.faceBottom = false;
this.faceLeft = false;
this.faceRight = false;
return this;
},
/**
* Reset faces.
*
* @returns {this}
*/
resetFaces: function ()
{
this.faceTop = false;
this.faceBottom = false;
this.faceLeft = false;
this.faceRight = false;
return this;
},
/**
* Sets the collision flags for each side of this tile and updates the interesting faces list.
*
* @param {boolean} left - Indicating collide with any object on the left.
* @param {boolean} right - Indicating collide with any object on the right.
* @param {boolean} up - Indicating collide with any object on the top.
* @param {boolean} down - Indicating collide with any object on the bottom.
* @returns {this}
*/
setCollision: function (left, right, up, down)
{
if (right === undefined) { right = left; }
if (up === undefined) { up = left; }
if (down === undefined) { down = left; }
this.collideLeft = left;
this.collideRight = right;
this.collideUp = up;
this.collideDown = down;
this.faceLeft = left;
this.faceRight = right;
this.faceTop = up;
this.faceBottom = down;
return this;
},
/**
* Set a callback to be called when this tile is hit by an object. The callback must true for
* collision processing to take place.
*
* @param {function} callback - Callback function.
* @param {object} context - Callback will be called within this context.
* @returns {this}
*/
setCollisionCallback: function (callback, context)
{
if (callback === null)
{
this.collisionCallback = undefined;
this.collisionCallbackContext = undefined;
}
else
{
this.collisionCallback = callback;
this.collisionCallbackContext = context;
}
return this;
},
/**
* Sets the size of the tile and updates its pixelX and pixelY.
*
* @param {integer} tileWidth - The width of the tile in pixels.
* @param {integer} tileHeight - The height of the tile in pixels.
* @param {integer} baseWidth - The base width a tile in the map (in pixels).
* @param {integer} baseHeight - The base height of the tile in pixels (in pixels).
* @returns {this}
*/
setSize: function (tileWidth, tileHeight, baseWidth, baseHeight)
{
if (tileWidth !== undefined) { this.width = tileWidth; }
if (tileHeight !== undefined) { this.height = tileHeight; }
if (baseWidth !== undefined) { this.baseWidth = baseWidth; }
if (baseHeight !== undefined) { this.baseHeight = baseHeight; }
this.updatePixelXY();
return this;
},
/**
* Used internally. Updates the tile's world XY position based on the current tile size.
*
* @returns {this}
*/
updatePixelXY: function ()
{
// Tiled places tiles on a grid of baseWidth x baseHeight. The origin for a tile is the
// bottom left, while the Phaser renderer assumes the origin is the top left. The y
// coordinate needs to be adjusted by the difference.
this.pixelX = this.x * this.baseWidth;
this.pixelY = this.y * this.baseHeight - (this.height - this.baseHeight);
return this;
},
/**
* True if this tile can collide on any of its faces or has a collision callback set.
* @property {boolean} canCollide
* @readonly
*/
canCollide: {
get: function ()
{
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown || this.collisionCallback);
}
},
/**
* True if this tile can collide on any of its faces.
* @property {boolean} canCollide
* @readonly
*/
collides: {
get: function ()
{
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown);
}
},
/**
* True if this tile has any interesting faces.
* @property {boolean} canCollide
* @readonly
*/
hasInterestingFace: {
get: function ()
{
return (this.faceTop || this.faceBottom || this.faceLeft || this.faceRight);
}
},
/**
* The world position of the left side of the tile. This does not factor in camera scroll, layer
* scale or layer position.
* @property {integer} left
* @readonly
*/
left: {
get: function ()
{
return this.pixelX;
}
},
/**
* The world position of the right side of the tile. This does not factor in camera scroll,
* layer scale or layer position.
* @property {integer} right
* @readonly
*/
right: {
get: function ()
{
return this.pixelX + this.width;
}
},
/**
* The world position of the top side of the tile. This does not factor in camera scroll,
* layer scale or layer position.
* @property {integer} top
* @readonly
*/
top: {
get: function ()
{
return this.pixelY;
}
},
/**
* The world position of the bottom side of the tile. This does not factor in camera scroll,
* layer scale or layer position.
* @property {integer} bottom
* @readonly
*/
bottom: {
get: function ()
{
return this.pixelY + this.height;
}
},
/**
* The x world position of the center of the tile. This does not factor in camera scroll, layer
* scale or layer position.
* @property {integer} centerX
* @readonly
*/
centerX: {
get: function ()
{
return this.pixelX + this.width / 2;
}
},
/**
* The y world position of the center of the tile. This does not factor in camera scroll, layer
* scale or layer position.
* @property {integer} centerY
* @readonly
*/
centerY: {
get: function ()
{
return this.pixelY + this.height / 2;
}
}
});
module.exports = Tile;