This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflxtilemap.monkey
1330 lines (997 loc) · 30.7 KB
/
flxtilemap.monkey
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
Strict
Import mojo
Import reflection
Import flxextern
Import flxobject
Import flxrect
Import flxg
Import flxcamera
Import flxgroup
Import flxbasic
Import flxpath
Import system.flxtile
Import system.flxtilemapbuffer
Import "data/autotiles_flx.png"
Import "data/autotiles_flx.png"
Class FlxTilemap Extends FlxObject
Global __CLASS__:Object
Const AUTOTILES:String = "autotiles" + FlxG.DATA_SUFFIX
Const AUTOTILES_ALT:String = "autotiles_alt" + FlxG.DATA_SUFFIX
Const OFF:Int = 0
Const AUTO:Int = 1
Const ALT:Int = 2
Field auto:Int
Field widthInTiles:Int
Field heightInTiles:Int
Field totalTiles:Int
Private
Field _tiles:Image
Field _buffers:IntMap<FlxTilemapBuffer>
Field _data:Int[]
Field _rects:FlxRect[]
Field _tileWidth:Int
Field _tileHeight:Int
Field _tileObjects:FlxTile[]
Field _startingIndex:Int = 0
Field _camera:FlxCamera
Field _buffer:FlxTilemapBuffer
Field _screenXInTiles:Int
Field _screenYInTiles:Int
Field _screenRows:Int
Field _screenColumns:Int
Field _animations:IntMap<FlxTileAnimation>
Field _firstAnimation:map.Node<Int, FlxTileAnimation>
Public
Method New()
Super.New()
auto = OFF
widthInTiles = 0
heightInTiles = 0
totalTiles = 0
_buffers = New IntMap<FlxTilemapBuffer>()
_tileWidth = 0
_tileHeight = 0
_tiles = Null
immovable = True
moves = False
_cameras = Null
_startingIndex = 0
_camera = Null
_buffer = Null
_animations = Null
End Method
Method Destroy:Void()
_tiles = Null
_camera = Null
_buffer = Null
Local i:Int = 0
Local l:Int = _tileObjects.Length()
While (i < l)
If (_tileObjects[i] <> Null) Then
_tileObjects[i].Destroy()
_tileObjects[i] = Null
End If
i += 1
Wend
_buffers.Clear()
_buffers = Null
If (_animations <> Null)
For Local anim:FlxTileAnimation = EachIn _animations.Values()
anim.Destroy()
Next
_animations.Clear()
_animations = Null
_firstAnimation = Null
End If
Super.Destroy()
End Method
Method LoadMap:FlxTilemap(mapData:String, tileGraphic:String, tileWidth:Int = 0, tileHeight:Int = 0, autoTile:Int = OFF, startIndex:Int = 0, drawIndex:Int = 1, collideIndex:Int = 1)
auto = autoTile
_startingIndex = startIndex
Local columns:String[]
Local rows:String[] = mapData.Split("~n")
Local row:Int = 0
Local column:Int
Local data:Stack<Int> = New Stack<Int>()
heightInTiles = rows.Length()
While (row < heightInTiles)
columns = rows[row].Split(",")
If (columns.Length() <= 1) Then
heightInTiles = heightInTiles - 1
Continue
End If
If (widthInTiles = 0) Then
widthInTiles = columns.Length()
End If
column = 0
While (column < widthInTiles)
data.Push(Int(columns[column]))
column += 1
Wend
row += 1
Wend
_data = data.ToArray()
Local i:Int
totalTiles = widthInTiles * heightInTiles
If (auto > OFF) Then
_startingIndex = 1
drawIndex = 1
collideIndex = 1
i = 0
While (i < totalTiles)
_AutoTile(i)
i += 1
Wend
End If
_tiles = FlxG.AddBitmap(New TileResource(tileGraphic))
_tileWidth = tileWidth
If (_tileWidth = 0) _tileWidth = _tiles.Height()
_tileHeight = tileHeight
If (_tileHeight = 0) _tileHeight = _tileWidth
i = 0
Local l:Int = (_tiles.Width() / _tileWidth) * (_tiles.Height() / _tileHeight) + _startingIndex
_tileObjects = _tileObjects.Resize(l)
While (i < l)
If (i >= collideIndex) Then
_tileObjects[i] = New FlxTile(Self, i, _tileWidth, _tileHeight, (i >= drawIndex), allowCollisions)
Else
_tileObjects[i] = New FlxTile(Self, i, _tileWidth, _tileHeight, (i >= drawIndex), NONE)
End If
i += 1
Wend
width = widthInTiles * _tileWidth
height = heightInTiles * _tileHeight
_rects = _rects.Resize(totalTiles)
i = 0
While (i < totalTiles)
_UpdateTile(i)
i += 1
Wend
_buffers.Clear()
Return Self
End Method
Method Draw:Void()
If (_flickerTimer <> 0) Then
_flicker = Not _flicker
If (_flicker) Return
End If
_camera = FlxG._CurrentCamera
If (_cameras <> Null And Not _cameras.Contains(_camera)) Return
_buffer = _buffers.Get(_camera.ID)
If (_buffer = Null) Then
_buffer = New FlxTilemapBuffer(_tileWidth, _tileHeight, widthInTiles, heightInTiles, _camera)
_buffers.Set(_camera.ID, _buffer)
End If
_screenRows = _buffer.rows
_screenColumns = _buffer.columns
If (Not _buffer.dirty) Then
_point.x = x - Int(_camera.scroll.x * scrollFactor.x) + _buffer.x
_point.y = y - Int(_camera.scroll.y * scrollFactor.y) + _buffer.y
_buffer.dirty = _point.x > 0 Or _point.y > 0 Or _point.x + _buffer.width < _camera.Width Or _point.y + _buffer.height < _camera.Height
End If
If (_buffer.dirty) Then
_point.x = Int(_camera.scroll.x * scrollFactor.x) - x
_point.y = Int(_camera.scroll.y * scrollFactor.y) - y
If (_point.x > 0) Then
_screenXInTiles = (_point.x + 0.0000001) / _tileWidth
Else
_screenXInTiles = (_point.x - 0.0000001) / _tileWidth
End If
If (_point.y > 0) Then
_screenYInTiles = (_point.y + 0.0000001) / _tileHeight
Else
_screenYInTiles = (_point.y - 0.0000001) / _tileHeight
End If
If (_screenXInTiles < 0) _screenXInTiles = 0
If (_screenXInTiles > widthInTiles - _screenColumns) _screenXInTiles = widthInTiles - _screenColumns
If (_screenYInTiles < 0) _screenYInTiles = 0
If (_screenYInTiles > heightInTiles - _screenRows) _screenYInTiles = heightInTiles - _screenRows
_buffer.x = _screenXInTiles * _tileWidth
_buffer.y = _screenYInTiles * _tileHeight
End If
_point.x = x - Int(_camera.scroll.x * scrollFactor.x) + _buffer.x
_point.y = y - Int(_camera.scroll.y * scrollFactor.y) + _buffer.y
If (FlxG._LastDrawingBlend <> AlphaBlend) Then
SetBlend(AlphaBlend)
FlxG._LastDrawingBlend = AlphaBlend
End If
If (FlxG._LastDrawingColor <> _camera.Color) Then
SetColor(_camera._color.r, _camera._color.g, _camera._color.b)
FlxG._LastDrawingColor = _camera.Color
End If
Local alpha:Float = _camera._color.a * _camera.Alpha
If (FlxG._LastDrawingAlpha <> alpha) Then
SetAlpha(alpha)
FlxG._LastDrawingAlpha = alpha
End If
PushMatrix()
Translate( _point.x, _point.y)
Local rowIndex:Int = _screenYInTiles * widthInTiles + _screenXInTiles
Local row:Int = 0
Local column:Int
Local columnIndex:Int
_point.y = 0
If (_buffer.scaleFixX = 1 Or _buffer.scaleFixY = 1) Then
While (row < _screenRows)
columnIndex = rowIndex
column = 0
_point.x = 0
While (column < _screenColumns)
If (_animations <> Null) Then
Local tileAnimation:FlxTileAnimation = _animations.Get(_data[columnIndex])
If tileAnimation <> Null Then
_rect = tileAnimation.GetFrame()
Else
_rect = _rects[columnIndex]
End If
Else
_rect = _rects[columnIndex]
End If
If (_rect <> Null) Then
DrawImageRect(_tiles, _point.x, _point.y, _rect.x, _rect.y, _rect.width, _rect.height)
#If FLX_DEBUG_ENABLED
If (FlxG.VisualDebug And Not ignoreDrawDebug) Then
'note: TODO:
End If
#End
End If
_point.x += _tileWidth
column += 1
columnIndex += 1
Wend
rowIndex += widthInTiles
_point.y += _tileHeight
row += 1
Wend
Else
row = _screenRows - 1
_point.y = row * _tileHeight
rowIndex += widthInTiles * row
While (row >= 0)
column = _screenColumns - 1
_point.x = column * _tileWidth
columnIndex = rowIndex + column
While (column >= 0)
If (_animations <> Null) Then
Local tileAnimation:FlxTileAnimation = _animations.Get(_data[columnIndex])
If tileAnimation <> Null Then
_rect = tileAnimation.GetFrame()
Else
_rect = _rects[columnIndex]
End If
Else
_rect = _rects[columnIndex]
End If
If (_rect <> Null) Then
DrawImageRect(_tiles, _point.x, _point.y, _rect.x, _rect.y, _rect.width, _rect.height, 0, _buffer.scaleFixX, _buffer.scaleFixY)
#If FLX_DEBUG_ENABLED
If (FlxG.VisualDebug And Not ignoreDrawDebug) Then
'note: TODO:
End If
#End
End If
_point.x -= _tileWidth
column -= 1
columnIndex -= 1
Wend
rowIndex -= widthInTiles
_point.y -= _tileHeight
row -= 1
Wend
End If
PopMatrix()
#If FLX_DEBUG_ENABLED
_VisibleCount += 1
#End
End Method
Method PostUpdate:Void()
Super.PostUpdate()
If (_animations <> Null) Then
Local node:map.Node<Int, FlxTileAnimation> = _firstAnimation
While (node <> Null)
node.Value.Update()
node = node.NextNode()
Wend
End If
End Method
Method GetData:Int[](simple:Bool = False)
If (Not simple) Return _data
Local i:Int = 0
Local l:Int = _data.Length()
Local data:Int[l]
While (i < l)
If (_tileObjects[_data[i]].allowCollisions > 0) Then
data[i] = 1
Else
data[i] = 0
End If
i += 1
Wend
Return data
End Method
Method SetDirty:Void(dirty:Bool = True)
For Local buffer:FlxTilemapBuffer = EachIn _buffers.Values()
buffer.dirty = dirty
Next
End Method
Method FindPath:FlxPath(start:FlxPoint, endPoint:FlxPoint, simplify:Bool = True, raySimplify:Bool = False)
Local startIndex:Int = Int((start.y - y) / _tileHeight) * widthInTiles + Int((start.x - x) / _tileWidth)
Local endIndex:Int = Int((endPoint.y - y) / _tileHeight) * widthInTiles + Int((endPoint.x - x) / _tileWidth)
If (_tileObjects[_data[startIndex]].allowCollisions > 0 Or _tileObjects[_data[endIndex]].allowCollisions > 0) Then
Return Null
End If
Local distances:Int[] = _ComputePathDistance(startIndex, endIndex)
If (distances.Length() = 0) Return Null
Local points:Stack<FlxPoint> = New Stack<FlxPoint>()
_WalkPath(distances, endIndex, points)
Local node:FlxPoint
node = points.Top()
node.x = start.x
node.y = start.y
node = points.Get(0)
node.x = endPoint.x
node.y = endPoint.y
If (simplify) _SimplifyPath(points)
If (raySimplify) _RaySimplifyPath(points)
Local path:FlxPath = New FlxPath()
Local i:Int = points.Length() - 1
While (i >= 0)
node = points.Get(i)
If (node <> Null) Then
path.AddPoint(node, true)
End If
i -= 1
Wend
Return path
End Method
Method Overlaps:Bool(objectOrGroup:FlxBasic, inScreenSpace:Bool = False, camera:FlxCamera = Null)
If (FlxGroup(objectOrGroup) <> Null) Then
Local results:Bool = False
Local basic:FlxBasic
Local i:Int = 0
Local members:FlxBasic[] = FlxGroup(objectOrGroup).Members
Local l:Int = FlxGroup(objectOrGroup).Length
While (i < l)
basic = members[i]
If (FlxObject(basic) <> Null) Then
If (OverlapsWithCallback(FlxObject(basic))) Then
results = True
End If
Else
If (Overlaps(basic, inScreenSpace, camera)) Then
results = True
End If
End If
i += 1
Wend
Return results
ElseIf (FlxObject(objectOrGroup) <> Null) Then
Return OverlapsWithCallback(FlxObject(objectOrGroup))
End If
Return False
End Method
Method OverlapsAt:Bool(x:Float, y:Float, objectOrGroup:FlxBasic, inScreenSpace:Bool = False, camera:FlxCamera = Null)
If (FlxGroup(objectOrGroup) <> Null) Then
Local results:Bool = False
Local basic:FlxBasic
Local i:Int = 0
Local members:FlxBasic[] = FlxGroup(objectOrGroup).Members
Local l:Int = FlxGroup(objectOrGroup).Length
While (i < l)
basic = members[i]
If (FlxObject(basic) <> Null) Then
_point.x = x
_point.y = y
If (OverlapsWithCallback(FlxObject(basic), Null, False, _point)) Then
results = True
End If
Else
If (OverlapsAt(x, y, basic, inScreenSpace, camera)) Then
results = True
End If
End If
i += 1
Wend
Return results
ElseIf (FlxObject(objectOrGroup) <> Null) Then
_point.x = x
_point.y = y
Return OverlapsWithCallback(FlxObject(objectOrGroup), Null, False, _point)
End If
Return False
End Method
Method OverlapsWithCallback:Bool(object:FlxObject, callback:FlxTileOverlapChecker = Null, flipCallbackParams:Bool = False, position:FlxPoint = Null)
Local results:Bool = False
Local lx:Float = x
Local ly:Float = y
If (position <> Null) Then
lx = position.x
ly = position.y
End If
Local selectionX:Int = Floor((object.x - lx) / _tileWidth)
Local selectionY:Int = Floor((object.y - ly) / _tileHeight)
Local selectionWidth:Int = selectionX + Ceil(object.width / _tileWidth) + 1
Local selectionHeight:Int = selectionY + Ceil(object.height / _tileHeight) + 1
If (selectionX < 0) selectionX = 0
If (selectionY < 0) selectionY = 0
If (selectionWidth > widthInTiles) selectionWidth = widthInTiles
If (selectionHeight > heightInTiles) selectionHeight = heightInTiles
Local rowStart:Int = selectionY * widthInTiles
Local row:Int = selectionY
Local column:Int
Local tile:FlxTile
Local overlapFound:Bool
Local deltaX:Float = lx - last.x
Local deltaY:Float = ly - last.y
While (row < selectionHeight)
column = selectionX
While (column < selectionWidth)
overlapFound = False
tile = _tileObjects[_data[rowStart + column]]
If (tile.allowCollisions) Then
tile.x = lx + column * _tileWidth
tile.y = ly + row * _tileHeight
tile.last.x = tile.x - deltaX
tile.last.y = tile.y - deltaY
If (callback <> Null) Then
If (flipCallbackParams) Then
overlapFound = callback.IsTileOverlap(object, tile)
Else
overlapFound = callback.IsTileOverlap(tile, object)
End If
Else
overlapFound = (object.x + object.width > tile.x And object.x < tile.x + tile.width And object.y + object.height > tile.y And object.y < tile.y + tile.height)
End If
If (overlapFound) Then
If (tile.callback <> Null And (tile.filter = Null Or object.GetClassInfo().ExtendsClass(tile.filter))) Then
tile.mapIndex = rowStart + column
tile.callback.OnTileHit(tile, object)
End If
results = True
End If
ElseIf(tile.callback <> Null And (tile.filter = Null Or object.GetClassInfo().ExtendsClass(tile.filter))) Then
tile.mapIndex = rowStart + column
tile.callback.OnTileHit(tile, object)
End If
column += 1
Wend
rowStart += widthInTiles
row += 1
Wend
Return results
End Method
Method OverlapsPoint:Bool(point:FlxPoint, inScreenSpace:Bool = False, camera:FlxCamera = Null)
If (Not inScreenSpace) Then
Return _tileObjects[_data[Int(Int((point.y - y) / _tileHeight) * widthInTiles + (point.x - x) / _tileWidth)]].allowCollisions > 0
End If
If (camera = Null) camera = FlxG.Camera
point.x -= camera.scroll.x
point.y -= camera.scroll.y
GetScreenXY(_point, camera)
Return _tileObjects[_data[Int(Int((point.y - _point.y) / _tileHeight) * widthInTiles + (point.x - _point.x) / _tileWidth)]].allowCollisions > 0
End Method
Method GetTile:Int(x:Int, y:Int)
Return _data[y * widthInTiles + x]
End Method
Method GetTileByIndex:Int(index:Int)
Return _data[index]
End Method
Method GetTileInstances:Stack<Int>(index:Int)
Local elements:Stack<Int> = Null
Local i:Int = 0
Local l:Int = widthInTiles * heightInTiles
While (i < l)
If (_data[i] = index) Then
If (elements = Null) elements = New Stack<Int>()
elements.Push(i)
End If
i += 1
Wend
Return elements
End Method
Method GetTileCoords:Stack<FlxPoint>(index:Int, midpoint:Bool = True)
Local elements:Stack<FlxPoint> = Null
Local point:FlxPoint
Local i:Int = 0
Local l:Int = widthInTiles * heightInTiles
While (i < l)
If (_data[i] = index) Then
point = New FlxPoint(x + Int(i Mod widthInTiles) * _tileWidth, y + Int(i / widthInTiles) * _tileHeight)
If (midpoint) Then
point.x += _tileWidth * .5
point.y += _tileHeight * .5
End If
If (elements = Null) elements = New Stack<FlxPoint>()
elements.Push(point)
End If
i += 1
Wend
Return elements
End Method
Method SetTile:Bool(x:Int, y:Int, tile:Int, updateGraphics:Bool = True)
If (x >= widthInTiles Or y >= heightInTiles) Then
Return False
End If
Return SetTileByIndex(y * widthInTiles + x, tile, updateGraphics)
End Method
Method SetTileByIndex:Bool(index:Int, tile:Int, updateGraphics:Bool = True)
If (index >= _data.Length()) Return False
_data[index] = tile
If (Not updateGraphics) Return True
If (auto = OFF) Then
_UpdateTile(index)
Return True
End If
Local i:Int = 0
Local row:Int = Int(index / widthInTiles) - 1
Local rowLength:Int = row + 3
Local column:Int = index Mod widthInTiles - 1
Local columnHeight:Int = column + 3
While (row < rowLength)
column = columnHeight - 3
While (column < columnHeight)
If (row >= 0 And row < heightInTiles And column >= 0 And column < widthInTiles) Then
i = row * widthInTiles + column
_AutoTile(i)
_UpdateTile(i)
End If
column += 1
Wend
row += 1
Wend
Return True
End Method
Method SetTileProperties:Void(tile:Int, allowCollisions:Int = $1111, callback:FlxTileHitListener = Null, callbackFilter:ClassInfo = Null, range:Int = 1)
If (range <= 0) range = 1
Local tileObject:FlxTile
Local i:Int = tile
Local l:Int = tile + range
While (i < l)
tileObject = _tileObjects[i]
tileObject.allowCollisions = allowCollisions
tileObject.callback = callback
tileObject.filter = callbackFilter
i += 1
Wend
End Method
Method Follow:Void(camera:FlxCamera = Null, border:Int = 0, updateWorld:Bool = True)
If (camera = Null) camera = FlxG.Camera
camera.SetBounds(x + border * _tileWidth, y + border * _tileHeight, width - border * _tileWidth * 2, height - border * _tileHeight * 2, updateWorld)
End Method
Method GetBounds:FlxRect(bounds:FlxRect = Null)
If (bounds = Null) bounds = New FlxRect()
Return bounds.Make(x, y, width, height)
End Method
Method Ray:Bool(startPoint:FlxPoint, endPoint:FlxPoint, result:FlxPoint = Null, resolution:Float = 1)
Local _step:Float = _tileWidth
If (_tileHeight < _tileWidth) Then
_step = _tileHeight
End If
_step /= resolution
Local deltaX:Float = endPoint.x - startPoint.x
Local deltaY:Float = endPoint.y - startPoint.y
Local distance:Float = Sqrt(deltaX * deltaX + deltaY * deltaY)
Local steps:Int = Ceil(distance / _step)
Local stepX:Float = deltaX / steps
Local stepY:Float = deltaY / steps
Local curX:Float = startPoint.x - stepX - x
Local curY:Float = startPoint.y - stepY - y
Local tileX:Int
Local tileY:Int
Local i:Int = 0
While (i < steps)
curX += stepX
curY += stepY
If (curX < 0 Or curX > width Or curY < 0 Or curY > height) Then
i += 1
Continue
End If
tileX = curX / _tileWidth
tileY = curY / _tileHeight
If (_tileObjects[_data[tileY * widthInTiles + tileX]].allowCollisions) Then
tileX *= _tileWidth
tileY *= _tileHeight
Local rx:Float = 0
Local ry:Float = 0
Local lx:Float = curX - stepX
Local ly:Float = curY - stepY
Local q:Float = tileX
If (deltaX < 0) q += _tileWidth
rx = q
ry = ly + stepY * ((q - lx) / stepX)
If (ry > tileY And ry < tileY + _tileHeight) Then
If (result <> Null) Then
result.x = rx
result.y = ry
End If
Return False
End If
q = tileY
If (deltaY < 0) q += _tileHeight
rx = lx + stepX * ((q - ly) / stepY)
ry = q
If (rx > tileX And rx < tileX + _tileWidth) Then
If (result <> Null) Then
result.x = rx
result.y = ry
End If
Return False
End If
Return True
End If
i += 1
Wend
Return True
End Method
Function ArrayToCSV:String(data:Int[], width:Int, invert:Bool = False)
Local row:Int = 0
Local column:Int
Local csv:StringStack = New StringStack()
Local height:Int = data.Length() / width
Local index:Int
While (row < height)
column = 0
While (column < width)
index = data[row * width + column]
If (invert) Then
If (index = 0) Then
index = 1
Else
index = 0
End If
End If
If (column = 0) Then
If (row = 0) Then
csv.Push(index)
Else
csv.Push("~n" + index)
End If
Else
csv.Push(", " + index)
End If
column += 1
Wend
row += 1
Wend
Return csv.Join("")
End Function
Method AddAnimation:Void(tileIndex:Int, frames:Int[], frameRate:Float = 0, looped:Bool = True)
If (_animations = Null) Then
_animations = New IntMap<FlxTileAnimation>()
_animations.Values()
End If
Local anim:FlxTileAnimation = New FlxTileAnimation(Self, frames, frameRate, looped)
_animations.Set(tileIndex, anim)
_firstAnimation = _animations.ObjectEnumerator().NextObject()
End Method
Method AddAnimation:Void(tileIndex:Int, startFrame:Int, endFrame:Int, frameRate:Float = 0, looped:Bool = True)
Local i:Int = 0
Local l:Int = endFrame - startFrame + 1
Local frames:Int[] = New Int[l]
While (i < l)
frames[i] = startFrame + i
i += 1
Wend
AddAnimation(tileIndex, frames, frameRate, looped)
End Method
Private
Method _SimplifyPath:Void(points:Stack<FlxPoint>)
Local deltaPrevious:Float
Local deltaNext:Float
Local last:FlxPoint = points.Get(0)
Local node:FlxPoint
Local nextPoint:FlxPoint
Local i:Int = 1
Local l:Int = points.Length() - 1
While (i < l)
node = points.Get(i)
nextPoint = points.Get(i + 1)
deltaPrevious = (node.x - last.x) / (node.y - last.y)
deltaNext = (node.x - nextPoint.x) / (node.y - nextPoint.y)
If (last.x = nextPoint.x Or last.y = nextPoint.y Or deltaPrevious = deltaNext) Then
points.Set(i, Null)
Else
last = node
End If
i += 1
Wend
End Method
Method _RaySimplifyPath:Void(points:Stack<FlxPoint>)
Local source:FlxPoint = points.Get(0)
Local lastIndex:Int = -1
Local node:FlxPoint
Local i:Int = 1
Local l:Int = points.Length()
While (i < l)
node = points.Get(i)
i += 1
If (node = Null) Continue
If (Ray(source, node, _point)) Then
If (lastIndex >= 0) points.Set(lastIndex, Null)
Else
source = points.Get(lastIndex)
End If
lastIndex = i - 1
Wend
End Method
Method _ComputePathDistance:Int[](startIndex:Int, endIndex:Int)
Local mapSize:Int = widthInTiles * heightInTiles
Local distances:Int[mapSize]
Local i:Int = 0
While (i < mapSize)
If (_tileObjects[_data[i]].allowCollisions) Then
distances[i] = -2
Else
distances[i] = -1
End If
i += 1
Wend
distances[startIndex] = 0
Local distance:Int = 1
Local neighbors:Stack<Int> = New Stack<Int>([startIndex])
Local current:Stack<Int>
Local currentIndex:Int
Local left:Bool
Local right:Bool
Local up:Bool
Local down:Bool
Local currentLength:Int
Local foundEnd:Bool = False
While (neighbors.Length() > 0)
current = neighbors
neighbors = New Stack<Int>()
i = 0
currentLength = current.Length()
While (i < currentLength)
currentIndex = current.Get(i)
If (currentIndex = endIndex) Then
foundEnd = True
neighbors.Clear()
Exit
End If