-
Notifications
You must be signed in to change notification settings - Fork 196
/
FlxTilemap.as
1488 lines (1399 loc) · 45.6 KB
/
FlxTilemap.as
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
package org.flixel
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import org.flixel.system.FlxTile;
import org.flixel.system.FlxTilemapBuffer;
/**
* This is a traditional tilemap display and collision class.
* It takes a string of comma-separated numbers and then associates
* those values with tiles from the sheet you pass in.
* It also includes some handy static parsers that can convert
* arrays or images into strings that can be loaded.
*
* @author Adam Atomic
*/
public class FlxTilemap extends FlxObject
{
[Embed(source="data/autotiles.png")] static public var ImgAuto:Class;
[Embed(source="data/autotiles_alt.png")] static public var ImgAutoAlt:Class;
/**
* No auto-tiling.
*/
static public const OFF:uint = 0;
/**
* Good for levels with thin walls that don'tile need interior corner art.
*/
static public const AUTO:uint = 1;
/**
* Better for levels with thick walls that look better with interior corner art.
*/
static public const ALT:uint = 2;
/**
* Set this flag to use one of the 16-tile binary auto-tile algorithms (OFF, AUTO, or ALT).
*/
public var auto:uint;
/**
* Read-only variable, do NOT recommend changing after the map is loaded!
*/
public var widthInTiles:uint;
/**
* Read-only variable, do NOT recommend changing after the map is loaded!
*/
public var heightInTiles:uint;
/**
* Read-only variable, do NOT recommend changing after the map is loaded!
*/
public var totalTiles:uint;
/**
* Rendering helper, minimize new object instantiation on repetitive methods.
*/
protected var _flashPoint:Point;
/**
* Rendering helper, minimize new object instantiation on repetitive methods.
*/
protected var _flashRect:Rectangle;
/**
* Internal reference to the bitmap data object that stores the original tile graphics.
*/
protected var _tiles:BitmapData;
/**
* Internal list of buffers, one for each camera, used for drawing the tilemaps.
*/
protected var _buffers:Array;
/**
* Internal representation of the actual tile data, as a large 1D array of integers.
*/
protected var _data:Array;
/**
* Internal representation of rectangles, one for each tile in the entire tilemap, used to speed up drawing.
*/
protected var _rects:Array;
/**
* Internal, the width of a single tile.
*/
protected var _tileWidth:uint;
/**
* Internal, the height of a single tile.
*/
protected var _tileHeight:uint;
/**
* Internal collection of tile objects, one for each type of tile in the map (NOTE one for every single tile in the whole map).
*/
protected var _tileObjects:Array;
/**
* Internal, used for rendering the debug bounding box display.
*/
protected var _debugTileNotSolid:BitmapData;
/**
* Internal, used for rendering the debug bounding box display.
*/
protected var _debugTilePartial:BitmapData;
/**
* Internal, used for rendering the debug bounding box display.
*/
protected var _debugTileSolid:BitmapData;
/**
* Internal, used for rendering the debug bounding box display.
*/
protected var _debugRect:Rectangle;
/**
* Internal flag for checking to see if we need to refresh
* the tilemap display to show or hide the bounding boxes.
*/
protected var _lastVisualDebug:Boolean;
/**
* Internal, used to sort of insert blank tiles in front of the tiles in the provided graphic.
*/
protected var _startingIndex:uint;
/**
* The tilemap constructor just initializes some basic variables.
*/
public function FlxTilemap()
{
super();
auto = OFF;
widthInTiles = 0;
heightInTiles = 0;
totalTiles = 0;
_buffers = new Array();
_flashPoint = new Point();
_flashRect = null;
_data = null;
_tileWidth = 0;
_tileHeight = 0;
_rects = null;
_tiles = null;
_tileObjects = null;
immovable = true;
cameras = null;
_debugTileNotSolid = null;
_debugTilePartial = null;
_debugTileSolid = null;
_debugRect = null;
_lastVisualDebug = FlxG.visualDebug;
_startingIndex = 0;
}
/**
* Clean up memory.
*/
override public function destroy():void
{
_flashPoint = null;
_flashRect = null;
_tiles = null;
var i:uint = 0;
var l:uint = _tileObjects.length;
while(i < l)
(_tileObjects[i++] as FlxTile).destroy();
_tileObjects = null;
i = 0;
l = _buffers.length;
while(i < l)
(_buffers[i++] as FlxTilemapBuffer).destroy();
_buffers = null;
_data = null;
_rects = null;
_debugTileNotSolid = null;
_debugTilePartial = null;
_debugTileSolid = null;
_debugRect = null;
super.destroy();
}
/**
* Load the tilemap with string data and a tile graphic.
*
* @param MapData A string of comma and line-return delineated indices indicating what order the tiles should go in.
* @param TileGraphic All the tiles you want to use, arranged in a strip corresponding to the numbers in MapData.
* @param TileWidth The width of your tiles (e.g. 8) - defaults to height of the tile graphic if unspecified.
* @param TileHeight The height of your tiles (e.g. 8) - defaults to width if unspecified.
* @param AutoTile Whether to load the map using an automatic tile placement algorithm. Setting this to either AUTO or ALT will override any values you put for StartingIndex, DrawIndex, or CollideIndex.
* @param StartingIndex Used to sort of insert empty tiles in front of the provided graphic. Default is 0, usually safest ot leave it at that. Ignored if AutoTile is set.
* @param DrawIndex Initializes all tile objects equal to and after this index as visible. Default value is 1. Ignored if AutoTile is set.
* @param CollideIndex Initializes all tile objects equal to and after this index as allowCollisions = ANY. Default value is 1. Ignored if AutoTile is set. Can override and customize per-tile-type collision behavior using <code>setTileProperties()</code>.
*
* @return A pointer this instance of FlxTilemap, for chaining as usual :)
*/
public function loadMap(MapData:String, TileGraphic:Class, TileWidth:uint=0, TileHeight:uint=0, AutoTile:uint=OFF, StartingIndex:uint=0, DrawIndex:uint=1, CollideIndex:uint=1):FlxTilemap
{
auto = AutoTile;
_startingIndex = StartingIndex;
//Figure out the map dimensions based on the data string
var columns:Array;
var rows:Array = MapData.split("\n");
heightInTiles = rows.length;
_data = new Array();
var row:uint = 0;
var column:uint;
while(row < heightInTiles)
{
columns = rows[row++].split(",");
if(columns.length <= 1)
{
heightInTiles = heightInTiles - 1;
continue;
}
if(widthInTiles == 0)
widthInTiles = columns.length;
column = 0;
while(column < widthInTiles)
_data.push(uint(columns[column++]));
}
//Pre-process the map data if it's auto-tiled
var i:uint;
totalTiles = widthInTiles*heightInTiles;
if(auto > OFF)
{
_startingIndex = 1;
DrawIndex = 1;
CollideIndex = 1;
i = 0;
while(i < totalTiles)
autoTile(i++);
}
//Figure out the size of the tiles
_tiles = FlxG.addBitmap(TileGraphic);
_tileWidth = TileWidth;
if(_tileWidth == 0)
_tileWidth = _tiles.height;
_tileHeight = TileHeight;
if(_tileHeight == 0)
_tileHeight = _tileWidth;
//create some tile objects that we'll use for overlap checks (one for each tile)
i = 0;
var l:uint = (_tiles.width/_tileWidth) * (_tiles.height/_tileHeight);
if(auto > OFF)
l++;
_tileObjects = new Array(l);
var ac:uint;
while(i < l)
{
_tileObjects[i] = new FlxTile(this,i,_tileWidth,_tileHeight,(i >= DrawIndex),(i >= CollideIndex)?allowCollisions:NONE);
i++;
}
//create debug tiles for rendering bounding boxes on demand
_debugTileNotSolid = makeDebugTile(FlxG.BLUE);
_debugTilePartial = makeDebugTile(FlxG.PINK);
_debugTileSolid = makeDebugTile(FlxG.GREEN);
_debugRect = new Rectangle(0,0,_tileWidth,_tileHeight);
//Then go through and create the actual map
width = widthInTiles*_tileWidth;
height = heightInTiles*_tileHeight;
_rects = new Array(totalTiles);
i = 0;
while(i < totalTiles)
updateTile(i++);
return this;
}
/**
* Internal function to clean up the map loading code.
* Just generates a wireframe box the size of a tile with the specified color.
*/
protected function makeDebugTile(Color:uint):BitmapData
{
var debugTile:BitmapData
debugTile = new BitmapData(_tileWidth,_tileHeight,true,0);
var gfx:Graphics = FlxG.flashGfx;
gfx.clear();
gfx.moveTo(0,0);
gfx.lineStyle(1,Color,0.5);
gfx.lineTo(_tileWidth-1,0);
gfx.lineTo(_tileWidth-1,_tileHeight-1);
gfx.lineTo(0,_tileHeight-1);
gfx.lineTo(0,0);
debugTile.draw(FlxG.flashGfxSprite);
return debugTile;
}
/**
* Main logic loop for tilemap is pretty simple,
* just checks to see if visual debug got turned on.
* If it did, the tilemap is flagged as dirty so it
* will be redrawn with debug info on the next draw call.
*/
override public function update():void
{
if(_lastVisualDebug != FlxG.visualDebug)
{
_lastVisualDebug = FlxG.visualDebug;
setDirty();
}
}
/**
* Internal function that actually renders the tilemap to the tilemap buffer. Called by draw().
*
* @param Buffer The <code>FlxTilemapBuffer</code> you are rendering to.
* @param Camera The related <code>FlxCamera</code>, mainly for scroll values.
*/
protected function drawTilemap(Buffer:FlxTilemapBuffer,Camera:FlxCamera):void
{
Buffer.fill();
//Copy tile images into the tile buffer
_point.x = int(Camera.scroll.x*scrollFactor.x) - x; //modified from getScreenXY()
_point.y = int(Camera.scroll.y*scrollFactor.y) - y;
var screenXInTiles:int = (_point.x + ((_point.x > 0)?0.0000001:-0.0000001))/_tileWidth;
var screenYInTiles:int = (_point.y + ((_point.y > 0)?0.0000001:-0.0000001))/_tileHeight;
var screenRows:uint = Buffer.rows;
var screenColumns:uint = Buffer.columns;
//Bound the upper left corner
if(screenXInTiles < 0)
screenXInTiles = 0;
if(screenXInTiles > widthInTiles-screenColumns)
screenXInTiles = widthInTiles-screenColumns;
if(screenYInTiles < 0)
screenYInTiles = 0;
if(screenYInTiles > heightInTiles-screenRows)
screenYInTiles = heightInTiles-screenRows;
var rowIndex:int = screenYInTiles*widthInTiles+screenXInTiles;
_flashPoint.y = 0;
var row:uint = 0;
var column:uint;
var columnIndex:uint;
var tile:FlxTile;
var debugTile:BitmapData;
while(row < screenRows)
{
columnIndex = rowIndex;
column = 0;
_flashPoint.x = 0;
while(column < screenColumns)
{
_flashRect = _rects[columnIndex] as Rectangle;
if(_flashRect != null)
{
Buffer.pixels.copyPixels(_tiles,_flashRect,_flashPoint,null,null,true);
if(FlxG.visualDebug && !ignoreDrawDebug)
{
tile = _tileObjects[_data[columnIndex]];
if(tile != null)
{
if(tile.allowCollisions <= NONE)
debugTile = _debugTileNotSolid; //blue
else if(tile.allowCollisions != ANY)
debugTile = _debugTilePartial; //pink
else
debugTile = _debugTileSolid; //green
Buffer.pixels.copyPixels(debugTile,_debugRect,_flashPoint,null,null,true);
}
}
}
_flashPoint.x += _tileWidth;
column++;
columnIndex++;
}
rowIndex += widthInTiles;
_flashPoint.y += _tileHeight;
row++;
}
Buffer.x = screenXInTiles*_tileWidth;
Buffer.y = screenYInTiles*_tileHeight;
}
/**
* Draws the tilemap buffers to the cameras and handles flickering.
*/
override public function draw():void
{
if(_flickerTimer != 0)
{
_flicker = !_flicker;
if(_flicker)
return;
}
if(cameras == null)
cameras = FlxG.cameras;
var camera:FlxCamera;
var buffer:FlxTilemapBuffer;
var i:uint = 0;
var l:uint = cameras.length;
while(i < l)
{
camera = cameras[i];
if(_buffers[i] == null)
_buffers[i] = new FlxTilemapBuffer(_tileWidth,_tileHeight,widthInTiles,heightInTiles,camera);
buffer = _buffers[i++] as FlxTilemapBuffer;
if(!buffer.dirty)
{
_point.x = x - int(camera.scroll.x*scrollFactor.x) + buffer.x; //copied from getScreenXY()
_point.y = y - int(camera.scroll.y*scrollFactor.y) + buffer.y;
buffer.dirty = (_point.x > 0) || (_point.y > 0) || (_point.x + buffer.width < camera.width) || (_point.y + buffer.height < camera.height);
}
if(buffer.dirty)
{
drawTilemap(buffer,camera);
buffer.dirty = false;
}
_flashPoint.x = x - int(camera.scroll.x*scrollFactor.x) + buffer.x; //copied from getScreenXY()
_flashPoint.y = y - int(camera.scroll.y*scrollFactor.y) + buffer.y;
_flashPoint.x += (_flashPoint.x > 0)?0.0000001:-0.0000001;
_flashPoint.y += (_flashPoint.y > 0)?0.0000001:-0.0000001;
buffer.draw(camera,_flashPoint);
_VISIBLECOUNT++;
}
}
/**
* Fetches the tilemap data array.
*
* @param Simple If true, returns the data as copy, as a series of 1s and 0s (useful for auto-tiling stuff). Default value is false, meaning it will return the actual data array (NOT a copy).
*
* @return An array the size of the tilemap full of integers indicating tile placement.
*/
public function getData(Simple:Boolean=false):Array
{
if(!Simple)
return _data;
var i:uint = 0;
var l:uint = _data.length;
var data:Array = new Array(l);
while(i < l)
{
data[i] = ((_tileObjects[_data[i]] as FlxTile).allowCollisions > 0)?1:0;
i++;
}
return data;
}
/**
* Set the dirty flag on all the tilemap buffers.
* Basically forces a reset of the drawn tilemaps, even if it wasn'tile necessary.
*
* @param Dirty Whether to flag the tilemap buffers as dirty or not.
*/
public function setDirty(Dirty:Boolean=true):void
{
var i:uint = 0;
var l:uint = _buffers.length;
while(i < l)
(_buffers[i++] as FlxTilemapBuffer).dirty = Dirty;
}
/**
* Find a path through the tilemap. Any tile with any collision flags set is treated as impassable.
* If no path is discovered then a null reference is returned.
*
* @param Start The start point in world coordinates.
* @param End The end point in world coordinates.
* @param Simplify Whether to run a basic simplification algorithm over the path data, removing extra points that are on the same line. Default value is true.
* @param RaySimplify Whether to run an extra raycasting simplification algorithm over the remaining path data. This can result in some close corners being cut, and should be used with care if at all (yet). Default value is false.
*
* @return A <code>FlxPath</code> from the start to the end. If no path could be found, then a null reference is returned.
*/
public function findPath(Start:FlxPoint,End:FlxPoint,Simplify:Boolean=true,RaySimplify:Boolean=false):FlxPath
{
//figure out what tile we are starting and ending on.
var startIndex:uint = int((Start.y-y)/_tileHeight) * widthInTiles + int((Start.x-x)/_tileWidth);
var endIndex:uint = int((End.y-y)/_tileHeight) * widthInTiles + int((End.x-x)/_tileWidth);
//check that the start and end are clear.
if( ((_tileObjects[_data[startIndex]] as FlxTile).allowCollisions > 0) ||
((_tileObjects[_data[endIndex]] as FlxTile).allowCollisions > 0) )
return null;
//figure out how far each of the tiles is from the starting tile
var distances:Array = computePathDistance(startIndex,endIndex);
if(distances == null)
return null;
//then count backward to find the shortest path.
var points:Array = new Array();
walkPath(distances,endIndex,points);
//reset the start and end points to be exact
var node:FlxPoint;
node = points[points.length-1] as FlxPoint;
node.x = Start.x;
node.y = Start.y;
node = points[0] as FlxPoint;
node.x = End.x;
node.y = End.y;
//some simple path cleanup options
if(Simplify)
simplifyPath(points);
if(RaySimplify)
raySimplifyPath(points);
//finally load the remaining points into a new path object and return it
var path:FlxPath = new FlxPath();
var i:int = points.length - 1;
while(i >= 0)
{
node = points[i--] as FlxPoint;
if(node != null)
path.addPoint(node,true);
}
return path;
}
/**
* Pathfinding helper function, strips out extra points on the same line.
*
* @param Points An array of <code>FlxPoint</code> nodes.
*/
protected function simplifyPath(Points:Array):void
{
var deltaPrevious:Number;
var deltaNext:Number;
var last:FlxPoint = Points[0];
var node:FlxPoint;
var i:uint = 1;
var l:uint = Points.length-1;
while(i < l)
{
node = Points[i];
deltaPrevious = (node.x - last.x)/(node.y - last.y);
deltaNext = (node.x - Points[i+1].x)/(node.y - Points[i+1].y);
if((last.x == Points[i+1].x) || (last.y == Points[i+1].y) || (deltaPrevious == deltaNext))
Points[i] = null;
else
last = node;
i++;
}
}
/**
* Pathfinding helper function, strips out even more points by raycasting from one point to the next and dropping unnecessary points.
*
* @param Points An array of <code>FlxPoint</code> nodes.
*/
protected function raySimplifyPath(Points:Array):void
{
var source:FlxPoint = Points[0];
var lastIndex:int = -1;
var node:FlxPoint;
var i:uint = 1;
var l:uint = Points.length;
while(i < l)
{
node = Points[i++];
if(node == null)
continue;
if(ray(source,node,_point))
{
if(lastIndex >= 0)
Points[lastIndex] = null;
}
else
source = Points[lastIndex];
lastIndex = i-1;
}
}
/**
* Pathfinding helper function, floods a grid with distance information until it finds the end point.
* NOTE: Currently this process does NOT use any kind of fancy heuristic! It's pretty brute.
*
* @param StartIndex The starting tile's map index.
* @param EndIndex The ending tile's map index.
*
* @return A Flash <code>Array</code> of <code>FlxPoint</code> nodes. If the end tile could not be found, then a null <code>Array</code> is returned instead.
*/
protected function computePathDistance(StartIndex:uint, EndIndex:uint):Array
{
//Create a distance-based representation of the tilemap.
//All walls are flagged as -2, all open areas as -1.
var mapSize:uint = widthInTiles*heightInTiles;
var distances:Array = new Array(mapSize);
var i:int = 0;
while(i < mapSize)
{
if((_tileObjects[_data[i]] as FlxTile).allowCollisions)
distances[i] = -2;
else
distances[i] = -1;
i++;
}
distances[StartIndex] = 0;
var distance:uint = 1;
var neighbors:Array = [StartIndex];
var current:Array;
var currentIndex:uint;
var left:Boolean;
var right:Boolean;
var up:Boolean;
var down:Boolean;
var currentLength:uint;
var foundEnd:Boolean = false;
while(neighbors.length > 0)
{
current = neighbors;
neighbors = new Array();
i = 0;
currentLength = current.length;
while(i < currentLength)
{
currentIndex = current[i++];
if(currentIndex == EndIndex)
{
foundEnd = true;
neighbors.length = 0;
break;
}
//basic map bounds
left = currentIndex%widthInTiles > 0;
right = currentIndex%widthInTiles < widthInTiles-1;
up = currentIndex/widthInTiles > 0;
down = currentIndex/widthInTiles < heightInTiles-1;
var index:uint;
if(up)
{
index = currentIndex - widthInTiles;
if(distances[index] == -1)
{
distances[index] = distance;
neighbors.push(index);
}
}
if(right)
{
index = currentIndex + 1;
if(distances[index] == -1)
{
distances[index] = distance;
neighbors.push(index);
}
}
if(down)
{
index = currentIndex + widthInTiles;
if(distances[index] == -1)
{
distances[index] = distance;
neighbors.push(index);
}
}
if(left)
{
index = currentIndex - 1;
if(distances[index] == -1)
{
distances[index] = distance;
neighbors.push(index);
}
}
if(up && right)
{
index = currentIndex - widthInTiles + 1;
if((distances[index] == -1) && (distances[currentIndex-widthInTiles] >= -1) && (distances[currentIndex+1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
if(right && down)
{
index = currentIndex + widthInTiles + 1;
if((distances[index] == -1) && (distances[currentIndex+widthInTiles] >= -1) && (distances[currentIndex+1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
if(left && down)
{
index = currentIndex + widthInTiles - 1;
if((distances[index] == -1) && (distances[currentIndex+widthInTiles] >= -1) && (distances[currentIndex-1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
if(up && left)
{
index = currentIndex - widthInTiles - 1;
if((distances[index] == -1) && (distances[currentIndex-widthInTiles] >= -1) && (distances[currentIndex-1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
}
distance++;
}
if(!foundEnd)
distances = null;
return distances;
}
/**
* Pathfinding helper function, recursively walks the grid and finds a shortest path back to the start.
*
* @param Data A Flash <code>Array</code> of distance information.
* @param Start The tile we're on in our walk backward.
* @param Points A Flash <code>Array</code> of <code>FlxPoint</code> nodes composing the path from the start to the end, compiled in reverse order.
*/
protected function walkPath(Data:Array,Start:uint,Points:Array):void
{
Points.push(new FlxPoint(x + uint(Start%widthInTiles)*_tileWidth + _tileWidth*0.5, y + uint(Start/widthInTiles)*_tileHeight + _tileHeight*0.5));
if(Data[Start] == 0)
return;
//basic map bounds
var left:Boolean = Start%widthInTiles > 0;
var right:Boolean = Start%widthInTiles < widthInTiles-1;
var up:Boolean = Start/widthInTiles > 0;
var down:Boolean = Start/widthInTiles < heightInTiles-1;
var current:uint = Data[Start];
var i:uint;
if(up)
{
i = Start - widthInTiles;
if((Data[i] >= 0) && (Data[i] < current))
{
walkPath(Data,i,Points);
return;
}
}
if(right)
{
i = Start + 1;
if((Data[i] >= 0) && (Data[i] < current))
{
walkPath(Data,i,Points);
return;
}
}
if(down)
{
i = Start + widthInTiles;
if((Data[i] >= 0) && (Data[i] < current))
{
walkPath(Data,i,Points);
return;
}
}
if(left)
{
i = Start - 1;
if((Data[i] >= 0) && (Data[i] < current))
{
walkPath(Data,i,Points);
return;
}
}
if(up && right)
{
i = Start - widthInTiles + 1;
if((Data[i] >= 0) && (Data[i] < current))
{
walkPath(Data,i,Points);
return;
}
}
if(right && down)
{
i = Start + widthInTiles + 1;
if((Data[i] >= 0) && (Data[i] < current))
{
walkPath(Data,i,Points);
return;
}
}
if(left && down)
{
i = Start + widthInTiles - 1;
if((Data[i] >= 0) && (Data[i] < current))
{
walkPath(Data,i,Points);
return;
}
}
if(up && left)
{
i = Start - widthInTiles - 1;
if((Data[i] >= 0) && (Data[i] < current))
{
walkPath(Data,i,Points);
return;
}
}
}
/**
* Checks to see if some <code>FlxObject</code> overlaps this <code>FlxObject</code> object in world space.
* If the group has a LOT of things in it, it might be faster to use <code>FlxG.overlaps()</code>.
* WARNING: Currently tilemaps do NOT support screen space overlap checks!
*
* @param Object The object being tested.
* @param InScreenSpace Whether to take scroll factors into account when checking for overlap.
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return Whether or not the two objects overlap.
*/
override public function overlaps(ObjectOrGroup:FlxBasic,InScreenSpace:Boolean=false,Camera:FlxCamera=null):Boolean
{
if(ObjectOrGroup is FlxGroup)
{
var results:Boolean = false;
var basic:FlxBasic;
var i:uint = 0;
var members:Array = (ObjectOrGroup as FlxGroup).members;
while(i < length)
{
basic = members[i++] as FlxBasic;
if(basic is FlxObject)
{
if(overlapsWithCallback(basic as FlxObject))
results = true;
}
else
{
if(overlaps(basic,InScreenSpace,Camera))
results = true;
}
}
return results;
}
else if(ObjectOrGroup is FlxObject)
return overlapsWithCallback(ObjectOrGroup as FlxObject);
return false;
}
/**
* Checks to see if this <code>FlxObject</code> were located at the given position, would it overlap the <code>FlxObject</code> or <code>FlxGroup</code>?
* This is distinct from overlapsPoint(), which just checks that point, rather than taking the object's size into account.
* WARNING: Currently tilemaps do NOT support screen space overlap checks!
*
* @param X The X position you want to check. Pretends this object (the caller, not the parameter) is located here.
* @param Y The Y position you want to check. Pretends this object (the caller, not the parameter) is located here.
* @param ObjectOrGroup The object or group being tested.
* @param InScreenSpace Whether to take scroll factors into account when checking for overlap. Default is false, or "only compare in world space."
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return Whether or not the two objects overlap.
*/
override public function overlapsAt(X:Number,Y:Number,ObjectOrGroup:FlxBasic,InScreenSpace:Boolean=false,Camera:FlxCamera=null):Boolean
{
if(ObjectOrGroup is FlxGroup)
{
var results:Boolean = false;
var basic:FlxBasic;
var i:uint = 0;
var members:Array = (ObjectOrGroup as FlxGroup).members;
while(i < length)
{
basic = members[i++] as FlxBasic;
if(basic is FlxObject)
{
_point.x = X;
_point.y = Y;
if(overlapsWithCallback(basic as FlxObject,null,false,_point))
results = true;
}
else
{
if(overlapsAt(X,Y,basic,InScreenSpace,Camera))
results = true;
}
}
return results;
}
else if(ObjectOrGroup is FlxObject)
{
_point.x = X;
_point.y = Y;
return overlapsWithCallback(ObjectOrGroup as FlxObject,null,false,_point);
}
return false;
}
/**
* Checks if the Object overlaps any tiles with any collision flags set,
* and calls the specified callback function (if there is one).
* Also calls the tile's registered callback if the filter matches.
*
* @param Object The <code>FlxObject</code> you are checking for overlaps against.
* @param Callback An optional function that takes the form "myCallback(Object1:FlxObject,Object2:FlxObject)", where Object1 is a FlxTile object, and Object2 is the object passed in in the first parameter of this method.
* @param FlipCallbackParams Used to preserve A-B list ordering from FlxObject.separate() - returns the FlxTile object as the second parameter instead.
* @param Position Optional, specify a custom position for the tilemap (useful for overlapsAt()-type funcitonality).
*
* @return Whether there were overlaps, or if a callback was specified, whatever the return value of the callback was.
*/
public function overlapsWithCallback(Object:FlxObject,Callback:Function=null,FlipCallbackParams:Boolean=false,Position:FlxPoint=null):Boolean
{
var results:Boolean = false;
var X:Number = x;
var Y:Number = y;
if(Position != null)
{
X = Position.x;
Y = Position.y;
}
//Figure out what tiles we need to check against
var selectionX:int = FlxU.floor((Object.x - X)/_tileWidth);
var selectionY:int = FlxU.floor((Object.y - Y)/_tileHeight);
var selectionWidth:uint = selectionX + (FlxU.ceil(Object.width/_tileWidth)) + 1;
var selectionHeight:uint = selectionY + FlxU.ceil(Object.height/_tileHeight) + 1;
//Then bound these coordinates by the map edges
if(selectionX < 0)
selectionX = 0;
if(selectionY < 0)
selectionY = 0;
if(selectionWidth > widthInTiles)
selectionWidth = widthInTiles;
if(selectionHeight > heightInTiles)
selectionHeight = heightInTiles;
//Then loop through this selection of tiles and call FlxObject.separate() accordingly
var rowStart:uint = selectionY*widthInTiles;
var row:uint = selectionY;
var column:uint;
var tile:FlxTile;
var overlapFound:Boolean;
var deltaX:Number = X - last.x;
var deltaY:Number = Y - last.y;
while(row < selectionHeight)
{
column = selectionX;
while(column < selectionWidth)
{
overlapFound = false;
tile = _tileObjects[_data[rowStart+column]] as FlxTile;
if(tile.allowCollisions)
{
tile.x = X+column*_tileWidth;
tile.y = Y+row*_tileHeight;
tile.last.x = tile.x - deltaX;
tile.last.y = tile.y - deltaY;
if(Callback != null)
{
if(FlipCallbackParams)
overlapFound = Callback(Object,tile);
else
overlapFound = Callback(tile,Object);
}
else
overlapFound = (Object.x + Object.width > tile.x) && (Object.x < tile.x + tile.width) && (Object.y + Object.height > tile.y) && (Object.y < tile.y + tile.height);
if(overlapFound)
{
if((tile.callback != null) && ((tile.filter == null) || (Object is tile.filter)))
{
tile.mapIndex = rowStart+column;
tile.callback(tile,Object);
}
results = true;
}
}
else if((tile.callback != null) && ((tile.filter == null) || (Object is tile.filter)))
{
tile.mapIndex = rowStart+column;
tile.callback(tile,Object);
}
column++;
}
rowStart += widthInTiles;
row++;
}
return results;
}
/**
* Checks to see if a point in 2D world space overlaps this <code>FlxObject</code> object.
*
* @param Point The point in world space you want to check.
* @param InScreenSpace Whether to take scroll factors into account when checking for overlap.
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return Whether or not the point overlaps this object.
*/
override public function overlapsPoint(Point:FlxPoint,InScreenSpace:Boolean=false,Camera:FlxCamera=null):Boolean
{