/
ProximityManager.as
351 lines (303 loc) · 12.9 KB
/
ProximityManager.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
/*
The MIT License
Copyright (c) 2009 Arnaud Gatouillat / iq12 (fu AT iq12 D0T com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package
{
import flash.display.DisplayObject;
import __AS3__.vec.Vector;
import flash.geom.Rectangle;
import flash.utils.Dictionary;
public class ProximityManager
{
/** Ratio of gridSize, calculated once to use multiplies instead of divisions */
private var _gridSizeRatio:Number;
/** The width of the grid in cells, includes an extra row on both edges */
private var _gridWidth:int;
/** The height of the grid in cells, includes an extra col on both edges */
private var _gridHeight:int;
/** bounds.x, stored in a class var for speedier access */
private var _boundsX:Number;
/** bounds.y, stored in a class var for speedier access */
private var _boundsY:Number;
/** The number of bits needed to store the height in a cellIndex */
private var _heightNeededBits:int;
/** Storage of items grouped by cellIndex */
private var _itemsByCell:Vector.<Vector.<DisplayObject>>;
/** The insertion point for each _itemsByCell vector */
private var _itemsByCellInsertIdx:Vector.<int>;
/** items.length at last #update */
private var _lastItemsLength:int;
/** Cache of cellIndexes by item */
private var _lastCellIndexByItem:Dictionary = new Dictionary;
/**
* Returns all display objects in the current and adjacent grid cells of the
* specified display object.
*/
public var getNeighbors:Function;
/**
* Specifies a Vector of DisplayObjects that will be used to populate the grid.
*/
public var update:Function;
/**
* The basic ideas are:
* - use a vector to store for each cell, a vector of displayobjects
* - identify each cell with an int. as the cellIndex calculations with be called a lot of times use bitwise operations
* - for getting the actual neighbors, calculate all 9 cell indexes, and use Vector#concat to merge the vectors
* - ints seem to be quicker than uints in this case (due to bitwise operators?)
*
* Implementation:
* - cell identifier:
* . calculate to row# and col#
* . shift the col# by the needed bits to store the maximum row# so they don't overlap each other
* . use bitwise-OR (|) to merge col# and row#
* - on the edges:
* . on bottom and right edges, we're created empty cells, nothing to do
* . on top and left edges, creating empty cells implied to +1 row# and col#, so it's slower
* we detect the cases and deflect the col# to the right cell (empty), the row# to the top cell (empty)
*
* Update 13.11.2009:
* - added a check between runs to use the old data if items didn't move
* that check takes time: this is an optimization choice favoring still situations
*
* @langversion ActionScript 3.0
* @playerversion Flash 10.0
*
* @author Arnaud Gatouillat / iq12 (fu AT iq12 D0T com)
* @since 12.11.2009
*/
public function ProximityManager(gridSize:int, bounds:Rectangle = null)
{
super();
_gridSizeRatio = 1 / Number(gridSize);
// to avoid to test if we're on right and bottom edges,
// we increase the grid dimensions by 1
_gridWidth = int( Math.ceil( bounds.width * _gridSizeRatio ) ) + 1;
_gridHeight = int( Math.ceil( bounds.height * _gridSizeRatio ) ) + 1;
// calculate the number of bits needed to store the height in the cellIndexes
_heightNeededBits = 1;
var gridHeight:int = _gridHeight;
while (gridHeight >>= 1)
++_heightNeededBits;
// the minimum vector length needed to store each cell with these parameters
var l:int = _gridWidth * (1 << _heightNeededBits);
_itemsByCell = new Vector.<Vector.<DisplayObject>>( l , true );
_itemsByCellInsertIdx = new Vector.<int>( l , true );
// if true, we won't have to take bounds coordonates into account: use quicker methods
if ( bounds.x == 0 && bounds.y == 0 )
{
update = updateZeroBased;
getNeighbors = getNeighborsZeroBased;
}
else
{
_boundsX = bounds.x;
_boundsY = bounds.y;
update = updateNonZeroBased;
getNeighbors = getNeighborsNonZeroBased;
}
}
/** Used to isolate #update speed */
private function getNeighborsEmpty(item:DisplayObject):Vector.<DisplayObject>
{
return new Vector.<DisplayObject>;
}
private function updateZeroBased(items:Vector.<DisplayObject>):void
{
var cellIndex:int, item:DisplayObject;
var itemLength:int = items.length;
// if the items vector length changed since last run, don't try to use the cache from the previous run
if (itemLength != _lastItemsLength)
{
var i:int = _itemsByCell.length;
while (i)
{
_itemsByCell[--i] = new Vector.<DisplayObject>;
_itemsByCellInsertIdx[i] = -1;
}
_lastItemsLength = itemLength;
while (itemLength)
{
item = items[--itemLength];
_lastCellIndexByItem[item] = cellIndex = (int(item.x * _gridSizeRatio) << _heightNeededBits) | int(item.y * _gridSizeRatio);
_itemsByCell[cellIndex][++_itemsByCellInsertIdx[cellIndex]] = item;
}
}
else
{
// this will store invalid cell indexes (cells from whicht least one old item was removed)
// we will remove these items later
// also we store the length of these cell before this run as a commodity
var cellsToCheck:Object = { };
// do a pass on the items to check if any moved
while (itemLength)
{
item = items[--itemLength];
cellIndex = (int(item.x * _gridSizeRatio) << _heightNeededBits) | int(item.y * _gridSizeRatio);
var lastCellIndex:int = _lastCellIndexByItem[item];
// is this item in the same cell as in the previous run?
if ( lastCellIndex != cellIndex )
{
// mark the old cell as invalid
cellsToCheck[lastCellIndex] = _itemsByCellInsertIdx[ cellIndex ];
// push the item in its new cell
_itemsByCell[ cellIndex ][ ++_itemsByCellInsertIdx[ cellIndex ] ] = item;
// store the cellIndex for the next run
_lastCellIndexByItem[item] = cellIndex;
}
}
// do a pass on invalid cells
for (var cellIndexS:String in cellsToCheck)
{
cellIndex = int(cellIndexS);
var cellItems:Vector.<DisplayObject> = _itemsByCell[ cellIndex ];
// remove the old items (we have to check) from the vector and store these
var cellItemsToCheck:Vector.<DisplayObject> = cellItems.splice(0, cellsToCheck[ cellIndex ])
var cellItemsInsertIdx:int = cellItems.length - 1;
i = cellItemsToCheck.length;
// for each old item
while (i)
{
item = cellItemsToCheck[--i];
// the item didn't move
if ( _lastCellIndexByItem[item] == cellIndex )
{
// so we insert him back in the cellItems vector
cellItems[++cellItemsInsertIdx] = item;
}
}
_itemsByCellInsertIdx[ cellIndex ] = cellItemsInsertIdx;
}
}
}
private function getNeighborsZeroBased(item:DisplayObject):Vector.<DisplayObject>
{
var gridX:int = int(item.x * _gridSizeRatio);
var gridY:int = int(item.y * _gridSizeRatio);
var encodedRightCol :int = ( ( gridX ? gridX : _gridWidth ) - 1 ) << _heightNeededBits;
var encodedCenterCol :int = gridX << _heightNeededBits;
var encodedLeftCol :int = (gridX + 1) << _heightNeededBits;
var encodedTopRow :int = ( ( gridY ? gridY : _gridHeight ) - 1 );
var encodedCenterRow :int = gridY;
var encodedBottomRow :int = gridY + 1;
// storing centerCell as a vector speeds speeds up concat
var centerCellVector :Vector.<DisplayObject> = _itemsByCell[ encodedCenterCol | encodedCenterRow ];
return centerCellVector.concat(
_itemsByCell[ encodedLeftCol | encodedTopRow ],
_itemsByCell[ encodedLeftCol | encodedCenterRow ],
_itemsByCell[ encodedLeftCol | encodedBottomRow ],
_itemsByCell[ encodedCenterCol | encodedTopRow ],
_itemsByCell[ encodedCenterCol | encodedBottomRow ],
_itemsByCell[ encodedRightCol | encodedTopRow ],
_itemsByCell[ encodedRightCol | encodedCenterRow ],
_itemsByCell[ encodedRightCol | encodedBottomRow ]
);
}
private function updateNonZeroBased(items:Vector.<DisplayObject>):void
{
var cellIndex:int, item:DisplayObject;
var itemLength:int = items.length;
// if the items vector length changed since last run, don't try to use the cache from the previous run
if (itemLength != _lastItemsLength)
{
var i:int = _itemsByCell.length;
while (i)
{
_itemsByCell[--i] = new Vector.<DisplayObject>;
_itemsByCellInsertIdx[i] = -1;
}
_lastItemsLength = itemLength;
while (itemLength)
{
item = items[--itemLength];
_lastCellIndexByItem[item] = cellIndex = (int( (item.x-_boundsX) * _gridSizeRatio) << _heightNeededBits) | int( (item.y-_boundsY) * _gridSizeRatio);
_itemsByCell[cellIndex][++_itemsByCellInsertIdx[cellIndex]] = item;
}
}
else
{
// this will store invalid cell indexes (cells from whicht least one old item was removed)
// we will remove these items later
// also we store the length of these cell before this run as a commodity
var cellsToCheck:Object = { };
// do a pass on the items to check if any moved
while (itemLength)
{
item = items[--itemLength];
cellIndex = (int( (item.x-_boundsX) * _gridSizeRatio) << _heightNeededBits) | int( (item.y-_boundsY) * _gridSizeRatio);
var lastCellIndex:int = _lastCellIndexByItem[item];
// is this item in the same cell as in the previous run?
if ( lastCellIndex != cellIndex )
{
// mark the old cell as invalid
cellsToCheck[lastCellIndex] = _itemsByCellInsertIdx[ cellIndex ];
// push the item in its new cell
_itemsByCell[ cellIndex ][ ++_itemsByCellInsertIdx[ cellIndex ] ] = item;
// store the cellIndex for the next run
_lastCellIndexByItem[item] = cellIndex;
}
}
// do a pass on invalid cells
for (var cellIndexS:String in cellsToCheck)
{
cellIndex = int(cellIndexS);
var cellItems:Vector.<DisplayObject> = _itemsByCell[ cellIndex ];
// remove the old items (we have to check) from the vector and store these
var cellItemsToCheck:Vector.<DisplayObject> = cellItems.splice(0, cellsToCheck[ cellIndex ])
var cellItemsInsertIdx:int = cellItems.length - 1;
i = cellItemsToCheck.length;
// for each old item
while (i)
{
item = cellItemsToCheck[--i];
// the item didn't move
if ( _lastCellIndexByItem[item] == cellIndex )
{
// so we insert him back in the cellItems vector
cellItems[++cellItemsInsertIdx] = item;
}
}
_itemsByCellInsertIdx[ cellIndex ] = cellItemsInsertIdx;
}
}
}
private function getNeighborsNonZeroBased(item:DisplayObject):Vector.<DisplayObject>
{
var gridX:int = int( (item.x-_boundsX) * _gridSizeRatio);
var gridY:int = int( (item.y-_boundsY) * _gridSizeRatio);
var encodedRightCol :int = ( ( gridX ? gridX : _gridWidth ) - 1 ) << _heightNeededBits;
var encodedCenterCol :int = gridX << _heightNeededBits;
var encodedLeftCol :int = (gridX + 1) << _heightNeededBits;
var encodedTopRow :int = ( ( gridY ? gridY : _gridHeight ) - 1 );
var encodedCenterRow :int = gridY;
var encodedBottomRow :int = gridY + 1;
// storing centerCell as a vector speeds speeds up concat
var centerCellVector :Vector.<DisplayObject> = _itemsByCell[ encodedCenterCol | encodedCenterRow ];
return centerCellVector.concat(
_itemsByCell[ encodedLeftCol | encodedTopRow ],
_itemsByCell[ encodedLeftCol | encodedCenterRow ],
_itemsByCell[ encodedLeftCol | encodedBottomRow ],
_itemsByCell[ encodedCenterCol | encodedTopRow ],
_itemsByCell[ encodedCenterCol | encodedBottomRow ],
_itemsByCell[ encodedRightCol | encodedTopRow ],
_itemsByCell[ encodedRightCol | encodedCenterRow ],
_itemsByCell[ encodedRightCol | encodedBottomRow ]
);
}
}
}