var Class = require('../../utils/Class'); var Components = require('../components'); var Rectangle = require('../../geom/rectangle'); var Tile = new Class({ Mixins: [Components.Alpha, Components.Flip, Components.Visible] , initialize: function Tile(layer, index, x, y, width, height, baseWidth, baseHeight){ this.layer = layer; this.index = index; this.x = x; this.y = y; this.width = width; this.height = height; this.baseWidth = (baseWidth !== undefined)? baseWidth: width; this.baseHeight = (baseHeight !== undefined)? baseHeight: height; this.pixelX = 0; this.pixelY = 0; this.updatePixelXY(); this.properties = { } ; 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.tint = 16777215; this.physics = { } ; } , containsPoint: function (x, y){ return !(x < this.pixelX || y < this.pixelY || x > this.right || y > this.bottom); } , 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; } , getCollisionGroup: function (){ return this.tileset? this.tileset.getTileCollisionGroup(this.index): null ; } , getTileData: function (){ return this.tileset? this.tileset.getTileData(this.index): null ; } , getLeft: function (camera){ var tilemapLayer = this.tilemapLayer; return tilemapLayer? tilemapLayer.tileToWorldX(this.x, camera): this.x * this.baseWidth; } , getRight: function (camera){ var tilemapLayer = this.tilemapLayer; return tilemapLayer? this.getLeft(camera) + this.width * tilemapLayer.scaleX: this.getLeft(camera) + this.width; } , getTop: function (camera){ var tilemapLayer = this.tilemapLayer; return tilemapLayer? tilemapLayer.tileToWorldY(this.y, camera) - (this.height - this.baseHeight) * tilemapLayer.scaleY: this.y * this.baseHeight - (this.height - this.baseHeight); } , getBottom: function (camera){ var tilemapLayer = this.tilemapLayer; return tilemapLayer? this.getTop(camera) + this.height * tilemapLayer.scaleY: this.getTop(camera) + this.height; } , getBounds: function (camera, output){ if (output === undefined) { output = new Rectangle(); } output.x = this.getLeft(); output.y = this.getTop(); output.width = this.getRight() - output.x; output.height = this.getBottom() - output.y; return output; } , getCenterX: function (camera){ return this.getLeft(camera) + this.width / 2; } , getCenterY: function (camera){ return this.getTop(camera) + this.height / 2; } , destroy: function (){ this.collisionCallback = undefined; this.collisionCallbackContext = undefined; this.properties = undefined; } , intersects: function (x, y, right, bottom){ return !(right <= this.pixelX || bottom <= this.pixelY || 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 ; return this; } , resetFaces: function (){ this.faceTop = false ; this.faceBottom = false ; this.faceLeft = false ; this.faceRight = false ; return 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; } , setCollisionCallback: function (callback, context){ if (callback === null ) { this.collisionCallback = undefined; this.collisionCallbackContext = undefined; } else { this.collisionCallback = callback; this.collisionCallbackContext = context; } return 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; } , updatePixelXY: function (){ this.pixelX = this.x * this.baseWidth; this.pixelY = this.y * this.baseHeight - (this.height - this.baseHeight); return this; } , canCollide: { get: function (){ return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown || this.collisionCallback); } } , collides: { get: function (){ return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown); } } , hasInterestingFace: { get: function (){ return (this.faceTop || this.faceBottom || this.faceLeft || this.faceRight); } } , tileset: { get: function (){ var tilemapLayer = this.tilemapLayer; return tilemapLayer? tilemapLayer.tileset: null ; } } , tilemapLayer: { get: function (){ return this.layer.tilemapLayer; } } , tilemap: { get: function (){ var tilemapLayer = this.tilemapLayer; return tilemapLayer? tilemapLayer.tilemap: null ; } } } ); module.exports = Tile;