Skip to content

Commit 747018b

Browse files
committed
Adding loader and file types for CSV and JSON tilemaps
1 parent 2c75c0d commit 747018b

4 files changed

Lines changed: 109 additions & 2 deletions

File tree

v3/src/loader/const.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ var FILE_CONST = {
1717
FILE_DESTROYED: 13, // file has been destroyed
1818

1919
TEXTURE_ATLAS_JSON_ARRAY: 20,
20-
TEXTURE_ATLAS_JSON_HASH: 21
20+
TEXTURE_ATLAS_JSON_HASH: 21,
21+
22+
TILEMAP_CSV: 30,
23+
TILEMAP_JSON: 31
2124

2225
};
2326

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var Class = require('../../utils/Class');
2+
var CONST = require('../const');
3+
var File = require('../File');
4+
5+
// Phaser.Loader.FileTypes.TilemapCSVFile
6+
7+
var TilemapCSVFile = new Class({
8+
9+
Extends: File,
10+
11+
initialize:
12+
13+
function TextFile (key, url, path, xhrSettings)
14+
{
15+
var fileConfig = {
16+
type: 'tilemapCSV',
17+
extension: '.csv',
18+
responseType: 'text',
19+
key: key,
20+
url: url,
21+
path: path,
22+
xhrSettings: xhrSettings
23+
};
24+
25+
File.call(this, fileConfig);
26+
},
27+
28+
onProcess: function (callback)
29+
{
30+
this.state = CONST.FILE_PROCESSING;
31+
32+
this.data = this.xhrLoader.responseText;
33+
34+
this.onComplete();
35+
36+
callback(this);
37+
}
38+
39+
});
40+
41+
TilemapCSVFile.create = function (loader, key, url, xhrSettings)
42+
{
43+
if (Array.isArray(key))
44+
{
45+
for (var i = 0; i < key.length; i++)
46+
{
47+
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
48+
loader.addFile(new TilemapCSVFile(key[i], url, loader.path, xhrSettings));
49+
}
50+
}
51+
else
52+
{
53+
loader.addFile(new TilemapCSVFile(key, url, loader.path, xhrSettings));
54+
}
55+
56+
// For method chaining
57+
return loader;
58+
};
59+
60+
module.exports = TilemapCSVFile;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var JSONFile = require('./JSONFile.js');
2+
3+
var TilemapJSONFile = function (key, url, path, xhrSettings)
4+
{
5+
var json = new JSONFile(key, url, path, xhrSettings);
6+
7+
// Override the File type
8+
json.type = 'tilemapJSON';
9+
10+
return json;
11+
};
12+
13+
TilemapJSONFile.create = function (loader, key, url, xhrSettings)
14+
{
15+
if (Array.isArray(key))
16+
{
17+
for (var i = 0; i < key.length; i++)
18+
{
19+
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
20+
loader.addFile(TilemapJSONFile(key[i], url, loader.path, xhrSettings));
21+
}
22+
}
23+
else
24+
{
25+
loader.addFile(TilemapJSONFile(key, url, loader.path, xhrSettings));
26+
}
27+
28+
// For method chaining
29+
return loader;
30+
};
31+
32+
module.exports = TilemapJSONFile;

v3/src/scene/plugins/Loader.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ var TextFile = require('../../loader/filetypes/TextFile');
1717
var UnityAtlasFile = require('../../loader/filetypes/UnityAtlasFile');
1818
var XMLFile = require('../../loader/filetypes/XMLFile');
1919
var AudioFile = require('../../loader/filetypes/AudioFile');
20+
var TilemapCSVFile = require('../../loader/filetypes/TilemapCSVFile');
21+
var TilemapJSONFile = require('../../loader/filetypes/TilemapJSONFile');
2022

2123
var Loader = new Class({
2224

@@ -92,7 +94,17 @@ var Loader = new Class({
9294
// config can include: instances
9395
audio: function (key, urls, config, xhrSettings)
9496
{
95-
return AudioFile.create(this, key, urls, config, xhrSettings)
97+
return AudioFile.create(this, key, urls, config, xhrSettings);
98+
},
99+
100+
tilemapCSV: function (key, url, xhrSettings)
101+
{
102+
return TilemapCSVFile.create(this, key, url, xhrSettings);
103+
},
104+
105+
tilemapJSON: function (key, url, xhrSettings)
106+
{
107+
return TilemapJSONFile.create(this, key, url, xhrSettings);
96108
},
97109

98110
// ---------------------------------------------------

0 commit comments

Comments
 (0)