Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Paddle/Paddle.cpp

Committer:
kocemax
Date:
2019-05-08
Revision:
8:9b77eea95088
Child:
12:b3ec47d606a5

File content as of revision 8:9b77eea95088:

#include "Paddle.h"

/** Constructor */
Paddle::Paddle()
{
    reset(); /** initial parameters of paddle */
}

/** Destructor - nothing happening here */
Paddle::~Paddle()
{
}

/** Adds the joystick control for the paddle */
void Paddle::controlPaddle(Gamepad &pad)
{
    /** Sets the joystick as the paddle controller */
    _d = pad.get_direction();

}

/** Determines the movement physics of the paddle */
void Paddle::move()
{
    /** Moving right */
    if (_d == E) {
        pos.x = pos.x + _speed; /** Adds speed to current position on x-axis */
        
        if (pos.x >= WIDTH - w) {
            pos.x = WIDTH - w; /** Sets the right-most limit to paddle*/
        }
    }
    /** Moving left */
    if (_d == W) {
        pos.x = pos.x - _speed; /** Subtracts speed from current position on x-axis */
        
        if (pos.x <= 0) {
            pos.x = 0; /** Sets the left-most limit to paddle*/
        }
    }
}

/** Resets paddle's initial parameters when game is over / lost */
void Paddle::reset()
{
    pos.x = WIDTH/2;        /** position of paddle on x-axis */
    pos.y = HEIGHT - GAP;   /** position of paddle on y-axis */
    velocity.x = 0;         /** x-velocity of paddle is zero as it does not move by itself */
    velocity.y = 0;         /** y-velocity of paddle is zero as it does not move by itself */
    w = 12;                 /** width of the paddle */
    h = 2;                  /** height of the paddle */
    _speed = 2;             /** speed of movement of paddle */
}