2013-11-27 16:33:49 +00:00
/ * *
* @ author Richard Davey < rich @ photonstorm . com >
2014-02-05 05:54:25 +00:00
* @ copyright 2014 Photon Storm Ltd .
2013-11-27 16:33:49 +00:00
* @ license { @ link https : //github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
* /
/ * *
2014-10-25 00:08:20 +00:00
* A TilemapLayer is a Phaser . Image / Sprite that renders a specific TileLayer of a Tilemap .
*
* Since a TilemapLayer is a Sprite it can be moved around the display , added to other groups or display objects , etc .
2014-12-31 17:04:55 +00:00
*
2014-11-11 01:02:16 +00:00
* By default TilemapLayers have fixedToCamera set to ` true ` . Changing this will break Camera follow and scrolling behaviour .
2013-11-27 16:33:49 +00:00
*
* @ class Phaser . TilemapLayer
2014-10-25 09:09:04 +00:00
* @ extends { Phaser . Image }
2013-11-27 16:33:49 +00:00
* @ constructor
* @ param { Phaser . Game } game - Game reference to the currently running game .
* @ param { Phaser . Tilemap } tilemap - The tilemap to which this layer belongs .
2014-10-25 00:08:20 +00:00
* @ param { integer } index - The index of the TileLayer to render within the Tilemap .
2014-10-26 05:17:31 +00:00
* @ param { integer } width - Width of the renderable area of the layer ( in pixels ) .
* @ param { integer } height - Height of the renderable area of the layer ( in pixels ) .
2013-11-27 16:33:49 +00:00
* /
2013-12-19 03:49:28 +00:00
Phaser . TilemapLayer = function ( game , tilemap , index , width , height ) {
2013-09-11 01:57:36 +00:00
2014-10-26 05:17:31 +00:00
width |= 0 ;
height |= 0 ;
2013-11-25 04:40:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* A reference to the currently running Game .
2014-10-29 05:21:47 +00:00
* @ property { Phaser . Game } game
2014-10-25 00:08:20 +00:00
* @ protected
* @ readonly
2013-11-25 04:40:04 +00:00
* /
2013-10-11 03:42:11 +00:00
this . game = game ;
2013-12-19 03:49:28 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* The Tilemap to which this layer is bound .
2014-10-29 05:21:47 +00:00
* @ property { Phaser . Tilemap } map
2014-10-25 00:08:20 +00:00
* @ protected
* @ readonly
2013-12-19 03:49:28 +00:00
* /
this . map = tilemap ;
2014-11-08 14:50:00 +00:00
/ * *
* The index of this layer within the Tilemap .
* @ property { number } index
* @ protected
* @ readonly
* /
this . index = index ;
2013-12-19 03:49:28 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* The layer object within the Tilemap that this layer represents .
2014-10-29 05:21:47 +00:00
* @ property { Phaser . TileLayer } layer
2014-10-25 00:08:20 +00:00
* @ protected
* @ readonly
2013-12-19 03:49:28 +00:00
* /
this . layer = tilemap . layers [ index ] ;
2013-11-25 04:40:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* The canvas to which this TilemapLayer draws .
2014-10-29 05:21:47 +00:00
* @ property { HTMLCanvasElement } canvas
2014-10-25 00:08:20 +00:00
* @ protected
2013-11-25 04:40:04 +00:00
* /
2014-12-31 17:04:55 +00:00
this . canvas = Phaser . Canvas . create ( width , height ) ;
2014-03-21 18:04:24 +00:00
2013-11-25 04:40:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* The 2 d context of the canvas .
2014-10-29 05:21:47 +00:00
* @ property { CanvasRenderingContext2D } context
2014-10-25 00:08:20 +00:00
* @ private
2013-11-25 04:40:04 +00:00
* /
2013-10-11 03:42:11 +00:00
this . context = this . canvas . getContext ( '2d' ) ;
2014-03-23 07:59:28 +00:00
2013-11-25 04:40:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Required Pixi var .
2014-10-29 05:21:47 +00:00
* @ property { PIXI . BaseTexture } baseTexture
2014-10-25 00:08:20 +00:00
* @ protected
2013-11-25 04:40:04 +00:00
* /
2013-10-11 03:42:11 +00:00
this . baseTexture = new PIXI . BaseTexture ( this . canvas ) ;
2014-03-23 07:59:28 +00:00
2013-11-25 04:40:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Required Pixi var .
2014-10-29 05:21:47 +00:00
* @ property { PIXI . Texture } texture
2014-10-25 00:08:20 +00:00
* @ protected
2013-11-25 04:40:04 +00:00
* /
2013-10-11 03:42:11 +00:00
this . texture = new PIXI . Texture ( this . baseTexture ) ;
2014-03-23 07:59:28 +00:00
2013-11-27 16:33:49 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Dimensions of the renderable area .
2014-10-29 05:21:47 +00:00
* @ property { Phaser . Frame } textureFrame
* @ protected
2013-11-27 16:33:49 +00:00
* /
2013-12-19 03:49:28 +00:00
this . textureFrame = new Phaser . Frame ( 0 , 0 , 0 , width , height , 'tilemapLayer' , game . rnd . uuid ( ) ) ;
2013-10-11 17:18:27 +00:00
2014-03-03 05:19:46 +00:00
Phaser . Image . call ( this , this . game , 0 , 0 , this . texture , this . textureFrame ) ;
2013-12-19 03:49:28 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* The name of the layer .
2014-10-29 05:21:47 +00:00
* @ property { string } name
2013-12-19 03:49:28 +00:00
* /
this . name = '' ;
2013-10-16 05:33:39 +00:00
2013-11-27 16:33:49 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* The const type of this object .
2014-10-29 05:21:47 +00:00
* @ property { number } type
2014-10-25 00:08:20 +00:00
* @ readonly
* @ protected
* @ default Phaser . TILEMAPLAYER
2013-11-27 16:33:49 +00:00
* /
2013-10-16 05:33:39 +00:00
this . type = Phaser . TILEMAPLAYER ;
2013-10-11 03:42:11 +00:00
2013-11-27 16:33:49 +00:00
/ * *
2013-12-17 05:07:00 +00:00
* An object that is fixed to the camera ignores the position of any ancestors in the display list and uses its x / y coordinates as offsets from the top left of the camera .
2014-10-29 05:21:47 +00:00
* @ property { boolean } fixToCamera
2013-11-27 16:33:49 +00:00
* @ default
* /
2013-10-16 05:33:39 +00:00
this . fixedToCamera = true ;
2013-10-14 18:37:44 +00:00
2013-12-17 05:07:00 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* If this object is fixed to the camera then use this Point to specify how far away from the Camera x / y it ' s rendered .
2014-10-29 05:21:47 +00:00
* @ property { Phaser . Point } cameraOffset
2013-12-17 05:07:00 +00:00
* /
2013-12-19 03:49:28 +00:00
this . cameraOffset = new Phaser . Point ( 0 , 0 ) ;
2013-10-11 03:42:11 +00:00
2013-12-18 04:40:10 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Settings that control standard ( non - diagnostic ) rendering .
*
2014-12-31 17:04:55 +00:00
* @ property { boolean } [ enableScrollDelta = true ] - Delta scroll rendering only draws tiles / edges as them come into view .
* This can greatly improve scrolling rendering performance , especially when there are many small tiles .
* It should only be disabled in rare cases .
*
* @ property { ? DOMCanvasElement } [ copyCanvas = ( auto ) ] - [ Internal ] If set , force using a separate ( shared ) copy canvas .
* Using a canvas bitblt / copy when the source and destinations region overlap produces unexpected behavior
* in some browsers , notably Safari .
*
2013-12-18 04:40:10 +00:00
* @ default
* /
2014-10-25 00:08:20 +00:00
this . renderSettings = {
enableScrollDelta : true ,
2014-12-31 17:04:55 +00:00
overdrawRatio : 0.20 ,
2015-01-25 02:37:28 +00:00
copyCanvas : null
2014-10-25 00:08:20 +00:00
} ;
2013-12-18 04:40:10 +00:00
2013-12-17 05:07:00 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Enable an additional "debug rendering" pass to display collision information .
*
2014-10-29 05:21:47 +00:00
* @ property { boolean } debug
2013-12-17 05:07:00 +00:00
* @ default
* /
this . debug = false ;
2013-12-18 00:44:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Settings used for debugging and diagnostics .
*
* @ property { ? string } missingImageFill - A tile is rendered as a rectangle using the following fill if a valid tileset / image cannot be found . A value of ` null ` prevents additional rendering for tiles without a valid tileset image . _This takes effect even when debug rendering for the layer is not enabled . _
*
* @ property { ? string } debuggedTileOverfill - If a Tile has ` Tile#debug ` true then , after normal tile image rendering , a rectangle with the following fill is drawn above / over it . _This takes effect even when debug rendering for the layer is not enabled . _
*
* @ property { boolean } forceFullRedraw - When debug rendering ( ` debug ` is true ) , and this option is enabled , the a full redraw is forced and rendering optimization is suppressed .
*
* @ property { number } debugAlpha - When debug rendering ( ` debug ` is true ) , the tileset is initially rendered with this alpha level . This can make the tile edges clearer .
*
* @ property { ? string } facingEdgeStroke - When debug rendering ( ` debug ` is true ) , this color / stroke is used to draw "face" edges . A value of ` null ` disables coloring facing edges .
*
* @ property { ? string } collidingTileOverfill - When debug rendering ( ` debug ` is true ) , this fill is used for tiles that are collidable . A value of ` null ` disables applying the additional overfill .
*
2013-12-18 00:44:04 +00:00
* /
2014-10-25 00:08:20 +00:00
this . debugSettings = {
2013-12-18 00:44:04 +00:00
2014-10-25 00:08:20 +00:00
missingImageFill : 'rgb(255,255,255)' ,
debuggedTileOverfill : 'rgba(0,255,0,0.4)' ,
2013-12-18 04:40:10 +00:00
2014-10-25 00:08:20 +00:00
forceFullRedraw : true ,
2013-12-18 04:40:10 +00:00
2014-10-25 00:08:20 +00:00
debugAlpha : 0.5 ,
facingEdgeStroke : 'rgba(0,255,0,1)' ,
collidingTileOverfill : 'rgba(0,255,0,0.2)'
2013-12-18 04:40:10 +00:00
2014-10-25 00:08:20 +00:00
} ;
2014-01-14 22:34:41 +00:00
2013-12-18 04:40:10 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Speed at which this layer scrolls horizontally , relative to the camera ( e . g . scrollFactorX of 0.5 scrolls half as quickly as the 'normal' camera - locked layers do ) .
2014-10-29 05:21:47 +00:00
* @ property { number } scrollFactorX
2014-10-30 09:00:20 +00:00
* @ public
2014-10-29 05:21:47 +00:00
* @ default
2013-12-18 04:40:10 +00:00
* /
this . scrollFactorX = 1 ;
/ * *
2014-10-25 00:08:20 +00:00
* Speed at which this layer scrolls vertically , relative to the camera ( e . g . scrollFactorY of 0.5 scrolls half as quickly as the 'normal' camera - locked layers do )
2014-10-29 05:21:47 +00:00
* @ property { number } scrollFactorY
2014-10-25 00:08:20 +00:00
* @ public
2014-10-29 05:21:47 +00:00
* @ default
2013-12-18 04:40:10 +00:00
* /
this . scrollFactorY = 1 ;
/ * *
2014-10-25 00:08:20 +00:00
* If true tiles will be force rendered , even if such is not believed to be required .
2014-10-29 05:21:47 +00:00
* @ property { boolean } dirty
2014-10-25 00:08:20 +00:00
* @ protected
2013-12-18 04:40:10 +00:00
* /
2013-12-19 03:49:28 +00:00
this . dirty = true ;
2013-12-18 04:40:10 +00:00
2014-03-14 02:33:58 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* When ray - casting against tiles this is the number of steps it will jump . For larger tile sizes you can increase this to improve performance .
2014-10-29 05:21:47 +00:00
* @ property { integer } rayStepRate
2014-03-14 02:33:58 +00:00
* @ default
* /
this . rayStepRate = 4 ;
2014-06-10 23:15:02 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Flag controlling if the layer tiles wrap at the edges .
2014-10-29 05:21:47 +00:00
* @ property { boolean } _wrap
2014-10-25 00:08:20 +00:00
* @ private
2014-06-10 23:15:02 +00:00
* /
2014-10-25 00:08:20 +00:00
this . _wrap = false ;
2014-06-10 23:15:02 +00:00
2013-12-18 04:40:10 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Local map data and calculation cache .
2014-10-29 05:21:47 +00:00
* @ property { object } _mc
2014-03-23 07:59:28 +00:00
* @ private
2013-10-11 05:30:28 +00:00
* /
2014-03-24 01:39:09 +00:00
this . _mc = {
2014-03-23 10:35:31 +00:00
2014-10-25 00:08:20 +00:00
// Used to bypass rendering without reliance on `dirty` and detect changes.
scrollX : 0 ,
scrollY : 0 ,
renderWidth : 0 ,
renderHeight : 0 ,
tileWidth : tilemap . tileWidth ,
tileHeight : tilemap . tileHeight ,
// Collision width/height (pixels)
// What purpose do these have? Most things use tile width/height directly.
// This also only extends collisions right and down.
2014-03-23 10:35:31 +00:00
cw : tilemap . tileWidth ,
ch : tilemap . tileHeight ,
2014-10-25 00:08:20 +00:00
// Cached tilesets from index -> Tileset
tilesets : [ ]
2014-03-23 10:35:31 +00:00
} ;
2013-10-11 05:30:28 +00:00
2013-11-27 16:33:49 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* The current canvas left after scroll is applied .
2014-10-29 05:21:47 +00:00
* @ property { number } _scrollX
2014-03-23 07:59:28 +00:00
* @ private
2013-11-27 16:33:49 +00:00
* /
2014-10-25 00:08:20 +00:00
this . _scrollX = 0 ;
2013-11-27 16:33:49 +00:00
2014-10-25 00:08:20 +00:00
/ * *
* The current canvas top after scroll is applied .
2014-10-29 05:21:47 +00:00
* @ propety { number } _scrollY
2014-10-25 00:08:20 +00:00
* @ private
* /
this . _scrollY = 0 ;
/ * *
* Used for caching the tiles / array of tiles .
2014-10-29 05:21:47 +00:00
* @ property { Phaser . Tile [ ] } _results
2014-10-25 00:08:20 +00:00
* @ private
* /
this . _results = [ ] ;
2013-10-14 18:37:44 +00:00
2014-12-31 17:04:55 +00:00
if ( ! game . device . canvasBitBltShift )
{
this . renderSettings . copyCanvas = Phaser . TilemapLayer . ensureSharedCopyCanvas ( ) ;
}
} ;
/ * *
* The shared double - copy canvas , created as needed .
*
* @ private
* @ static
* /
Phaser . TilemapLayer . sharedCopyCanvas = null ;
/ * *
* Create if needed ( and return ) a shared copy canvas that is shared across all TilemapLayers .
*
* Code that uses the canvas is responsible to ensure the dimensions and save / restore state as appropriate .
*
* @ protected
* @ static
* /
Phaser . TilemapLayer . ensureSharedCopyCanvas = function ( ) {
if ( ! this . sharedCopyCanvas )
{
this . sharedCopyCanvas = Phaser . Canvas . create ( 2 , 2 ) ;
}
return this . sharedCopyCanvas ;
2013-09-11 01:57:36 +00:00
} ;
2014-03-03 05:19:46 +00:00
Phaser . TilemapLayer . prototype = Object . create ( Phaser . Image . prototype ) ;
2013-10-16 05:33:39 +00:00
Phaser . TilemapLayer . prototype . constructor = Phaser . TilemapLayer ;
2013-09-11 01:57:36 +00:00
2014-10-25 00:08:20 +00:00
/ * *
* If no valid tileset / image can be found for a tile , the tile is rendered as a rectangle using this as a fill value .
*
* Set to ` null ` to disable rendering anything for tiles without value tileset images .
*
2014-10-29 05:21:47 +00:00
* @ property { ? string } tileColor
2014-10-25 00:08:20 +00:00
* @ memberof Phaser . TilemapLayer
* @ default 'rgb(255, 255, 255)'
* @ deprecated Use ` debugSettings.missingImageFill ` instead .
* /
Object . defineProperty ( Phaser . TilemapLayer . prototype , 'tileColor' , {
get : function ( ) {
return this . debugSettings . missingImageFill ;
} ,
set : function ( value ) {
this . debugSettings . missingImageFill = value ;
}
} ) ;
/ * *
* Automatically called by World . postUpdate . Handles cache updates .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # postUpdate
2014-10-25 00:08:20 +00:00
* @ protected
2013-11-27 16:33:49 +00:00
* /
2013-12-01 01:40:24 +00:00
Phaser . TilemapLayer . prototype . postUpdate = function ( ) {
2013-10-16 01:09:12 +00:00
2014-03-23 07:59:28 +00:00
Phaser . Image . prototype . postUpdate . call ( this ) ;
2013-12-06 04:34:27 +00:00
// Stops you being able to auto-scroll the camera if it's not following a sprite
2014-10-25 00:08:20 +00:00
var camera = this . game . camera ;
2015-02-07 18:48:44 +00:00
this . scrollX = camera . x * this . scrollFactorX / this . scale . x ;
this . scrollY = camera . y * this . scrollFactorY / this . scale . y ;
2013-10-16 01:09:12 +00:00
2013-10-16 05:33:39 +00:00
this . render ( ) ;
2013-10-16 01:09:12 +00:00
2014-03-21 18:04:24 +00:00
// Fixed to Camera?
2014-03-22 08:32:33 +00:00
if ( this . _cache [ 7 ] === 1 )
{
2014-10-25 00:08:20 +00:00
this . position . x = ( camera . view . x + this . cameraOffset . x ) / camera . scale . x ;
this . position . y = ( camera . view . y + this . cameraOffset . y ) / camera . scale . y ;
2014-03-22 08:32:33 +00:00
}
2014-03-21 18:04:24 +00:00
// Update any Children
// for (var i = 0, len = this.children.length; i < len; i++)
// {
// this.children[i].postUpdate();
// }
2014-03-23 06:31:02 +00:00
} ;
2013-10-16 01:09:12 +00:00
2013-11-27 16:33:49 +00:00
/ * *
* Sets the world size to match the size of this layer .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # resizeWorld
2014-10-25 00:08:20 +00:00
* @ public
2013-11-27 16:33:49 +00:00
* /
2013-10-16 05:33:39 +00:00
Phaser . TilemapLayer . prototype . resizeWorld = function ( ) {
2013-10-16 01:09:12 +00:00
2015-02-07 18:48:44 +00:00
this . game . world . setBounds ( 0 , 0 , this . layer . widthInPixels * this . scale . x , this . layer . heightInPixels * this . scale . y ) ;
2013-12-18 04:40:10 +00:00
2014-03-23 06:31:02 +00:00
} ;
2013-10-15 03:28:16 +00:00
2013-11-07 18:44:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Take an x coordinate that doesn 't account for scrollFactorX and ' fix ' it into a scrolled local space .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # _fixX
2013-11-27 16:33:49 +00:00
* @ private
* @ param { number } x - x coordinate in camera space
* @ return { number } x coordinate in scrollFactor - adjusted dimensions
* /
2014-10-25 00:08:20 +00:00
Phaser . TilemapLayer . prototype . _fixX = function ( x ) {
2013-11-13 20:57:09 +00:00
2013-12-09 16:40:48 +00:00
if ( x < 0 )
{
x = 0 ;
}
2013-11-25 04:40:04 +00:00
if ( this . scrollFactorX === 1 )
2013-11-13 20:57:09 +00:00
{
return x ;
}
2014-10-25 00:08:20 +00:00
return this . _scrollX + ( x - ( this . _scrollX / this . scrollFactorX ) ) ;
2013-11-13 20:57:09 +00:00
2014-03-23 06:31:02 +00:00
} ;
2013-11-13 20:57:09 +00:00
2013-11-07 18:44:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Take an x coordinate that _does _ account for scrollFactorX and 'unfix' it back to camera space .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # _unfixX
2013-11-27 16:33:49 +00:00
* @ private
* @ param { number } x - x coordinate in scrollFactor - adjusted dimensions
* @ return { number } x coordinate in camera space
* /
2014-10-25 00:08:20 +00:00
Phaser . TilemapLayer . prototype . _unfixX = function ( x ) {
2013-11-13 20:57:09 +00:00
2013-11-25 04:40:04 +00:00
if ( this . scrollFactorX === 1 )
2013-11-13 20:57:09 +00:00
{
return x ;
}
2014-10-25 00:08:20 +00:00
return ( this . _scrollX / this . scrollFactorX ) + ( x - this . _scrollX ) ;
2013-11-13 20:57:09 +00:00
2014-03-23 06:31:02 +00:00
} ;
2013-11-13 20:57:09 +00:00
2013-11-07 18:44:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Take a y coordinate that doesn 't account for scrollFactorY and ' fix ' it into a scrolled local space .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # _fixY
2013-11-27 16:33:49 +00:00
* @ private
* @ param { number } y - y coordinate in camera space
* @ return { number } y coordinate in scrollFactor - adjusted dimensions
* /
2014-10-25 00:08:20 +00:00
Phaser . TilemapLayer . prototype . _fixY = function ( y ) {
2013-11-13 20:57:09 +00:00
2013-12-09 16:40:48 +00:00
if ( y < 0 )
{
y = 0 ;
}
2013-11-25 04:40:04 +00:00
if ( this . scrollFactorY === 1 )
2013-11-13 20:57:09 +00:00
{
return y ;
}
2014-10-25 00:08:20 +00:00
return this . _scrollY + ( y - ( this . _scrollY / this . scrollFactorY ) ) ;
2013-11-13 20:57:09 +00:00
2014-03-23 06:31:02 +00:00
} ;
2013-11-13 20:57:09 +00:00
2013-11-07 18:44:04 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Take a y coordinate that _does _ account for scrollFactorY and 'unfix' it back to camera space .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # _unfixY
2013-11-27 16:33:49 +00:00
* @ private
* @ param { number } y - y coordinate in scrollFactor - adjusted dimensions
* @ return { number } y coordinate in camera space
* /
2014-10-25 00:08:20 +00:00
Phaser . TilemapLayer . prototype . _unfixY = function ( y ) {
2013-11-13 20:57:09 +00:00
2013-11-25 04:40:04 +00:00
if ( this . scrollFactorY === 1 )
2013-11-13 20:57:09 +00:00
{
return y ;
}
2014-10-25 00:08:20 +00:00
return ( this . _scrollY / this . scrollFactorY ) + ( y - this . _scrollY ) ;
2013-11-13 20:57:09 +00:00
2014-03-23 06:31:02 +00:00
} ;
2013-11-07 18:44:04 +00:00
2013-10-16 20:25:51 +00:00
/ * *
* Convert a pixel value to a tile coordinate .
2014-10-25 00:08:20 +00:00
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # getTileX
2014-10-25 00:08:20 +00:00
* @ public
* @ param { number } x - X position of the point in target tile ( in pixels ) .
* @ return { integer } The X map location of the tile .
2013-10-16 20:25:51 +00:00
* /
Phaser . TilemapLayer . prototype . getTileX = function ( x ) {
2013-12-19 03:49:28 +00:00
// var tileWidth = this.tileWidth * this.scale.x;
2014-10-25 00:08:20 +00:00
return Math . floor ( this . _fixX ( x ) / this . _mc . tileWidth ) ;
2013-10-16 20:25:51 +00:00
2014-03-23 06:31:02 +00:00
} ;
2013-10-16 20:25:51 +00:00
/ * *
* Convert a pixel value to a tile coordinate .
2014-10-25 00:08:20 +00:00
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # getTileY
2014-10-25 00:08:20 +00:00
* @ public
* @ param { number } y - Y position of the point in target tile ( in pixels ) .
* @ return { integer } The Y map location of the tile .
2013-10-16 20:25:51 +00:00
* /
Phaser . TilemapLayer . prototype . getTileY = function ( y ) {
2013-12-19 03:49:28 +00:00
// var tileHeight = this.tileHeight * this.scale.y;
2014-10-25 00:08:20 +00:00
return Math . floor ( this . _fixY ( y ) / this . _mc . tileHeight ) ;
2013-10-16 20:25:51 +00:00
2014-03-23 06:31:02 +00:00
} ;
2013-10-16 20:25:51 +00:00
2013-11-27 16:33:49 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Convert a pixel coordinate to a tile coordinate .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # getTileXY
2014-10-25 00:08:20 +00:00
* @ public
* @ param { number } x - X position of the point in target tile ( in pixels ) .
* @ param { number } y - Y position of the point in target tile ( in pixels ) .
* @ param { ( Phaser . Point | object ) } point - The Point / object to update .
* @ return { ( Phaser . Point | object ) } A Point / object with its ` x ` and ` y ` properties set .
2013-11-27 16:33:49 +00:00
* /
2013-10-16 20:25:51 +00:00
Phaser . TilemapLayer . prototype . getTileXY = function ( x , y , point ) {
point . x = this . getTileX ( x ) ;
point . y = this . getTileY ( y ) ;
return point ;
2014-03-23 06:31:02 +00:00
} ;
2013-10-16 20:25:51 +00:00
2014-03-14 00:19:45 +00:00
/ * *
* Gets all tiles that intersect with the given line .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # getRayCastTiles
2014-10-25 00:08:20 +00:00
* @ public
2014-03-14 00:19:45 +00:00
* @ param { Phaser . Line } line - The line used to determine which tiles to return .
2014-10-25 00:08:20 +00:00
* @ param { integer } [ stepRate = ( rayStepRate ) ] - How many steps through the ray will we check ? Defaults to ` rayStepRate ` .
* @ param { boolean } [ collides = false ] - If true , _only _ return tiles that collide on one or more faces .
* @ param { boolean } [ interestingFace = false ] - If true , _only _ return tiles that have interesting faces .
* @ return { Phaser . Tile [ ] } An array of Phaser . Tiles .
2014-03-14 00:19:45 +00:00
* /
2014-03-14 02:33:58 +00:00
Phaser . TilemapLayer . prototype . getRayCastTiles = function ( line , stepRate , collides , interestingFace ) {
2014-03-06 06:27:16 +00:00
2014-10-25 00:08:20 +00:00
if ( ! stepRate ) { stepRate = this . rayStepRate ; }
2014-03-14 02:33:58 +00:00
if ( typeof collides === 'undefined' ) { collides = false ; }
if ( typeof interestingFace === 'undefined' ) { interestingFace = false ; }
// First get all tiles that touch the bounds of the line
var tiles = this . getTiles ( line . x , line . y , line . width , line . height , collides , interestingFace ) ;
2014-03-06 06:27:16 +00:00
2014-03-14 02:33:58 +00:00
if ( tiles . length === 0 )
{
return [ ] ;
}
2014-03-06 06:27:16 +00:00
2014-03-14 02:33:58 +00:00
// Now we only want the tiles that intersect with the points on this line
var coords = line . coordinatesOnLine ( stepRate ) ;
var results = [ ] ;
for ( var i = 0 ; i < tiles . length ; i ++ )
{
2014-10-25 00:08:20 +00:00
for ( var t = 0 ; t < coords . length ; t ++ )
2014-03-14 02:33:58 +00:00
{
2014-10-25 00:08:20 +00:00
var tile = tiles [ i ] ;
var coord = coords [ t ] ;
if ( tile . containsPoint ( coord [ 0 ] , coord [ 1 ] ) )
2014-03-14 02:33:58 +00:00
{
2014-10-25 00:08:20 +00:00
results . push ( tile ) ;
2014-03-14 02:33:58 +00:00
break ;
}
}
}
return results ;
2014-03-06 06:27:16 +00:00
2014-03-23 06:31:02 +00:00
} ;
2014-03-06 06:27:16 +00:00
2013-10-16 05:33:39 +00:00
/ * *
2013-12-17 05:07:00 +00:00
* Get all tiles that exist within the given area , defined by the top - left corner , width and height . Values given are in pixels , not tiles .
2014-10-25 00:08:20 +00:00
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # getTiles
2014-10-25 00:08:20 +00:00
* @ public
* @ param { number } x - X position of the top left corner ( in pixels ) .
* @ param { number } y - Y position of the top left corner ( in pixels ) .
* @ param { number } width - Width of the area to get ( in pixels ) .
* @ param { number } height - Height of the area to get ( in pixels ) .
* @ param { boolean } [ collides = false ] - If true , _only _ return tiles that collide on one or more faces .
* @ param { boolean } [ interestingFace = false ] - If true , _only _ return tiles that have interesting faces .
* @ return { array < Phaser . Tile > } An array of Tiles .
2013-10-16 05:33:39 +00:00
* /
2014-03-13 22:49:08 +00:00
Phaser . TilemapLayer . prototype . getTiles = function ( x , y , width , height , collides , interestingFace ) {
2013-12-17 05:07:00 +00:00
// 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 ; }
2014-03-13 22:49:08 +00:00
if ( typeof interestingFace === 'undefined' ) { interestingFace = false ; }
2013-12-17 05:07:00 +00:00
2014-10-25 00:08:20 +00:00
var fetchAll = ! ( collides || interestingFace ) ;
// Adjust the x,y coordinates for scrollFactor
2013-12-17 05:07:00 +00:00
x = this . _fixX ( x ) ;
y = this . _fixY ( y ) ;
// Convert the pixel values into tile coordinates
2015-02-07 18:48:44 +00:00
var tx = Math . floor ( x / ( this . _mc . cw * this . scale . x ) ) ;
var ty = Math . floor ( y / ( this . _mc . ch * this . scale . y ) ) ;
2014-10-25 00:08:20 +00:00
// Don't just use ceil(width/cw) to allow account for x/y diff within cell
2015-02-07 18:48:44 +00:00
var tw = Math . ceil ( ( x + width ) / ( this . _mc . cw * this . scale . x ) ) - tx ;
var th = Math . ceil ( ( y + height ) / ( this . _mc . ch * this . scale . y ) ) - ty ;
2013-12-17 05:07:00 +00:00
2014-10-25 00:08:20 +00:00
while ( this . _results . length )
{
this . _results . pop ( ) ;
}
2013-10-16 05:33:39 +00:00
2014-10-25 00:08:20 +00:00
for ( var wy = ty ; wy < ty + th ; wy ++ )
2013-10-16 05:33:39 +00:00
{
2014-10-25 00:08:20 +00:00
for ( var wx = tx ; wx < tx + tw ; wx ++ )
2013-10-15 03:28:16 +00:00
{
2014-10-25 00:08:20 +00:00
var row = this . layer . data [ wy ] ;
if ( row && row [ wx ] )
2013-10-15 03:28:16 +00:00
{
2014-10-25 00:08:20 +00:00
if ( fetchAll || row [ wx ] . isInteresting ( collides , interestingFace ) )
2013-10-15 03:28:16 +00:00
{
2014-10-25 00:08:20 +00:00
this . _results . push ( row [ wx ] ) ;
2013-10-15 03:28:16 +00:00
}
}
}
2013-10-16 05:33:39 +00:00
}
2013-10-15 03:28:16 +00:00
2015-02-03 20:58:45 +00:00
return this . _results . slice ( ) ;
2013-10-14 18:37:44 +00:00
2014-03-23 06:31:02 +00:00
} ;
2013-10-11 05:30:28 +00:00
2013-11-27 16:33:49 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Flag controlling if the layer tiles wrap at the edges . Only works if the World size matches the Map size .
*
2014-10-29 05:21:47 +00:00
* @ property { boolean } wrap
2013-11-27 16:33:49 +00:00
* @ memberof Phaser . TilemapLayer
2014-10-25 00:08:20 +00:00
* @ public
* @ default false
2013-11-27 16:33:49 +00:00
* /
2014-10-25 00:08:20 +00:00
Object . defineProperty ( Phaser . TilemapLayer . prototype , "wrap" , {
2013-10-11 05:30:28 +00:00
2014-10-25 00:08:20 +00:00
get : function ( ) {
return this . _wrap ;
} ,
2013-10-11 05:30:28 +00:00
2014-10-25 00:08:20 +00:00
set : function ( value ) {
this . _wrap = value ;
this . dirty = true ;
}
2013-10-11 05:30:28 +00:00
2014-10-25 00:08:20 +00:00
} ) ;
2013-10-11 05:30:28 +00:00
2013-11-27 16:33:49 +00:00
/ * *
2014-12-31 17:04:55 +00:00
* Returns the appropriate tileset for the index , updating the internal cache as required .
* This should only be called if ` tilesets[index] ` evaluates to undefined .
2014-10-25 00:08:20 +00:00
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # resolveTileset
2014-10-25 00:08:20 +00:00
* @ private
* @ param { integer } Tile index
* @ return { Phaser . Tileset | null } Returns the associated tileset or null if there is no such mapping .
2013-11-27 16:33:49 +00:00
* /
2014-10-25 00:08:20 +00:00
Phaser . TilemapLayer . prototype . resolveTileset = function ( tileIndex )
{
var tilesets = this . _mc . tilesets ;
// Try for dense array if reasonable
if ( tileIndex < 2000 ) {
while ( tilesets . length < tileIndex ) {
tilesets . push ( undefined ) ;
}
2014-01-31 05:42:20 +00:00
}
2013-10-16 20:25:51 +00:00
2014-10-25 00:08:20 +00:00
var setIndex = this . map . tiles [ tileIndex ] && this . map . tiles [ tileIndex ] [ 2 ] ;
if ( setIndex != null ) // number: not null or undefined
2014-01-31 05:42:20 +00:00
{
2014-10-25 00:08:20 +00:00
var tileset = this . map . tilesets [ setIndex ] ;
if ( tileset && tileset . containsTileIndex ( tileIndex ) )
{
return ( tilesets [ tileIndex ] = tileset ) ;
}
2014-01-31 05:42:20 +00:00
}
2013-10-11 05:30:28 +00:00
2014-10-25 00:08:20 +00:00
return ( tilesets [ tileIndex ] = null ) ;
2013-10-11 05:30:28 +00:00
2014-10-25 00:08:20 +00:00
} ;
2013-10-11 05:30:28 +00:00
2014-10-25 00:08:20 +00:00
/ * *
2014-12-31 17:04:55 +00:00
* The TilemapLayer caches tileset look - ups .
*
* Call this method of clear the cache if tilesets have been added or updated after the layer has been rendered .
2014-10-25 00:08:20 +00:00
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # resetTilesetCache
2014-10-25 00:08:20 +00:00
* @ public
* /
Phaser . TilemapLayer . prototype . resetTilesetCache = function ( )
{
2013-10-11 05:30:28 +00:00
2014-10-25 00:08:20 +00:00
var tilesets = this . _mc . tilesets ;
while ( tilesets . length ) {
tilesets . pop ( ) ;
}
} ;
2014-01-31 03:32:12 +00:00
2015-02-07 18:48:44 +00:00
/ * *
* This method will set the scale of the tilemap as well as update the underlying block data of this layer
*
* @ method Phaser . TilemapLayer # setScale
* @ param { number } [ xScale = 1 ] - The scale factor along the X - plane of this tilemap
* @ param { number } [ yScale ] - The scale factor along the Y - plane of this tilemap
* /
Phaser . TilemapLayer . prototype . setScale = function ( xScale , yScale ) {
xScale = xScale || 1 ;
yScale = yScale || xScale ;
for ( var y = 0 ; y < this . layer . data . length ; y ++ ) {
var row = this . layer . data [ y ] ;
for ( var x = 0 ; x < row . length ; x ++ ) {
var tile = row [ x ] ;
tile . width = this . map . tileWidth * xScale ;
tile . height = this . map . tileHeight * yScale ;
tile . worldX = tile . x * tile . width ;
tile . worldY = tile . y * tile . height ;
}
}
this . scale . setTo ( xScale , yScale ) ;
} ;
2014-10-25 00:08:20 +00:00
/ * *
2014-12-12 08:24:52 +00:00
* Shifts the contents of the canvas - does extra math so that different browsers agree on the result .
2014-12-31 17:04:55 +00:00
*
2014-12-12 08:24:52 +00:00
* The specified ( x / y ) will be shifted to ( 0 , 0 ) after the copy and the newly exposed canvas area will need to be filled in .
2014-10-25 00:08:20 +00:00
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # shiftCanvas
2014-10-25 00:08:20 +00:00
* @ private
* @ param { CanvasRenderingContext2D } context - The context to shift
* @ param { integer } x
* @ param { integer } y
* /
2014-12-31 18:06:48 +00:00
Phaser . TilemapLayer . prototype . shiftCanvas = function ( context , x , y )
2014-10-25 00:08:20 +00:00
{
2013-12-19 03:49:28 +00:00
2014-10-25 00:08:20 +00:00
var canvas = context . canvas ;
var copyW = canvas . width - Math . abs ( x ) ;
var copyH = canvas . height - Math . abs ( y ) ;
2013-12-17 05:07:00 +00:00
2014-10-25 00:08:20 +00:00
// When x/y non-negative
var dx = 0 ;
var dy = 0 ;
var sx = x ;
var sy = y ;
if ( x < 0 )
2013-12-17 05:07:00 +00:00
{
2014-10-25 00:08:20 +00:00
dx = - x ;
sx = 0 ;
2013-12-17 05:07:00 +00:00
}
2013-10-11 17:18:27 +00:00
2014-10-25 00:08:20 +00:00
if ( y < 0 )
2014-03-03 05:19:46 +00:00
{
2014-10-25 00:08:20 +00:00
dy = - y ;
sy = 0 ;
}
2014-03-03 05:19:46 +00:00
2014-12-31 18:06:48 +00:00
var copyCanvas = this . renderSettings . copyCanvas ;
2015-01-25 02:37:28 +00:00
if ( copyCanvas )
2015-01-25 02:31:07 +00:00
{
// Use a second copy buffer, without slice support, for Safari .. again.
// Ensure copy canvas is large enough
if ( copyCanvas . width < copyW || copyCanvas . height < copyH )
{
copyCanvas . width = copyW ;
copyCanvas . height = copyH ;
}
var copyContext = copyCanvas . getContext ( '2d' ) ;
copyContext . clearRect ( 0 , 0 , copyW , copyH ) ;
copyContext . drawImage ( canvas , dx , dy , copyW , copyH , 0 , 0 , copyW , copyH ) ;
// clear allows default 'source-over' semantics
context . clearRect ( sx , sy , copyW , copyH ) ;
context . drawImage ( copyCanvas , 0 , 0 , copyW , copyH , sx , sy , copyW , copyH ) ;
}
else
2014-12-12 08:24:52 +00:00
{
2015-01-25 02:37:28 +00:00
// Avoids a second copy but flickers in Safari / Safari Mobile
// Ref. https://github.com/photonstorm/phaser/issues/1439
context . save ( ) ;
context . globalCompositeOperation = 'copy' ;
context . drawImage ( canvas , dx , dy , copyW , copyH , sx , sy , copyW , copyH ) ;
context . restore ( ) ;
2014-12-31 17:04:55 +00:00
}
2015-01-25 02:37:28 +00:00
2014-10-25 00:08:20 +00:00
} ;
/ * *
* Render tiles in the given area given by the virtual tile coordinates biased by the given scroll factor .
* This will constrain the tile coordinates based on wrapping but not physical coordinates .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # renderRegion
2014-10-25 00:08:20 +00:00
* @ private
* @ param { integer } scrollX - Render x offset / scroll .
* @ param { integer } scrollY - Render y offset / scroll .
* @ param { integer } left - Leftmost column to render .
* @ param { integer } top - Topmost row to render .
* @ param { integer } right - Rightmost column to render .
* @ param { integer } bottom - Bottommost row to render .
* /
Phaser . TilemapLayer . prototype . renderRegion = function ( scrollX , scrollY , left , top , right , bottom ) {
var context = this . context ;
var width = this . layer . width ;
var height = this . layer . height ;
var tw = this . _mc . tileWidth ;
var th = this . _mc . tileHeight ;
var tilesets = this . _mc . tilesets ;
var lastAlpha = NaN ;
if ( ! this . _wrap )
{
if ( left <= right ) // Only adjust if going to render
2014-05-27 21:51:44 +00:00
{
2014-10-25 00:08:20 +00:00
left = Math . max ( 0 , left ) ;
right = Math . min ( width - 1 , right ) ;
2014-05-27 21:51:44 +00:00
}
2014-10-25 00:08:20 +00:00
if ( top <= bottom )
2014-05-27 21:51:44 +00:00
{
2014-10-25 00:08:20 +00:00
top = Math . max ( 0 , top ) ;
bottom = Math . min ( height - 1 , bottom ) ;
2014-05-27 21:51:44 +00:00
}
2014-10-25 00:08:20 +00:00
}
// top-left pixel of top-left cell
var baseX = ( left * tw ) - scrollX ;
var baseY = ( top * th ) - scrollY ;
2014-05-27 21:51:44 +00:00
2014-10-25 00:08:20 +00:00
// Fix normStartX/normStartY such it is normalized [0..width/height). This allows a simple conditional and decrement to always keep in range [0..width/height) during the loop. The major offset bias is to take care of negative values.
var normStartX = ( left + ( ( 1 << 20 ) * width ) ) % width ;
var normStartY = ( top + ( ( 1 << 20 ) * height ) ) % height ;
2014-05-27 21:51:44 +00:00
2014-10-25 00:08:20 +00:00
// tx/ty - are pixel coordinates where tile is drawn
// x/y - is cell location, normalized [0..width/height) in loop
// xmax/ymax - remaining cells to render on column/row
var tx , ty , x , y , xmax , ymax ;
2014-03-03 05:19:46 +00:00
2014-10-25 00:08:20 +00:00
context . fillStyle = this . tileColor ;
2014-03-03 05:19:46 +00:00
2014-10-25 00:08:20 +00:00
for ( y = normStartY , ymax = bottom - top , ty = baseY ;
ymax >= 0 ;
y ++ , ymax -- , ty += th )
{
2014-03-03 05:19:46 +00:00
2014-10-25 00:08:20 +00:00
if ( y >= height ) { y -= height ; }
2014-04-28 01:42:38 +00:00
2014-10-25 00:08:20 +00:00
var row = this . layer . data [ y ] ;
for ( x = normStartX , xmax = right - left , tx = baseX ;
xmax >= 0 ;
x ++ , xmax -- , tx += tw )
{
if ( x >= width ) { x -= width ; }
2014-03-03 05:19:46 +00:00
2014-10-25 00:08:20 +00:00
var tile = row [ x ] ;
if ( ! tile || tile . index < 0 )
{
continue ;
}
var index = tile . index ;
2014-05-27 21:51:44 +00:00
2014-10-25 00:08:20 +00:00
var set = tilesets [ index ] ;
if ( set === undefined )
{
set = this . resolveTileset ( index ) ;
2014-05-27 21:51:44 +00:00
}
2014-03-03 05:19:46 +00:00
2014-10-25 00:08:20 +00:00
// Setting the globalAlpha is "surprisingly expensive" in Chrome (38)
if ( tile . alpha !== lastAlpha && ! this . debug )
{
context . globalAlpha = tile . alpha ;
lastAlpha = tile . alpha ;
}
if ( set )
{
set . draw ( context , tx , ty , index ) ;
}
else if ( this . debugSettings . missingImageFill )
{
context . fillStyle = this . debugSettings . missingImageFill ;
context . fillRect ( tx , ty , tw , th ) ;
}
if ( tile . debug && this . debugSettings . debuggedTileOverfill )
{
context . fillStyle = this . debugSettings . debuggedTileOverfill ;
context . fillRect ( tx , ty , tw , th ) ;
}
2014-03-03 05:19:46 +00:00
}
2014-10-25 00:08:20 +00:00
}
} ;
/ * *
* Shifts the canvas and render damaged edge tiles .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # renderDeltaScroll
2014-10-25 00:08:20 +00:00
* @ private
* /
Phaser . TilemapLayer . prototype . renderDeltaScroll = function ( shiftX , shiftY ) {
var scrollX = this . _mc . scrollX ;
var scrollY = this . _mc . scrollY ;
var renderW = this . canvas . width ;
var renderH = this . canvas . height ;
var tw = this . _mc . tileWidth ;
var th = this . _mc . tileHeight ;
// Only cells with coordinates in the "plus" formed by `left <= x <= right` OR `top <= y <= bottom` are drawn. These coordinates may be outside the layer bounds.
// Start in pixels
var left = 0 ;
var right = - tw ;
var top = 0 ;
var bottom = - th ;
if ( shiftX < 0 ) // layer moving left, damage right
{
left = renderW + shiftX ; // shiftX neg.
right = renderW - 1 ;
}
else if ( shiftX > 0 )
{
// left -> 0
right = shiftX ;
}
if ( shiftY < 0 ) // layer moving down, damage top
{
top = renderH + shiftY ; // shiftY neg.
bottom = renderH - 1 ;
}
else if ( shiftY > 0 )
{
// top -> 0
bottom = shiftY ;
}
2014-12-31 18:06:48 +00:00
this . shiftCanvas ( this . context , shiftX , shiftY ) ;
2014-10-25 00:08:20 +00:00
// Transform into tile-space
left = Math . floor ( ( left + scrollX ) / tw ) ;
right = Math . floor ( ( right + scrollX ) / tw ) ;
top = Math . floor ( ( top + scrollY ) / th ) ;
bottom = Math . floor ( ( bottom + scrollY ) / th ) ;
if ( left <= right )
{
// Clear left or right edge
this . context . clearRect ( ( ( left * tw ) - scrollX ) , 0 , ( right - left + 1 ) * tw , renderH ) ;
var trueTop = Math . floor ( ( 0 + scrollY ) / th ) ;
2015-01-25 01:49:20 +00:00
var trueBottom = Math . floor ( ( renderH - 1 + scrollY ) / th ) ;
2014-10-25 00:08:20 +00:00
this . renderRegion ( scrollX , scrollY , left , trueTop , right , trueBottom ) ;
}
if ( top <= bottom )
{
// Clear top or bottom edge
this . context . clearRect ( 0 , ( ( top * th ) - scrollY ) , renderW , ( bottom - top + 1 ) * th ) ;
var trueLeft = Math . floor ( ( 0 + scrollX ) / tw ) ;
var trueRight = Math . floor ( ( renderW - 1 + scrollX ) / tw ) ;
this . renderRegion ( scrollX , scrollY , trueLeft , top , trueRight , bottom ) ;
}
} ;
/ * *
* Clear and render the entire canvas .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # renderFull
2014-10-25 00:08:20 +00:00
* @ private
* /
Phaser . TilemapLayer . prototype . renderFull = function ( )
{
var scrollX = this . _mc . scrollX ;
var scrollY = this . _mc . scrollY ;
var renderW = this . canvas . width ;
var renderH = this . canvas . height ;
var tw = this . _mc . tileWidth ;
var th = this . _mc . tileHeight ;
var left = Math . floor ( scrollX / tw ) ;
var right = Math . floor ( ( renderW - 1 + scrollX ) / tw ) ;
var top = Math . floor ( scrollY / th ) ;
var bottom = Math . floor ( ( renderH - 1 + scrollY ) / th ) ;
this . context . clearRect ( 0 , 0 , renderW , renderH ) ;
this . renderRegion ( scrollX , scrollY , left , top , right , bottom ) ;
} ;
/ * *
* Renders the tiles to the layer canvas and pushes to the display .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # render
2014-10-25 00:08:20 +00:00
* @ protected
* /
Phaser . TilemapLayer . prototype . render = function ( ) {
var redrawAll = false ;
if ( ! this . visible )
{
return ;
}
if ( this . dirty || this . layer . dirty )
{
this . layer . dirty = false ;
redrawAll = true ;
}
var renderWidth = this . canvas . width ; // Use Sprite.width/height?
var renderHeight = this . canvas . height ;
// Scrolling bias; whole pixels only
var scrollX = this . _scrollX | 0 ;
var scrollY = this . _scrollY | 0 ;
var mc = this . _mc ;
var shiftX = mc . scrollX - scrollX ; // Negative when scrolling right/down
var shiftY = mc . scrollY - scrollY ;
if ( ! redrawAll &&
shiftX === 0 && shiftY === 0 &&
mc . renderWidth === renderWidth && mc . renderHeight === renderHeight )
{
// No reason to redraw map, looking at same thing and not invalidated.
return ;
}
mc . scrollX = scrollX ;
mc . scrollY = scrollY ;
if ( mc . renderWidth !== renderWidth || mc . renderHeight !== renderHeight )
{
// Could support automatic canvas resizing
mc . renderWidth = renderWidth ;
mc . renderHeight = renderHeight ;
}
if ( this . debug )
{
this . context . globalAlpha = this . debugSettings . debugAlpha ;
if ( this . debugSettings . forceFullRedraw )
{
redrawAll = true ;
}
}
2014-03-03 05:19:46 +00:00
2014-10-25 00:08:20 +00:00
if ( ! redrawAll &&
this . renderSettings . enableScrollDelta &&
( Math . abs ( shiftX ) + Math . abs ( shiftY ) ) < Math . min ( renderWidth , renderHeight ) )
{
this . renderDeltaScroll ( shiftX , shiftY ) ;
}
else
{
// Too much change or otherwise requires full render
this . renderFull ( ) ;
2014-03-03 05:19:46 +00:00
}
if ( this . debug )
{
this . context . globalAlpha = 1 ;
this . renderDebug ( ) ;
}
2014-10-25 09:09:04 +00:00
this . baseTexture . dirty ( ) ;
2013-10-11 17:18:27 +00:00
2013-10-16 05:33:39 +00:00
this . dirty = false ;
2013-10-16 20:25:51 +00:00
2013-10-16 05:33:39 +00:00
return true ;
2014-03-23 06:31:02 +00:00
} ;
2013-10-11 05:30:28 +00:00
2013-12-18 04:40:10 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Renders a debug overlay on - top of the canvas . Called automatically by render when ` debug ` is true .
*
* See ` debugSettings ` for assorted configuration options .
*
2014-10-30 09:00:20 +00:00
* @ method Phaser . TilemapLayer # renderDebug
2014-10-25 00:08:20 +00:00
* @ private
2013-12-18 04:40:10 +00:00
* /
2013-12-18 00:44:04 +00:00
Phaser . TilemapLayer . prototype . renderDebug = function ( ) {
2014-10-25 00:08:20 +00:00
var scrollX = this . _mc . scrollX ;
var scrollY = this . _mc . scrollY ;
var context = this . context ;
var renderW = this . canvas . width ;
var renderH = this . canvas . height ;
2013-12-18 00:44:04 +00:00
2014-10-25 00:08:20 +00:00
var width = this . layer . width ;
var height = this . layer . height ;
var tw = this . _mc . tileWidth ;
var th = this . _mc . tileHeight ;
2013-12-18 00:44:04 +00:00
2014-10-25 00:08:20 +00:00
var left = Math . floor ( scrollX / tw ) ;
var right = Math . floor ( ( renderW - 1 + scrollX ) / tw ) ;
var top = Math . floor ( scrollY / th ) ;
var bottom = Math . floor ( ( renderH - 1 + scrollY ) / th ) ;
var baseX = ( left * tw ) - scrollX ;
var baseY = ( top * th ) - scrollY ;
var normStartX = ( left + ( ( 1 << 20 ) * width ) ) % width ;
var normStartY = ( top + ( ( 1 << 20 ) * height ) ) % height ;
var tx , ty , x , y , xmax , ymax ;
context . strokeStyle = this . debugSettings . facingEdgeStroke ;
for ( y = normStartY , ymax = bottom - top , ty = baseY ;
ymax >= 0 ;
y ++ , ymax -- , ty += th )
2013-12-18 00:44:04 +00:00
{
2014-10-25 00:08:20 +00:00
if ( y >= height ) { y -= height ; }
var row = this . layer . data [ y ] ;
2013-12-18 00:44:04 +00:00
2014-10-25 00:08:20 +00:00
for ( x = normStartX , xmax = right - left , tx = baseX ;
xmax >= 0 ;
x ++ , xmax -- , tx += tw )
2014-05-27 21:51:44 +00:00
{
2014-10-25 00:08:20 +00:00
if ( x >= width ) { x -= width ; }
var tile = row [ x ] ;
if ( ! tile || tile . index < 0 || ! tile . collides )
2013-12-18 00:44:04 +00:00
{
2014-10-25 00:08:20 +00:00
continue ;
}
2013-12-18 00:44:04 +00:00
2014-10-25 00:08:20 +00:00
if ( this . debugSettings . collidingTileOverfill )
{
context . fillStyle = this . debugSettings . collidingTileOverfill ;
context . fillRect ( tx , ty , this . _mc . cw , this . _mc . ch ) ;
}
if ( this . debugSettings . facingEdgeStroke )
{
context . beginPath ( ) ;
if ( tile . faceTop )
2013-12-18 04:40:10 +00:00
{
2014-10-25 00:08:20 +00:00
context . moveTo ( tx , ty ) ;
context . lineTo ( tx + this . _mc . cw , ty ) ;
2013-12-18 04:40:10 +00:00
}
2014-10-25 00:08:20 +00:00
if ( tile . faceBottom )
2013-12-18 00:44:04 +00:00
{
2014-10-25 00:08:20 +00:00
context . moveTo ( tx , ty + this . _mc . ch ) ;
context . lineTo ( tx + this . _mc . cw , ty + this . _mc . ch ) ;
2013-12-18 00:44:04 +00:00
}
2014-10-25 00:08:20 +00:00
if ( tile . faceLeft )
2013-12-18 00:44:04 +00:00
{
2014-10-25 00:08:20 +00:00
context . moveTo ( tx , ty ) ;
context . lineTo ( tx , ty + this . _mc . ch ) ;
2013-12-18 00:44:04 +00:00
}
2014-10-25 00:08:20 +00:00
if ( tile . faceRight )
2013-12-18 00:44:04 +00:00
{
2014-10-25 00:08:20 +00:00
context . moveTo ( tx + this . _mc . cw , ty ) ;
context . lineTo ( tx + this . _mc . cw , ty + this . _mc . ch ) ;
2014-05-27 21:51:44 +00:00
}
2013-12-18 00:44:04 +00:00
2014-10-25 00:08:20 +00:00
context . stroke ( ) ;
2014-05-27 21:51:44 +00:00
}
2014-10-25 00:08:20 +00:00
2013-12-18 00:44:04 +00:00
}
}
2014-03-23 06:31:02 +00:00
} ;
2013-12-18 00:44:04 +00:00
2013-11-27 16:33:49 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Scrolls the map horizontally or returns the current x position .
*
2014-10-29 05:21:47 +00:00
* @ property { number } scrollX
2014-10-25 00:08:20 +00:00
* @ memberof Phaser . TilemapLayer
* @ public
2013-11-27 16:33:49 +00:00
* /
2013-10-16 05:33:39 +00:00
Object . defineProperty ( Phaser . TilemapLayer . prototype , "scrollX" , {
2014-03-23 07:59:28 +00:00
2013-10-11 17:18:27 +00:00
get : function ( ) {
2014-10-25 00:08:20 +00:00
return this . _scrollX ;
2013-10-11 17:18:27 +00:00
} ,
2013-09-11 01:57:36 +00:00
2013-10-11 17:18:27 +00:00
set : function ( value ) {
2014-10-25 00:08:20 +00:00
this . _scrollX = value ;
2013-09-11 01:57:36 +00:00
}
2013-10-11 17:18:27 +00:00
} ) ;
2013-11-27 16:33:49 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* Scrolls the map vertically or returns the current y position .
*
2014-10-29 05:21:47 +00:00
* @ property { number } scrollY
2014-10-25 00:08:20 +00:00
* @ memberof Phaser . TilemapLayer
* @ public
2013-11-27 16:33:49 +00:00
* /
2013-10-16 05:33:39 +00:00
Object . defineProperty ( Phaser . TilemapLayer . prototype , "scrollY" , {
2014-03-23 07:59:28 +00:00
2013-10-11 17:18:27 +00:00
get : function ( ) {
2014-10-25 00:08:20 +00:00
return this . _scrollY ;
2013-10-11 17:18:27 +00:00
} ,
set : function ( value ) {
2014-10-25 00:08:20 +00:00
this . _scrollY = value ;
2013-10-11 17:18:27 +00:00
}
2013-09-12 03:24:01 +00:00
2013-10-11 17:18:27 +00:00
} ) ;
2013-12-19 05:09:49 +00:00
/ * *
2014-10-25 00:08:20 +00:00
* The width of the collision tiles ( in pixels ) .
*
2014-10-29 05:21:47 +00:00
* @ property { integer } collisionWidth
2014-10-25 00:08:20 +00:00
* @ memberof Phaser . TilemapLayer
* @ public
2013-12-19 05:09:49 +00:00
* /
Object . defineProperty ( Phaser . TilemapLayer . prototype , "collisionWidth" , {
2014-03-23 07:59:28 +00:00
2013-12-19 05:09:49 +00:00
get : function ( ) {
2014-03-24 01:39:09 +00:00
return this . _mc . cw ;
2013-12-19 05:09:49 +00:00
} ,
set : function ( value ) {
2014-10-29 05:21:47 +00:00
this . _mc . cw = value | 0 ;
2013-12-19 05:09:49 +00:00
this . dirty = true ;
}
} ) ;
/ * *
2014-10-25 00:08:20 +00:00
* The height of the collision tiles ( in pixels ) .
*
2014-10-29 05:21:47 +00:00
* @ property { integer } collisionHeight
2014-10-25 00:08:20 +00:00
* @ memberof Phaser . TilemapLayer
* @ public
2013-12-19 05:09:49 +00:00
* /
Object . defineProperty ( Phaser . TilemapLayer . prototype , "collisionHeight" , {
2014-03-23 07:59:28 +00:00
2013-12-19 05:09:49 +00:00
get : function ( ) {
2014-03-24 01:39:09 +00:00
return this . _mc . ch ;
2013-12-19 05:09:49 +00:00
} ,
set : function ( value ) {
2014-10-29 05:21:47 +00:00
this . _mc . ch = value | 0 ;
2013-12-19 05:09:49 +00:00
this . dirty = true ;
}
} ) ;