/
QMovement.js
3916 lines (3637 loc) · 127 KB
/
QMovement.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
//=============================================================================
// QMovement
//=============================================================================
var Imported = Imported || {};
if (!Imported.QPlus || !QPlus.versionCheck(Imported.QPlus, '1.6.0')) {
alert('Error: QMovement requires QPlus 1.6.0 or newer to work.');
throw new Error('Error: QMovement requires QPlus 1.6.0 or newer to work.');
}
Imported.QMovement = '1.6.3';
//=============================================================================
/*:
* @plugindesc <QMovement>
* More control over character movement
* @version 1.6.3
* @author Quxios | Version 1.6.3
* @site https://quxios.github.io/
* @updateurl https://quxios.github.io/data/pluginsMin.json
*
* @repo https://github.com/quxios/QMovement
*
* @requires QPlus
*
* @video TODO
*
* @param Main Settings
*
* @param Grid
* @parent Main Settings
* @desc The amount of pixels you want to move per Movement.
* Plugin Default: 1 MV Default: 48
* @type Number
* @min 1
* @default 1
*
* @param Tile Size
* @parent Main Settings
* @desc Size of tiles in pixels
* Default: 48
* @type Number
* @min 1
* @default 48
*
* @param Off Grid
* @parent Main Settings
* @desc Allow characters to move off grid?
* @type boolean
* @on Yes
* @off No
* @default true
*
* @param Optional Settings
*
* @param Smart Move
* @parent Optional Settings
* @desc If the move didn't succeed, try again at lower speeds or at
* a different direction
* @type select
* @option Disabled
* @value 0
* @option Adjust Speed
* @value 1
* @option Adjust Direction
* @value 2
* @option Adjust Speed and Direction
* @value 3
* @default 2
*
* @param Mid Pass
* @parent Optional Settings
* @desc An extra collision check for the midpoint of the Movement.
* @type boolean
* @on Enable
* @off Disable
* @default false
*
* @param Move on click
* @parent Optional Settings
* @desc Set if player moves with mouse click
* * Requires QPathfind to work
* @type boolean
* @on Enable
* @off Disable
* @default true
*
* @param Diagonal
* @parent Optional Settings
* @desc Allow for diagonal movement?
* @type boolean
* @on Yes
* @off No
* @default true
*
* @param Diagonal Speed
* @parent Optional Settings
* @desc Adjust the speed when moving diagonal.
* Default: 0 TODO not functional
* @type Number
* @min 0
* @default 0
*
* @param Colliders
*
* @param Player Collider
* @text Default Player Collider
* @parent Colliders
* @desc Default collider for the player.
* @type Struct<Collider>
* @default {"Type":"box","Width":"36","Height":"24","Offset X":"6","Offset Y":"24"}
*
* @param Event Collider
* @text Default Event Collider
* @parent Colliders
* @desc Default collider for events.
* @type Struct<Collider>
* @default {"Type":"box","Width":"36","Height":"24","Offset X":"6","Offset Y":"24"}
*
* @param Presets
* @parent Colliders
* @desc List of preset colliders that you can assign to
* events
* @type Struct<ColliderPreset>[]
* @default []
*
* @param Debug Settings
*
* @param Show Colliders
* @parent Debug Settings
* @desc Show the Box Colliders by default during testing.
* -Can be toggled on/off with F10 during play test
* @type boolean
* @on Show by default
* @off Hide by default
* @default true
*
* @help
* ============================================================================
* ## About
* ============================================================================
* This plugin completely rewrites the collision system to use colliders. Using
* colliders enabled more accurate collision checking with dealing with pixel
* movement. This plugin also lets you change how many pixels the characters
* move per step, letting you set up a 24x24 movement or a 1x1 (pixel movement)
*
* Note there are a few mv features disabled/broken; mouse movement, followers,
* and vehicles.
* ============================================================================
* ## Setting up
* ============================================================================
* To setup a pixel based movement, you'll need to change the plugin parameters
* to something like:
*
* - Grid = 1
* - Off Grid = true
* - Mid Pass = false
*
* Other parameters can be set to your preference.
*
* For a grid based movement, set it something like:
*
* - Grid = GRIDSIZE YOU WANT
* - Off Grid = false
* - Mid Pass = true
*
* When in grid based movement, you want your colliders to fill up most of the
* grid size but with a padding of 4 pixels on all sides (this is because some
* tile colliders are 4 tiles wide or tall). So if your grid size was 48, your
* colliders shouldn't be 48x48, instead they should be 40x40, with an ox and oy
* of 4. So your collider setting would look like: box, 40, 40, 4, 4
* ============================================================================
* ## Colliders
* ============================================================================
* There are 3 types of colliders; Polygon, Box and Circle. Though you can only
* create box and circle colliders, unless you modify the code to accept
* polygons. This is intentional since polygon would be "harder" to setup.
*
* ![Colliders Image](https://quxios.github.io/imgs/qmovement/colliders.png)
*
* - Boxes takes in width, height, offset x and offset y
* - Circles similar to boxes, takes in width, height, offset x and offset y
* ----------------------------------------------------------------------------
* **Setting up colliders**
* ----------------------------------------------------------------------------
* Colliders are setup inside the Players notebox or as a comment inside an
* Events page. Events colliders depends it's page, so you may need to make the
* collider on all pages.
*
* There are two ways to setup colliders. using `<collider:-,-,-,->` and using
* `<colliders>-</colliders>`. The first method sets the 'Default' collider for
* that character. The second one you create the colliders for every collider
* type.
* ----------------------------------------------------------------------------
* **Collider Types**
* ----------------------------------------------------------------------------
* There are 3 collider types. Default, Collision and Interaction.
* - Default: This is the collider to use if collider type that was called was
* not found
* - Collision: This collider is used for collision checking
* - Interaction: This collider is used for checking interaction.
* ----------------------------------------------------------------------------
* **Collider Presets**
* ----------------------------------------------------------------------------
* You can create colliders in the plugin parameters which you can use when
* setting up other colliders.
* ----------------------------------------------------------------------------
* **Collider Terms**
* ----------------------------------------------------------------------------
* ![Colliders Terms Image](https://quxios.github.io/imgs/qmovement/colliderInfo.png)
* ----------------------------------------------------------------------------
* **Collider Notetag**
* ----------------------------------------------------------------------------
* ~~~
* <collider: [SHAPE], [WIDTH], [HEIGHT], [OX], [OY]>
* ~~~
* This notetag sets all collider types to these values.
* - SHAPE: Set to box, circle or poly
* - If poly read next section on poly shape
* - WIDTH: The width of the collider, in pixels
* - HEIGHT: The height of the collider, in pixels
* - OX: The X Offset of the collider, in pixels
* - OY: The Y Offset of the collider, in pixels
* ----------------------------------------------------------------------------
* **Colliders Notetag**
* ----------------------------------------------------------------------------
* ~~~
* <colliders>
* [TYPE]: [SHAPE], [WIDTH], [HEIGHT], [OX], [OY]
* </colliders>
* ~~~
* This notetag sets all collider types to these values.
* - TYPE: The type of collider, set to default, collision or interaction
* - SHAPE: Set to box, circle or poly
* - If poly read next section on poly shape
* - WIDTH: The width of the collider, in pixels
* - HEIGHT: The height of the collider, in pixels
* - OX: The X Offset of the collider, in pixels
* - OY: The Y Offset of the collider, in pixels
*
* To add another type, just add `type: shape, width, height, ox, oy` on
* another line.
*
* Example:
* ~~~
* <colliders>
* default: box, 48, 48
* collision: circle, 24, 24, 12, 12
* interaction: box, 32, 32, 8, 8
* </colliders>
* ~~~
* ----------------------------------------------------------------------------
* **Using Preset**
* ----------------------------------------------------------------------------
* To use a collider preset in the notetag, the format is:
* ~~~
* preset, [PRESETID]
* ~~~
* - PRESETID: The PresetID you set in the preset parameter.
*
* You will use this format instead of the: `SHAPE, WIDTH, HEIGHT, OX, OY`
*
* Example:
* ~~~
* <collider: preset, largeCollider>
* ~~~
* Will look for the preset with the ID `largeCollider`
*
* Example 2:
* ~~~
* <colliders>
* default: preset, largeDefault
* collision: preset, largeCollider
* interaction: preset, largeInteraction
* </colliders>
* ~~~
* Will use the presets; `largeDefault`, `largeCollider`, and `largeInteraction`
* ----------------------------------------------------------------------------
* **Poly Colliders**
* ----------------------------------------------------------------------------
* To create a polygon collider, set the shape to poly. After that the rest
* of the line should be a list of points separated with a comma. Points are
* written as "(X,Y)". An example polygon would be:
* ~~~
* poly,(24,0),(48,24),(24,48),(0,24)
* ~~~
* Would create a diamond shaped polygon.
*
* Example of using it inside a collider tag
* ~~~
* <collider:poly,(24,0),(48,24),(24,48),(0,24)>
* ~~~
* ============================================================================
* ## Move Routes
* ============================================================================
* By default, event move commands (moveup, movedown, ect) will convert to a
* qmove that moves the character based off your tilesize. So if your tilesize
* is 48 and your gridsize is 1. Then a moveup command will move the character
* up 48 pixels not 1. But if you want to move the character by a fixed amount
* of pixels, then you will use the QMove commands.
* ----------------------------------------------------------------------------
* **QMove**
* ----------------------------------------------------------------------------
* ![QMove Script Call](https://quxios.github.io/imgs/qmovement/qmove.png)
*
* To do a QMove, add a script in the move route in the format:
* ~~~
* qmove(DIR, AMOUNT, MULTIPLER)
* ~~~
* - DIR: Set to a number representing the direction to move;
* - 4: left, 6: right, 8: up 2: down,
* - 1: lower left, 3: lower right, 7: upper left, 9: upper right,
* - 5: current direction, 0: reverse direction
* - AMOUNT: The amount to move in that direction, in pixels
* - MULTIPLIER: multiplies against amount to make larger values easier [OPTIONAL]
*
* Example:
* ~~~
* qmove(4, 24)
* ~~~
* Will move that character 24 pixels to the left.
* ----------------------------------------------------------------------------
* **Arc**
* ----------------------------------------------------------------------------
* Arcing is used to make a character orbit around a position. Note that collisions
* are ignored when arcing, but interactions still work. To add a arc add a script
* in the move route in the format:
* ~~~
* arc(PIVOTX, PIVOTY, RADIAN, CCWISE?, FRAMES)
* ~~~
* - PIVOTX: The x position to orbit around, in pixels
* - PIVOTY: The y position to orbit around, in pixels
* - RADIAN: The degrees to move, in radians
* - CCWISE?: set to true or false; if true it will arc countclock wise
* - FRAMES: The amount of frames to complete the arc
*
* Example:
* ~~~
* arc(480,480,Math.PI*2,false,60)
* ~~~
* Will make the character do a full 360 arc clockwise around the point 480, 480
* and it'll take 60 frames.
* ============================================================================
* ## Event Notetags/Comments
* ============================================================================
* **Offsets**
* ----------------------------------------------------------------------------
* To shift an events initial starting position, you can use the following
* note tags:
* ~~~
* <ox:X>
* ~~~
* or
* ~~~
* <oy:X>
* ~~~
* Where X is the number of pixels to shift the event. Can be negative.
* ----------------------------------------------------------------------------
* **SmartDir**
* ----------------------------------------------------------------------------
* By default, when the player collides with an event it won't trigger the
* Smart Move Dir effect. To enable this, add following notetag to the event:
* ~~~
* <smartDir>
* ~~~
* ----------------------------------------------------------------------------
* **IgnoreCharas**
* ----------------------------------------------------------------------------
* You can have an event ignore certain characters when collision checking. This
* allows you to let some events move through some events or the player. Note that
* this is not 2 ways, so if an event can move through the player, that doesn't
* mean the player can move through the event.
* ~~~
* <ignoreCharas:CHARAIDS>
* ~~~
* Where CHARAIDS is a list of character Ids, separated by a comma
* ============================================================================
* ## Map Notetags
* ============================================================================
* **GridSize**
* ----------------------------------------------------------------------------
* You can set the grid size for certain maps by using the notetag:
* ~~~
* <grid:X>
* ~~~
* Where X is the grid size to use for this map.
* ----------------------------------------------------------------------------
* **OffGrid**
* ----------------------------------------------------------------------------
* You can set weither you can or can't move off the grid for certain maps by
* using the notetag:
* ~~~
* <offGrid:BOOL>
* ~~~
* Where BOOL is true or false
* ----------------------------------------------------------------------------
* **MidPass**
* ----------------------------------------------------------------------------
* You can set weither you want to use the mid pass function for certain maps by
* using the notetag:
* ~~~
* <midPass:BOOL>
* ~~~
* Where BOOL is true or false
* ============================================================================
* ## Plugin Commands
* ============================================================================
* **Transfer**
* ----------------------------------------------------------------------------
* MV event transfers are grid based. So this plugin command lets you map transfer
* to a pixel x / y position.
* ~~~
* qMovement transfer [MAPID] [X] [Y] [OPTIONS]
* ~~~
* - MAPID: The id of the map to transfer to
* - X: The x position to transfer to, in pixels
* - Y: The y position to transfer to, in pixels
*
* Possible options:
*
* - dirX: Set X to the dir to face after the transfer.
* - Can be 2, 4, 6, 8, or for diagonals 1, 3, 7, 9
* - fadeBlack: Will fade black when transfering
* - fadeWhite: Will fade white when transfering
*
* Example:
* ~~~
* qMovement transfer 1 100 116 dir2 fadeBlack
* ~~~
* Will transfer the player to map 1 at x100, y116. There will be a black fade
* and player will be facing down
* ~~~
* qMovement transfer 1 100 116
* ~~~
* Will transfer the player to map 1 at x100, y116. There will be no fade and
* players direction won't change
* ----------------------------------------------------------------------------
* **Set Pos**
* ----------------------------------------------------------------------------
* This command will let you move a character to a x / y pixel position. Note
* this will not "walk" the character to that position! This will place the
* character at this position, similar to a transfer.
* ~~~
* qMovement setPos [CHARAID] [X] [Y] [OPTIONS]
* ~~~
* - CHARAID: The character identifier.
* - For player: 0, p, or player
* - For events: EVENTID, eEVENTID, eventEVENTID or this for the event that called this (replace EVENTID with a number)
* - X: The x position to set to, in pixels
* - Y: The y position to set to, in pixels
*
* Possible options:
*
* - dirX: Set X to the dir to face after the transfer.
* - Can be 2, 4, 6, 8, or for diagonals 1, 3, 7, 9
*
* ----------------------------------------------------------------------------
* **Change Collider**
* ----------------------------------------------------------------------------
* This command will let you change a collider for a character. Note that you
* should use this carefully. If you don't you can get that character stuck.
* ~~~
* qMovement changeCollider [CHARAID] [TYPE] [SHAPE] [WIDTH] [HEIGHT] [OX] [OY]
* ~~~
* - CHARAID: The character identifier.
* - For player: 0, p, or player
* - For events: EVENTID, eEVENTID, eventEVENTID or this for the event that called this (replace EVENTID with a number)
* - TYPE: The type of collider, set to default, collision or interaction
* - SHAPE: Set to box or circle
* - WIDTH: The width of the collider, in pixels
* - HEIGHT: The height of the collider, in pixels
* - OX: The X Offset of the collider, in pixels
* - OY: The Y Offset of the collider, in pixels
*
* You can also set it to a preset by using the format:
* ~~~
* qMovement changeCollider [CHARAID] [TYPE] preset [PRESETID]
* ~~~
* - CHARAID: The character identifier.
* - For player: 0, p, or player
* - For events: EVENTID, eEVENTID, eventEVENTID or this for the event that called this (replace EVENTID with a number)
* - TYPE: The type of collider, set to default, collision or interaction
* - PRESETID: The PresetID you set in the preset parameter.
* ============================================================================
* ## Tips
* ============================================================================
* **No closed open spaces!**
* ----------------------------------------------------------------------------
* For performance reasons, you should try to avoid having open spaces that are
* closed off.
*
* ![Example](https://quxios.github.io/imgs/qmovement/openSpaces.png)
*
* On the left we can see some tiles that have a collider border, but their inside
* is "open". This issue is should be corrected when using QPathfind because
* if someone was to click inside that "open" space, it is passable and QPathfind
* will try to find a way in even though there is no way in and will cause massive
* lag. The fix can be pretty simple, you could add a CollisionMap (though that
* may be another issue in its own) or add a RegionCollider to fill up the full
* tile like I did on the correct side of that image.
* ----------------------------------------------------------------------------
* **Collision Maps - Heavy**
* ----------------------------------------------------------------------------
* Try to use collision maps only if you absolutely need to. Collision maps
* can be very large images which will make your game use more memory and can
* cause some slower pcs to start lagging. The collision checking for collision
* maps are also take about 2-4x more time to compute and is a lot less accurate
* since it only checks if the colliders edge collided with the collision map.
* So using collision maps, might be pretty, but use it with caution as it can
* slow down your game! A better solution for this would be to use a PolygonMap
* where you create polygon colliders and add them into the map.
* ============================================================================
* ## Addons
* ============================================================================
* **Pathfind**
* ----------------------------------------------------------------------------
* https://quxios.github.io/plugins/QPathfind
*
* QPathfind is an A* pathfinding algorithm. This algorithm can be pretty heavy
* if you are doing pixel based movements. So avoid having to many pathfinders
* running at the same time.
*
* For the interval settings, you want to set this to a value where the path
* can be found in 1-3 frames. You can think of intervals as the number of
* moves to try per frame. The default setting 100, is good for grid based
* since that will take you 100 grid spaces away. But for a pixel based, 100
* steps might not be as far. If most of your pathfinds will be short (paths less then
* 10 tiles away), then you should set this to a value between 100-300. For medium
* paths (10-20 tiles away) try a value between 300-700. For large or complicated
* paths (20+ tiles away or lots of obsticles) try something between 1000-2000.
* I would avoid going over 2000. My opinion is to keep it below 1000, and simplify
* any of your larger paths by either splitting it into multiple pathfinds or
* just making the path less complex.
*
* ----------------------------------------------------------------------------
* **Collision Map**
* ----------------------------------------------------------------------------
* https://quxios.github.io/plugins/QM+CollisionMap
*
* Collision Map is an addon for this plugin that lets you use images for
* collisions. Note that collision map checks are a lot heavier then normal
* collision checks. So this plugin can make your game laggier if used with
* other heavy plugins.
*
* ----------------------------------------------------------------------------
* **Region Colliders**
* ----------------------------------------------------------------------------
* https://quxios.github.io/plugins/QM+RegionColliders
*
* Region Colliders is an addon for this plugin that lets you add colliders
* to regions by creating a json file.
* ============================================================================
* ## Showcase
* ============================================================================
* This section is for user created stuff. If you created a video, game, tutorial,
* or an addon for QMovement feel free to send me a link and I'll showcase it here!
* ----------------------------------------------------------------------------
* **Videos**
* ----------------------------------------------------------------------------
* Great example of using the collision map addon:
*
* https://www.youtube.com/watch?v=-BN4Pyr5IBo
*
* ============================================================================
* ## Links
* ============================================================================
* Formated Help:
*
* https://quxios.github.io/plugins/QMovement
*
* RPGMakerWebs:
*
* http://forums.rpgmakerweb.com/index.php?threads/qplugins.73023/
*
* Terms of use:
*
* https://github.com/quxios/QMV-Master-Demo/blob/master/readme.md
*
* Like my plugins? Support me on Patreon!
*
* https://www.patreon.com/quxios
*
* @tags movement, pixel, character
*/
/*~struct~Collider:
* @param Type
* @desc Set to box or circle
* @type select
* @option Box
* @value box
* @option Circle
* @value circle
* @default box
*
* @param Width
* @desc Set to the width of the collider.
* @type Number
* @default 36
*
* @param Height
* @desc Set to the height of the collider.
* @type Number
* @default 24
*
* @param Offset X
* @desc Set to the x offset of the collider.
* @type Number
* @min -9999
* @default 6
*
* @param Offset Y
* @desc Set to the y offset of the collider.
* @type Number
* @min -9999
* @default 24
*/
/*~struct~ColliderPreset:
* @param ID
* @desc The ID of this preset, needs to be unique!
* @default
*
* @param Type
* @desc Set to box or circle
* @type select
* @option Box
* @value box
* @option Circle
* @value circle
* @default box
*
* @param Width
* @desc Set to the width of the collider.
* @type Number
* @default 36
*
* @param Height
* @desc Set to the height of the collider.
* @type Number
* @default 24
*
* @param Offset X
* @desc Set to the x offset of the collider.
* @type Number
* @default 6
*
* @param Offset Y
* @desc Set to the y offset of the collider.
* @type Number
* @default 24
*/
//=============================================================================
//=============================================================================
// QMovement Static Class
function QMovement() {
throw new Error('This is a static class');
}
(function() {
var _PARAMS = QPlus.getParams('<QMovement>', {
'Player Collider': {
"Type": "box", "Width": 36,"Height":24 ,"Offset X": 6,"Offset Y": 24
},
'Event Collider': {
"Type": "box", "Width": 36,"Height":24 ,"Offset X": 6,"Offset Y": 24
},
'Presets': []
});
QMovement.grid = _PARAMS['Grid'];
QMovement.tileSize = _PARAMS['Tile Size'];
QMovement.offGrid = _PARAMS['Off Grid'];
QMovement.smartMove = _PARAMS['Smart Move'];
QMovement.midPass = _PARAMS['Mid Pass'];
QMovement.moveOnClick = _PARAMS['Move on click'];
QMovement.diagonal = _PARAMS['Diagonal'];
QMovement.collision = '#FF0000'; // will be changable in a separate addon
QMovement.water1 = '#00FF00'; // will be changable in a separate addon
QMovement.water2 = '#0000FF'; // will be changable in a separate addon
QMovement.water1Tag = 1; // will be changable in a separate addon
QMovement.water2Tag = 2; // will be changable in a separate addon
QMovement.playerCollider = convertColliderStruct(_PARAMS['Player Collider']);
QMovement.eventCollider = convertColliderStruct(_PARAMS['Event Collider']);
QMovement.presets = {};
_PARAMS['Presets'].forEach(function(preset) {
QMovement.presets[preset.ID] = convertColliderStruct(preset);
});
QMovement.showColliders = _PARAMS['Show Colliders'];
QMovement.tileBoxes = {
1537: [48, 6, 0, 42],
1538: [6, 48],
1539: [[48, 6, 0, 42], [6, 48]],
1540: [6, 48, 42],
1541: [[48, 6, 0, 42], [6, 48, 42]],
1542: [[6, 48], [6, 48, 42]],
1543: [[48, 6, 0, 42], [6, 48], [6, 48, 42]],
1544: [48, 6],
1545: [[48, 6], [48, 6, 0, 42]],
1546: [[48, 6], [6, 48]],
1547: [[48, 6], [48, 6, 0, 42], [6, 48]],
1548: [[48, 6], [6, 48, 42]],
1549: [[48, 6], [48, 6, 0, 42], [6, 48, 42]],
1550: [[48, 6], [6, 48], [6, 48, 42]],
1551: [48, 48], // Impassable A5, B
2063: [48, 48], // Impassable A1
2575: [48, 48],
3586: [6, 48],
3588: [6, 48, 42],
3590: [[6, 48], [6, 48, 42]],
3592: [48, 6],
3594: [[48, 6], [6, 48]],
3596: [[48, 6], [6, 48, 42]],
3598: [[48, 6], [6, 48], [6, 48, 42]],
3599: [48, 48], // Impassable A2, A3, A4
3727: [48, 48]
};
var rs = QMovement.tileSize / 48;
for (var key in QMovement.tileBoxes) {
if (QMovement.tileBoxes.hasOwnProperty(key)) {
for (var i = 0; i < QMovement.tileBoxes[key].length; i++) {
if (QMovement.tileBoxes[key][i].constructor === Array) {
for (var j = 0; j < QMovement.tileBoxes[key][i].length; j++) {
QMovement.tileBoxes[key][i][j] *= rs;
}
} else {
QMovement.tileBoxes[key][i] *= rs;
}
}
}
}
// following will be changable in a separate addon
QMovement.regionColliders = {};
QMovement.colliderMap = {};
function convertColliderStruct(struct) {
return [
struct.Type,
struct.Width,
struct.Height,
struct['Offset X'],
struct['Offset Y']
]
}
})();
//=============================================================================
// Colliders
//-----------------------------------------------------------------------------
// Polygon_Collider
function Polygon_Collider() {
this.initialize.apply(this, arguments);
}
(function() {
Polygon_Collider._counter = 0;
Polygon_Collider.prototype.initialize = function(points) {
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
this.initMembers.apply(this, args);
this.makeVertices(points);
};
Polygon_Collider.prototype.initMembers = function(x, y) {
x = x !== undefined ? x : 0;
y = y !== undefined ? y : 0;
this._position = new Point(x, y);
this._scale = new Point(1, 1);
this._offset = new Point(0, 0);
this._pivot = new Point(0, 0);
this._radian = 0;
this._note = '';
this.meta = {};
this.id = Polygon_Collider._counter++;
};
Object.defineProperty(Polygon_Collider.prototype, 'note', {
get() {
return this._note;
},
set(note) {
this._note = note;
this.meta = QPlus.getMeta(note);
}
});
Object.defineProperty(Polygon_Collider.prototype, 'x', {
get() {
return this._position.x;
},
set(x) {
this._position.x = x;
}
});
Object.defineProperty(Polygon_Collider.prototype, 'y', {
get() {
return this._position.y;
},
set(y) {
this._position.y = y;
}
});
Object.defineProperty(Polygon_Collider.prototype, 'ox', {
get() {
return this._offset.x + this._pivot.x;
},
set(value) {
this._offset.x = value;
this.refreshVertices();
}
});
Object.defineProperty(Polygon_Collider.prototype, 'oy', {
get() {
return this._offset.y + this._pivot.y;
},
set(value) {
this._offset.y = value - this._pivot.y;
this.refreshVertices();
}
});
Polygon_Collider.prototype.isPolygon = function() {
return true;
};
Polygon_Collider.prototype.isBox = function() {
return true;
};
Polygon_Collider.prototype.isCircle = function() {
return false;
};
Polygon_Collider.prototype.makeVertices = function(points) {
this._vertices = [];
this._baseVertices = [];
this._edges = [];
this._vectors = [];
this._xMin = null;
this._xMax = null;
this._yMin = null;
this._yMax = null;
for (var i = 0; i < points.length; i++) {
var x = points[i].x - this._pivot.x;
var y = points[i].y - this._pivot.y;
var x2 = x + this.x + this.ox;
var y2 = y + this.y + this.oy;
this._vertices.push(new Point(x2, y2));
this._baseVertices.push(new Point(x, y));
if (i !== 0) {
var prev = this._vertices[i - 1];
this._edges.push({
x1: prev.x, x2: x2,
y1: prev.y, y2: y2
})
}
if (i === points.length - 1) {
var first = this._vertices[0];
this._edges.push({
x1: x2, x2: first.x,
y1: y2, y2: first.y
})
}
var radian = Math.atan2(y, x);
radian += radian < 0 ? Math.PI * 2 : 0;
var dist = Math.sqrt(x * x + y * y);
this._vectors.push({ radian, dist });
if (this._xMin === null || this._xMin > x) {
this._xMin = x;
}
if (this._xMax === null || this._xMax < x) {
this._xMax = x;
}
if (this._yMin === null || this._yMin > y) {
this._yMin = y;
}
if (this._yMax === null || this._yMax < y) {
this._yMax = y;
}
}
this.width = Math.abs(this._xMax - this._xMin);
this.height = Math.abs(this._yMax - this._yMin);
var x1 = this._xMin + this.x + this.ox;
var y1 = this._yMin + this.y + this.oy;
this.center = new Point(x1 + this.width / 2, y1 + this.height / 2);
};
Polygon_Collider.prototype.makeVectors = function() {
this._vectors = this._baseVertices.map((function(vertex) {
var dx = vertex.x - this._pivot.x;
var dy = vertex.y - this._pivot.y;
var radian = Math.atan2(dy, dx);
radian += radian < 0 ? Math.PI * 2 : 0;
var dist = Math.sqrt(dx * dx + dy * dy);
return { radian, dist };
}).bind(this));
};
Polygon_Collider.prototype.setBounds = function() {
this._xMin = null;
this._xMax = null;
this._yMin = null;
this._yMax = null;
for (var i = 0; i < this._baseVertices.length; i++) {
var x = this._baseVertices[i].x;
var y = this._baseVertices[i].y;
if (this._xMin === null || this._xMin > x) {
this._xMin = x;
}
if (this._xMax === null || this._xMax < x) {
this._xMax = x;
}
if (this._yMin === null || this._yMin > y) {
this._yMin = y;
}
if (this._yMax === null || this._yMax < y) {
this._yMax = y;
}
}
this.width = Math.abs(this._xMax - this._xMin);
this.height = Math.abs(this._yMax - this._yMin);
var x1 = this._xMin + this.x + this.ox;
var y1 = this._yMin + this.y + this.oy;
this.center = new Point(x1 + this.width / 2, y1 + this.height / 2);
};
Polygon_Collider.prototype.refreshVertices = function() {
this._edges = [];
var i, j;
for (i = 0, j = this._vertices.length; i < j; i++) {
var vertex = this._vertices[i];
vertex.x = this.x + this._baseVertices[i].x + this.ox;
vertex.y = this.y + this._baseVertices[i].y + this.oy;
if (i !== 0) {
var prev = this._vertices[i - 1];
this._edges.push({
x1: prev.x, x2: vertex.x,
y1: prev.y, y2: vertex.y
})
}
if (i === j - 1) {
var first = this._vertices[0];
this._edges.push({
x1: vertex.x, x2: first.x,
y1: vertex.y, y2: first.y
})
}
}
this.setBounds();
};
Polygon_Collider.prototype.sectorEdge = function() {
var x1 = this._xMin + this.x + this.ox;
var x2 = this._xMax + this.x + this.ox - 1;
var y1 = this._yMin + this.y + this.oy;
var y2 = this._yMax + this.y + this.oy - 1;
x1 = Math.floor(x1 / ColliderManager._sectorSize);
x2 = Math.floor(x2 / ColliderManager._sectorSize);
y1 = Math.floor(y1 / ColliderManager._sectorSize);
y2 = Math.floor(y2 / ColliderManager._sectorSize);
return {
x1: x1, x2: x2,
y1: y1, y2: y2
}
};
Polygon_Collider.prototype.gridEdge = function() {
var x1 = this._xMin + this.x + this.ox;
var x2 = this._xMax + this.x + this.ox - 1;
var y1 = this._yMin + this.y + this.oy;
var y2 = this._yMax + this.y + this.oy - 1;
x1 = Math.floor(x1 / QMovement.tileSize);
x2 = Math.floor(x2 / QMovement.tileSize);
y1 = Math.floor(y1 / QMovement.tileSize);
y2 = Math.floor(y2 / QMovement.tileSize);
return {
x1: x1, x2: x2,
y1: y1, y2: y2
}
};
Polygon_Collider.prototype.edge = function() {
var x1 = this._xMin + this.x + this.ox;
var x2 = this._xMax + this.x + this.ox - 1;
var y1 = this._yMin + this.y + this.oy;
var y2 = this._yMax + this.y + this.oy - 1;
return {
x1: x1, x2: x2,
y1: y1, y2: y2
}
};