-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDotMoveSystem.js
3450 lines (3378 loc) · 142 KB
/
DotMoveSystem.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
"use strict";
/*:
@target MV MZ
@plugindesc Dot movement system v2.2.4
@author unagi ootoro
@url https://raw.githubusercontent.com/unagiootoro/RPGMZ/master/DotMoveSystem.js
@help
It is a plugin that allows you to move in dot units.
【How to use】
Basically, it can be used just by installing it, but more detailed control is possible by setting the following contents.
■ Setting of movement unit
In the move route script
this.setMoveUnit (Movement unit (decimal between 0 and 1));
By writing, you can specify the movement unit per step.
For example, to move an event half a step
this.setMoveUnit(0.5);
It is described as.
■ Move to any angle in dot units
In the move route script
this.dotMoveByDeg(angle (integer from 0 to 359));
By writing, you can move in the direction of the angle specified in dot units.
■ Move the event in the direction of the player in dot units
In the move route script
this.dotMoveToPlayer();
By writing, you can move the event in the direction of the player in dot units.
■ Move to the specified coordinates
In the move route script
this.moveToTarget(X coordinate, Y coordinate);
You can move it toward the specified coordinates by writing.
※1 If it collides with a wall etc. on the way, the arrival coordinates will shift.
※2 The movement with respect to the specified coordinates is performed with the upper left of the character as the origin.
■ Event contact judgment settings
Annotate the very first event command on the EV page on the first page of the event
Event contact judgment can be set in more detail by describing the following contents in the annotation.
・ Contact range on the horizontal axis
<widthArea: Contact width (decimal number between 0 and 1)>
・ Contact range on the vertical axis
<heightArea: Contact width (decimal number between 0 and 1)>
If you set the event priority below the regular character or above the regular character,
Both the contact range on the horizontal axis and the contact range on the vertical axis are used as the contact range.
When set to the same as a normal character, the contact range on the horizontal axis will be changed when touching upward or downward.
When touching to the left or right, the contact range on the vertical axis is used.
If neither the horizontal axis nor the vertical axis is set, 0.5 will be applied.
■ Event size setting
Annotate the very first event command on the EV page on the first page of the event
You can set the size of the event in more detail by including the following in the annotation.
・ Horizontal size
<width: width (real numbers greater than or equal to 0.5)>
・ Vertical size
<height: height (real numbers greater than or equal to 0.5)>
・ X coordinate display offset
<offsetX: offset (real number)>
・ Y coordinate display offset
<offsetY: offset (real number)>
For example, to set a 96 * 96 size character with width: 2 and height: 2, it will be as follows.
When displaying a character larger than 48 * 48, the display start position is different from the actual XY coordinates.
You need to adjust the display offset.
<width: 2>
<height: 2>
<offsetX: 0.5>
<offsetY: 1>
■ Setting the event slide length
When the character collides with the corner of the wall, it will move to the side where there is no corner.
If the character size is less than 1, half the size is applied to the slide width, and 0.5 is applied if it is 1 or more.
Also, after annotating the very first event command on the EV page on the first page of the event,
you can set the slide width of the event in more detail by writing the following content in the annotation.
・X axis slide length
<slideLengthX: Width (0 or more real number>
・Y axis slide length
<slideLengthY: Vertical width (0 or more real number)>
■ Change character size/display offset/slide width from script
By describing the following script in the movement route script
Character size/display offset/slide width can be changed.
・Horizontal size change
this.setWidth(width value);
・Vertical size change
this.setHeight(height value);
・Change the X coordinate display offset
this.setOffsetX(offset value);
・Change Y coordinate display offset
this.setOffsetY(offset value);
・Change the slide width in the X-axis direction
this.setSlideLengthX(slide width value);
・Change the slide width in the Y-axis direction
this.setSlideLengthY(slide width value);
・Change the contact range of the horizontal axis
this.setWidthArea(contact width value);
・Change the contact range of the vertical axis
this.setHeightArea(contact width value);
■ Other functions that can be used in scripts
Game_CharacterBase#isMoved()
Gets whether the character has moved into that frame.
Game_CharacterBase#calcFar(targetCharacter)
Gets the distance between the characters. The origin of the distance calculation uses the center point.
Game_CharacterBase#calcDeg(targetCharacter)
Gets the angle to the target character. The origin of the angle calculation uses the center point.
Game_CharacterBase#moveByDirection(direction)
Moves in the direction of the character direction in the movement unit specified by setMoveUnit.
Game_CharacterBase#stopMove()
Stop character movement. If you move the character while stopped, it will automatically resume movement.
Game_CharacterBase#resumeMove()
Resume the movement of the character.
Game_CharacterBase#cancelMove()
Cancels the movement of the character to a specific point by moveToTarget etc.
Game_CharacterBase#checkCharacter(character)
Checks if it collides with the character specified by the argument, and if it collides, returns a CollisionResult object.
Game_CharacterBase#checkHitCharacters(targetCharacterClass)
Checks for collisions with all characters that match the character class specified
by targetCharacterClass, and returns an array of CollisionResult objects.
【License】
This plugin is available under the terms of the MIT license.
*/
/*:ja
@target MV MZ
@plugindesc ドット移動システム v2.2.4
@author うなぎおおとろ
@url https://raw.githubusercontent.com/unagiootoro/RPGMZ/master/DotMoveSystem.js
@help
ドット単位での移動が可能になるプラグインです。
【使用方法】
基本的に導入するだけで使用可能ですが、以下の内容を設定することでより詳細な制御が可能になります。
■ 移動単位の設定
移動ルートのスクリプトで
this.setMoveUnit(移動単位(0~1の間の小数));
と記載することで、一歩あたりの移動単位を指定することができます。
例えば、イベントを半歩移動させるには
this.setMoveUnit(0.5);
と記載します。
■ ドット単位で任意の角度に移動させる
移動ルートのスクリプトで
this.dotMoveByDeg(角度(0~359の整数));
と記載することで、ドット単位で指定した角度の方向へ移動させることができます。
■ ドット単位でイベントをプレイヤーの方向に移動させる
移動ルートのスクリプトで
this.dotMoveToPlayer();
と記載することで、イベントをドット単位でプレイヤーの方向に移動させることができます。
■ 指定の座標に移動させる
移動ルートのスクリプトで
this.moveToTarget(X座標, Y座標);
と記載することで、指定した座標に向けて移動させることができます。
※1 途中で壁などに衝突した場合は到達座標がずれます。
※2 指定の座標に対する移動はキャラクターの左上を原点として行います。
■ イベント接触判定の設定
イベントの1ページ目のEVページの一番最初のイベントコマンドを注釈にしたうえで、
注釈に以下の内容を記載することでイベント接触判定をより詳細に設定することができます。
・横軸の接触範囲
<widthArea: 接触幅(0~1の間の小数)>
・縦軸の接触範囲
<heightArea: 接触幅(0~1の間の小数)>
イベントのプライオリティを通常キャラの下または通常キャラの上に設定した場合、
横軸の接触範囲と縦軸の接触範囲の両方を接触範囲として使用します。
通常キャラと同じに設定した場合、上または下方向に接触した場合は横軸の接触範囲が、
左または右方向に接触した場合は縦軸の接触範囲が使用されます。
横軸、縦軸ともに設定しなかった場合は0.5が適用されます。
■ イベントのサイズの設定
イベントの1ページ目のEVページの一番最初のイベントコマンドを注釈にしたうえで、
注釈に以下の内容を記載することでイベントのサイズをより詳細に設定することができます。
・横方向サイズ
<width: 横幅(0.5以上の実数)>
・縦方向サイズ
<height: 縦幅(0.5以上の実数)>
・X座標表示オフセット
<offsetX: オフセット(実数)>
・Y座標表示オフセット
<offsetY: オフセット(実数)>
例えば96*96サイズのキャラクターをwidth: 2, height: 2で設定する場合は次のようになります。
48*48より大きいサイズのキャラクターを表示する場合、表示開始位置が実際のXY座標とは異なるため、
表示オフセットを調整する必要があります。
<width: 2>
<height: 2>
<offsetX: 0.5>
<offsetY: 1>
■ イベントのスライド幅の設定
キャラクターが壁の角に衝突したとき、角がない方にずらす動作を行いますが、
このときのずらしを許容する角との衝突幅をスライド幅と言います。
スライド幅はキャラクターのサイズが1未満であればサイズの半分が適用され、
1以上であれば0.5が適用されます。
また、イベントの1ページ目のEVページの一番最初のイベントコマンドを注釈にしたうえで、
注釈に以下の内容を記載することでイベントのスライド幅をより詳細に設定することができます。
・X軸方向のスライド幅
<slideLengthX: 横幅(0以上の実数>
・Y軸方向のスライド幅
<slideLengthY: 縦幅(0以上の実数)>
■ スクリプトからキャラクターのサイズ/表示オフセット/スライド幅を変更する
以下のスクリプトを移動ルートのスクリプトで記載することで
キャラクターのサイズ/表示オフセット/スライド幅を変更することができます。
・横方向サイズの変更
this.setWidth(横幅の値);
・縦方向サイズの変更
this.setHeight(縦幅の値);
・X座標表示オフセットの変更
this.setOffsetX(オフセットの値);
・Y座標表示オフセットの変更
this.setOffsetY(オフセットの値);
・X軸方向のスライド幅の変更
this.setSlideLengthX(スライド幅の値);
・Y軸方向のスライド幅の変更
this.setSlideLengthY(スライド幅の値);
・横軸の接触範囲の変更
this.setWidthArea(接触幅の値);
・縦軸の接触範囲の変更
this.setHeightArea(接触幅の値);
■ その他スクリプトで使用可能な関数
Game_CharacterBase#isMoved()
キャラクターがそのフレーム中に移動したか否かを取得します。
Game_CharacterBase#calcFar(targetCharacter)
キャラクター間の距離を取得します。距離計算の原点は中心点を使用します。
Game_CharacterBase#calcDeg(targetCharacter)
ターゲットのキャラクターに対する角度を取得します。角度計算の原点は中心点を使用します。
Game_CharacterBase#moveByDirection(direction)
キャラクターdirectionの方向にsetMoveUnitで指定した移動単位で移動させます。
Game_CharacterBase#stopMove()
キャラクターの移動を停止します。停止中にキャラクターを移動した場合、自動的に移動を再開します。
Game_CharacterBase#resumeMove()
キャラクターの移動を再開します。
Game_CharacterBase#cancelMove()
moveToTargetなどによって行われているキャラクターの特定地点への移動をキャンセルします。
Game_CharacterBase#checkCharacter(character)
引数で指定したcharacterと衝突しているかをチェックし、衝突していればCollisionResultオブジェクトを返します。
Game_CharacterBase#checkHitCharacters(targetCharacterClass)
targetCharacterClassで指定したキャラクタークラスに該当する
全てのキャラクターと衝突しているかをチェックし、CollisionResultオブジェクトの配列を返します。
【ライセンス】
このプラグインは、MITライセンスの条件の下で利用可能です。
*/
const DotMoveSystemPluginName = document.currentScript ? decodeURIComponent(document.currentScript.src.match(/^.*\/(.+)\.js$/)[1]) : "DotMoveSystem";
var DotMoveSystem;
(function (DotMoveSystem) {
class DotMovePoint {
get x() { return this._x; }
set x(_x) { this._x = _x; }
get y() { return this._y; }
set y(_y) { this._y = _y; }
static fromDegAndFar(deg, far) {
const rad = deg.toRad();
const x = far * Math.cos(rad);
const y = far * Math.sin(rad);
return new DotMovePoint(x, y);
}
constructor(...args) {
this.initialize(...args);
}
initialize(x = 0, y = 0) {
this._x = x;
this._y = y;
}
clone() {
return new DotMovePoint(this._x, this._y);
}
equals(point) {
return this.x === point.x && this.y === point.y;
}
add(point) {
return new DotMovePoint(this.x + point.x, this.y + point.y);
}
calcDeg(targetPoint) {
const ox = $gameMap.deltaX(targetPoint.x, this.x);
const oy = $gameMap.deltaY(targetPoint.y, this.y);
const rad = Math.atan2(oy, ox);
return Degree.fromRad(rad);
}
calcFar(targetPoint) {
const xDiff = $gameMap.deltaX(targetPoint.x, this.x);
const yDiff = $gameMap.deltaY(targetPoint.y, this.y);
if (xDiff === 0 && yDiff === 0)
return 0;
return Math.sqrt(xDiff ** 2 + yDiff ** 2);
}
}
DotMoveSystem.DotMovePoint = DotMovePoint;
class DotMoveRectangle {
get x() { return this._x; }
set x(_x) { this._x = _x; }
get y() { return this._y; }
set y(_y) { this._y = _y; }
get width() { return this._width; }
set width(_width) { this._width = _width; }
get height() { return this._height; }
set height(_height) { this._height = _height; }
get x2() { return this.x + this.width; }
get y2() { return this.y + this.height; }
constructor(...args) {
this.initialize(...args);
}
initialize(x = 0, y = 0, width = 0, height = 0) {
this._x = x;
this._y = y;
this._width = width;
this._height = height;
}
clone() {
return new DotMoveRectangle(this._x, this._y, this._width, this._height);
}
equals(rect) {
return this.x === rect.x && this.y === rect.y && this.width === rect.width && this.height === rect.height;
}
isCollidedRect(rect) {
if ((this.x > rect.x && this.x < rect.x2 || this.x2 > rect.x && this.x2 < rect.x2 || rect.x >= this.x && rect.x2 <= this.x2) &&
(this.y > rect.y && this.y < rect.y2 || this.y2 > rect.y && this.y2 < rect.y2 || rect.y >= this.y && rect.y2 <= this.y2)) {
return true;
}
return false;
}
union(rect) {
const x1 = this.x < rect.x ? this.x : rect.x;
const x2 = this.x2 > rect.x2 ? this.x2 : rect.x2;
const y1 = this.y < rect.y ? this.y : rect.y;
const y2 = this.y2 > rect.y2 ? this.y2 : rect.y2;
return new DotMoveRectangle(x1, y1, x2 - x1, y2 - y1);
}
}
DotMoveSystem.DotMoveRectangle = DotMoveRectangle;
class Degree {
get value() {
return this._value;
}
static fromDirection(direction) {
switch (direction) {
case 8:
return Degree.UP;
case 9:
return Degree.UP_RIGHT;
case 6:
return Degree.RIGHT;
case 3:
return Degree.RIGHT_DOWN;
case 2:
return Degree.DOWN;
case 1:
return Degree.DOWN_LEFT;
case 4:
return Degree.LEFT;
case 7:
return Degree.LEFT_UP;
default:
throw new Error(`${direction} is not found`);
}
}
static fromRad(rad) {
return new Degree((rad * 180 / Math.PI) + 90);
}
constructor(...args) {
this.initialize(...args);
}
initialize(value) {
value %= 360;
if (value < 0)
value = 360 + value;
this._value = value;
}
toRad() {
return (this._value - 90) * Math.PI / 180;
}
toDirection8() {
const t = Math.round(this._value / 45);
if (t === 0 || t === 8) {
return 8;
}
else if (t === 1) {
return 9;
}
else if (t === 2) {
return 6;
}
else if (t === 3) {
return 3;
}
else if (t === 4) {
return 2;
}
else if (t === 5) {
return 1;
}
else if (t === 6) {
return 4;
}
else if (t === 7) {
return 7;
}
else {
throw new Error(`${this._value} is not found`);
}
}
toDirection4(lastDirection) {
const t = Math.round(this._value / 45);
if (t === 0 || t === 8) {
return 8;
}
else if (t === 1) {
if (lastDirection === 8)
return 8;
return 6;
}
else if (t === 2) {
return 6;
}
else if (t === 3) {
if (lastDirection === 6)
return 6;
return 2;
}
else if (t === 4) {
return 2;
}
else if (t === 5) {
if (lastDirection === 2)
return 2;
return 4;
}
else if (t === 6) {
return 4;
}
else if (t === 7) {
if (lastDirection === 4)
return 4;
return 8;
}
else {
throw new Error(`${this._value} is not found`);
}
}
}
Degree.UP = new Degree(0);
Degree.UP_RIGHT = new Degree(45);
Degree.RIGHT = new Degree(90);
Degree.RIGHT_DOWN = new Degree(135);
Degree.DOWN = new Degree(180);
Degree.DOWN_LEFT = new Degree(225);
Degree.LEFT = new Degree(270);
Degree.LEFT_UP = new Degree(315);
DotMoveSystem.Degree = Degree;
class AStarNode {
get parent() { return this._parent; }
set parent(_parent) { this._parent = _parent; }
get x() { return this._x; }
get y() { return this._y; }
get f() { return this._f; }
set f(_f) { this._f = _f; }
get g() { return this._g; }
set g(_g) { this._g = _g; }
get closed() { return this._closed; }
set closed(_closed) { this._closed = _closed; }
constructor(...args) {
this.initialize(...args);
}
initialize(parent, x, y, f, g, closed = false) {
this._parent = parent;
this._x = x;
this._y = y;
this._f = f;
this._g = g;
this._closed = closed;
}
}
DotMoveSystem.AStarNode = AStarNode;
class AStarUtils {
// 8方向A*経路探索を行い最適ノードと初期ノードを返す
static computeRoute(character, startX, startY, goalX, goalY, searchLimit) {
if (startX === goalX && startY === goalY) {
return undefined;
}
const openList = [];
const nodes = {};
const start = new AStarNode(undefined, startX, startY, $gameMap.distance(startX, startY, goalX, goalY), 0);
const posStart = startY * $gameMap.width() + startX;
openList.push(posStart);
nodes[posStart] = start;
let best = start;
while (openList.length > 0) {
let openListIdx1 = 0;
if (openList.length > 1) {
for (let i = 1; i < openList.length; i++) {
const nodeA = nodes[openList[i]];
const nodeB = nodes[openList[openListIdx1]];
if (nodeA.f < nodeB.f)
openListIdx1 = i;
}
}
const pos1 = openList[openListIdx1];
const node1 = nodes[pos1];
const x1 = node1.x;
const y1 = node1.y;
const g1 = node1.g;
if (x1 === goalX && y1 === goalY)
return { best: node1, start };
if (node1.g >= searchLimit)
return { best, start };
node1.closed = true;
openList.splice(openListIdx1, 1);
for (const direction of [8, 9, 6, 3, 2, 1, 4, 7]) {
const { horz, vert } = DotMoveUtils.direction2HorzAndVert(direction);
const x2 = $gameMap.roundXWithDirection(x1, horz);
const y2 = $gameMap.roundYWithDirection(y1, vert);
const pos2 = y2 * $gameMap.width() + x2;
let node2 = nodes[pos2];
if (node2 && node2.closed)
continue;
let successPass = false;
if (direction % 2 === 0) {
if (character.canPass(x1, y1, direction)) {
successPass = true;
}
}
else {
if (character.canPassDiagonally(x1, y1, horz, vert)) {
successPass = true;
}
}
if (!successPass) {
if (x2 === goalX && y2 === goalY) {
if (direction % 2 === 0) {
if (character.canPass(x1, y1, direction, { needCheckCharacters: false })) {
return { best: node1, start };
}
}
}
continue;
}
const cost = direction % 2 === 0 ? 1 : DotMoveUtils.DIAGONAL_COST;
const g2 = g1 + cost;
const f2 = g2 + $gameMap.distance(x2, y2, goalX, goalY);
const openListIdx2 = openList.indexOf(pos2);
if (openListIdx2 < 0) {
node2 = new AStarNode(node1, x2, y2, f2, g2);
nodes[pos2] = node2;
openList.push(pos2);
}
else if (f2 < node2.f) {
node2.f = f2;
node2.g = g2;
node2.parent = node1;
openList.splice(openListIdx2, 1);
openList.push(pos2);
}
if (node2.f - node2.g < best.f - best.g)
best = node2;
}
}
return { best, start };
}
}
DotMoveSystem.AStarUtils = AStarUtils;
class MassInfo {
get x() { return this._x; }
get y() { return this._y; }
constructor(...args) {
this.initialize(...args);
}
initialize(x, y) {
this._x = x;
this._y = y;
}
}
DotMoveSystem.MassInfo = MassInfo;
class MassRange {
static fromRect(rect) {
const x = Math.floor(rect.x);
const y = Math.floor(rect.y);
const x2 = Math.ceil(rect.x2) - 1;
const y2 = Math.ceil(rect.y2) - 1;
return new MassRange(x, y, x2, y2);
}
get x() { return this._x; }
get y() { return this._y; }
get x2() { return this._x2; }
get y2() { return this._y2; }
constructor(...args) {
this.initialize(...args);
}
initialize(x, y, x2, y2) {
this._x = x;
this._y = y;
this._x2 = x2;
this._y2 = y2;
}
masses() {
const masses = new Set();
for (let ix = this.x; ix <= this.x2; ix++) {
for (let iy = this.y; iy <= this.y2; iy++) {
const ix2 = $gameMap.roundX(ix);
const iy2 = $gameMap.roundY(iy);
if ($gameMap.isValid(ix2, iy2)) {
const i = iy2 * $gameMap.width() + ix2;
masses.add(i);
}
}
}
return masses;
}
}
DotMoveSystem.MassRange = MassRange;
class DotMoveUtils {
static isFloatLt(left, right, margin = 1.0 / DotMoveUtils.MARGIN_UNIT) {
if (left <= right - margin)
return true;
if (left <= right + margin)
return true;
return false;
}
static isFloatGt(left, right, margin = 1.0 / DotMoveUtils.MARGIN_UNIT) {
if (left >= right - margin)
return true;
if (left >= right + margin)
return true;
return false;
}
static calcDistance(deg, far) {
const dis = DotMovePoint.fromDegAndFar(deg, far);
const marginUnit = DotMoveUtils.MARGIN_UNIT;
dis.x = Math.round(dis.x * marginUnit) / marginUnit;
dis.y = Math.round(dis.y * marginUnit) / marginUnit;
return dis;
}
static nextPointWithDirection(point, direction, unit = 1) {
let x = point.x;
let y = point.y;
const signPoint = this.direction2SignPoint(direction);
x += signPoint.x * unit;
y += signPoint.y * unit;
x = $gameMap.roundX(x);
y = $gameMap.roundY(y);
return new DotMovePoint(x, y);
}
static prevPointWithDirection(point, direction, unit = 1) {
let x = point.x;
let y = point.y;
const signPoint = this.direction2SignPoint(direction);
x -= signPoint.x * unit;
y -= signPoint.y * unit;
x = $gameMap.roundX(x);
y = $gameMap.roundY(y);
return new DotMovePoint(x, y);
}
static direction2Axis(direction4) {
if (direction4 === 4 || direction4 === 6) {
return "x";
}
else if (direction4 === 8 || direction4 === 2) {
return "y";
}
else {
throw new Error(`${direction4} is not found`);
}
}
static direction2SignPoint(direction) {
const { horz, vert } = this.direction2HorzAndVert(direction);
const xSign = horz === 4 ? -1 : horz === 6 ? 1 : 0;
const ySign = vert === 8 ? -1 : vert === 2 ? 1 : 0;
return new DotMovePoint(xSign, ySign);
}
static direction2HorzAndVert(direction) {
let horz = 0, vert = 0;
switch (direction) {
case 8:
vert = 8;
break;
case 9:
horz = 6;
vert = 8;
break;
case 6:
horz = 6;
break;
case 3:
horz = 6;
vert = 2;
break;
case 2:
vert = 2;
break;
case 1:
horz = 4;
vert = 2;
break;
case 4:
horz = 4;
break;
case 7:
horz = 4;
vert = 8;
break;
}
return { horz, vert };
}
static horzAndVert2Direction(horz, vert) {
if (vert === 8 && horz === 6) {
return 9;
}
else if (vert === 2 && horz === 6) {
return 3;
}
else if (vert === 2 && horz === 4) {
return 1;
}
else if (vert === 8 && horz === 4) {
return 7;
}
return vert === 0 ? horz : vert;
}
static checkCollidedRect(rect1, rect2, targetObject) {
if (rect1.isCollidedRect(rect2)) {
const result = new CollisionResult(rect1, rect2, targetObject);
if (result.collisionLengthX() > 0 && result.collisionLengthY() > 0)
return result;
}
return undefined;
}
}
DotMoveUtils.DIAGONAL_COST = 1 / Math.sin(Math.PI / 4);
DotMoveUtils.MARGIN_UNIT = 65536;
DotMoveUtils.MOVED_MARGIN_UNIT = Math.sqrt(DotMoveUtils.MARGIN_UNIT);
DotMoveSystem.DotMoveUtils = DotMoveUtils;
class CollisionResult {
constructor(...args) {
this.initialize(...args);
}
initialize(subjectRect, targetRect, targetObject) {
this._subjectRect = subjectRect;
this._targetRect = targetRect;
this._targetObject = targetObject;
const margin = 1.0 / DotMoveUtils.MARGIN_UNIT;
const rightCollisionLength = this.calcRightCollisionLength();
const leftCollisionLength = this.calcLeftCollisionLength();
const upCollisionLength = this.calcUpCollisionLength();
const downCollisionLength = this.calcDownCollisionLength();
this._rightCollisionLength = rightCollisionLength < margin ? 0 : rightCollisionLength;
this._leftCollisionLength = leftCollisionLength < margin ? 0 : leftCollisionLength;
this._upCollisionLength = upCollisionLength < margin ? 0 : upCollisionLength;
this._downCollisionLength = downCollisionLength < margin ? 0 : downCollisionLength;
}
get subjectRect() { return this._subjectRect; }
get targetRect() { return this._targetRect; }
get targetObject() { return this._targetObject; }
getCollisionLength(axis) {
if (axis === "x") {
return this.collisionLengthX();
}
else {
return this.collisionLengthY();
}
}
getCollisionLengthByDirection(dir) {
switch (dir) {
case 8:
return this.upCollisionLength();
case 6:
return this.rightCollisionLength();
case 2:
return this.downCollisionLength();
case 4:
return this.leftCollisionLength();
default:
return 0;
}
}
collisionLengthX() {
const leftCollisionLength = this.leftCollisionLength();
const rightCollisionLength = this.rightCollisionLength();
if (leftCollisionLength < rightCollisionLength) {
return leftCollisionLength;
}
else {
return rightCollisionLength;
}
}
collisionLengthY() {
const upCollisionLength = this.upCollisionLength();
const downCollisionLength = this.downCollisionLength();
if (upCollisionLength < downCollisionLength) {
return upCollisionLength;
}
else {
return downCollisionLength;
}
}
upCollisionLength() {
return this._upCollisionLength;
}
rightCollisionLength() {
return this._rightCollisionLength;
}
downCollisionLength() {
return this._downCollisionLength;
}
leftCollisionLength() {
return this._leftCollisionLength;
}
calcUpCollisionLength() {
if (this._subjectRect.y < this._targetRect.y2) {
return this._subjectRect.y2 - this._targetRect.y;
}
else {
return this._subjectRect.height;
}
}
calcRightCollisionLength() {
if (this._targetRect.x2 < this._subjectRect.x2) {
return this._targetRect.x2 - this._subjectRect.x;
}
else {
return this._subjectRect.width;
}
}
calcDownCollisionLength() {
if (this._targetRect.y2 < this._subjectRect.y2) {
return this._targetRect.y2 - this._subjectRect.y;
}
else {
return this._subjectRect.height;
}
}
calcLeftCollisionLength() {
if (this._subjectRect.x < this._targetRect.x2) {
return this._subjectRect.x2 - this._targetRect.x;
}
else {
return this._subjectRect.width;
}
}
}
DotMoveSystem.CollisionResult = CollisionResult;
class MapCharactersCache {
constructor(...args) {
this.initialize(...args);
}
initialize(width, height) {
this._cache = new Array(width * height);
}
massCharacters(mass) {
return this._cache[mass];
}
addMapCharactersCache(mass, character) {
if (this._cache[mass] == null)
this._cache[mass] = new Set();
this._cache[mass].add(character);
}
removeMapCharactersCache(mass, character) {
if (this._cache[mass] != null)
this._cache[mass].delete(character);
}
}
DotMoveSystem.MapCharactersCache = MapCharactersCache;
// キャラクターの衝突判定を実施する。
class CharacterCollisionChecker {
constructor(...args) {
this.initialize(...args);
}
initialize(character, opt = {}) {
this._character = character;
const pos = this._character.positionPoint();
this._origX = opt.origX == null ? pos.x : opt.origX;
this._origY = opt.origY == null ? pos.y : opt.origY;
// overComplementModeがtrueの場合は衝突幅がキャラクターの幅を超える場合に衝突幅の修正を行う
this._overComplementMode = opt.overComplementMode == null ? false : opt.overComplementMode;
// throughIfCollidedがtrueの場合は既に衝突が発生していたときの衝突判定を無効にする
this._throughIfCollided = opt.throughIfCollided == null ? false : opt.throughIfCollided;
}
checkCollision(x, y, d) {
const collisionResults = [];
collisionResults.push(...this.checkCollisionMasses(x, y, d));
// マップの範囲有効判定をマスの衝突確認で実施する必要があるため
// すり抜けを行う場合このタイミングでreturnする
if (this._character.isThrough() || this._character.isDebugThrough())
return collisionResults;
collisionResults.push(...this.checkCollisionCharacters(x, y, d, Game_CharacterBase));
return collisionResults;
}
checkHitCharacters(x, y, d, targetCharacterClass) {
const collisionResults = [];
for (const character of this.enteringMassesCharacters(x, y)) {
if (!(character instanceof targetCharacterClass))
continue;
const result = this.checkCharacter(x, y, d, character);
if (result)
collisionResults.push(result);
}
return collisionResults;
}
checkCharacter(x, y, d, character) {
const targetPos = character.positionPoint();
const targetX = this._characterIntPosMode ? character.x : targetPos.x;
const targetY = this._characterIntPosMode ? character.y : targetPos.y;
const subjectRect = new DotMoveRectangle(x, y, this._character.width(), this._character.height());
const targetRect = new DotMoveRectangle(targetX, targetY, character.width(), character.height());
return this.checkCollidedRect(d, subjectRect, targetRect, character);
}
checkCollisionMasses(x, y, d) {
const collisionResults = [];
const subjectRect = new DotMoveRectangle(x, y, this._character.width(), this._character.height());
const massRange = this.calcSubjectCharacterMassRange(x, y);
for (let ix = massRange.x; ix <= massRange.x2; ix++) {
for (let iy = massRange.y; iy <= massRange.y2; iy++) {
const ix2 = $gameMap.roundX(ix);
const iy2 = $gameMap.roundY(iy);
if (!this.isNoCheckMass(ix, iy, d, massRange)) {
collisionResults.push(...this.checkCollisionMass(subjectRect, d, ix2, iy2));
}
}
}
if (collisionResults.length > 0)
return collisionResults;
const cliffCollisionResult = this.checkCollisionCliff(subjectRect, massRange, d);
collisionResults.push(...cliffCollisionResult);
return collisionResults;
}
// 最後尾のマスは衝突確認の対象外とする。
isNoCheckMass(ix, iy, d, massRange) {
switch (d) {
case 8:
if (iy === massRange.y2)
return true;
break;
case 6:
if (ix === massRange.x)
return true;
break;
case 2:
if (iy === massRange.y)
return true;
break;
case 4:
if (ix === massRange.x2)
return true;
break;
}
return false;
}
checkCollisionMass(subjectRect, d, ix, iy) {