In this chapter we are going to create the PaddleController. The PaddleController
will handle getting player input, and positioning and rendering the paddle.
Topics Covered in this Chapter:
Creating The Paddle
Adding the Paddle to Breakout
The PaddleController:
The PaddleController class does exactly what you would expect it to do, it
controls the paddle. Take a look at the PaddleController from the part 2, chapter
5 code. Like the Level class the PaddleController also has a number of defines
at the top. PADDLEY is the y position of the paddle which stays constant, the
paddle is only allowed to move left and right. The PADDLE_MIN_X, and PADDLE_MAX_X
control how far left and how far right the paddle can move.
After the top, the first function of interest is init.
Init loads the paddle model, and keeps a local copy of the InputManager.
bool PaddleController::init(InputManager* input,
LPDIRECT3DDEVICE9 device){
//Local pointer to the input manager
myInput = input;
//set the paddle to start at 0
paddleX = 0;
paddle = new Model();
//load the paddle
paddle->loadModel(device, "paddle.x");
//set the scale of the model
paddle->setScale(D3DXVECTOR3(0.25f,0.25f,0.25f));
return true;
}
The render function sets the position of the paddle
model and renders it.
Update gets the user input, and moves the paddle
based on the X velosity of the mouse. The paddle is stopped from going past
the PADDLE_MAX_X, and PADDLE_MIN_X.
svoid PaddleController::update(){
myInput->getInput();
//updates the paddleX
paddleX+= myInput->getMouseMovingX()/10;
//stop the paddle from going past the Max and Min paddle positions
if (paddleX > PADDLE_MAX_X){
paddleX = PADDLE_MAX_X;
} else if (paddleX < PADDLE_MIN_X){
paddleX = PADDLE_MIN_X;
}
}
The last 2 functions are getPaddleXY, and getWidth.
getPaddleXY fills the passed pointers with the
current paddle x and y positions. getWidth returns
the width of the paddle. Both of these functions are used for collision deteciton
purposes.
That's all there is to the PaddleController class, adding it to the Breakout
class requires adding commands to init, update,
and render.
Changes made to Breakout init:
bool Breakout::init(InputManager* input, SoundManager*
sounds, LPDIRECT3DDEVICE9 device){
.....
//create the paddle controller
paddle = new PaddleController();
paddle->init(input, device);
//create the ball controller
....
return true;
}
Changes made to Breakout update:
void Breakout::updateFrames(int numberOfFrames){ //update the paddle
paddle->update();
//update the ball controller
}
Changes made to Breakout render:
void Breakout::render(LPDIRECT3DDEVICE9 device){
//render the background
background->render(device);
//render the level
level->render(device);
//render the ball
//render the paddle
paddle->render(device);
}
Now that the PaddleController class has been added to the Breakout class, we
can compile and run the project. Starting a new game, you should now have a
paddle controlled by the mouse at the bottom of the level.
.
Summing Up - Chapter 5
The PaddleController is a fairly simple class, the paddle size is controlled
by the constants and the scale. Changing these would allow you to scale the
paddle. Often breakout games allow powerups to adjust the size of the paddle,
with a few changes our paddle could be made to have a dynamic size as well.