lundi 30 mars 2015

Efficiency in C++ Function Logic

I have some code that implements the illusion of a side-scroller. I'm trying to make it more efficient in that I'm repeating a few lines over and over again and I know when I do that there is always a better way. Ideas?


The code is as follows:



void sideScroll(MegaMan* mega, Block** blockArray, int numBlocks)
{


//Compare Megaman's position to screen width
if(mega->getPosX() >= ((SCREEN_WIDTH/2)-MM_SPRITE_WIDTH)&& mega->getState()==RUN_RIGHT)
{
for(int i=0; i<numBlocks; i++)
{
//if Greater, move blocks by Megamans movement speed
blockArray[i]->setPosX(blockArray[i]->getPosX()-MM_RUN_SPEED);
}
}

//Uncomment for further functionality

if(mega->getPosX() <= 0 && mega->getState()==RUN_LEFT)
{
for(int i=0; i<numBlocks; i++)
{
//move blocks by Megamans movement speed
blockArray[i]->setPosX(blockArray[i]->getPosX()+MM_RUN_SPEED);
}
}
if (mega->getPosY() >= SCREEN_HEIGHT-MM_SPRITE_HEIGHT && mega->getState()==CLIMB)
{
for(int i=0; i<numBlocks; i++)
{
//move blocks by Megamans climb speed
blockArray[i]->setPosY(blockArray[i]->getPosY()-MM_CLIMB_SPEED);
}
}
if (mega->getPosY() <= 0 && mega->getState()==CLIMB)
{
for(int i=0; i<numBlocks; i++)
{
//move blocks by Megamans climb speed
blockArray[i]->setPosY(blockArray[i]->getPosY()+MM_CLIMB_SPEED);
}
}

}

Aucun commentaire:

Enregistrer un commentaire