Skip to content

Commit ca326df

Browse files
committed
a
1 parent 5c9c192 commit ca326df

File tree

2 files changed

+985
-541
lines changed

2 files changed

+985
-541
lines changed

source/yutautil/FlxArray.hx

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package yutautil;
2+
3+
import flixel.FlxBasic;
4+
import flixel.group.FlxGroup;
5+
6+
abstract FlxArray<T:FlxBasic>(FlxTypedGroup<T>) {
7+
8+
public var length(get, never):Int;
9+
public var capacity(get, set):Int;
10+
11+
public function new(?capacity:Int) {
12+
this = new FlxTypedGroup<T>(capacity);
13+
}
14+
15+
public function add(item:T):Void {
16+
this.add(item);
17+
}
18+
19+
public function remove(item:T):Void {
20+
this.remove(item);
21+
}
22+
23+
public function clear():Void {
24+
this.clear();
25+
}
26+
27+
public function get_length():Int {
28+
return this.length;
29+
}
30+
31+
public function get_capacity():Int {
32+
return this.maxSize;
33+
}
34+
35+
public function set_capacity(value:Int):Void {
36+
this.maxSize = value;
37+
}
38+
39+
// Array access: a[index]
40+
@:arrayAccess
41+
public function get(index:Int):T {
42+
return this.members[index];
43+
}
44+
45+
@:arrayAccess
46+
public function set(index:Int, value:T):Void {
47+
this.members[index] = value;
48+
}
49+
50+
@:to
51+
public function toArray():Array<T> {
52+
return this.members;
53+
}
54+
55+
@:from
56+
public static function fromArray<T:FlxBasic>(array:Array<T>):FlxArray<T> {
57+
var flxArray = new FlxArray<T>();
58+
for (item in array) {
59+
flxArray.add(item);
60+
}
61+
return flxArray;
62+
}
63+
64+
@:from
65+
public static function fromFlxTypedGroup<T:FlxBasic>(group:FlxTypedGroup<T>):FlxArray<T> {
66+
var flxArray = new FlxArray<T>(group.maxSize);
67+
for (item in group.members) {
68+
flxArray.add(item);
69+
}
70+
return flxArray;
71+
}
72+
73+
public function toString():String {
74+
return "FlxArray<" + Type.getClassName(Type.getClass(this.members[0])) + ">(" + this.length + " items)";
75+
}
76+
77+
public function destroy():Void {
78+
this.clear();
79+
this.maxSize = 0;
80+
this.destroy();
81+
}
82+
83+
public function superDestroy():Void {
84+
// Try/catch destroy each member
85+
for (member in this.members) {
86+
try {
87+
if (member != null) member.destroy();
88+
} catch (e:Dynamic) {
89+
trace("Warning: Failed to destroy member: " + e);
90+
}
91+
}
92+
this.clear();
93+
@:privateAccess
94+
this.members = null;
95+
this.maxSize = 0;
96+
destroy();
97+
}
98+
99+
public inline function iterator():Iterator<T> {
100+
return this.members.iterator();
101+
}
102+
103+
104+
}

0 commit comments

Comments
 (0)