Skip to content

Commit 1c7b331

Browse files
committed
Merge pull request phaserjs#2050 from joshpmcghee/dev
Feature: Add support for loading single-layer Pyxel Edit TileMap as an Atlas
2 parents 31d0d39 + fb249b2 commit 1c7b331

3 files changed

Lines changed: 235 additions & 169 deletions

File tree

src/animation/AnimationParser.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,62 @@ Phaser.AnimationParser = {
144144

145145
},
146146

147+
/**
148+
* Parse the JSON data and extract the animation frame data from it.
149+
*
150+
* @method Phaser.AnimationParser.JSONDataPyxel
151+
* @param {Phaser.Game} game - A reference to the currently running game.
152+
* @param {object} json - The JSON data from the Texture Atlas. Must be in Pyxel JSON format.
153+
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
154+
*/
155+
JSONDataPyxel: function (game, json) {
156+
157+
// Malformed? There are a few keys to check here.
158+
var signature = ['layers', 'tilewidth','tileheight','tileswide', 'tileshigh'];
159+
160+
signature.forEach( function(key) {
161+
if (!json[key])
162+
{
163+
console.warn("Phaser.AnimationParser.JSONDataPyxel: Invalid Pyxel Tilemap JSON given, missing '" + key + "' key.");
164+
console.log(json);
165+
return;
166+
}
167+
});
168+
169+
// For this purpose, I only care about parsing tilemaps with a single layer.
170+
if(json['layers'].length != 1) {
171+
console.warn("Phaser.AnimationParser.JSONDataPyxel: Too many layers, this parser only supports flat Tilemaps.");
172+
console.log(json);
173+
return;
174+
}
175+
176+
var data = new Phaser.FrameData();
177+
178+
var tileheight = json['tileheight'];
179+
var tilewidth = json['tilewidth'];
180+
181+
var frames = json['layers'][0]['tiles'];
182+
var newFrame;
183+
184+
for (var i = 0; i < frames.length; i++)
185+
{
186+
newFrame = data.addFrame(new Phaser.Frame(
187+
i,
188+
frames[i].x,
189+
frames[i].y,
190+
tilewidth,
191+
tileheight,
192+
"frame_" + i // No names are included in pyxel tilemap data.
193+
));
194+
195+
// No trim data is included.
196+
newFrame.setTrim(false);
197+
}
198+
199+
return data;
200+
201+
},
202+
147203
/**
148204
* Parse the JSON data and extract the animation frame data from it.
149205
*
@@ -237,7 +293,7 @@ Phaser.AnimationParser = {
237293
for (var i = 0; i < frames.length; i++)
238294
{
239295
frame = frames[i].attributes;
240-
296+
241297
name = frame.name.value;
242298
x = parseInt(frame.x.value, 10);
243299
y = parseInt(frame.y.value, 10);

src/loader/Cache.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
/**
88
* Phaser has one single cache in which it stores all assets.
9-
*
9+
*
1010
* The cache is split up into sections, such as images, sounds, video, json, etc. All assets are stored using
1111
* a unique string-based key as their identifier. Assets stored in different areas of the cache can have the
1212
* same key, for example 'playerWalking' could be used as the key for both a sprite sheet and an audio file,
1313
* because they are unique data types.
14-
*
14+
*
1515
* The cache is automatically populated by the Phaser.Loader. When you use the loader to pull in external assets
1616
* such as images they are automatically placed into their respective cache. Most common Game Objects, such as
1717
* Sprites and Videos automatically query the cache to extract the assets they need on instantiation.
@@ -604,6 +604,10 @@ Phaser.Cache.prototype = {
604604
{
605605
obj.frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key);
606606
}
607+
else if (format === Phaser.Loader.TEXTURE_ATLAS_JSON_PYXEL)
608+
{
609+
obj.frameData = Phaser.AnimationParser.JSONDataPyxel(this.game, atlasData, key);
610+
}
607611
else
608612
{
609613
// Let's just work it out from the frames array
@@ -986,7 +990,7 @@ Phaser.Cache.prototype = {
986990

987991
/**
988992
* Get an item from a cache based on the given key and property.
989-
*
993+
*
990994
* This method is mostly used internally by other Cache methods such as `getImage` but is exposed
991995
* publicly for your own use as well.
992996
*
@@ -1017,16 +1021,16 @@ Phaser.Cache.prototype = {
10171021
return this._cacheMap[cache][key][property];
10181022
}
10191023
}
1020-
1024+
10211025
return null;
10221026

10231027
},
10241028

10251029
/**
10261030
* Gets a Canvas object from the cache.
1027-
*
1031+
*
10281032
* The object is looked-up based on the key given.
1029-
*
1033+
*
10301034
* Note: If the object cannot be found a `console.warn` message is displayed.
10311035
*
10321036
* @method Phaser.Cache#getCanvas
@@ -1041,13 +1045,13 @@ Phaser.Cache.prototype = {
10411045

10421046
/**
10431047
* Gets a Image object from the cache. This returns a DOM Image object, not a Phaser.Image object.
1044-
*
1048+
*
10451049
* The object is looked-up based on the key given.
1046-
*
1050+
*
10471051
* Note: If the object cannot be found a `console.warn` message is displayed.
1048-
*
1052+
*
10491053
* Only the Image cache is searched, which covers images loaded via Loader.image, Sprite Sheets and Texture Atlases.
1050-
*
1054+
*
10511055
* If you need the image used by a bitmap font or similar then please use those respective 'get' methods.
10521056
*
10531057
* @method Phaser.Cache#getImage
@@ -1084,7 +1088,7 @@ Phaser.Cache.prototype = {
10841088

10851089
/**
10861090
* Get a single texture frame by key.
1087-
*
1091+
*
10881092
* You'd only do this to get the default Frame created for a non-atlas / spritesheet image.
10891093
*
10901094
* @method Phaser.Cache#getTextureFrame
@@ -1099,9 +1103,9 @@ Phaser.Cache.prototype = {
10991103

11001104
/**
11011105
* Gets a Phaser.Sound object from the cache.
1102-
*
1106+
*
11031107
* The object is looked-up based on the key given.
1104-
*
1108+
*
11051109
* Note: If the object cannot be found a `console.warn` message is displayed.
11061110
*
11071111
* @method Phaser.Cache#getSound
@@ -1116,9 +1120,9 @@ Phaser.Cache.prototype = {
11161120

11171121
/**
11181122
* Gets a raw Sound data object from the cache.
1119-
*
1123+
*
11201124
* The object is looked-up based on the key given.
1121-
*
1125+
*
11221126
* Note: If the object cannot be found a `console.warn` message is displayed.
11231127
*
11241128
* @method Phaser.Cache#getSoundData
@@ -1133,9 +1137,9 @@ Phaser.Cache.prototype = {
11331137

11341138
/**
11351139
* Gets a Text object from the cache.
1136-
*
1140+
*
11371141
* The object is looked-up based on the key given.
1138-
*
1142+
*
11391143
* Note: If the object cannot be found a `console.warn` message is displayed.
11401144
*
11411145
* @method Phaser.Cache#getText
@@ -1150,11 +1154,11 @@ Phaser.Cache.prototype = {
11501154

11511155
/**
11521156
* Gets a Physics Data object from the cache.
1153-
*
1157+
*
11541158
* The object is looked-up based on the key given.
1155-
*
1159+
*
11561160
* Note: If the object cannot be found a `console.warn` message is displayed.
1157-
*
1161+
*
11581162
* You can get either the entire data set, a single object or a single fixture of an object from it.
11591163
*
11601164
* @method Phaser.Cache#getPhysicsData
@@ -1212,9 +1216,9 @@ Phaser.Cache.prototype = {
12121216

12131217
/**
12141218
* Gets a raw Tilemap data object from the cache. This will be in either CSV or JSON format.
1215-
*
1219+
*
12161220
* The object is looked-up based on the key given.
1217-
*
1221+
*
12181222
* Note: If the object cannot be found a `console.warn` message is displayed.
12191223
*
12201224
* @method Phaser.Cache#getTilemapData
@@ -1229,9 +1233,9 @@ Phaser.Cache.prototype = {
12291233

12301234
/**
12311235
* Gets a binary object from the cache.
1232-
*
1236+
*
12331237
* The object is looked-up based on the key given.
1234-
*
1238+
*
12351239
* Note: If the object cannot be found a `console.warn` message is displayed.
12361240
*
12371241
* @method Phaser.Cache#getBinary
@@ -1246,9 +1250,9 @@ Phaser.Cache.prototype = {
12461250

12471251
/**
12481252
* Gets a BitmapData object from the cache.
1249-
*
1253+
*
12501254
* The object is looked-up based on the key given.
1251-
*
1255+
*
12521256
* Note: If the object cannot be found a `console.warn` message is displayed.
12531257
*
12541258
* @method Phaser.Cache#getBitmapData
@@ -1263,9 +1267,9 @@ Phaser.Cache.prototype = {
12631267

12641268
/**
12651269
* Gets a Bitmap Font object from the cache.
1266-
*
1270+
*
12671271
* The object is looked-up based on the key given.
1268-
*
1272+
*
12691273
* Note: If the object cannot be found a `console.warn` message is displayed.
12701274
*
12711275
* @method Phaser.Cache#getBitmapFont
@@ -1280,11 +1284,11 @@ Phaser.Cache.prototype = {
12801284

12811285
/**
12821286
* Gets a JSON object from the cache.
1283-
*
1287+
*
12841288
* The object is looked-up based on the key given.
1285-
*
1289+
*
12861290
* Note: If the object cannot be found a `console.warn` message is displayed.
1287-
*
1291+
*
12881292
* You can either return the object by reference (the default), or return a clone
12891293
* of it by setting the `clone` argument to `true`.
12901294
*
@@ -1317,9 +1321,9 @@ Phaser.Cache.prototype = {
13171321

13181322
/**
13191323
* Gets an XML object from the cache.
1320-
*
1324+
*
13211325
* The object is looked-up based on the key given.
1322-
*
1326+
*
13231327
* Note: If the object cannot be found a `console.warn` message is displayed.
13241328
*
13251329
* @method Phaser.Cache#getXML
@@ -1334,9 +1338,9 @@ Phaser.Cache.prototype = {
13341338

13351339
/**
13361340
* Gets a Phaser.Video object from the cache.
1337-
*
1341+
*
13381342
* The object is looked-up based on the key given.
1339-
*
1343+
*
13401344
* Note: If the object cannot be found a `console.warn` message is displayed.
13411345
*
13421346
* @method Phaser.Cache#getVideo
@@ -1351,9 +1355,9 @@ Phaser.Cache.prototype = {
13511355

13521356
/**
13531357
* Gets a fragment shader object from the cache.
1354-
*
1358+
*
13551359
* The object is looked-up based on the key given.
1356-
*
1360+
*
13571361
* Note: If the object cannot be found a `console.warn` message is displayed.
13581362
*
13591363
* @method Phaser.Cache#getShader
@@ -1368,9 +1372,9 @@ Phaser.Cache.prototype = {
13681372

13691373
/**
13701374
* Gets a RenderTexture object from the cache.
1371-
*
1375+
*
13721376
* The object is looked-up based on the key given.
1373-
*
1377+
*
13741378
* Note: If the object cannot be found a `console.warn` message is displayed.
13751379
*
13761380
* @method Phaser.Cache#getRenderTexture
@@ -1444,9 +1448,9 @@ Phaser.Cache.prototype = {
14441448

14451449
/**
14461450
* Gets a Phaser.FrameData object from the Image Cache.
1447-
*
1451+
*
14481452
* The object is looked-up based on the key given.
1449-
*
1453+
*
14501454
* Note: If the object cannot be found a `console.warn` message is displayed.
14511455
*
14521456
* @method Phaser.Cache#getFrameData
@@ -1580,7 +1584,7 @@ Phaser.Cache.prototype = {
15801584

15811585
/**
15821586
* Gets a PIXI.BaseTexture by key from the PIXI.BaseTextureCache.
1583-
*
1587+
*
15841588
* If the texture isn't found in the cache, then it searches the Phaser Image Cache.
15851589
*
15861590
* @method Phaser.Cache#getPixiBaseTexture
@@ -1684,7 +1688,7 @@ Phaser.Cache.prototype = {
16841688

16851689
/**
16861690
* Removes an image from the cache.
1687-
*
1691+
*
16881692
* You can optionally elect to destroy it as well. This calls BaseTexture.destroy on it.
16891693
*
16901694
* Note that this only removes it from the Phaser and PIXI Caches. If you still have references to the data elsewhere

0 commit comments

Comments
 (0)