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
/
flxgroup.monkey
1034 lines (804 loc) · 24.8 KB
/
flxgroup.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
#rem
header:This module contains the FlxGroup class
#end
Strict
Import reflection
Import flxextern
Import flxbasic
Import flxobject
Import flxu
Import system.flxarray
Private
Import brl.pool
Public
#Rem
summary:This is an organizational class that can update and render a bunch of FlxBasics.
[i][b]NOTE:[/b] Although FlxGroup extends [a flxbasic.monkey.html]FlxBasic[/a], it will not automatically add itself to the global collisions quad tree, it will only add its members.[/i]
#End
Class FlxGroup Extends FlxBasic
Global __CLASS__:Object
#Rem
summary:See detail.
Use with [a #Sort]Sort()[/a] to sort in ascending order.
#End
Const ASCENDING:Bool = False
#Rem
summary:See detail.
Use with [a #Sort]Sort()[/a] to sort in descending order.
#End
Const DESCENDING:Bool = True
Private
Field _maxSize:Int
Field _marker:Int
Field _length:Int
Field _members:FlxBasic[]
Field _comparators:StringMap<Comparator>
Field _comparator:Comparator
Field _slowIndex:String
Field _sortDescending:Bool
Field _dirty:Bool
Field _defrag:Bool
Public
#Rem
summary:Constructor
Params:
[list]
[*]maxSize:Int - the maximum capacity of this group.
[/list]
#End
Method New(maxSize:Int = 0)
Super.New()
_maxSize = maxSize
_marker = 0
_length = 0
_cameras = Null
_comparators = New StringMap<Comparator>()
_dirty = False
_defrag = False
End Method
#Rem
summary:The number of entries in the members array.
For performance and safety you should check this variable instead of members.length unless you really know what you're doing!
#End
Method Length:Int() Property
Return _length
End Method
#Rem
summary:See detail.
Array of all the [a flxbasic.monkey.html]FlxBasic[/a]s that exist in this group.
#End
Method Members:FlxBasic[]() Property
Return _members
End Method
#Rem
summary:Override this function to handle any deleting or "shutdown" type operations you might need,such as removing traditional Monkey/Mojo children like Images objects.
#End
Method Destroy:Void()
Local basic:FlxBasic
Local i:Int = 0
While(i < _length)
basic = _members[i]
If (basic <> Null) basic.Destroy()
i+=1
Wend
_length = 0
_members = _members.Resize(_length)
_cameras = Null
If (_comparator <> Null) Then
_comparator.Clear()
_comparator = Null
End If
If (_comparators <> Null) Then
_comparators.Clear()
_comparators = Null
End If
Super.Destroy()
End Method
#Rem
summary:Just making sure we don't increment the active objects count.
#End
Method PreUpdate:Void()
End Method
#Rem
summary:Automatically goes through and calls update on everything you added.
#End
Method Update:Void()
Local basic:FlxBasic
Local i:Int = 0
While(i < _length)
basic = _members[i]
If (basic <> Null And basic.exists And basic.active) Then
basic.PreUpdate()
basic.Update()
If (basic.HasTween) basic.UpdateTweens()
basic.PostUpdate()
End If
i+=1
Wend
If (HasTween) UpdateTweens()
End Method
#Rem
summary:Automatically goes through and calls render on everything you added.
#End
Method Draw:Void()
If (_cameras <> Null And Not _cameras.Contains(FlxG._CurrentCamera)) Return
Local basic:FlxBasic
Local i:Int = 0
While(i < _length)
basic = _members[i]
If (basic <> Null And basic.exists And basic.visible) basic.Draw()
i+=1
Wend
End Method
#Rem
summary:The maximum capacity of this group. Default is 0, meaning no max capacity, and the group can just grow.
#End
Method MaxSize:Int() Property
Return _maxSize
End Method
Method MaxSize:Void(size:Int) Property
_maxSize = size
If (_marker >= _maxSize) _marker = 0
If (_maxSize = 0 Or _maxSize >= _length) Return
Local basic:FlxBasic
Local i:Int = _maxSize
Local l:Int = _length
While(i < l)
basic = _members[i]
If (basic <> Null) basic.Destroy()
i+=1
Wend
_members = _members.Resize(_maxSize)
_length = _maxSize
End Method
#Rem
summary:See details.
Adds a new [a flxbasic.monkey.html]FlxBasic[/a] subclass ([a flxbasic.monkey.html]FlxBasic[/a], [a flxsprite.monkey.html]FlxSprite[/a], Enemy, etc) to the group. FlxGroup will try to replace a null member of the array first. Failing that, FlxGroup will add it to the end of the member array, assuming there is room for it, and doubling the size of the array if necessary.
[b]WARNING[/b]: If the group has a maxSize that has already been met, the object will NOT be added to the group!
Params:
[list]
[*][a flxbasic.monkey.html]object:FlxBasic[/a] - the object you want to add to the group.
[/list]
Return the same [a flxbasic.monkey.html]FlxBasic[/a] object that was passed in.
#End
Method Add:FlxBasic(object:FlxBasic)
If (_IndexOf(object) >= 0) Return object
If (Not object) Then
FlxG.Log("WARNING: Cannot add a `null` object to a FlxGroup.")
Return Null
End If
_dirty = True
Local i:Int = 0
While (i < _length)
If (_members[i] = Null) Then
_members[i] = object
If (i >= _length) _length = i+1
Return object
End If
i+=1
Wend
If (_maxSize > 0) Then
If (_length = _members.Length()) Then
If (_length >= _maxSize) Then
Return object
ElseIf (_length*2 + 10 <= _maxSize)
_members = _members.Resize(_length*2 + 10)
Else
_members = _members.Resize(_maxSize)
End If
End if
Else
If (_length = _members.Length()) Then
_members = _members.Resize(_length*2 + 10)
End If
End If
_members[i] = object
_length+=1
Return object
End Method
#Rem
summary:Recycling is designed to help you reuse game objects without always re-allocating or "newing" them.
If you specified a maximum size for this group (like in [a flxemitter.monkey.html]FlxEmitter[/a],then recycle will employ what we're calling "rotating" recycling. Recycle() will first check to see if the group is at capacity yet. If group is not yet at capacity, Recycle() returns a new object. If the group IS at capacity, then Recycle() just returns the next object in line
If you did NOT specify a maximum size for this group, then Recycle() will employ what we're calling "grow-style" recycling. Recycle() will return either the first object with exists = false, or, finding none, add a new object to the array, doubling the size of the array if necessary.
[b]WARNING:[/b] If this function needs to create a new object, and no object class was provided, it will return null instead of a valid object!
Params:
[list]
[*]objectClass:FlxClass - the type objectClass implements FlxClass you want to recycle (e.g. FlxSpriteClass, EvilRobotClass, etc). Do NOT "new" the class in the parameter!
[/list]
Return a reference to the object that was created. Don't forget to cast it back to the objectClass you want (e.g. myObject = myObjectClass(myGroup.recycle(myObjectClassClass))).
#End
Method Recycle:FlxBasic(objectClass:ClassInfo = Null)
'note: TODO: check objectClass first
If (_maxSize > 0) Then
If (_length < _maxSize) Then
If (objectClass = Null) Return Null
_dirty = True
Return Add(FlxBasic(objectClass.NewInstance()))
Else
Local basic:FlxBasic = _members[_marker]
_marker+=1
If (_marker >= _maxSize) _marker = 0
Return basic
End If
Else
Local basic:FlxBasic = GetFirstAvailable(objectClass)
If (basic <> Null) Return basic
If (objectClass = Null) Return Null
_dirty = True
Return Add(FlxBasic(objectClass.NewInstance()))
End If
End Method
Method Recycle:FlxBasic(objectOrClass:Object)
If (ClassInfo(objectOrClass) <> Null) Then
Return Recycle(ClassInfo(objectOrClass))
Else
Return Recycle(GetClass(objectOrClass))
End If
End Method
#Rem
summary:Removes an object from the group.
Params:
[list]
[*][a flxbasic.monkey.html]object:FlxBasic[/a] - The [a flxbasic.monkey.html]FlxBasic[/a] you want to remove.
[*]splice:Bool - whether the object should be cut from the array entirely or not.
[/list]
Return the removed object.
#End
Method Remove:FlxBasic(object:FlxBasic, splice:Bool = False)
Local index:Int = _IndexOf(object)
If (index < 0) Return Null
If (splice) Then
Local i:Int = index
Local l:Int = _length
While(i < _length - 1)
_members[i] = _members[i+1]
i+=1
Wend
_length-=1
Else
_members[index] = Null
End If
_dirty = True
Return object
End Method
#Rem
summary:Replaces an existing [a flxbasic.monkey.html]FlxBasic[/a] with a new one.
Params:
[list]
[*][a flxbasic.monkey.html]oldObject:FlxBasic[/a] - the object you want to replace.
[*][a flxbasic.monkey.html]newObject:FlxBasic[/a] - the new object you want to use instead.
[/list]
Return the new object.
#End
Method Replace:FlxBasic(oldObject:FlxBasic, newObject:FlxBasic)
Local index:Int = _IndexOf(oldObject)
If (index < 0) Return Null
_members[index] = newObject
_dirty = True
Return newObject
End Method
#Rem
summary:Call this function to sort the group according to a particular value and order.
For example, to sort game objects for Zelda-style overlaps you might call myGroup.Sort(FlxObject.YComparator, FlxGroup.ASCENDING) at the bottom of your [a flxstate.monkey.html]FlxState.Update()[/a] override. To sort all existing objects after a big explosion or bomb attack, you might call myGroup.Sort(FlxBasic.ExistsComparator, FlxGroup.DESCENDING)
Params:
[list]
[*]comparator:FlxBasicComparator - The FlxBasicComparator you want to sort on. Default value is FlxObject.YComparator.
[*]order:Bool - A FlxGroup constant that defines the sort order. Possible values are ASCENDING and DESCENDING. Default value is ASCENDING.
[/list]
Return the new object.
#End
Method Sort:Void(index:String = "y", order:Bool = ASCENDING)
If (_dirty) _Validate()
If ( Not _defrag) Then
Local b:FlxBasic = GetFirstNotNull()
If (b = Null) Return
_comparator = _GetComparator(GetFirstNotNull().GetClassInfo().GetField(index))
If (_comparator = Null) Return
_sortDescending = order
_FastQSort(0, _length - 1)
Else
_slowIndex = index
_sortDescending = order
_SlowQSort(0, _length - 1)
End If
End Method
Method SetAll:Void(variableName:String, value:Object, recurse:Bool = True)
If (_dirty) _Validate()
If ( Not _defrag) Then
_FastSetAll(variableName, value, recurse)
Else
_SlowSetAll(variableName, value, recurse)
End If
End Method
Method CallAll:Void(functionName:String, recurse:Bool = True)
If (_dirty) _Validate()
If ( Not _defrag) Then
_FastCallAll(functionName, recurse)
Else
_SlowCallAll(functionName, recurse)
End If
End Method
Method GetFirstAvailable:FlxBasic(objectClass:ClassInfo = null)
Local basic:FlxBasic
Local i:Int = 0
While(i < _length)
basic = _members[i]
If (basic <> Null And Not basic.exists And
(objectClass = Null Or basic.GetClassInfo().ExtendsClass(objectClass))) Return basic
i+=1
Wend
Return Null
End Method
Method GetFirstNull:Int()
Local i:Int = 0
While(i < _length)
If (_members[i] = Null) Return i
i+=1
Wend
Return -1
End Method
Method GetFirstNotNull:FlxBasic()
Local i:Int = 0
Local basic:FlxBasic
While(i < _length)
basic = _members[i]
If (basic <> Null) Return basic
i+=1
Wend
Return Null
End Method
Method GetFirstExtant:FlxBasic()
Local i:Int = 0
Local basic:FlxBasic
While(i < _length)
basic = _members[i]
If (basic <> Null And basic.exists) Return basic
i+=1
Wend
Return Null
End Method
Method GetFirstAlive:FlxBasic()
Local i:Int = 0
Local basic:FlxBasic
While(i < _length)
basic = _members[i]
If (basic <> Null And basic.exists And basic.alive) Return basic
i+=1
Wend
Return Null
End Method
Method GetFirstDead:FlxBasic()
Local i:Int = 0
Local basic:FlxBasic
While(i < _length)
basic = _members[i]
If (basic <> Null And Not basic.alive) Return basic
i+=1
Wend
Return Null
End Method
Method CountLiving:Int()
Local count:Int = -1
Local i:Int = 0
Local basic:FlxBasic
While(i < _length)
basic = _members[i]
If (basic <> Null) Then
If (count < 0) count = 0
If (basic.exists And basic.alive) count+=1
End If
i+=1
Wend
Return count
End Method
Method CountDead:Int()
Local count:Int = -1
Local i:Int = 0
Local basic:FlxBasic
While(i < _length)
basic = _members[i]
If (basic <> Null) Then
If (count < 0) count = 0
If (Not basic.alive) count+=1
End If
i+=1
Wend
Return count
End Method
Method GetRandom:FlxBasic(startIndex:Int = 0, length:Int = 0)
If (length = 0) length = _length
Return FlxArray<FlxBasic>.GetSafeRandom(_members, startIndex, length)
End Method
Method Clear:Void()
_length = 0
End Method
Method ReviveAll:Void()
Local i:Int = 0
Local basic:FlxBasic
While(i < _length)
basic = _members[i]
If (basic <> Null And Not basic.exists) Then
If (FlxGroup(basic) <> Null) Then
FlxGroup(basic).ReviveAll()
Else
basic.Revive()
End If
End If
i+=1
Wend
Super.Revive()
End Method
Method Kill:Void()
Local i:Int = 0
Local basic:FlxBasic
While(i < _length)
basic = _members[i]
If (basic <> Null And basic.exists) basic.Kill()
i+=1
Wend
Super.Kill()
End Method
Method ObjectEnumerator:Enumerator()
Return _Enumerators.Allocate()._Reset(Self)
End
Private
Method _FastSetAll:Void(variableName:String, value:Object, recurse:Bool = True)
Local basic:FlxBasic = GetFirstNotNull()
If (basic = Null) Return
Local f:FieldInfo = basic.GetClassInfo().GetField(variableName)
If (f = Null) Then
Local prop:MethodInfo = basic.GetClassInfo().GetMethod(variableName,[GetClass(value)])
If (prop <> Null) _FastCallAll(prop,[value], recurse)
Return
End If
Local i:Int = 0
While(i < _length)
basic = _members[i]
If (basic <> Null) Then
If (FlxGroup(basic) <> Null) Then
If (recurse) FlxGroup(basic).SetAll(variableName, value, recurse)
Local groupField:FieldInfo = basic.GetClassInfo().GetField(variableName)
If (groupField = Null) Then
Local groupProp:MethodInfo = basic.GetClassInfo().GetMethod(variableName,[GetClass(value)])
If (groupProp <> Null) _FastCallAll(groupProp,[value], recurse)
Else
groupField.SetValue(basic, value)
End If
Else
f.SetValue(basic, value)
End If
End If
i+=1
Wend
End Method
Method _SlowSetAll:Void(variableName:String, value:Object, recurse:Bool = True)
Local basic:FlxBasic, f:FieldInfo, p:MethodInfo
Local valClass:ClassInfo[] =[GetClass(value)]
Local valArray:Object[] =[value]
Local i:Int = 0
While (i < _length)
basic = _members[i]
If (basic <> Null) Then
If (recurse And FlxGroup(basic) <> Null) FlxGroup(basic)._SlowSetAll(variableName, value, recurse)
f = basic.GetClassInfo().GetField(variableName)
If (f = Null) Then
p = basic.GetClassInfo().GetMethod(variableName, valClass)
If (p <> Null) p.Invoke(basic, valArray)
Else
f.SetValue(basic, value)
End If
End If
i += 1
Wend
End Method
Method _FastCallAll:Void(methodName:String, recurse:Bool = True)
Local basic:FlxBasic = GetFirstNotNull()
If (basic = Null) Return
Local m:MethodInfo = basic.GetClassInfo().GetMethod(methodName,[])
If (m <> Null) Then
_FastCallAll(m,[], recurse)
End If
End Method
Method _FastCallAll:Void(methodObject:MethodInfo, values:Object[], recurse:Bool = True)
Local basic:FlxBasic
Local i:Int = 0
While(i < _length)
basic = _members[i]
If (basic <> Null) Then
If (FlxGroup(basic) <> Null) Then
If (recurse) FlxGroup(basic)._FastCallAll(methodObject, values, recurse)
Local groupMethod:MethodInfo
If (values.Length() > 0) Then
groupMethod = basic.GetClassInfo().GetMethod(methodObject.Name,[GetClass(values[0])])
Else
groupMethod = basic.GetClassInfo().GetMethod(methodObject.Name,[])
End If
If (groupMethod <> Null) groupMethod.Invoke(basic, values)
Else
methodObject.Invoke(basic, values)
End If
End If
i += 1
Wend
End Method
Method _SlowCallAll:Void(methodName:String, recurse:Bool = True)
Local basic:FlxBasic, m:MethodInfo
Local i:Int = 0
While (i < _length)
basic = _members[i]
If (basic <> Null) Then
If (recurse And FlxGroup(basic) <> Null) FlxGroup(basic)._SlowCallAll(methodName, recurse)
m = basic.GetClassInfo().GetMethod(methodName,[])
If (m <> Null) Then
m.Invoke(basic,[])
End If
End If
i += 1
Wend
End Method
Method _Validate:Void()
_dirty = False
_defrag = False
Local basic:FlxBasic = GetFirstNotNull()
If (basic = Null) Then Return
Local i:Int = 0
Local first:ClassInfo = basic.GetClassInfo()
While (i < _length)
If (_members[i] <> Null And Not first.ExtendsClass(_members[i].GetClassInfo()) And FlxGroup(_members[i]) = Null) Then
_defrag = True
Return
End If
i+=1
Wend
End Method
Method _IndexOf:Int(object:FlxBasic)
Local i:Int = 0
While(i < _length)
If (_members[i] = object) Return i
i+=1
Wend
Return -1
End Method
Method _FastQSort:Void(left:Int, right:Int)
If (right > left) Then
Local pivot:Int = left + (right-left)/2
Local newPivot:Int = _FastDoSort(left, right, pivot)
_FastQSort(left, newPivot - 1)
_FastQSort(newPivot + 1, right)
End If
End Method
Method _FastDoSort:Int(left:Int, right:Int, pivot:Int)
Local basic:FlxBasic = _members[pivot]
_members[pivot] = _members[right]
_members[right] = basic
Local store:Int = left
Local basicToCompare:FlxBasic
Local i:Int = left
While (i < right)
basicToCompare = _members[i]
If (basicToCompare = Null Or basic = Null) Then
If (basicToCompare <> Null And basic = Null) Then
_members[i] = _members[store]
_members[store] = basicToCompare
store+=1
End If
i += 1
Continue
End If
If (_sortDescending) Then
If (_comparator.Compare(basicToCompare, basic) >= 0) Then
_members[i] = _members[store]
_members[store] = basicToCompare
store+=1
End If
Else
If (_comparator.Compare(basicToCompare, basic) <= 0) Then
_members[i] = _members[store]
_members[store] = basicToCompare
store+=1
End If
End If
i+=1
Wend
basic = _members[store]
_members[store] = _members[right]
_members[right] = basic
Return store
End Method
Method _SlowQSort:Void(left:Int, right:Int)
If (right > left) Then
Local pivot:Int = left + (right-left)/2
Local newPivot:Int = _SlowDoSort(left, right, pivot)
_SlowQSort(left, newPivot - 1)
_SlowQSort(newPivot + 1, right)
End If
End Method
Method _SlowDoSort:Int(left:Int, right:Int, pivot:Int)
Local basic:FlxBasic = _members[pivot]
_members[pivot] = _members[right]
_members[right] = basic
Local store:Int = left
Local basicToCompare:FlxBasic
Local i:Int = left
While (i < right)
basicToCompare = _members[i]
If (basicToCompare = Null Or basic = Null) Then
If (basicToCompare <> Null And basic = Null) Then
_members[i] = _members[store]
_members[store] = basicToCompare
store+=1
End If
i += 1
Continue
End If
_comparator = _GetComparator(basicToCompare.GetClassInfo().GetField(_slowIndex))
If (_comparator <> Null) Then
If (_sortDescending) Then
If (_comparator.Compare(basicToCompare, basic) >= 0) Then
_members[i] = _members[store]
_members[store] = basicToCompare
store+=1
End If
Else
If (_comparator.Compare(basicToCompare, basic) <= 0) Then
_members[i] = _members[store]
_members[store] = basicToCompare
store+=1
End If
End If
End If
i+=1
Wend
basic = _members[store]
_members[store] = _members[right]
_members[right] = basic
Return store
End Method
Method _GetComparator:Comparator(sortIndex:FieldInfo)
If (sortIndex = Null) Return Null
Local sortIndexType:ClassInfo = sortIndex.Type
Local key:String = sortIndexType.Name
Local comparator:Comparator = _comparators.Get(key)
If (comparator = Null) Then
Select key
Case FloatClass().Name
comparator = New FloatComparator()
Case IntClass().Name
comparator = New IntComparator()
Case BoolClass().Name
comparator = New BoolComparator()
Case StringClass().Name
comparator = New StringComparator()
Default
key = FlxU.GetObjectClass().Name
comparator = _comparators.Get(key)
If (comparator = Null) comparator = New ObjectComparator()
End Select
_comparators.Set(key, comparator)
End If
comparator.Clear()
If (key <> FlxU.GetObjectClass().Name) Then
comparator.Reset(sortIndex)
Else
Local sortMethod:MethodInfo = sortIndexType.GetMethod("Compare",[sortIndexType])
If (sortMethod = Null) sortMethod = sortIndexType.GetMethod("Compare",[FlxU.GetObjectClass()])
If (sortMethod <> Null) Then
comparator.Reset(sortIndex, sortMethod)
Else
comparator = Null
End If
End If
Return comparator
End Method
End Class
Class Enumerator
Method HasNext:Bool()
If (_dirty) Then
_dirty = False
_hasNext = False
Repeat
If (_nextIndex = _group._length) Then
_Enumerators.Free(Self)
Return _hasNext
End If
_basic = _group._members[_nextIndex]
_nextIndex += 1
Until (_basic <> Null)
_hasNext = True
End If
If ( Not _hasNext) _Enumerators.Free(Self)
Return _hasNext
End
Method NextObject:FlxBasic()
If (_dirty And Not HasNext()) Return Null
_dirty = True
Return _basic
End
Method Destroy:Void()
_Reset(Null)
_basic = Null
End Method
Private
Field _group:FlxGroup
Field _index:Int
Field _nextIndex:Int
Field _hasNext:Bool
Field _dirty:Bool
Field _basic:FlxBasic
Method _Reset:Enumerator(group:FlxGroup)
_group = group
_nextIndex = 0
_hasNext = False
_dirty = True
Return Self
End Method
End
Private
Global _Enumerators:Pool<Enumerator> = New Pool<Enumerator>()
Class Comparator
Method Compare:Int(basic1:FlxBasic, basic2:FlxBasic) Abstract
Method Reset:Comparator(sortIndex:FieldInfo, sortMethod:MethodInfo = Null)
_sortIndex = sortIndex
_sortMethod = sortMethod
Return Self
End Method
Method Clear:Void()
_sortIndex = Null
_sortMethod = Null
End Method
Private
Field _sortIndex:FieldInfo
Field _sortMethod:MethodInfo
End Class
Class BoolComparator Extends Comparator
Method Compare:Int(basic1:FlxBasic, basic2:FlxBasic)
Return Int(UnboxBool(_sortIndex.GetValue(basic1))) - Int(UnboxBool(_sortIndex.GetValue(basic2)))
End Method
End Class
Class IntComparator Extends Comparator
Method Compare:Int(basic1:FlxBasic, basic2:FlxBasic)
Return UnboxInt(_sortIndex.GetValue(basic1)) - UnboxInt(_sortIndex.GetValue(basic2))
End Method
End Class
Class FloatComparator Extends Comparator
Method Compare:Int(basic1:FlxBasic, basic2:FlxBasic)
Return UnboxFloat(_sortIndex.GetValue(basic1)) - UnboxFloat(_sortIndex.GetValue(basic2))
End Method
End Class
Class StringComparator Extends Comparator
Method Compare:Int(basic1:FlxBasic, basic2:FlxBasic)
Return UnboxString(_sortIndex.GetValue(basic1)).Compare(UnboxString(_sortIndex.GetValue(basic2)))
End Method
End Class
Class ObjectComparator Extends Comparator
Method Compare:Int(basic1:FlxBasic, basic2:FlxBasic)
Return UnboxInt(_sortMethod.Invoke(_sortIndex.GetValue(basic1),[_sortIndex.GetValue(basic2)]))
End Method