@theatrejs/loader-aseprite 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -82,17 +82,18 @@ npm install @theatrejs/loader-aseprite --save-dev
82
82
 
83
83
  ```javascript
84
84
  import {Stage} from '@theatrejs/theatrejs';
85
- import * as PLUGINASEPRITE from '@theatrejs/plugin-aseprite';
85
+ import * as PLUGIN_ASEPRITE from '@theatrejs/plugin-aseprite';
86
86
 
87
+ import * as ANIMATIONS_HERO from './hero-16x16.animations.js';
87
88
  import asepriteHero from './hero-16x16.aseprite';
88
89
 
89
90
  class Level1 extends Stage {
90
91
  onCreate() {
91
92
  this.createActor(
92
- PLUGINASEPRITE.FACTORIES.ActorWithSpritesheet({
93
+ PLUGIN_ASEPRITE.FACTORIES.ActorWithSpritesheet({
93
94
  $aseprite: asepriteHero,
94
95
  $loop: true,
95
- $tag: 'idle'
96
+ $tag: ANIMATIONS_HERO.IDLE
96
97
  })
97
98
  );
98
99
  }
@@ -103,17 +104,18 @@ class Level1 extends Stage {
103
104
 
104
105
  ```javascript
105
106
  import {FACTORIES} from '@theatrejs/theatrejs';
106
- import * as PLUGINASEPRITE from '@theatrejs/plugin-aseprite';
107
+ import * as PLUGIN_ASEPRITE from '@theatrejs/plugin-aseprite';
107
108
 
109
+ import * as ANIMATIONS_HERO from './hero-16x16.animations.js';
108
110
  import asepriteHero from './hero-16x16.aseprite';
109
111
 
