How to optimise Andengine's PathModifer (with singleton or pool)?

Posted by Casla on Game Development See other posts from Game Development or by Casla
Published on 2012-06-04T23:30:40Z Indexed on 2012/06/05 4:49 UTC
Read the original article Hit count: 402

Filed under:
|

I am trying to build a game where the character find and follows a new path when a new destination is issued by the player, kinda like how units in RTS games work.

This is done on a TMX map and I am using the A Star path finder utilities in Andengine to do this.David helped me on that: How can I change the path a sprite is following in real time?

At the moment, every-time a new path is issued, I have to abandon the existing PathModifer and Path instances, and create new ones, and from what I read so far, creating new objects when you could re-use existing ones are a big waste for mobile applications.

This is how I coded it at the moment:

private void loadPathFound() {

    if (mAStarPath != null) {

        modifierPath = new org.andengine.entity.modifier.PathModifier.Path(mAStarPath.getLength());

        /* replace the first node in the path as the player's current position */
        modifierPath.to(player.convertLocalToSceneCoordinates(12, 31)[Constants.VERTEX_INDEX_X]-12, player.convertLocalToSceneCoordinates(12, 31)[Constants.VERTEX_INDEX_Y]-31);

        for (int i=1; i<mAStarPath.getLength(); i++) {

            modifierPath.to(mAStarPath.getX(i)*TILE_WIDTH, mAStarPath.getY(i)*TILE_HEIGHT);


        /* passing in the duration depended on the length of the path, so that the animation has a constant duration for every step */
        player.registerEntityModifier(new PathModifier(modifierPath.getLength()/100, modifierPath, null, mIPathModifierListener));
    }
}

The ideal implementation will be to always have just one object of PathModifer and just reset the destination of the path.

But I don't know how you can apply the singleton patther on Andengine's PathModifer, there is no method to reset attribute of the path nor the pathModifer.

So without re-write the PathModifer and the Path class, or use reflection, is there any other way to implement singleton PathModifer?

Thanks for your help.

© Game Development or respective owner

Related posts about android

Related posts about andengine