@@ -9,6 +9,7 @@ var GetFastValue = require('../../utils/object/GetFastValue');
99var Set = require ( '../../structs/Set' ) ;
1010var Solver = require ( './Solver' ) ;
1111var TYPE = require ( './TYPE' ) ;
12+ var TILEMAP_FORMATS = require ( '../../gameobjects/tilemap/Formats' ) ;
1213
1314var World = new Class ( {
1415
@@ -60,7 +61,7 @@ var World = new Class({
6061 * @property {object } walls - An object containing the 4 wall bodies that bound the physics world.
6162 */
6263 this . walls = { left : null , right : null , top : null , bottom : null } ;
63-
64+
6465 this . delta = 0 ;
6566
6667 this . _lastId = 0 ;
@@ -95,9 +96,52 @@ var World = new Class({
9596 }
9697 } ,
9798
98- setCollisionMap : function ( tilesize , data )
99+ /**
100+ * Sets the collision map for the world either from a Weltmeister JSON level in the cache or from
101+ * a 2D array. If loading from a Weltmeister level, the map must have a layer called "collision".
102+ *
103+ * @param {string|integer[][] } key - Either a string key that corresponds to a Weltmeister level
104+ * in the cache, or a 2D array of collision IDs.
105+ * @param {integer } tileSize - The size of a tile. This is optional if loading from a Weltmeister
106+ * level in the cache.
107+ * @return {CollisionMap|null } The newly created CollisionMap, or null if the method failed to
108+ * create the CollisionMap.
109+ */
110+ setCollisionMap : function ( key , tileSize )
99111 {
100- this . collisionMap = new CollisionMap ( tilesize , data ) ;
112+ if ( typeof key === 'string' )
113+ {
114+ var tilemapData = this . scene . cache . tilemap . get ( key ) ;
115+
116+ if ( ! tilemapData || tilemapData . format !== TILEMAP_FORMATS . WELTMEISTER )
117+ {
118+ console . warn ( 'The specified key does not correspond to a Weltmeister tilemap: ' + key ) ;
119+ return null ;
120+ }
121+
122+ var layers = tilemapData . data . layer ;
123+ var collisionLayer ;
124+ for ( var i = 0 ; i < layers . length ; i ++ )
125+ {
126+ if ( layers [ i ] . name === 'collision' )
127+ {
128+ collisionLayer = layers [ i ] ;
129+ break ;
130+ }
131+ }
132+
133+ if ( tileSize === undefined ) { tileSize = collisionLayer . tilesize ; }
134+
135+ this . collisionMap = new CollisionMap ( tileSize , collisionLayer . data ) ;
136+ }
137+ else if ( Array . isArray ( key ) )
138+ {
139+ this . collisionMap = new CollisionMap ( tileSize , key ) ;
140+ }
141+ else
142+ {
143+ console . warn ( 'Invalid Weltmeister collision map data: ' + key ) ;
144+ }
101145
102146 return this . collisionMap ;
103147 } ,
@@ -334,21 +378,21 @@ var World = new Class({
334378 {
335379 bodyA . check ( bodyB ) ;
336380 }
337-
381+
338382 if ( bodyB . checkAgainst & bodyA . type )
339383 {
340384 bodyB . check ( bodyA ) ;
341385 }
342-
386+
343387 if ( bodyA . collides && bodyB . collides && bodyA . collides + bodyB . collides > COLLIDES . ACTIVE )
344388 {
345389 Solver ( this , bodyA , bodyB ) ;
346390 }
347391 } ,
348392
349- //////////////
393+ // ////////////
350394 // Helpers //
351- //////////////
395+ // ////////////
352396
353397 setCollidesNever : function ( bodies )
354398 {
0 commit comments