110
- class Level1 extends FACTORIES.StageWithPreloadables([PLUGINASEPRITE.FACTORIES.PreloadableAseprite(asepriteHero)]) {
112
+ class Level1 extends FACTORIES.StageWithPreloadables([PLUGIN_ASEPRITE.FACTORIES.PreloadableAseprite(asepriteHero)]) {
111
113
  onCreate() {
112
114
  this.createActor(
113
- PLUGINASEPRITE.FACTORIES.ActorWithSpritesheet({
115
+ PLUGIN_ASEPRITE.FACTORIES.ActorWithSpritesheet({
114
116
  $aseprite: asepriteHero,
115
117
  $loop: true,
116
- $tag: 'idle'
118
+ $tag: ANIMATIONS_HERO.IDLE
117
119
  })
118
120
  );
119
121
  }
package/package.json CHANGED
@@ -57,5 +57,5 @@
57
57
  "postversion": "node ./tools/custom/postversion.cjs"
58
58
  },
59
59
  "type": "commonjs",
60
- "version": "1.6.1"
60
+ "version": "1.7.0"
61
61
  }
@@ -29,6 +29,59 @@ module.exports = function loader() {
29
29
  * @private
30
30
  */
31
31
 
32
+ /**
33
+ * @typedef {Object} TypeAseprite An Aseprite JSON data.
34
+ * @property {Array<TypeAsepriteFrame>} TypeAseprite.frames The Aseprite JSON frames data.
35
+ * @property {TypeAsepriteMeta} TypeAseprite.meta The Aseprite JSON meta data.
36
+ * @private
37
+ */
38
+
39
+ /**
40
+ * @typedef {Object} TypeAsepriteFrame An Aseprite JSON frame data.
41
+ * @property {number} TypeAsepriteFrame.duration The duration.
42
+ * @property {string} TypeAsepriteFrame.filename The file name.
43
+ * @property {Object} TypeAsepriteFrame.frame The frame.
44
+ * @property {number} TypeAsepriteFrame.frame.x The x position of the frame.
45
+ * @property {number} TypeAsepriteFrame.frame.y The y position of the frame.
46
+ * @property {number} TypeAsepriteFrame.frame.w The width of the frame.
47
+ * @property {number} TypeAsepriteFrame.frame.h The height of the frame.
48
+ * @property {boolean} TypeAsepriteFrame.rotated The rotated status.
49
+ * @property {Object} TypeAsepriteFrame.spriteSourceSize The sprite source size.
50
+ * @property {number} TypeAsepriteFrame.spriteSourceSize.x The x position of the sprite source.
51
+ * @property {number} TypeAsepriteFrame.spriteSourceSize.y The y position of the sprite source.
52
+ * @property {number} TypeAsepriteFrame.spriteSourceSize.w The width of the sprite source.
53
+ * @property {number} TypeAsepriteFrame.spriteSourceSize.h The height of the sprite source.
54
+ * @property {Object} TypeAsepriteFrame.sourceSize The sprite size.
55
+ * @property {number} TypeAsepriteFrame.sourceSize.w The width of the source.
56
+ * @property {number} TypeAsepriteFrame.sourceSize.h The height of the source.
57
+ * @property {boolean} TypeAsepriteFrame.trimmed The trimmed status.
58
+ * @private
59
+ */
60
+
61
+ /**
62
+ * @typedef {Object} TypeAsepriteMeta An Aseprite JSON meta data.
63
+ * @property {string} TypeAsepriteMeta.app The app meta data.
64
+ * @property {string} TypeAsepriteMeta.format The format meta data.
65
+ * @property {string} TypeAsepriteMeta.image The image meta data.
66
+ * @property {string} TypeAsepriteMeta.scale The scale meta data.
67
+ * @property {Object} TypeAsepriteMeta.size The size meta data.
68
+ * @property {number} TypeAsepriteMeta.size.w The size width meta data.
69
+ * @property {number} TypeAsepriteMeta.size.h The size height meta data.
70
+ * @property {string} TypeAsepriteMeta.version The version meta data.
71
+ * @property {Array<TypeAsepriteFrameTag>} TypeAsepriteMeta.frameTags The Aseprite JSON tags meta data.
72
+ * @private
73
+ */
74
+
75
+ /**
76
+ * @typedef {Object} TypeAsepriteFrameTag An Aseprite JSON tag meta data.
77
+ * @property {string} TypeAsepriteFrameTag.name The name.
78
+ * @property {number} TypeAsepriteFrameTag.from The first frame.
79
+ * @property {number} TypeAsepriteFrameTag.to The last frame.
80
+ * @property {string} TypeAsepriteFrameTag.direction The animation direction.
81
+ * @property {string} TypeAsepriteFrameTag.color The color.
82
+ * @private
83
+ */
84
+
32
85
  const context = /** @type {webpack.LoaderContext<TypeOptions>} */(this);
33
86
 
34
87
  const file = context.resourcePath;
@@ -53,11 +106,14 @@ module.exports = function loader() {
53
106
  const filename = path.basename(file, '.aseprite');
54
107
  const sourceTexture = filename + '.png';
55
108
  const sourceData = filename + '.json';
109
+ const sourceAnimations = filename + '.animations.js';
56
110
 
57
111
  if (fs.existsSync(path.resolve(location, sourceTexture)) === false
58
112
  || fs.existsSync(path.resolve(location, sourceData)) === false
113
+ || fs.existsSync(path.resolve(location, sourceAnimations)) === false
59
114
  || fs.statSync(path.resolve(location, sourceTexture)).mtime < fs.statSync(file).mtime
60
- || fs.statSync(path.resolve(location, sourceData)).mtime < fs.statSync(file).mtime) {
115
+ || fs.statSync(path.resolve(location, sourceData)).mtime < fs.statSync(file).mtime
116
+ || fs.statSync(path.resolve(location, sourceAnimations)).mtime < fs.statSync(file).mtime) {
61
117
 
62
118
  if (typeof aseprite === 'undefined') {
63
119
 
@@ -85,7 +141,7 @@ module.exports = function loader() {
85
141
  ' --data "' + sourceData + '"' +
86
142
  ' --list-tags' +
87
143
  ' --format json-array' +
88
- ' --filename-format {tag}#{tagframe001}@{title}.{extension}'
144
+ ' --filename-format frame-{frame001}@{title}.{extension}'
89
145
  );
90
146
 
91
147
  if (typeof processing !== 'undefined'
@@ -132,6 +188,43 @@ module.exports = function loader() {
132
188
  const bufferTarget = pngjs.write(image);
133
189
  fs.writeFileSync(path.resolve(location, sourceTexture), bufferTarget);
134
190
  }
191
+
192
+ const json = /** @type {TypeAseprite} */(JSON.parse(fs.readFileSync(path.resolve(location, sourceData), 'utf-8')));
193
+ const tags = json.meta.frameTags
194
+ .map(($tag) => ($tag.name))
195
+ .sort();
196
+
197
+ const content = tags.length > 0
198
+ ? (
199
+ '/**\n' +
200
+ ' * @typedef {(' + tags.map(($tag) => ($tag.toUpperCase().replace(/-/g, '_'))).join(' | ') + ')} TypeAnimation An animation.\n' +
201
+ ' */\n' +
202
+ '\n' +
203
+ tags.map(($tag) => (
204
+
205
+ '/**\n' +
206
+ ' * The \'' + $tag.toUpperCase().replace(/-/g, '_') + '\' animation.\n' +
207
+ ' * @type {\'' + $tag + '\'}\n' +
208
+ ' * @constant\n' +
209
+ ' */\n' +
210
+ 'const ' + $tag.toUpperCase().replace(/-/g, '_') + ' = \'' + $tag + '\';\n'
211
+
212
+ )).join('\n') + '\n' +
213
+ 'export {\n' +
214
+ '\n' +
215
+ tags.map(($tag) => (
216
+
217
+ ' ' + $tag.toUpperCase().replace(/-/g, '_'))
218
+
219
+ ).join(',' + '\n') + '\n' +
220
+ '};\n'
221
+ )
222
+ : (
223
+
224
+ 'export {};\n'
225
+ );
226
+
227
+ fs.writeFileSync(path.resolve(location, sourceAnimations), content, 'utf-8');
135
228
  }
136
229
 
137
230
  return (