/
utilstile.js
1740 lines (1143 loc) · 55.2 KB
/
utilstile.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
ig.module(
'plusplus.helpers.utilstile'
)
.requires(
'plusplus.core.config',
'plusplus.core.collision-map',
'plusplus.helpers.utils',
'plusplus.helpers.utilsintersection',
'plusplus.helpers.utilsvector2'
)
.defines(function() {
"use strict";
var _c = ig.CONFIG;
var _ut = ig.utils;
var _uti = ig.utilsintersection;
var _utv2 = ig.utilsvector2;
var TILE_ONE_WAY_UP = _c.COLLISION.TILE_ONE_WAY_UP;
var TILE_ONE_WAY_DOWN = _c.COLLISION.TILE_ONE_WAY_DOWN;
var TILE_ONE_WAY_RIGHT = _c.COLLISION.TILE_ONE_WAY_RIGHT;
var TILE_ONE_WAY_LEFT = _c.COLLISION.TILE_ONE_WAY_LEFT;
var TILE_CLIMBABLE_WITH_TOP = _c.COLLISION.TILE_CLIMBABLE_WITH_TOP;
var TILE_CLIMBABLE = _c.COLLISION.TILE_CLIMBABLE;
var TILE_CLIMBABLE_STAIRS_WITH_TOP = _c.COLLISION.TILE_STAIRS_WITH_TOP;
var TILE_CLIMBABLE_STAIRS = _c.COLLISION.TILE_STAIRS;
var HASH_WALKABLE = _c.COLLISION.TILES_HASH_WALKABLE;
var HASH_WALKABLE_STRICT = _c.COLLISION.TILES_HASH_WALKABLE_STRICT;
var HASH_ONE_WAY = _c.COLLISION.TILES_HASH_ONE_WAY;
var HASH_CLIMBABLE = _c.COLLISION.TILES_HASH_CLIMBABLE;
var HASH_CLIMBABLE_ONE_WAY = _c.COLLISION.TILES_HASH_CLIMBABLE_ONE_WAY;
var HASH_CLIMBABLE_STAIRS = _c.COLLISION.TILES_HASH_CLIMBABLE_STAIRS;
var SEGMENT_A = 1;
var SEGMENT_B = 2;
/**
* Static utilities for working with tiles and collision maps.
* <br>- shapes are extracted by {@link ig.GameExtended#loadLevel}, but the work is done by {@link ig.utilstile.shapesFromCollisionMap}
* <span class="alert alert-info"><strong>Tip:</strong> Impact++ can automatically convert collision maps into shapes (vertices, edges, etc).</span>
* @memberof ig
* @namespace ig.utilstile
* @author Collin Hover - collinhover.com
**/
ig.utilstile = {};
/**
* Definitions of tile types as vertices so we don't have to recalculate each tile.
* @type {Object}
* @readonly
*/
ig.utilstile.defaultTileVerticesDef = {};
/**
* Definitions of tile types as segments so we don't have to recalculate each tile.
* @type {Object}
* @readonly
*/
ig.utilstile.defaultTileSegmentsDef = {};
/*
* Current collision map.
*/
var _collisionMap = null;
/*
* Current collision map tile def.
*/
var _collisionMapTileDef = null;
/**
* Rebuilds tile shapes (vertices, segments, normals) based on a collision map.
* <br>- this is called automatically by {@link ig.GameExtended#buildLevel}, and only does rebuild if collision map tiledef differs from current tiledef
* <span class="alert alert-danger"><strong>IMPORTANT:</strong> if using a {@link ig.PathfindingMap}, the tilesize must match the {@link ig.CollisionMap}'s tilesize!</span>
* @param {ig.CollisionMap} collisionMap map with tile definition.
*/
ig.utilstile.rebuild = function(collisionMap) {
if (collisionMap instanceof ig.CollisionMap && collisionMap.tiledef !== _collisionMapTileDef) {
ig.utilstile.unload();
_collisionMap = collisionMap;
_collisionMapTileDef = _collisionMap.tiledef || ig.CollisionMap.defaultTileDef;
// calculate shapes (vertices, segments, normals) from each kind of tile
for (var tileId in _collisionMapTileDef) {
ig.utilstile.shapeFromTile(tileId, _collisionMapTileDef);
}
// don't forget about solid
ig.utilstile.shapeFromTile(1, _collisionMapTileDef);
} else {
_collisionMap = collisionMap;
}
};
/**
* Unload tile shapes.
* <span class="alert"><strong>IMPORTANT:</strong> this is called automatically by {@link ig.GameExtended#unloadLevel}.</span>
*/
ig.utilstile.unload = function() {
_collisionMap = null;
_collisionMapTileDef = ig.CollisionMap.defaultTileDef;
ig.utilstile.defaultTileVerticesDef = {};
ig.utilstile.defaultTileSegmentsDef = {};
};
/**
* Gets if a tile is walkabke.
* @param {Number} tileId tile id.
* @returns {Boolean} whether tile is one-way
**/
ig.utilstile.isTileWalkable = function(tileId) {
return HASH_WALKABLE[tileId];
};
/**
* Gets if a tile is strictly walkabke.
* @param {Number} tileId tile id.
* @returns {Boolean} whether tile is one-way
**/
ig.utilstile.isTileWalkableStrict = function(tileId) {
return HASH_WALKABLE_STRICT[tileId];
};
/**
* Gets if a tile is one-way.
* @param {Number} tileId tile id.
* @returns {Boolean} whether tile is one-way
**/
ig.utilstile.isTileOneWay = function(tileId) {
return HASH_ONE_WAY[tileId];
};
/**
* Gets if a tile is climbable.
* @param {Number} tileId tile id.
* @returns {Boolean} whether tile is climbable
**/
ig.utilstile.isTileClimbable = function(tileId) {
return HASH_CLIMBABLE[tileId];
};
/**
* Gets if a tile is stairs.
* @param {Number} tileId tile id.
* @returns {Boolean} whether tile is stairs
**/
ig.utilstile.isTileClimbableStairs = function(tileId) {
return HASH_CLIMBABLE_STAIRS[tileId];
};
/**
* Gets if a tile is climbable and one way.
* @param {Number} tileId tile id.
* @returns {Boolean} whether tile is climbable and one way
**/
ig.utilstile.isTileClimbableOneWay = function(tileId) {
return HASH_CLIMBABLE_ONE_WAY[tileId];
};
/**
* Extracts all shapes from an impact collision map, with vertices in clockwise order.
* <br>- this method does its best to intelligently combine tiles into as big of shapes as it can
* <br>- shapes consist of an x and y position, and a settings object with an array of vertices and a size object
* @param {ig.CollisionMap} map map data object
* @param {Object} options options object
* @returns {Array} array of shapes including a list of oneWays, edges, and climbables
* @example
* // options is a plain object
* options = {};
* // we can ignore climbable tiles when extracting shapes
* options.ignoreClimbables = true;
* // we can ignore one way tiles when extracting shapes
* options.ignoreOneWays = true;
* // we can ignore solid tiles when extracting shapes
* options.ignoreSolids = true;
* // we can keep the outer boundary edge shape when converting a collision map
* // but by default, the outer boundary is thrown away
* // this is because it is unlikely the player will ever be outside the level
* // so the outer boundary edge is usually useless
* options.retainBoundaryOuter = true;
* // we can throw away the inner boundary edge shape
* // i.e. the edge shape on the inside of the outer boundary
* options.discardBoundaryInner = true;
* // we can also throw away any shapes inside the inner boundary edge shape
* options.discardBoundaryInner = true;
* // we can force rectangles to be created instead of contour shapes
* // this is useful when we may have concave shapes that don't play nice with bounding boxes
* options.rectangles = true;
* // we can force just climbables to be rectangles
* options.contourClimbables = false;
* // we can force just one ways to be rectangles
* options.contourOneWays = false;
* // we can force shapes to only be constructed by tiles of the same type / id
* options.groupByTileId = true;
* // we can throw away all collinear vertices to improve performance
* options.discardCollinear = true;
* // we can reverse the shape vertex order to counter-clockwise
* options.reverse = true;
**/
ig.utilstile.shapesFromCollisionMap = function(map, options) {
var shapes = {
oneWays: [],
solids: [],
climbables: []
};
if (map instanceof ig.CollisionMap) {
if (options) {
options = ig.copy(options, {});
} else {
options = {};
}
// copy data so we can clear spots we've already visited and used
// data is edited as we go so we don't extract duplicates
var data = ig.copy(map.data);
// extract each tile shape from map
var tilesize = map.tilesize;
var width = map.width;
var height = map.height;
var solids = [];
var climbables = [];
var oneWays = [];
var vertices, scaledVertices, segments, segment;
var ix, iy, x, y;
var i, il, tile, shape;
for (iy = 0; iy < height; iy++) {
for (ix = 0; ix < width; ix++) {
shape = ig.utilstile.shapeFromTile(map.data[iy][ix], map.tiledef);
tile = {
id: map.data[iy][ix],
ix: ix,
iy: iy,
x: ix * tilesize,
y: iy * tilesize,
width: tilesize,
height: tilesize,
shape: shape
};
// not empty
if (shape.vertices.length > 0) {
// copy, absolutely position, and scale vertices
scaledVertices = [];
vertices = shape.vertices;
segments = shape.segments;
for (i = 0, il = segments.length; i < il; i++) {
segment = segments[i];
var va = vertices[segment.a];
scaledVertices[segment.a] = {
x: tile.x + va.x * tilesize,
y: tile.y + va.y * tilesize
};
}
shape.vertices = scaledVertices;
// add to list by type
if (HASH_CLIMBABLE[tile.id]) {
if (!options.ignoreClimbables) {
climbables.push(tile);
}
} else if (HASH_ONE_WAY[tile.id]) {
if (!options.ignoreOneWays) {
oneWays.push(tile);
}
} else if (!options.ignoreSolids) {
solids.push(tile);
}
}
// store in copied data so other tiles can compare
data[iy][ix] = tile;
}
}
// store original options
// we'll need to reset it with each shape type
var rectangles = options.rectangles;
// solid tiles to shapes
shapes.solids = shapes.solids.concat(ig.utilstile.shapedTilesToShapes(solids, data, options));
// generally climabables and one ways should be grouped by tile
if (typeof options.groupByTileId === 'undefined') {
options.groupByTileId = true;
}
// climbable tiles to shapes
options.rectangles = typeof rectangles !== 'undefined' ? rectangles : !options.contourClimbables;
shapes.climbables = shapes.climbables.concat(ig.utilstile.shapedTilesToShapes(climbables, data, options));
// adjust climbable shapes by id
for (i = 0, il = shapes.climbables.length; i < il; i++) {
shape = shapes.climbables[i];
if (HASH_CLIMBABLE_ONE_WAY[shape.id]) {
shape.settings.oneWay = true;
} else {
shape.settings.sensor = true;
}
if (HASH_CLIMBABLE_STAIRS[shape.id]) {
shape.settings.climbableStairs = true;
}
}
// one-way tiles to shapes
options.rectangles = typeof rectangles !== 'undefined' ? rectangles : !options.contourOneWays;
shapes.oneWays = shapes.oneWays.concat(ig.utilstile.shapedTilesToShapes(oneWays, data, options));
// adjust one-way shapes by id
for (i = 0, il = shapes.oneWays.length; i < il; i++) {
shape = shapes.oneWays[i];
// one-way
if (HASH_ONE_WAY[shape.id]) {
shape.settings.oneWay = true;
// set one way facing (default up)
if (shape.id === TILE_ONE_WAY_DOWN) {
shape.settings.oneWayFacing = {
x: 0,
y: 1
};
} else if (shape.id === TILE_ONE_WAY_RIGHT) {
shape.settings.oneWayFacing = {
x: 1,
y: 0
};
} else if (shape.id === TILE_ONE_WAY_LEFT) {
shape.settings.oneWayFacing = {
x: -1,
y: 0
};
}
}
}
}
return shapes;
};
/**
* Converts a list of tiles with vertices into shapes.
* <span class="alert alert-info"><strong>Tip:</strong> when converting tiles to shapes, is usually better to call {@link ig.utilstile.shapesFromCollisionMap}.</span>
* @param {Array} tiles shaped tiles to convert
* @param {Array} data 2d list of all tiles in map
* @param {Object} options options object
* @returns {Array} list of shapes
**/
ig.utilstile.shapedTilesToShapes = function(tiles, data, options) {
options = options || {};
var shapes = [];
var vertices = [];
var contours = [];
var i, il, j, jl, index;
// create tile groups from tiles
if (options.groupByTileId) {
// lets avoid infinite recursion!
delete options.groupByTileId;
// group by id
var ids = [];
var id;
var groups = {};
var group;
for (i = 0, il = tiles.length; i < il; i++) {
var tile = tiles[i];
if (groups[tile.id]) {
groups[tile.id].push(tile);
} else {
ids.push(tile.id);
groups[tile.id] = [tile];
}
}
// create shapes for each group
for (i = 0, il = ids.length; i < il; i++) {
id = ids[i];
group = groups[id];
options.id = id;
shapes = shapes.concat(ig.utilstile.shapedTilesToShapes(group, data, options));
}
} else {
// rectangle shapes that may or may not be concave
if (options.rectangles) {
// step horizontal connected tiles
// add line if matches last, else create new rectangle
var tilePool = tiles.slice(0);
var rectangles = [];
var line, length, stepped, rectangle;
while (tilePool.length > 0) {
// get first horizontal line of tiles
line = ig.utilstile.findShapedTileLine(tilePool);
_ut.arrayCautiousRemoveMulti(tilePool, line);
length = line.length;
rectangle = line;
stepped = true;
// find as many matching length rows as possible
while (stepped) {
stepped = false;
var tileLast = line[0];
var tileFrom = data[tileLast.iy][tileLast.ix + 1];
if (tileFrom) {
// get tile at start of next row and make sure it is part of tile pool
index = _ut.indexOfValue(tilePool, tileFrom);
if (index !== -1) {
line = ig.utilstile.findShapedTileLine(tilePool, false, index, length);
if (line.length === length) {
_ut.arrayCautiousRemoveMulti(tilePool, line);
rectangle = rectangle.concat(line);
stepped = true;
}
}
}
}
if (rectangle.length > 0) {
rectangles.push(rectangle);
}
}
for (j = 0, jl = rectangles.length; j < jl; j++) {
rectangle = rectangles[j];
// keep non-duplicate edge vertices
vertices = [];
for (i = 0, il = rectangle.length; i < il; i++) {
vertices = vertices.concat(ig.utilstile.getNonDuplicateSegmentVertices(rectangle[i], data, rectangle));
}
// vertices to contours
contours = contours.concat(ig.utilstile.verticesToContours(vertices, options));
}
}
// general shapes that may or may not be concave
else {
// keep non-duplicate edge vertices
for (i = 0, il = tiles.length; i < il; i++) {
vertices = vertices.concat(ig.utilstile.getNonDuplicateSegmentVertices(tiles[i], data, tiles));
}
// vertices to contours
contours = ig.utilstile.verticesToContours(vertices, options);
}
// contours to shapes
for (i = 0, il = contours.length; i < il; i++) {
var contour = contours[i];
shapes.push({
id: options.id,
x: contour.minX,
y: contour.minY,
settings: {
size: {
x: contour.width,
y: contour.height
},
vertices: contour.vertices
}
});
}
}
return shapes;
};
/**
* Finds the first line in either horizontal or vertical direction from tiles.
* <span class="alert alert-info"><strong>Tip:</strong> when converting tiles to shapes, is usually better to call {@link ig.utilstile.shapesFromCollisionMap}.</span>
* @param {Array} tiles shaped tiles to search.
* @param {Boolean} [horizontal=false] line is horizontal, else vertical.
* @param {Number} [indexFrom=0] index in tiles to start from.
* @param {Number} [length=0] max length of line.
* @returns {Array} list of tiles in line.
**/
ig.utilstile.findShapedTileLine = function(tiles, horizontal, indexFrom, length) {
indexFrom = indexFrom || 0;
length = length || 0;
var tileFrom = tiles[indexFrom];
var line = [];
var stepped = true;
var i, il;
while (stepped) {
stepped = false;
// add tile to line
line.push(tileFrom);
if (line.length === length) {
break;
}
// step to next in line
var tileNext = horizontal ? ig.utilstile.stepShapedTileHorizontally(tiles, tileFrom) : ig.utilstile.stepShapedTileVertically(tiles, tileFrom);
if (tileFrom !== tileNext) {
stepped = true;
tileFrom = tileNext;
}
}
return line;
};
/**
* Attempts to step to the next tile horizontally.
* <span class="alert alert-info"><strong>Tip:</strong> when converting tiles to shapes, is usually better to call {@link ig.utilstile.shapesFromCollisionMap}.</span>
* @param {Array} tiles shaped tiles to search
* @param {Object} tileFrom tile stepping from
* @returns {Object} next tile, or current tile if next not found
**/
ig.utilstile.stepShapedTileHorizontally = function(tiles, tileFrom) {
for (var i = 0, il = tiles.length; i < il; i++) {
var tileNext = tiles[i];
if (tileFrom.iy === tileNext.iy && tileFrom.ix + 1 === tileNext.ix) {
return tileNext;
}
}
return tileFrom;
};
/**
* Attempts to step to the next tile vertically.
* <span class="alert alert-info"><strong>Tip:</strong> when converting tiles to shapes, is usually better to call {@link ig.utilstile.shapesFromCollisionMap}.</span>
* @param {Array} tiles shaped tiles to search
* @param {Object} tileFrom tile stepping from
* @returns {Object} next tile, or current tile if next not found
**/
ig.utilstile.stepShapedTileVertically = function(tiles, tileFrom) {
for (var i = 0, il = tiles.length; i < il; i++) {
var tileNext = tiles[i];
if (tileFrom.ix === tileNext.ix && tileFrom.iy + 1 === tileNext.iy) {
return tileNext;
}
}
return tileFrom;
};
/**
* Converts a list of vertices into contours.
* <span class="alert alert-info"><strong>Tip:</strong> when converting tiles to shapes, is usually better to call {@link ig.utilstile.shapesFromCollisionMap}.</span>
* @param {Array} vertices list of vertices
* @param {Object} options options object
* @returns {Array} list of contours
**/
ig.utilstile.verticesToContours = function(vertices, options) {
var contours = [];
if (vertices.length > 1) {
options = options || {};
// find each contour within vertices
var vertexPool = vertices.slice(0);
var contour = {
vertices: [],
minX: Number.MAX_VALUE,
minY: Number.MAX_VALUE,
maxX: -Number.MAX_VALUE,
maxY: -Number.MAX_VALUE
};
var contourVertices = contour.vertices;
var vb = vertexPool.pop();
var va = vertexPool.pop();
var pva, pvb;
var sva, svb;
var i, il, j, jl;
// length > -2 because we need 1 extra loop for final segment/contour
while (vertexPool.length > -2) {
var stepped = false;
// if we haven't looped around, try to step to next
sva = contourVertices[0];
svb = contourVertices[1];
if (contourVertices.length <= 2 || vb.x !== sva.x || vb.y !== sva.y) {
for (i = 0, il = vertexPool.length; i < il; i += 2) {
pva = vertexPool[i];
pvb = vertexPool[i + 1];
if (vb.x === pva.x && vb.y === pva.y) {
stepped = true;
break;
}
}
}
// only add the second vector of each pair
contourVertices.push(vb);
// update contour min/max
if (vb.x < contour.minX) contour.minX = vb.x;
if (vb.x > contour.maxX) contour.maxX = vb.x;
if (vb.y < contour.minY) contour.minY = vb.y;
if (vb.y > contour.maxY) contour.maxY = vb.y;
if (stepped === true) {
vertexPool.splice(i, 2);
va = pva;
vb = pvb;
} else {
if (contour.vertices.length >= 3) {
contours.push(contour);
}
if (vertexPool.length > 0) {
contour = {
vertices: []
};
contour.minX = contour.minY = Number.MAX_VALUE;
contour.maxX = contour.maxY = -Number.MAX_VALUE;
contourVertices = contour.vertices;
vb = vertexPool.pop();
va = vertexPool.pop();
} else {
break;
}
}
}
// set contour size
for (i = 0, il = contours.length; i < il; i++) {
contour = contours[i];
contour.width = contour.maxX - contour.minX;
contour.height = contour.maxY - contour.minY;
}
// sort contours by largest up
contours.sort(function(a, b) {
return (b.width * b.width + b.height * b.height) - (a.width * a.width + a.height * a.height);
});
// test each contour to find containing contours
// if shape's AABB is fully contained by another shape, make chain ordered from smallest to largest
var contourPool = contours.slice(0);
var containerChains = [];
var containerChain = [];
var containingContour, contained;
contour = contourPool.pop();
while (contourPool.length > -1) {
contained = false;
if (contour) {
// search contours instead of contour pool so we can find all containers
for (i = contours.length - 1; i > -1; i--) {
containingContour = contours[i];
if (contour !== containingContour && _uti.AABBContains(contour.minX, contour.minY, contour.maxX, contour.maxY, containingContour.minX, containingContour.minY, containingContour.maxX, containingContour.maxY)) {
contained = true;
break;
}
}
containerChain.push(contour);
}
if (contained) {
_ut.arrayCautiousRemove(contourPool, containingContour);
contour = containingContour;
} else {
if (containerChain.length > 1) {
containerChains.push(containerChain);
}
if (contourPool.length > 0) {
containerChain = [];
contour = contourPool.pop();
} else {
break;
}
}
}
// check each container chain
var contoursReversed = [];
var contoursRemoved = [];
for (i = 0, il = containerChains.length; i < il; i++) {
containerChain = containerChains[i];
var outerBoundary = containerChain[containerChain.length - 1];
var innerBoundary = containerChain[containerChain.length - 2];
// reverse vertices of every other contour to avoid creating ccw contours
// this happens because converting tiles to vertices cannot control the direction of the segments
// even length chain, start with first
if (containerChain.length % 2 === 0) {
j = 0;
}
// odd length chain, start with second
else {
j = 1;
}
for (jl = containerChain.length; j < jl; j += 2) {
contour = containerChain[j];
if (_ut.indexOfValue(contoursReversed, contour) === -1) {
contour.vertices.reverse();
contoursReversed.push(contour);
}
}
// discard outer boundary contour
// generally, we know that the tiles have edges on both sides
// so there should always be a container at the end of the chain that wraps the outside
// we don't need these edges/vertices as it is unlikely the player will ever walk outside the map
if (!options.retainBoundaryOuter && _ut.indexOfValue(contoursRemoved, outerBoundary) === -1) {
contoursRemoved.push(outerBoundary);
_ut.arrayCautiousRemove(contours, outerBoundary);
}
// discard inner boundary contour
if (options.discardBoundaryInner && _ut.indexOfValue(contoursRemoved, innerBoundary) === -1) {
contoursRemoved.push(innerBoundary);
_ut.arrayCautiousRemove(contours, innerBoundary);
}
// discard anything beyond inner boundary contour
if (options.discardEdgesInner && containerChain.length > 2) {
var otherContours = containerChain.slice(0, containerChain.length - 2);
contoursRemoved = contoursRemoved.concat(otherContours);
_ut.arrayCautiousRemoveMulti(contours, otherContours);
}
}
// finalize contours
for (i = 0, il = contours.length; i < il; i++) {
contour = contours[i];
contourVertices = contour.vertices;
// optimization (default): find and remove all intermediary collinear vertices
if (!options.discardCollinear) {
sva = contourVertices[0];