2017-11-10 02:50:25 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-11-14 21:34:33 +00:00
|
|
|
var Components = require('../components');
|
2017-11-10 02:50:25 +00:00
|
|
|
|
|
|
|
var Tile = new Class({
|
|
|
|
|
2017-11-16 19:09:07 +00:00
|
|
|
// TODO: custom rotation or use transform component
|
2017-11-14 21:34:33 +00:00
|
|
|
// TODO: Add in bounds mixin, or custom replacement
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.Flip,
|
|
|
|
Components.Visible
|
|
|
|
],
|
|
|
|
|
2017-11-10 02:50:25 +00:00
|
|
|
initialize:
|
|
|
|
|
|
|
|
function Tile (layer, index, x, y, width, height)
|
|
|
|
{
|
|
|
|
this.layer = layer;
|
|
|
|
this.index = index;
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2017-11-14 21:34:33 +00:00
|
|
|
this.worldX = x * width;
|
|
|
|
this.worldY = y * height;
|
2017-11-10 02:50:25 +00:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2017-11-16 19:09:07 +00:00
|
|
|
this.properties = {};
|
2017-11-18 21:38:46 +00:00
|
|
|
this.rotation = 0;
|
|
|
|
this.collideLeft = false;
|
|
|
|
this.collideRight = false;
|
|
|
|
this.collideUp = false;
|
|
|
|
this.collideDown = false;
|
|
|
|
this.faceLeft = false;
|
|
|
|
this.faceRight = false;
|
|
|
|
this.faceTop = false;
|
|
|
|
this.faceBottom = false;
|
|
|
|
this.collisionCallback = null;
|
|
|
|
this.collisionCallbackContext = this;
|
|
|
|
this.scanned = false;
|
2017-11-10 02:50:25 +00:00
|
|
|
|
2017-11-14 21:34:33 +00:00
|
|
|
// TODO: update renders to allow for using Components.Tint
|
|
|
|
this.tint = 0xFFFFFF;
|
2017-11-16 19:09:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Copy everything except position
|
|
|
|
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;
|
2017-11-18 21:38:46 +00:00
|
|
|
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;
|
2017-11-16 19:09:07 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-18 21:38:46 +00:00
|
|
|
// Does not factor in scroll offset or tilemap layer position
|
|
|
|
containsPoint: function (x, y)
|
|
|
|
{
|
|
|
|
return !(x < this.worldX || y < this.worldY || x > this.right || y > this.bottom);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.collisionCallback = undefined;
|
|
|
|
this.collisionCallbackContext = undefined;
|
|
|
|
this.properties = undefined;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Does not factor in scroll offset or tilemap layer position
|
|
|
|
intersects: function (x, y, right, bottom)
|
|
|
|
{
|
|
|
|
return !(right <= this.worldX || bottom <= this.worldY || x >= this.right || y >= this.bottom);
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCollision: function (left, right, up, down)
|
|
|
|
{
|
|
|
|
this.collideLeft = left;
|
|
|
|
this.collideRight = right;
|
|
|
|
this.collideUp = up;
|
|
|
|
this.collideDown = down;
|
|
|
|
|
|
|
|
this.faceLeft = left;
|
|
|
|
this.faceRight = right;
|
|
|
|
this.faceTop = up;
|
|
|
|
this.faceBottom = down;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCollisionCallback: function (callback, context)
|
|
|
|
{
|
|
|
|
this.collisionCallback = callback;
|
|
|
|
this.collisionCallbackContext = context;
|
|
|
|
},
|
|
|
|
|
2017-11-18 21:40:27 +00:00
|
|
|
setSize: function (tileWidth, tileHeight)
|
|
|
|
{
|
|
|
|
this.worldX = this.x * tileWidth;
|
|
|
|
this.worldY = this.y * tileHeight;
|
|
|
|
this.width = tileWidth;
|
|
|
|
this.height = tileHeight;
|
|
|
|
},
|
|
|
|
|
2017-11-18 21:38:46 +00:00
|
|
|
// True if this tile can collide on any of its faces or has a collision callback set.
|
|
|
|
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.
|
|
|
|
collides: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// True if this tile has any interesting faces
|
|
|
|
hasInterestingFace: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return (this.faceTop || this.faceBottom || this.faceLeft || this.faceRight);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-11-16 19:09:07 +00:00
|
|
|
left: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.worldX;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
right: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.worldX + this.width;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
top: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.worldY;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
bottom: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.worldY + this.height;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
centerX: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.worldX + this.width / 2;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
centerY: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.worldY + this.height / 2;
|
|
|
|
}
|
2017-11-14 21:34:33 +00:00
|
|
|
}
|
2017-11-16 19:09:07 +00:00
|
|
|
|
2017-11-10 02:50:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Tile;
|