1

I am developing a game in AndEngine for Android. In my game, I have to create different types of Tiles (AnimatedSprite) after every second. I have done that. But I am feeling jerks and lags in my game. I think it is due to the allocation and de-allocation of objects frequently. So I want to implement Object Pool pattern in my game. My current code for creating Tile:

public class TileFactory implements EntityFactory {

private static TileFactory tileFactory;

@Override
public AnimatedEntity createEntity(PlayLevelActivity mGameWorld, ResourceType type) {
    // TODO Auto-generated method stub
    ITiledTextureRegion textureRegion = ResourceManager.getTextureRegion(mGameWorld, type);;
    switch (type) {
        case STATIC_TILE:
            return new StaticTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(), 
                    BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
        case DYNAMIC_TILE :
            return new DynamicTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(), 
                    BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
        case DANGER_TILE:
            return new DangerTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(), 
                    BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
        case FIRE_TILE:
            return new FireTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(), 
                    BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
        case HIGH_JUMP_TILE:
            return new HighJumpTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(), 
                    BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
        case RISK_TILE:
            return new RiskyTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(), 
                    BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
        default :
            return null;
    }
}


    public static TileFactory getIntance() {
        if(tileFactory == null) {
            tileFactory = new TileFactory();
        }
        return tileFactory;
    }
}

I have seen some examples of using ObjectPool in AndEngine but they work for only a single type of entity. I have several types of Entities to create. Any guideline how to convert my current scenario to ObjecPool one?

StaticTilePool class:

public class StaticTilePool extends GenericPool<StaticTile> {

PlayLevelActivity mGameWorld;
ITiledTextureRegion textureRegion;

public StaticTilePool(PlayLevelActivity mGameWorld) {
    this.mGameWorld = mGameWorld;
    textureRegion = ResourceManager.getTextureRegion(mGameWorld, ResourceType.STATIC_TILE);
}

/**
 * Called when a Tile is required but there isn't one in the pool
 */
@Override
protected StaticTile onAllocatePoolItem() {
    Log.d("count:", "count: " + getAvailableItemCount() + " ,    maximum count: " + getAvailableItemCountMaximum() + " ,    unrecycled count: " + getUnrecycledItemCount());

    return new StaticTile(mGameWorld, -100, -100, textureRegion.getWidth(), textureRegion.getHeight(), 
            BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
}

/**
 * Called just before a Tile is returned to the caller, this is where you write your initialize code
 * i.e. set location, rotation, etc.
*/
@Override
protected void onHandleObtainItem(final StaticTile pItem) {
    pItem.reset();
    pItem.mBody.setAwake(false);
}


/**
 * Called when a Tile is sent to the pool
*/
@Override
protected void onHandleRecycleItem(final StaticTile pItem) {
    Log.d("onHandle recycle", "onhandle recycle oitem");
    pItem.mBody.setAwake(true);
    pItem.setVisible(false);
    pItem.setIgnoreUpdate(true);
}

}

Khawar Raza
  • 15,870
  • 24
  • 70
  • 127

1 Answers1

2

Check out....

org.andengine.util.adt.pool.MultiPool

It's exactly what you want. A pool of pools. You'll want to extend MultiPool with your own custom implementation. Here's a sample one I use in my project. (I've cut down on the number of pools in the multipool but left enough in so you get an idea.)

public class CritterPool extends MultiPool {
    private WorldActivity   mContext;

    public static enum TYPE {
        PROTOGUY, COW, MALE, SKELETON
    }

    public CritterPool(WorldActivity mContext) {
        super();
        this.mContext = mContext;
        this.registerPool(TYPE.PROTOGUY.ordinal(), new ProtoGuyPool(mContext));
        this.registerPool(TYPE.COW.ordinal(), new CowPool(mContext));
        this.registerPool(TYPE.MALE.ordinal(), new MalePool(mContext));
        this.registerPool(TYPE.SKELETON.ordinal(), new SkeletonPool(mContext));
    }

    public BaseCritter get(TYPE type) {
        return (BaseCritter) this.obtainPoolItem(type.ordinal());
    }

    public void recycle(BaseCritter critter) {
        this.recyclePoolItem(critter.getType().ordinal(), critter);
    }

}

So now, from the outside, I create the multipool, and then I can call get() with whatever type of (in this case "critters") I want from the pool, be it a cow, a skeleton, etc.

Cameron Fredman
  • 1,259
  • 11
  • 24
  • @KhawarRaza For whatever reason, I find it incredibly satisfying to make a multipool with everything in your game in it. "Multipool, give me a, umm, dinosaur." – Cameron Fredman Feb 22 '13 at 08:07
  • I am facing a problem that I am getting this message "com.android.mygame.StaticTilePool was exhausted, with 65 item not yet recycled. Allocated 1 more." Even though I am recycling items periodically and it should not exceed to more than 40 items as per scenario. I have updated my question. Kindly see the StaticTilePool class. Can you see what's going wrong? – Khawar Raza Feb 22 '13 at 10:12
  • @KhawarRaza Just means you're not recycling them as often as you think. Problem isn't in the StaticTilePool class. Where are you calling recycle()? – Cameron Fredman Feb 22 '13 at 14:30
  • Yes. Actually I was missing a place where I would had to recycle items. Now I have found that place and working fine. Thanks... – Khawar Raza Feb 25 '13 at 07:19
  • @CameronFredman: thanks for share.can explain more. My problem is here: http://stackoverflow.com/questions/21090279/use-different-sprites-texture-in-one-generic-pool-andengine – Shihab Uddin Jan 22 '14 at 09:23