-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPP_EncounterEffect.js
1737 lines (1547 loc) · 57.1 KB
/
MPP_EncounterEffect.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=============================================================================
// MPP_EncounterEffect.js
//=============================================================================
// Copyright (c) 2018 Mokusei Penguin
// Released under the MIT license
// http://opensource.org/licenses/mit-license.php
//=============================================================================
/*:
* @target MV MZ
* @plugindesc Change the effect at the time of encounter to a special effect.
* @author Mokusei Penguin
* @url
*
* @help [version 3.2]
* This plugin is for RPG Maker MV and MZ.
*
* ▼ Plugin command(MV)
* In the item to enter a numerical value, the variable N is
* referred to by writing v [N].
*
* 〇 EncEffType type
* type : Effect type
* - The effect type will be changed to the specified number
* only once next.
*
* 〇 EncEffChar evId
* evId : ID of the event that is the core of the effect
* - The effect will be executed around the specified character
* only once next.
* (-1:Player, 0:This Event, 1-:Event with specified ID)
* - This setting is valid only for effect type 1.
*
* 〇 EncEffColor r g b
* r g b : Effect color(RGB)
* - The effect will change to the specified color
* only once next.
*
* ▼ Plugin command supplement (MZ)
* In the item to enter a numerical value, select the Text and
* write v[N] to refer to the variable N.
*
* ▼ Effect type
* 0 : Default
* 1 : Breaks around the character
* 2 : Break from the left side of the screen
* 3 : The entire screen scatters to the front
*
* ▼ Other
* - In effect type 1, if the main character is not specified,
* [Player] will be the center for random encounters,
* and [This Event] will be the center for
* the event command [Battle Processing].
* - When executed on a mobile device, some effects will be
* omitted due to weight reduction.
*
* ================================
* Mail : wood_penguin@yahoo.co.jp (@ is half-width)
* Blog : http://woodpenguin.blog.fc2.com/
* License : MIT license
*
* @param Effect Type
* @desc
* @type number
* @max 3
* @default 1
*
* @param Effect Color
* @desc R,G,B
* @default 255,255,255
*
*
* @command setType
* @desc Change the effect type only in the next battle.
*
* @arg type
* @desc
* @type number
* @max 3
* @default 1
*
*
* @command setCharacter
* @desc Specify the main character for the next effect.
* This setting is valid only for effect type 1.
*
* @arg character
* @desc -1:Player, 0:This event, 1-:Event with specified ID
* @type number
* @min -1
* @default 0
*
*
* @command setColor
* @desc Specifies the color of the next effect.
*
* @arg color
* @desc
* @default 255,255,255
*
*/
/*:ja
* @target MV MZ
* @plugindesc エンカウント時の演出を特殊なエフェクトに変更します。
* @author 木星ペンギン
* @url
*
* @help [version 3.2]
* このプラグインはRPGツクールMVおよびMZ用です。
*
* ▼ プラグインコマンド(MV)
* 数値を入力する項目では、v[N] と記述することで変数N番を参照します。
*
* 〇 EncEffType type
* type : エフェクトタイプ
* - 次の1回のみ、エフェクトタイプが指定した番号に変更されます。
*
* 〇 EncEffChar evId
* evId : エフェクトの中心となるイベントのID
* - 次の1回のみ、エフェクトが指定したキャラクターを中心に実行されます。
* (-1:プレイヤー, 0:このイベント, 1~:指定したIDのイベント)
* - この設定はエフェクトタイプ1のみ有効です。
*
* 〇 EncEffColor r g b
* r g b : エフェクトの色(RGB)
* - 次の1回のみ、エフェクトが指定した色に変更されます。
*
* ▼ プラグインコマンド 補足(MZ)
* 数値を入力する項目で、テキストを選択して v[N] と記述することで
* 変数N番を参照します。
*
* ▼ エフェクトタイプ
* 0 : デフォルト
* 1 : キャラクターを中心に割れる
* 2 : 画面の左から割れる
* 3 : 画面全体が前面に飛び散る
*
* ▼ その他
* - エフェクトタイプ1にて、中心となるキャラクターを指定していない場合、
* ランダムエンカウントでは[プレイヤー]、
* イベントコマンドの【戦闘の処理】では[このイベント]が中心となります。
* - モバイル機器で実行した場合、軽量化のため一部の演出が省略されます。
*
* ================================
* Mail : wood_penguin@yahoo.co.jp (@は半角)
* Blog : http://woodpenguin.blog.fc2.com/
* License : MIT license
*
* @param Effect Type
* @text エフェクトタイプ
* @desc 画面割れエフェクトのタイプ
* @type number
* @max 3
* @default 1
*
* @param Effect Color
* @text エフェクト色
* @desc エフェクトのデフォルト色
* (r,g,bで指定)
* @default 255,255,255
*
*
* @command setType
* @text エフェクトタイプ変更
* @desc 次に行う戦闘のみエフェクトタイプを変更します。
*
* @arg type
* @text タイプ
* @desc エフェクトタイプ
* @type number
* @max 3
* @default 1
*
*
* @command setCharacter
* @text 中心キャラ指定
* @desc 次に行うエフェクトで中心となるキャラクターを指定します。
* この設定はエフェクトタイプ1でのみ有効です。
*
* @arg character
* @text キャラクター
* @desc 中心となるキャラクター
* -1:プレイヤー, 0:このイベント, 1~:指定したIDのイベント
* @type number
* @min -1
* @default 0
*
*
* @command setColor
* @text エフェクト色指定
* @desc 次に行うエフェクトの色を指定します。
*
* @arg color
* @text 色(RGB)
* @desc
* @default 255,255,255
*
*/
(() => {
'use strict';
const pluginName = "MPP_EncounterEffect";
//Plugin Parameters
const parameters = PluginManager.parameters(pluginName);
const param_EffectType = Number(parameters["Effect Type"] || 2);
const param_DefaultColor = parameters["Effect Color"].split(",").map(Number);
//Dealing with other plugins
const importedPlugin = (...names) => {
return names.some(name => PluginManager._scripts.includes(name));
};
const pluginOptions = [
"MPP_EncounterEffect_Op1",
"MPP_EncounterEffect_Op2"
];
//Database
const DATABASE = [null,
{//Type 1
"Shape Type":"square",
"Break Duration":45,
"Interval Duration":45,
"Scatter Duration":0,
"Move Duration":60,
"Split Radial":8,
"Radial Random Rate":90,
"Circle Radius":96,
"Circle Increase Rate":150,
"Circle Random Rate":40
},
{//Type 2
"Shape Type":"random",
"Break Direction":"left",
"Break Duration":30,
"Interval Duration":45,
"Scatter Direction":"left",
"Scatter Duration":35,
"Move Duration":70,
"Split X":7,
"X Random Rate":80,
"Split Y":5,
"Y Random Rate":80
},
{//Type 3
"Shape Type":"triangle",
"Break Direction":"inside",
"Break Duration":35,
"Interval Duration":40,
"Scatter Duration":16,
"Move Duration":100,
"Split X":8,
"X Random Rate":80,
"Split Y":6,
"Y Random Rate":80
}
];
//-------------------------------------------------------------------------
// Array
if (!Array.prototype.flat) {
Array.prototype.flat = function (depth) {
const rec = (flattend, array, depth) => {
for (const elem of array) {
if (Array.isArray(elem) && depth > 0) {
rec(flattend, elem, depth - 1);
} else {
flattend.push(elem);
}
}
return flattend;
};
return rec([], this, depth !== undefined ? Math.floor(depth) : 1);
};
}
//-------------------------------------------------------------------------
// EncounterEffect
function EncounterEffect() {
throw new Error('This is a static class');
}
if (importedPlugin(...pluginOptions)) {
window.EncounterEffect = EncounterEffect;
}
EncounterEffect.snap = null;
EncounterEffect.center = new Point();
EncounterEffect.opacity = 0;
EncounterEffect._fragments = null;
EncounterEffect._type = param_EffectType;
EncounterEffect._character = null;
EncounterEffect._color = param_DefaultColor;
EncounterEffect._effectDelay = 0;
EncounterEffect.clear = function() {
this.snap = null;
this.opacity = 0;
this._fragments = null;
this._type = param_EffectType;
this._character = null;
this._color = param_DefaultColor;
this._effectDelay = 0;
};
EncounterEffect.destroy = function() {
this.snap.destroy();
this._fragments.forEach(fragment => fragment.destroy());
};
EncounterEffect.params = function() {
return DATABASE[this._type];
};
EncounterEffect.zoom = function() {
return Utils.isMobileDevice() ? 4 : 2;
};
EncounterEffect.setup = function(snap) {
this.snap = snap;
this.opacity = 255;
this._opacityDuration = 0;
this._fragments = [];
this._params = this.params();
this.setupWindowSides();
this.setupCenter();
this.createFrames();
this.startEffectDelay();
};
EncounterEffect.setupWindowSides = function() {
this.windowSides = {
minX:0, maxX:Graphics.width,
minY:0, maxY:Graphics.height
};
};
EncounterEffect.setupCenter = function() {
const sides = this.windowSides;
const char = this.centerCharacter();
const cx = Math.round(char.screenX()).clamp(sides.minX, sides.maxX);
const cy = Math.round(char.screenY() - 24).clamp(sides.minY, sides.maxY);
this.center.set(cx, cy);
};
EncounterEffect.createFrames = function() {
switch (this._type) {
case 1:
this.createFramesT1();
break;
case 2:
case 3:
this.createFramesT2();
break;
}
};
EncounterEffect.setType = function(type) {
this._type = type;
};
EncounterEffect.type = function() {
return this._type;
};
EncounterEffect.setColor = function(color) {
this._color = color;
};
EncounterEffect.color = function() {
return this._color;
};
EncounterEffect.setCharacter = function(character) {
this._character = character;
};
EncounterEffect.centerCharacter = function() {
return this._character ? this._character :
$gameMap.getInterpreterCharacter(0) || $gamePlayer;
};
EncounterEffect.createFramesT1 = function() {
const sType = this._params["Shape Type"] || "square";
const allPoints = this.createAllPointsT1();
this.iteratePointT1(allPoints, (p1, p2, p3, p4) => {
const frame = [];
if (p2) {
if (this.isOutFrame(p1, p2)) return;
if (sType === "square") {
frame.push(p1, p2);
} else {
this.addFrame(this.uniqFrame([p1, p2, p3]));
frame.push(p1);
}
} else {
frame.push(p1);
}
if (this.setCorrectPoints(frame, p3, p4)) {
this.addFrame(this.uniqFrame(frame));
}
});
};
EncounterEffect.iteratePointT1 = function(allPoints, callback) {
const splitX = this._params["Split Radial"] || 8;
this._cornerFlag = [null, null, null, null];
for (let i = 0; i < allPoints.length; i++) {
const inPoints = i > 0 ? allPoints[i - 1] : null;
const outPoints = allPoints[i];
for (let j = 0; j < splitX; j++) {
const point3 = outPoints[j];
const point4 = outPoints[(j + 1) % splitX];
if (i > 0) {
const point1 = inPoints[(j + 1) % splitX];
const point2 = inPoints[j];
callback(point1, point2, point3, point4);
} else {
callback(this.center, null, point3, point4);
}
}
if (!this._cornerFlag.includes(null)) break;
}
};
EncounterEffect.uniqFrame = function(frame) {
return frame.filter((point1, i) => {
return frame.findIndex( point2 => point2.equals(point1) ) === i;
});
};
EncounterEffect.createAllPointsT1 = function() {
const {
"Shape Type": sType = "square",
"Split Radial": splitX = 8,
"Circle Radius": baseHeight = 96,
"Circle Increase Rate": circleRate = 150
} = this._params;
const maxRadius = this.maxRadius();
const allPoints = [];
let startR = Math.random() * 2;
let baseR = 0;
let height = baseHeight;
for (let i = 0; i < 8; i++) {
if (sType === "triangle") startR += 1 / splitX;
if (i === 7) baseR = 1000000;
allPoints[i] = this.createRoundPoints(startR, baseR, height);
if (baseR > maxRadius) break;
baseR += height;
height *= circleRate / 100;
}
return allPoints;
};
EncounterEffect.createRoundPoints = function(startR, baseR, height) {
const {
"Split Radial": splitX = 8,
"Radial Random Rate": rRate = 90,
"Circle Random Rate": cRate = 40
} = this._params;
const baseAngle = 2 / splitX;
const randomAngle = n => startR + baseAngle * n +
baseAngle * rRate * Math.random() / 100;
const randomRadius = () => baseR +
height * (1 - cRate * (0.75 - Math.random()) / 100);
const points = [];
for (let i = 0; i < splitX; i++) {
points[i] = this.calculatePoint(randomAngle(i) % 2, randomRadius());
}
return points;
};
EncounterEffect.calculatePoint = function(angle, radius) {
const sides = this.windowSides;
const { x: cx, y: cy } = this.center;
let sx = radius * Math.cos(angle * Math.PI);
let sy = radius * Math.sin(angle * Math.PI);
if (sx !== 0 && cx + sx < sides.minX) {
sy *= (sides.minX - cx) / sx;
sx = sides.minX - cx;
}
if (sx !== 0 && cx + sx > sides.maxX) {
sy *= (sides.maxX - cx) / sx;
sx = sides.maxX - cx;
}
if (sy !== 0 && cy + sy < sides.minY) {
sx *= (sides.minY - cy) / sy;
sy = sides.minY - cy;
}
if (sy !== 0 && cy + sy > sides.maxY) {
sx *= (sides.maxY - cy) / sy;
sy = sides.maxY - cy;
}
return new Point(Math.round(cx + sx), Math.round(cy + sy));
};
EncounterEffect.maxRadius = function() {
const { width, height } = Graphics;
const { x: cx, y: cy } = this.center;
const deltaX = cx < width / 2 ? width - cx : cx;
const deltaY = cy < height / 2 ? height - cy : cy;
return Math.hypot(deltaX, deltaY);
};
EncounterEffect.createFramesT2 = function() {
const {
"Shape Type": sType = "square",
"Break Direction": direction = "left"
} = this._params;
switch (sType) {
case "square":
this.createFramesT2Square();
break;
case "triangle":
this.createFramesT2Triangle();
break;
case "random":
this.createFramesT2Random();
break;
}
this.sortFragment(direction);
};
EncounterEffect.createFramesT2Square = function() {
const allPoints = this.createAllPointsT2Square();
this.iteratePointT2(allPoints, (...points) => {
this.addFrame(points);
});
};
EncounterEffect.iteratePointT2 = function(allPoints, callback) {
const {
"Split X": splitX = 12,
"Split Y": splitY = 9
} = this._params;
for (let i = 0; i < splitX; i++) {
const leftPoints = allPoints[i];
const rightPoints = allPoints[i + 1];
for (let j = 0; j < splitY; j++) {
const point1 = leftPoints[j + 1];
const point2 = leftPoints[j];
const point3 = rightPoints[j];
const point4 = rightPoints[j + 1];
callback(point1, point2, point3, point4);
}
}
};
EncounterEffect.createAllPointsT2Square = function() {
const {
"Split X": splitX = 12,
"Split Y": splitY = 9,
"X Random Rate": xRate = 80,
"Y Random Rate": yRate = 80
} = this._params;
const sides = this.windowSides;
const width = Graphics.width / Math.max(splitX - xRate / 100, 1);
const height = Graphics.height / Math.max(splitY - yRate / 100, 1);
const pointX = i => i === 0 ? sides.minX : i === splitX ? sides.maxX :
this.randomValue(width, i, xRate);
const pointY = j => j === 0 ? sides.minY : j === splitY ? sides.maxY :
this.randomValue(height, j, yRate);
const allPoints = [];
for (let i = 0; i <= splitX; i++) {
const points = [];
for (let j = 0; j <= splitY; j++) {
points[j] = new Point(pointX(i), pointY(j));
}
allPoints[i] = points;
}
return allPoints;
};
EncounterEffect.randomValue = function(value, index, rate) {
return Math.round(value * (index - rate * Math.random() / 100));
};
EncounterEffect.createFramesT2Triangle = function() {
const allPoints = this.createAllPointsT2Triangle();
this.iteratePointT2(allPoints, (p1, p2, p3, p4) => {
if (p2.y > p3.y || (p2.y === p3.y && p1.y > p4.y)) {
this.addFrame([p1, p2, p4]);
this.addFrame([p2, p3, p4]);
} else {
this.addFrame([p1, p2, p3]);
this.addFrame([p1, p3, p4]);
}
});
};
EncounterEffect.createAllPointsT2Triangle = function() {
const {
"Split X": splitX = 12,
"Split Y": splitY = 9,
"X Random Rate": xRate = 80,
"Y Random Rate": yRate = 80
} = this._params;
const sides = this.windowSides;
const width = Graphics.width / Math.max(splitX - xRate / 100, 1);
const height = Graphics.height / Math.max(splitY - 0.5, 1);
const bottomY = height / 2 * yRate / 100;
const pointX = i => i === 0 ? sides.minX : i === splitX ? sides.maxX :
this.randomValue(width, i, xRate);
const pointY = (i, j) => {
if (j === 0) return sides.minY;
if (j === splitY) return sides.maxY;
let py = bottomY;
if (i % 2 === 1) {
if (j === 1) return this.randomValue(height / 2, j, yRate);
py -= height / 2;
}
return py + this.randomValue(height, j, yRate);
};
const allPoints = [];
for (let i = 0; i <= splitX; i++) {
const points = [];
for (let j = 0; j <= splitY; j++) {
points[j] = new Point(pointX(i), pointY(i, j));
}
allPoints[i] = points;
}
return allPoints;
};
EncounterEffect.createFramesT2Random = function() {
const allBaseFrames = this.createBaseFramesT2Random();
const sideFrame = (n, m, pos) => {
const si = n + (pos === 0 || pos === 1 ? -1 : 1);
const subFrames = allBaseFrames[si];
if (subFrames) {
const sj = Math.floor(m / 2) * 2;
const frame2 = subFrames[sj];
if (Math.floor(pos / 2) === Math.floor(frame2.pos / 2)) {
return subFrames[sj + 1];
} else {
return frame2;
}
}
return null;
};
allBaseFrames.forEach((mainFrames, i) => {
mainFrames.forEach((frame1, j) => {
if (frame1.state === "none") {
const d = Math.randomInt(3);
const frame2 = d === 0 ? mainFrames[j - 1] :
d === 1 ? mainFrames[j + 1] :
sideFrame(i, j, frame1.pos);
if (frame2 && frame2.state === "none") {
this.joinFrame(frame1, frame2, d);
}
}
});
});
allBaseFrames.flat().forEach(frame => {
if (frame.state === "none" || frame.state === "received") {
this.addFrame(frame.frame);
}
});
};
EncounterEffect.createBaseFramesT2Random = function() {
const allPoints = this.createAllPointsT2Square();
const frames = [];
this.iteratePointT2(allPoints, (p1, p2, p3, p4) => {
if (Math.random() < 0.5) {
frames.push(
{ frame:[p1, p2, p3], pos:0, state:"none" },
{ frame:[p1, p3, p4], pos:3, state:"none" }
);
} else {
frames.push(
{ frame:[p2, p3, p4], pos:2, state:"none" },
{ frame:[p1, p2, p4], pos:1, state:"none" }
);
}
});
const splitY = this._params["Split Y"] || 9;
const allFrames = [];
while (frames.length > 0) {
allFrames.push(frames.splice(0, splitY * 2));
}
return allFrames;
};
EncounterEffect.joinFrame = function(frame1, frame2, d) {
const pos1 = frame1.pos;
const pos2 = frame2.pos;
let start = 0, index = 0;
if (d === 0) {
start = (pos1 === 0 || pos1 === 1 ? 2 : 1);
index = 1;
} else if (d === 1) {
start = 0;
index = (pos1 === 0 ? 2 : pos1 === 2 ? 0 : pos2 === 0 ? 0 : 2);
} else {
start = (pos1 === 0 || pos1 === 1 ? 1 : 2);
index = (pos2 === 0 || pos2 === 1 ? 2 : 0);
}
frame1.frame.splice(start, 0, frame2.frame[index]);
frame1.state = "received";
frame2.state = "joined";
};
EncounterEffect.addFrame = function(frame) {
if (frame.length > 2) {
const fragment = new Encounter_Fragment(frame);
fragment.setSnap(this.snap);
this._fragments.push(fragment);
}
};
EncounterEffect.isOutFrame = function(pos1, pos2) {
const sides = this.windowSides;
if (pos1.x === sides.minX && pos2.x === sides.minX) return true;
if (pos1.x === sides.maxX && pos2.x === sides.maxX) return true;
if (pos1.y === sides.minY && pos2.y === sides.minY) return true;
if (pos1.y === sides.maxY && pos2.y === sides.maxY) return true;
return false;
};
EncounterEffect.setCorrectPoints = function(frame, pos1, pos2) {
const sides = this.windowSides;
frame.push(pos1);
if (pos1.x === sides.minX && pos2.y === sides.minY) {
if (!this.pushCorner(frame, 0)) return false;
} else if (pos1.y === sides.maxY && pos2.x === sides.minX) {
if (!this.pushCorner(frame, 1)) return false;
} else if (pos1.y === sides.minY && pos2.x === sides.maxX) {
if (!this.pushCorner(frame, 2)) return false;
} else if (pos1.x === sides.maxX && pos2.y === sides.maxY) {
if (!this.pushCorner(frame, 3)) return false;
} else if (pos1.x === sides.minX && pos2.x === sides.maxX) {
if (!this.pushCorner(frame, 0)) return false;
if (!this.pushCorner(frame, 2)) return false;
} else if (pos1.y === sides.maxY && pos2.y === sides.minY) {
if (!this.pushCorner(frame, 1)) return false;
if (!this.pushCorner(frame, 0)) return false;
} else if (pos1.y === sides.minY && pos2.y === sides.maxY) {
if (!this.pushCorner(frame, 2)) return false;
if (!this.pushCorner(frame, 3)) return false;
} else if (pos1.x === sides.maxX && pos2.x === sides.minX) {
if (!this.pushCorner(frame, 3)) return false;
if (!this.pushCorner(frame, 1)) return false;
}
frame.push(pos2);
return true;
};
EncounterEffect.pushCorner = function(frame, c) {
if (!this._cornerFlag[c]) {
frame.push(this.getCornerPoint(c));
this._cornerFlag[c] = frame;
return true;
} else {
return false;
}
};
EncounterEffect.getCornerPoint = function(c) {
const sides = this.windowSides;
return new Point(
c === 0 || c === 1 ? sides.minX : sides.maxX,
c === 0 || c === 2 ? sides.minY : sides.maxY
);
};
EncounterEffect.sortFragment = function(direction) {
const formula = this.compareFormula(direction);
if (formula) {
this._fragments.sort((a, b) => formula(a) - formula(b));
}
};
EncounterEffect.compareFormula = function(direction) {
const cx = Graphics.width / 2;
const cy = Graphics.height / 2;
switch (direction) {
case "left":
return (p) => p.x;
case "center":
return (p) => Math.abs(cx - p.x);
case "right":
return (p) => -p.x;
case "inside":
return (p) => Math.abs(cx - p.x) + Math.abs(cy - p.y) * cy / cx;
case "outside":
return (p) => -Math.abs(cx - p.x) - Math.abs(cy - p.y) * cy / cx;
}
return null;
};
EncounterEffect.breakFragments = function(start, end, duration) {
this._fragments.slice(start, end).forEach(fragment =>
fragment.onBreak(this._color, duration)
);
};
EncounterEffect.onClip = function(frame, rect) {
const context = this.snap.context;
context.save();
context.beginPath();
frame.forEach((point, i) => {
i === 0 ? context.moveTo(point.x, point.y) :
context.lineTo(point.x, point.y);
});
context.closePath();
context.clip();
context.clearRect(rect.x, rect.y, rect.width, rect.height);
context.restore();
this.snap._baseTexture.update();
};
EncounterEffect.onBreakEnd = function() {
this.snap.fillAll("black");
this._fragments.forEach(fragment => fragment.clearClip());
};
EncounterEffect.onBattleStart = function() {
const {
"Move Duration": duration = 60,
"Scatter Duration": delay = 0
} = this._params;
this.startFragments(duration);
this.setFragmentsDelay(delay);
this._opacityDuration = duration;
};
EncounterEffect.startFragments = function(duration) {
switch (this._type) {
case 1:
this._fragments.forEach(f => f.onBattleStartT1(duration));
this.opacity = 300;
break;
case 2:
const d = this._params["Scatter Direction"] || "left";
this.sortFragment(d);
this._fragments.forEach(f => f.onBattleStartT2(duration, d));
this.opacity = 640;
break;
case 3:
this.sortFragment("inside");
this._fragments.forEach(f => f.onBattleStartT3(duration));
this.opacity = 640;
break;
}
};
EncounterEffect.setFragmentsDelay = function(delay) {
const maxItems = this.maxItems();
this._fragments.forEach((fragment, i) =>
fragment.setDelay(Math.floor(delay * i / maxItems ))
);
};
EncounterEffect.update = function() {
if (this.isRunning()) {
this._fragments.forEach(fragment => fragment.update());
this.updateOpacity();
if (this.opacity === 0) {
this.destroy();
this.clear();
}
}
};
EncounterEffect.updateOpacity = function() {
if (this._opacityDuration > 0) {
const d = this._opacityDuration;
this.opacity *= (d - 1) / d;
this._opacityDuration--;
}
};
EncounterEffect.maxItems = function() {
return this._fragments ? this._fragments.length : 0;
};
EncounterEffect.fragments = function() {
return this._fragments || [];
};
EncounterEffect.isValid = function() {
return !!this.params();
};
EncounterEffect.isRunning = function() {
return !!this._fragments;
};
EncounterEffect.isReady = function() {
return !this.isRunning() || this._opacityDuration < 45;
};
EncounterEffect.breakDuration = function() {
return this._params ? this._params["Break Duration"] || 45 : 45;
};
EncounterEffect.effectSpeed = function() {
const d = this._params ? this._params["Interval Duration"] || 45 : 45;
return this.breakDuration() + d;
};
EncounterEffect.fadeSpeed = function() {
return this._params ?
Math.min(this._params["Move Duration"] || 60, 60) : 60;
};
EncounterEffect.startEffectDelay = function() {
this._effectDelay = 4;
};
EncounterEffect.updateEffectDelay = function() {
if (this._effectDelay > 0) this._effectDelay--;
};
EncounterEffect.isEffectReady = function() {
return this._effectDelay === 0;
};
//-------------------------------------------------------------------------
// Encounter_Fragment
function Encounter_Fragment() {
this.initialize(...arguments);
}
if (importedPlugin(...pluginOptions)) {
window.Encounter_Fragment = Encounter_Fragment;
}
Encounter_Fragment.prototype.initialize = function(frame) {
this._frame = frame;
this.initRect();
this.createBitmap();
this.initFrame();
this.initPosition();
this.initRotation();
this._flash = false;
this._clip = false;
this._delay = 0;
this.visible = false;
};
Encounter_Fragment.prototype.initRect = function() {
const frame = this._frame;
const allX = frame.map( point => point.x );
const allY = frame.map( point => point.y );
const rect = new Rectangle();
rect.x = Math.min(...allX);
rect.y = Math.min(...allY);
rect.width = Math.max(...allX) - rect.x;
rect.height = Math.max(...allY) - rect.y;
this._rect = rect;
};
Encounter_Fragment.prototype.createBitmap = function() {
const rect = this._rect;
const zoom = EncounterEffect.zoom();
const bw = Math.ceil(rect.width / zoom) + 2;
const bh = Math.ceil(rect.height / zoom) + 2;
this.bitmap = new Bitmap(bw, bh);
this.bitmap.smooth = true;
};
Encounter_Fragment.prototype.initFrame = function() {
const frame = this._frame;
const rect = this._rect;
const zoom = EncounterEffect.zoom();
const context = this.bitmap.context;
context.translate(-rect.x / zoom + 1, -rect.y / zoom + 1);
context.beginPath();
frame.forEach((point, i) => {
i === 0 ? context.moveTo(point.x / zoom, point.y / zoom) :
context.lineTo(point.x / zoom, point.y / zoom);
});
context.closePath();
};
Encounter_Fragment.prototype.initPosition = function() {
const rect = this._rect;
this.x = rect.x + Math.floor(rect.width / 2);
this.y = rect.y + Math.floor(rect.height / 2);
this.jumpHeight = 0;
this._targetX = 0;