Is my way of doing this wrong (deleting the rectangles at the end)? I was thinking about writing some methods that are used to set the different corners instead of calling the constructor over and over. Any idea?
Code: Select all
bool collisionWithPaddleArray(GameObject * object, GameObject * ball)
{
Rectangle * paddleRect = NULL;
Rectangle * ballRect = NULL;
for(int i = 0; i < NUM_PADDLES; i++)
{
paddleRect = new Rectangle(paddleArray[i]->getX(), paddleArray[i]->getY(),
(paddleArray[i]->getX() + paddleArray[i]->getWidth()), (paddleArray[i]->getY() + paddleArray[i]->getHeight()));
ballRect = new Rectangle(ball->getX(), ball->getY(), ( ball->getX() + ball->getWidth() ), ( ball->getY() + ball->getHeight() ) );
if(doRectanglesCollide(paddleRect, ballRect))
{
BALL_SPEED_X *= -1;
BALL_SPEED_Y *= -1;
bounceBall(ball);
if(ball->get_X_Direction() == RIGHT)
{
while(doRectanglesCollide(paddleRect, ballRect) == true)
{
paddleRect = new Rectangle(paddleArray[i]->getX(), paddleArray[i]->getY(),
(paddleArray[i]->getX() + paddleArray[i]->getWidth()), (paddleArray[i]->getY() + paddleArray[i]->getHeight()));
ballRect = new Rectangle(ball->getX(), ball->getY(), ( ball->getX() + ball->getWidth() ), ( ball->getY() + ball->getHeight() ) );
moveGameObjectOnePixel(ball,MOVE_LEFT, DO_NOT_MOVE);
}
}
else if(ball->get_X_Direction() == LEFT)
{
while(doRectanglesCollide(paddleRect, ballRect) == true)
{
paddleRect = new Rectangle(paddleArray[i]->getX(), paddleArray[i]->getY(),
(paddleArray[i]->getX() + paddleArray[i]->getWidth()), (paddleArray[i]->getY() + paddleArray[i]->getHeight()));
ballRect = new Rectangle(ball->getX(), ball->getY(), ( ball->getX() + ball->getWidth() ), ( ball->getY() + ball->getHeight() ) );
moveGameObjectOnePixel(ball, MOVE_RIGHT, DO_NOT_MOVE);
}
}
else
{
myLog->writeToFile("ball->get_X_Direction() is not equal to east or west. \n");
}
if(ball->get_Y_Direction() == UP)
{
while(doRectanglesCollide(paddleRect, ballRect) == true)
{
paddleRect = new Rectangle(paddleArray[i]->getX(), paddleArray[i]->getY(),
(paddleArray[i]->getX() + paddleArray[i]->getWidth()), (paddleArray[i]->getY() + paddleArray[i]->getHeight()));
ballRect = new Rectangle(ball->getX(), ball->getY(), ( ball->getX() + ball->getWidth() ), ( ball->getY() + ball->getHeight() ) );
moveGameObjectOnePixel(ball, DO_NOT_MOVE, MOVE_DOWN);
}
}
else if(ball->get_Y_Direction() == DOWN)
{
while(doRectanglesCollide(paddleRect, ballRect) == true)
{
paddleRect = new Rectangle(paddleArray[i]->getX(), paddleArray[i]->getY(),
(paddleArray[i]->getX() + paddleArray[i]->getWidth()), (paddleArray[i]->getY() + paddleArray[i]->getHeight()));
ballRect = new Rectangle(ball->getX(), ball->getY(), ( ball->getX() + ball->getWidth() ), ( ball->getY() + ball->getHeight() ) );
moveGameObjectOnePixel(ball, DO_NOT_MOVE, MOVE_UP);
}
}
else
{
myLog->writeToFile("ball->get_Y_Direction() is not equal to north or south. \n");
}
delete(paddleRect);
delete(ballRect); // cleanup
return true;
}
}
delete(paddleRect);
delete(ballRect); // cleanup
return false;
}