Utility class for creating entity pool


(Thawfeek_Yahya) #1

Hi Everyone,

Recently i was working in a game where i have to create 100 of Entities at runtime , so thought of creating an entity pool which would create the entities before game starts and uses those entites object again if needed.

Here’s the class

   package utils {
import net.flashpunk.Entity;

public class EntityPool {

    private var pool:Array;
    private var counter:int;
    private var totalCount:int;
    private var watchList:Vector.<int>;

    public function EntityPool(numCount:int,classType:Array) {
        pool = new Array();
         watchList = new Vector.<int>();
         counter = numCount *classType.length
         totalCount = counter;


         if(classType.length == 0) throw new Error("Pass At least 1 class type");

         var i:int = counter;
         for (var classCount:int=classType.length-1;classCount >= 0; classCount--) {
            while (--i >= numCount*classCount) {
                pool[i] = new classType[classCount];
            }
            i++ 
         }
    }

    public function getEntity(index:int):Entity {
        if(counter > 0 && index > -1 && index < pool.length){
            if(watchList.indexOf(index) == -1){
                watchList.push(index);
                counter--;
                return pool[index];
            } else {
                var targetIndex:int = index;
                while(watchList.indexOf(targetIndex) != -1){
                    (targetIndex <= 0) ? targetIndex = totalCount-1 :targetIndex--;
                }
                watchList.push(targetIndex);
                counter--;
                return pool[targetIndex];
            }
        } else {
            throw new Error("Pool Exhausted or Invalid Index");
        }
    }

    public function putEntity(entity:Entity):void {
        counter++;
        if(watchList.length > 0){
            var targetIndex:int = pool.indexOf(entity);
            watchList.splice(watchList.indexOf(targetIndex),1);
            pool[targetIndex] = entity;
        } else {
            pool[counter] = entity;
        }
        if(counter > totalCount){
            throw new Error("Counter value cannot go beyond total count");
        }
    }

    public function destroy():void {
        for (var i:int = pool.length - 1; i >= 0; i--) {
            pool.splice(i,1);
        }
        pool = null;
        for (i = watchList.length - 1; i >= 0; i--) {
            watchList.splice(i,1);
        }
        watchList = null;
        counter = 0;
        totalCount = 0;
    }
}
}

Thanks, Thawfeek.


(Willard Torres) #2

Doesn’t FlashPunk have it’s own pooling system with World.create() and World.recycle()?


(Ultima2876) #3

Yup. I did the same thing when I first started using it - for some reason it’s not very well known or used nearly as often as it should be!


(TaylorAnderson) #4

I feel like thats true of a lot of Flashpunk stuff :confused: Especially in the FP class.


(Thawfeek_Yahya) #5

@Anheurystics,

Not even aware of that the FP has its own pooling method. Wasted some time writing this class LOL.