-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFGroup.as
More file actions
178 lines (148 loc) · 3.17 KB
/
FGroup.as
File metadata and controls
178 lines (148 loc) · 3.17 KB
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
package Framework
{
public class FGroup extends FObject
{
public var members:Array;
public var length:int;
protected var _maxSize:uint;
public function FGroup(maxSize:uint = 0)
{
members = new Array();
_maxSize = maxSize;
super();
draws = true;
}
/*
// Adds the given FObject to the group by first attempting to
// add the object to an empty spot in members, but, failing that,
// will push the object onto the end of the list.
*/
public function Add(o:FObject):FObject
{
// Don't need to add the same object twice
if(members.indexOf(o) >= 0)
return o;
// Search for an empty space in the group to place the new object in
var i:uint;
var l:uint = members.length;
while(i < l)
{
if(members[i] == null)
{
members[i] = o;
addChild(o);
if(i >= length)
length = i + 1;
return o;
}
i++;
}
// If there's no null spot, make room and add our object
if(_maxSize > 0)
{
if(members.length >= _maxSize)
return o;
else if(members.length * 2 <= _maxSize)
members.length *= 2;
else
members.length = _maxSize;
}
else
{
members.length *= 2;
}
addChild(o);
members[i] = o;
length = i + 1;
return o;
}
public function Remove(o:FObject, Splice:Boolean = false):FObject
{
if(o != null)
{
var i:int = members.indexOf(o);
// If member doesn't exist, we're done
if(i < 0 || i >= members.length)
return null;
// Remove the sprite from being displayed in the group
removeChild(o);
// Either splice it (expensive) or just set obj to null
if(Splice)
{
members.splice(i, 1);
length--;
}
else
{
members[i] = null
}
return o;
}
else
{
return null;
}
}
override public function Destroy():void
{
for(var i:int = members.length - 1; i >= 0; i--)
{
if(members[i] != null)
{
Remove(members[i]).Destroy();
}
}
}
override public function Update():void
{
super.Update();
if(!paused && thinks)
{
for each(var o:FObject in members)
{
if(o != null && o.exists && o.thinks)
{
o.Update();
}
}
}
}
override public function Draw():void
{
if(!paused)
{
for each(var o:FObject in members)
{
if(o != null && o.draws && o.exists)
o.Draw();
}
}
}
// To-do: Implement maxSize recycling handling
public function Recycle(ObjectClass:Class = null):FObject
{
var o:FObject = getFirstAvailable(ObjectClass);
if(o != null)
return o;
else if(ObjectClass == null)
return null;
return Add(new ObjectClass() as FObject);
}
private function getFirstAvailable(ObjectClass:Class = null):FObject
{
var o:FObject;
var i:uint = 0;
while(i < length)
{
o = members[i++] as FObject;
// Things in () must be encased for some reason. Don't change.
if(o != null && !o.exists && ((ObjectClass == null) || (o is ObjectClass)))
return o;
}
return null;
}
public function Pause():void { paused = true; }
public function Unpause():void { paused = false; }
public function TogglePause():void { paused = !paused; }
}
}