HTML and CSS Reference
In-Depth Information
// get and remove head from freeElements linked-list
var index = this.freeElements.remove(0);
// retrieve the particle from the elements array
var particle = this.elements[index];
// mark the particle allocated so we use it when iterating on the pool
particle.allocated = true;
// reset particle position, velocity etc.
// done
return particle;
Returning Objects to the Pool
When the game doesn't need an object anymore (see the section “Updating Only What's Important”), it can return the
object to the pool (see Listing 3-4). To do this, push the object index to the free-indexes array, and mark the object as
unallocated so that your loop will skip it.
Listing 3-4. Object 8 Has Been Returned to the Pool and Marked as Free, and Its Index, Added to the
Free-Indexes Array = function( particle ){
if( particle.allocated === true ){
// mark the particle as not allocated
particle.allocated = false
// add the particle's index to the free elements array
this.freeElements.push( particle._poolindex )
Iterating on a Pool
After a while, your pool will look like the one shown in Listing 3-5. You can see here the benefit of both having an array
of free indexes and embedding in each object its own index; otherwise, you would have to iterate over the objects
array to get objects from the pool as well as to free them.
Listing 3-5. Your Pool after Many Requests and Returns
Search WWH ::

Custom Search