@@ -217,6 +217,52 @@ Phaser.Loader.prototype = {
217217
218218 } ,
219219
220+ /**
221+ * Add a new tilemap loading request.
222+ * @param key {string} Unique asset key of the tilemap data.
223+ * @param tilesetURL {string} The url of the tile set image file.
224+ * @param [mapDataURL] {string} The url of the map data file (csv/json)
225+ * @param [mapData] {object} An optional JSON data object (can be given in place of a URL).
226+ * @param [format] {string} The format of the map data.
227+ */
228+ tilemap : function ( key , tilesetURL , mapDataURL , mapData , format ) {
229+
230+ if ( typeof mapDataURL === "undefined" ) { mapDataURL = null ; }
231+ if ( typeof mapData === "undefined" ) { mapData = null ; }
232+ if ( typeof format === "undefined" ) { format = Phaser . Tilemap . CSV ; }
233+
234+ if ( this . checkKeyExists ( key ) === false )
235+ {
236+ // A URL to a json/csv file has been given
237+ if ( mapDataURL )
238+ {
239+ this . addToFileList ( 'tilemap' , key , tilesetURL , { mapDataURL : mapDataURL , format : format } ) ;
240+ }
241+ else
242+ {
243+ switch ( format )
244+ {
245+ // A csv string or object has been given
246+ case Phaser . Tilemap . CSV :
247+ break ;
248+
249+ // An xml string or object has been given
250+ case Phaser . Tilemap . JSON :
251+
252+ if ( typeof mapData === 'string' )
253+ {
254+ mapData = JSON . parse ( mapData ) ;
255+ }
256+ break ;
257+ }
258+
259+ this . addToFileList ( 'tilemap' , key , tilesetURL , { mapDataURL : null , mapData : mapData , format : format } ) ;
260+
261+ }
262+ }
263+
264+ } ,
265+
220266 /**
221267 * Add a new bitmap font loading request.
222268 * @param key {string} Unique asset key of the bitmap font.
@@ -437,6 +483,7 @@ Phaser.Loader.prototype = {
437483 case 'spritesheet' :
438484 case 'textureatlas' :
439485 case 'bitmapfont' :
486+ case 'tilemap' :
440487 file . data = new Image ( ) ;
441488 file . data . name = file . key ;
442489 file . data . onload = function ( ) {
@@ -572,14 +619,50 @@ Phaser.Loader.prototype = {
572619 switch ( file . type )
573620 {
574621 case 'image' :
622+
575623 this . game . cache . addImage ( file . key , file . url , file . data ) ;
576624 break ;
577625
578626 case 'spritesheet' :
627+
579628 this . game . cache . addSpriteSheet ( file . key , file . url , file . data , file . frameWidth , file . frameHeight , file . frameMax ) ;
580629 break ;
581630
631+ case 'tilemap' :
632+
633+ if ( file . mapDataURL == null )
634+ {
635+ this . game . cache . addTilemap ( file . key , file . url , file . data , file . mapData , file . format ) ;
636+ }
637+ else
638+ {
639+ // Load the JSON or CSV before carrying on with the next file
640+ loadNext = false ;
641+ this . _xhr . open ( "GET" , this . baseURL + file . mapDataURL , true ) ;
642+ this . _xhr . responseType = "text" ;
643+
644+ if ( file . format == Phaser . Tilemap . JSON )
645+ {
646+ this . _xhr . onload = function ( ) {
647+ return _this . jsonLoadComplete ( file . key ) ;
648+ } ;
649+ }
650+ else if ( file . format == Phaser . Tilemap . CSV )
651+ {
652+ this . _xhr . onload = function ( ) {
653+ return _this . csvLoadComplete ( file . key ) ;
654+ } ;
655+ }
656+
657+ this . _xhr . onerror = function ( ) {
658+ return _this . dataLoadError ( file . key ) ;
659+ } ;
660+ this . _xhr . send ( ) ;
661+ }
662+ break ;
663+
582664 case 'textureatlas' :
665+
583666 if ( file . atlasURL == null )
584667 {
585668 this . game . cache . addTextureAtlas ( file . key , file . url , file . data , file . atlasData , file . format ) ;
@@ -612,6 +695,7 @@ Phaser.Loader.prototype = {
612695 break ;
613696
614697 case 'bitmapfont' :
698+
615699 if ( file . xmlURL == null )
616700 {
617701 this . game . cache . addBitmapFont ( file . key , file . url , file . data , file . xmlData ) ;
@@ -686,7 +770,29 @@ Phaser.Loader.prototype = {
686770 var data = JSON . parse ( this . _xhr . response ) ;
687771 var file = this . _fileList [ key ] ;
688772
689- this . game . cache . addTextureAtlas ( file . key , file . url , file . data , data , file . format ) ;
773+ if ( file . type == 'tilemap' )
774+ {
775+ this . game . cache . addTilemap ( file . key , file . url , file . data , data , file . format ) ;
776+ }
777+ else
778+ {
779+ this . game . cache . addTextureAtlas ( file . key , file . url , file . data , data , file . format ) ;
780+ }
781+
782+ this . nextFile ( key , true ) ;
783+
784+ } ,
785+
786+ /**
787+ * Successfully loaded a CSV file.
788+ * @param key {string} Key of the loaded CSV file.
789+ */
790+ csvLoadComplete : function ( key ) {
791+
792+ var data = this . _xhr . response ;
793+ var file = this . _fileList [ key ] ;
794+
795+ this . game . cache . addTilemap ( file . key , file . url , file . data , data , file . format ) ;
690796
691797 this . nextFile ( key , true ) ;
692798
0 commit comments