Exemple #1
0
void updateBulletPool(BulletPool *bp){
	int i;
	for (i = 0; i < 5; i++) {
		if (bp->particles_[i]->inUse == 1 && bp->particles_[i]->explosion_counter == 0){
			moveBullet(bp->particles_[i]);
			if(checkScreenCollision(bp->particles_[i])){
				bp->particles_[i]->inUse = 0;
				bp->particles_[i]->explosion_counter = 1;
				bp->particles_[i]->bullet_exp = loadBitmap("/home/lcom/Tanks/res/Spark1.bmp");
			}
		}
		else if(bp->particles_[i]->inUse == 0 && bp->particles_[i]->explosion_counter == 5)
			bp->particles_[i] = CreateBullet(0);
	}
}
void Paddle::checkAI(int yres, Ball &ball)
{
	//If paddle is CPU
	int center = yPos + (height / 2);
	float absoluteBallYVel = abs(ball.getYVel());
	if(isCpu){
		if(ball.getYPos() == center){
			this->setYVel(0.0f);
		}
		else{
			if(ball.getYPos() > center){
				this->setYVel(absoluteBallYVel - 0.5f);
			}
			else if(ball.getYPos() < center){
				this->setYVel(-absoluteBallYVel + 0.5f);
			}

		}
		checkScreenCollision(yres);
		
	}


}
bool Paddle::checkCollision(int yres, Ball &ball)
{
	float ballspeed = 15.0f;
	float ballXVel = ballspeed * cos(0)+10;
	float ballYVel = ballspeed * -sin(35);
    
	//ball collision with paddles
	bool onLeftSide = ball.getXPos() < 150 && xPos < 150;
	bool onRightSide = ball.getXPos() > 150 && xPos > 150;
	bool hitLeftPaddle = (ball.getXPos()-ball.getRadius() <= xPos) &&
		ball.getYPos() >= yPos && ball.getYPos() <= yPos + height;
	bool hitRightPaddle  = (ball.getXPos() >= xPos) &&
		ball.getYPos() >= yPos && ball.getYPos() <= yPos + height;


	//check if paddle is moving up or down
	bool paddleMovingUp = yVel > 0;
	bool paddleMovingDown = yVel < 0;


	checkAI(yres, ball);
	checkScreenCollision(yres);
	float angle = PI/2;


	//collision with ball
	//left paddle
	if(onLeftSide && hitLeftPaddle){
		ball.setXVel(ballXVel);
		createSound(1);
        
		if(paddleMovingUp){
			ballYVel = ballspeed * -sin(-angle);
			ball.setYVel(ballYVel);
			ball.setXVel(ballXVel);
		}
		else if(paddleMovingDown){
			ballYVel = ballspeed * -sin(-angle);
			ball.setYVel(-ballYVel);
			ball.setXVel(ballXVel);
		}
		return true;
	}
	//right paddle
	else if(onRightSide && hitRightPaddle){
		ball.setXVel(-ballXVel);
		createSound(1);
		if(paddleMovingUp){
			ballYVel = ballspeed * -sin(angle);
			ball.setYVel(ballYVel);
			ball.setXVel(-ballXVel);
		}
		else if(paddleMovingDown){
			ballYVel = ballspeed * -sin(-angle);
			ball.setYVel(-ballYVel);
			ball.setXVel(-ballXVel);
		}
		return true;
	}
	

	return false;
}