-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathGabeMZ_PictureSpritesheet.js
442 lines (411 loc) · 13.5 KB
/
GabeMZ_PictureSpritesheet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//============================================================================
// Gabe MZ - Picture Spritesheet
//----------------------------------------------------------------------------
// 14/09/20 | Version: 1.0.0 | Released
//----------------------------------------------------------------------------
// This software is released under the zlib License.
//============================================================================
/*:
* @target MZ
* @plugindesc [v1.0.0] Allows to transform the pictures into a animated spritesheets.
* @author Gabe (Gabriel Nascimento)
* @url http://patreon.com/gabriel_nfd
*
* @help Gabe MZ - Picture Spritesheet
* - This plugin is released under the zlib License.
*
* This plugin allows to turn pictures into a animated spritesheets.
* To use it just call the Plugin Commands.
*
* Plugin Commands:
* Single Spritesheet
* | This command allows to set a single picture to turn a spritesheet.
*
* Multi Spritesheet
* | This command allows to set multiple pictures to turn a spritesheet
* | each with its own spritesheet settings.
*
* Range of Spritesheet
* | This command allows to set a range of pictures to turn a spritesheet
* | all with the same spritesheet settings.
*
* Erase Spritesheet
* | This command allows to erase the spritesheet settings from one or
* | more pictures.
*
* Erase Range of Spritesheet
* | This command allows to erase the spritesheet settings from a range
* | of pictures.
*
* For support and new plugins join our Discord server:
* https://discord.gg/GG85QRz
*
* @command setSingleSpritesheet
* @text Single Spritesheet
* @desc Set a single picture to spritesheet.
*
* @arg spriteSheetSettings
* @text Spritesheet Settings
* @desc Set the spritesheet settings.
* @type struct<spriteSheetSettings>
*
* @command setMultiSpritesheet
* @text Multi Spritesheet
* @desc Set multi pictures to spritesheet.
*
* @arg spriteSheetSettings
* @text Spritesheet Settings
* @desc Set the spritesheet settings.
* @type struct<spriteSheetSettings>[]
*
* @command setRangeSpritesheet
* @text Range of Spritesheet
* @desc Set a range of pictures to spritesheet.
*
* @arg from
* @text From
* @desc The first picture ID.
* @type number
* @min 1
* @max 100
*
* @arg to
* @text To
* @desc The last picture ID.
* @type number
* @min 1
* @max 100
*
* @arg spriteSheetSettings
* @text Spritesheet Settings
* @desc Set the spritesheet settings.
* @type struct<singleSpriteSheetSettings>
*
* @command eraseSpritesheet
* @text Erase Spritesheet
* @desc Erase a spritesheet from picture.
*
* @arg id
* @text Picture ID
* @desc The picture ID.
* @type number[]
* @min 1
* @max 100
*
* @command eraseRangeSpritesheet
* @text Erase Range of Spritesheet
* @desc Erase a spritesheet from a range of pictures.
*
* @arg from
* @text From
* @desc The first picture ID.
* @type number
* @min 1
* @max 100
*
* @arg to
* @text To
* @desc The last picture ID.
* @type number
* @min 1
* @max 100
*/
/*~struct~spriteSheetSettings:
* @param id
* @text Picture ID
* @desc The picture ID.
* @type number
* @min 1
* @max 100
*
* @param width
* @text Width Frame
* @desc The spritesheet horizontal frame count.
* @type number
* @min 1
*
* @param height
* @text Height Frame
* @desc The spritesheet vertical frame count.
* @type number
* @min 1
*
* @param speed
* @text Speed
* @desc The spritesheet frame speed.
* @type number
* @min 1
*
* @param loop
* @text Loop
* @desc ON: The spritesheet will be looping.
* OFF: The spritesheet will run only once.
* @type boolean
*
* @param type
* @text Type
* @desc Normal: The spritesheet will run normally.
* InOut: The spritesheet will run in pingpong style.
* @type select
* @option Normal
* @value 0
* @option InOut
* @value 1
*/
/*~struct~singleSpriteSheetSettings:
* @param width
* @text Width Frame
* @desc The spritesheet horizontal frame count.
* @type number
* @min 1
*
* @param height
* @text Height Frame
* @desc The spritesheet vertical frame count.
* @type number
* @min 1
*
* @param speed
* @text Speed
* @desc The spritesheet frame speed.
* @type number
* @min 1
*
* @param loop
* @text Loop
* @desc ON: The spritesheet will be looping.
* OFF: The spritesheet will run only once.
* @type boolean
*
* @param type
* @text Type
* @desc Normal: The spritesheet will run normally.
* InOut: The spritesheet will run in pingpong style.
* @type select
* @option Normal
* @value 0
* @option InOut
* @value 1
*/
var GabeMZ = GabeMZ || {};
GabeMZ.PictureSpritesheet = GabeMZ.PictureSpritesheet || {};
GabeMZ.PictureSpritesheet.VERSION = [1, 0, 0];
(() => {
const pluginName = "GabeMZ_PictureSpritesheet";
GabeMZ.PictureSpritesheet.spriteSheetInfo = [];
//-----------------------------------------------------------------------------
// PluginManager
//
// The static class that manages the plugins.
PluginManager.registerCommand(pluginName, "setSingleSpritesheet", args => {
const settings = JSON.parse(args.spriteSheetSettings);
const id = parseInt(settings.id);
const w = parseInt(settings.width);
const h = parseInt(settings.height);
const f = w * h - 1;
const s = parseInt(settings.speed);
const l = JSON.parse(settings.loop);
const t = JSON.parse(settings.type);
GabeMZ.PictureSpritesheet.spriteSheetInfo[id] = {id: id, w: w, h: h, f: f, s: s, l: l, t: t};
if ($gameScreen.picture(id)) $gameScreen.picture(id).setSpriteSheet(w, h, f, s, l, t);
});
PluginManager.registerCommand(pluginName, "setMultiSpritesheet", args => {
const settings = JSON.parse(args.spriteSheetSettings);
settings.forEach(picture => {
picture = JSON.parse(picture)
const id = parseInt(picture.id);
const w = parseInt(picture.width);
const h = parseInt(picture.height);
const f = w * h - 1;
const s = parseInt(picture.speed);
const l = JSON.parse(picture.loop);
const t = JSON.parse(picture.type);
GabeMZ.PictureSpritesheet.spriteSheetInfo[id] = {id: id, w: w, h: h, f: f, s: s, l: l, t: t};
if ($gameScreen.picture(id)) $gameScreen.picture(id).setSpriteSheet(w, h, f, s, l, t);
});
});
PluginManager.registerCommand(pluginName, "setRangeSpritesheet", args => {
const settings = JSON.parse(args.spriteSheetSettings);
let from = parseInt(args.from);
const to = parseInt(args.to);
for (; from <= to; from++) {
const id = from;
const w = parseInt(settings.width);
const h = parseInt(settings.height);
const f = w * h - 1;
const s = parseInt(settings.speed);
const l = JSON.parse(settings.loop);
const t = JSON.parse(settings.type);
GabeMZ.PictureSpritesheet.spriteSheetInfo[id] = {id: id, w: w, h: h, f: f, s: s, l: l, t: t};
if ($gameScreen.picture(id)) $gameScreen.picture(id).setSpriteSheet(w, h, f, s, l, t);
};
});
PluginManager.registerCommand(pluginName, "eraseSpritesheet", args => {
const settings = JSON.parse(args.id);
settings.forEach(id => {
id = parseInt(id);
GabeMZ.PictureSpritesheet.spriteSheetInfo[id] = null;
if ($gameScreen.picture(id)) $gameScreen.picture(id).desetSpriteSheet();
})
});
PluginManager.registerCommand(pluginName, "eraseRangeSpritesheet", args => {
let from = parseInt(args.from);
const to = parseInt(args.to);
for (; from < to; from++) {
const id = from;
GabeMZ.PictureSpritesheet.spriteSheetInfo[id] = null;
if ($gameScreen.picture(id)) $gameScreen.picture(id).desetSpriteSheet();
}
});
//-----------------------------------------------------------------------------
// Game_Screen
//
// The game object class for screen effect data, such as changes in color tone
// and flashes.
const _Game_Screen_showPicture = Game_Screen.prototype.showPicture;
Game_Screen.prototype.showPicture = function(pictureId) {
_Game_Screen_showPicture.call(this, ...arguments);
const realPictureId = this.realPictureId(pictureId);
if (GabeMZ.PictureSpritesheet.spriteSheetInfo[realPictureId]) {
const w = GabeMZ.PictureSpritesheet.spriteSheetInfo[realPictureId].w;
const h = GabeMZ.PictureSpritesheet.spriteSheetInfo[realPictureId].h;
const f = GabeMZ.PictureSpritesheet.spriteSheetInfo[realPictureId].f;
const s = GabeMZ.PictureSpritesheet.spriteSheetInfo[realPictureId].s;
const l = GabeMZ.PictureSpritesheet.spriteSheetInfo[realPictureId].l;
const t = GabeMZ.PictureSpritesheet.spriteSheetInfo[realPictureId].t;
this._pictures[realPictureId].setSpriteSheet(w, h, f, s, l, t);
}
};
const _Game_Screen_erasePicture = Game_Screen.prototype.erasePicture;
Game_Screen.prototype.erasePicture = function(pictureId) {
_Game_Screen_erasePicture.call(this, ...arguments);
const realPictureId = this.realPictureId(pictureId);
GabeMZ.PictureSpritesheet.spriteSheetInfo[realPictureId] = null;
};
//-----------------------------------------------------------------------------
// Game_Picture
//
// The game object class for a picture.
const _Game_Picture_initBasic = Game_Picture.prototype.initBasic;
Game_Picture.prototype.initBasic = function() {
_Game_Picture_initBasic.call(this);
this._spriteSheet = {};
this._isSpriteSheet = false;
this._aniFrame = 0;
this._aniCol = 0;
this._aniRow = 0;
this._timer = 0;
this._isLooped = false;
this._isReversal = false;
this._isSpriteSheet = false;
};
Game_Picture.prototype.setSpriteSheet = function(w, h, f, s, l, t) {
this._spriteSheet = {
width: w,
height: h,
frames: f,
speed: s,
loop: l,
type: t
}
this._aniFrame = 0;
this._aniCol = 0;
this._aniRow = 0;
this._timer = 0;
this._isLooped = false;
this._isReversal = false;
this._isSpriteSheet = true;
};
Game_Picture.prototype.desetSpriteSheet = function() {
this._spriteSheet = {};
this._isSpriteSheet = false;
};
Game_Picture.prototype.spriteSheet = function() {
return this._spriteSheet;
};
Game_Picture.prototype.isSpriteSheet = function() {
return this._isSpriteSheet;
};
const _Game_Picture_update = Game_Picture.prototype.update;
Game_Picture.prototype.update = function() {
_Game_Picture_update.call(this);
this.updateSpriteSheet();
};
Game_Picture.prototype.updateSpriteSheet = function() {
if (this._isSpriteSheet) {
if (this._timer == 0) this.updateAnimFrame();
this.updateTimer();
}
};
Game_Picture.prototype.updateTimer = function() {
if (this._isLooped) return
if (this._timer < this._spriteSheet.speed) {
this._timer++
} else {
this._timer = 0
}
};
Game_Picture.prototype.updateAnimFrame = function() {
if (this.currentCond()) {
if (this._isReversal) {
this._aniFrame--;
this._aniCol--;
} else {
this._aniFrame++;
this._aniCol++;
}
} else {
if (this._spriteSheet.loop) {
if (this._spriteSheet.type == 1 && !this._isReversal) {
this._aniFrame--;
this._aniCol--;
this._isReversal = true;
} else {
this._aniFrame = 0;
this._aniCol = 0;
this._aniRow = 0;
this._isReversal = false;
}
} else {
this._isLooped = true;
}
}
if (this._isReversal) {
if (this._aniCol < 0) {
this._aniCol = this._spriteSheet.width - 1;
this._aniRow--;
}
} else {
if (this._aniCol == this._spriteSheet.width) {
this._aniCol = 0;
this._aniRow++;
}
}
};
Game_Picture.prototype.currentCond = function() {
if (this._isReversal) {
return this._aniFrame > 1;
} else {
return this._aniFrame < this._spriteSheet.frames;
}
}
Game_Picture.prototype.currentFrameRect = function(w, h) {
const width = Math.floor(w / this._spriteSheet.width);
const height = Math.floor(h / this._spriteSheet.height);
const x = width * this._aniCol;
const y = height * this._aniRow;
return new Rectangle(x, y, width, height);
};
//-----------------------------------------------------------------------------
// Sprite_Picture
//
// The sprite for displaying a picture.
const _Sprite_Picture_update = Sprite_Picture.prototype.update;
Sprite_Picture.prototype.update = function() {
_Sprite_Picture_update.call(this);
const picture = this.picture();
if (this.bitmap && picture.isSpriteSheet()) this.bitmap.addLoadListener(() => {
this.texture.frame = (picture.currentFrameRect(this.bitmap.width, this.bitmap.height))
});
};
})();