Example #1
0
    // Returns the specified object from the pool
    public BasicObject GetObjectFromPool(int localIndex, ObjectType objectType)
    {
        BasicObject        obj         = null;
        int                objectIndex = GetObjectIndexFromLocalIndex(localIndex, objectType);
        List <BasicObject> objectPool  = objectsPool[objectIndex];
        int                poolIndex   = objectPoolIndex[objectIndex];

        // keep a start index to prevent the constant pushing and popping from the list
        if (objectPool.Count > 0 && objectPool[poolIndex].IsActive() == false)
        {
            obj = objectPool[poolIndex];
            objectPoolIndex[objectIndex] = (poolIndex + 1) % objectPool.Count;
            return(obj);
        }

        // No inactive objects, need to instantiate a new one
        BasicObject[] objects = null;
        switch (objectType)
        {
        case ObjectType.Platform:
            objects = platforms;
            break;

        case ObjectType.Scene:
            objects = scenes;
            break;

        case ObjectType.Obstacle:
            objects = obstacles;
            break;

        case ObjectType.Donut:
            objects = donuts;
            break;

        case ObjectType.PowerUp:
            objects = powerUps;
            break;

        case ObjectType.Fuel:
            objects = fuels;
            break;
        }

        obj = (GameObject.Instantiate(objects[localIndex].gameObject) as GameObject).GetComponent <BasicObject>();

        AssignParent(obj, objectType);
        obj.SetLocalIndex(localIndex);

        objectPool.Insert(poolIndex, obj);
        objectPoolIndex[objectIndex] = (poolIndex + 1) % objectPool.Count;
        return(obj);
    }