@@ -2,7 +2,6 @@ var Class = require('../../../utils/Class');
22var GameObject = require ( '../../GameObject' ) ;
33var Components = require ( '../../components' ) ;
44var DynamicTilemapLayerRender = require ( './DynamicTilemapLayerRender' ) ;
5- var Tile = require ( './Tile' ) ;
65
76var DynamicTilemapLayer = new Class ( {
87
@@ -35,7 +34,6 @@ var DynamicTilemapLayer = new Class({
3534 this . layer = tilemap . layers [ layerIndex ] ;
3635 this . tileset = tileset ;
3736
38- this . tileArray = [ ] ;
3937 this . culledTiles = [ ] ;
4038
4139 this . setTexture ( tileset . image . key ) ;
@@ -45,8 +43,6 @@ var DynamicTilemapLayer = new Class({
4543 this . setSize ( this . map . tileWidth * this . layer . width , this . map . tileheight * this . layer . height ) ;
4644
4745 this . skipIndexZero = false ;
48-
49- this . buildTilemap ( ) ;
5046 } ,
5147
5248 getTotalTileCount : function ( )
@@ -59,85 +55,50 @@ var DynamicTilemapLayer = new Class({
5955 return this . cull ( camera ) . length ;
6056 } ,
6157
62- buildTilemap : function ( )
58+ cull : function ( camera )
6359 {
64- var tileArray = this . tileArray ;
6560 var mapData = this . layer . data ;
66- var tileWidth = this . map . tileWidth ;
67- var tileHeight = this . map . tileHeight ;
68- var tileset = this . tileset ;
69- var width = this . texture . source [ 0 ] . width ;
70- var height = this . texture . source [ 0 ] . height ;
7161 var mapWidth = this . layer . width ;
7262 var mapHeight = this . layer . height ;
73-
74- tileArray . length = 0 ;
75-
76- for ( var row = 0 ; row < mapHeight ; ++ row )
77- {
78- for ( var col = 0 ; col < mapWidth ; ++ col )
79- {
80- var tileIndex = mapData [ row ] [ col ] . index ;
81- if ( tileIndex <= 0 && this . skipIndexZero ) { continue ; }
82-
83- var texCoords = tileset . getTileTextureCoordinates ( tileIndex ) ;
84- if ( texCoords === null ) { continue ; }
85-
86- tileArray . push ( new Tile ( {
87- index : tileIndex ,
88- id : tileIndex ,
89- x : col * tileWidth ,
90- y : row * tileHeight ,
91- width : tileWidth ,
92- height : tileHeight ,
93- frameX : texCoords . x ,
94- frameY : texCoords . y ,
95- frameWidth : tileWidth ,
96- frameHeight : tileHeight ,
97- textureWidth : width ,
98- textureHeight : height
99- } ) ) ;
100- }
101- }
102- } ,
103-
104- cull : function ( camera )
105- {
10663 var culledTiles = this . culledTiles ;
107- var tiles = this . tileArray ;
108- var length = tiles . length ;
10964 var scrollX = camera . scrollX * this . scrollFactorX ;
11065 var scrollY = camera . scrollY * this . scrollFactorY ;
11166 var cameraW = camera . width ;
11267 var cameraH = camera . height ;
11368
11469 culledTiles . length = 0 ;
11570
116- for ( var index = 0 ; index < length ; ++ index )
71+ for ( var row = 0 ; row < mapHeight ; ++ row )
11772 {
118- var tile = tiles [ index ] ;
119- var tileX = tile . x - scrollX ;
120- var tileY = tile . y - scrollY ;
121- var tileW = tile . width ;
122- var tileH = tile . height ;
123- var cullW = cameraW + tileW ;
124- var cullH = cameraH + tileH ;
125-
126- if ( tile . visible &&
127- tileX > - tileW && tileY > - tileH &&
128- tileX < cullW && tileY < cullH )
73+ for ( var col = 0 ; col < mapWidth ; ++ col )
12974 {
130- culledTiles . push ( tile ) ;
75+ var tile = mapData [ row ] [ col ] ;
76+
77+ if ( tile === null || ( tile . index <= 0 && this . skipIndexZero ) ) { continue ; }
78+
79+ var tileX = tile . worldX - scrollX ;
80+ var tileY = tile . worldY - scrollY ;
81+ var tileW = tile . width ;
82+ var tileH = tile . height ;
83+ var cullW = cameraW + tileW ;
84+ var cullH = cameraH + tileH ;
85+
86+ if ( tile . visible &&
87+ tileX > - tileW && tileY > - tileH &&
88+ tileX < cullW && tileY < cullH )
89+ {
90+ culledTiles . push ( tile ) ;
91+ }
13192 }
13293 }
13394
13495 return culledTiles ;
135- } ,
96+ }
13697
137- forEach : function ( callback )
138- {
139- this . tileArray . forEach ( callback ) ;
140- } ,
98+ // forEach: function (callback)
99+ // {
100+ // this.tileArray.forEach(callback);
101+ // },
141102
142103 // Returns Object containing:
143104 // {
@@ -157,32 +118,32 @@ var DynamicTilemapLayer = new Class({
157118 // y
158119 // }
159120
160- getTileAt : function ( x , y )
161- {
162- var ix = ( x | 0 ) ;
163- var iy = ( y | 0 ) ;
164- var tiles = this . tileArray ;
165- var index = iy * this . mapWidth + ix ;
121+ // getTileAt: function (x, y)
122+ // {
123+ // var ix = (x|0);
124+ // var iy = (y|0);
125+ // var tiles = this.tileArray;
126+ // var index = iy * this.mapWidth + ix;
166127
167- if ( index < tiles . length )
168- {
169- return tiles [ index ] ;
170- }
128+ // if (index < tiles.length)
129+ // {
130+ // return tiles[index];
131+ // }
171132
172- return null ;
173- } ,
133+ // return null;
134+ // },
174135
175- getTileAtIndex : function ( index )
176- {
177- var tiles = this . tileArray ;
136+ // getTileAtIndex: function (index)
137+ // {
138+ // var tiles = this.tileArray;
178139
179- if ( index < tiles . length )
180- {
181- return tiles [ index ] ;
182- }
140+ // if (index < tiles.length)
141+ // {
142+ // return tiles[index];
143+ // }
183144
184- return null ;
185- }
145+ // return null;
146+ // }
186147
187148} ) ;
188149
0 commit comments