Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Ball/Ball.cpp

Committer:
kocemax
Date:
2019-03-23
Revision:
3:fe856d0890ee
Parent:
2:006a2ddfabb6
Child:
4:0e01cbb95434

File content as of revision 3:fe856d0890ee:

#include "Ball.h"
#include "PlayerControl.h"

int g_xBall = WIDTH/2; // draw ball in the middle of the columns initially
int g_yBall = HEIGHT - GAP - 2; // draw ball close to the bottom of the LCD
int g_counterx = 1;
int g_countery = 1;

// Constructor
Ball::Ball()
{

}

// Destructor
Ball::~Ball()
{

}

void Ball::drawBall(N5110 &lcd) 
{
    lcd.drawRect(g_xBall,g_yBall,2,2,FILL_BLACK);
}

void Ball::moveBall() 
{   
    
    g_xBall += g_counterx;
    g_yBall -= g_countery;
    
    if (g_xBall > 82) 
    {   
        g_counterx = -1;
    }
    else if(g_xBall < 1)
    {
        g_counterx = 1;
    }
    if (g_yBall < 1)
    {   
        g_countery = -1;
    }
    else if (g_yBall > 46) 
    {
        g_countery = 1;
    }
}

void Ball::hitPad(Gamepad &pad) 
{
    PlayerControl pl;
    Vector2D posBall = get_ballPos(pad);
    Vector2D posPad = pl.get_padPos(pad);
    if (posBall.y == posPad.y - 2 && (posBall.x >= posPad.x && posBall.x <= posPad.x + 12))
    {
        g_countery = !g_countery;
        printf("\nball x = %f, ball y = %f \n pad x = %f, pad y = %f ",posBall.x, posBall.y, posPad.x, posPad.y);
    }
    
    
}

Vector2D Ball::get_ballPos(Gamepad &pad)
{
    Vector2D posBall = {g_xBall,g_yBall};
    return posBall;
}

bool Ball::endCondition(Gamepad &pad)
{
    PlayerControl pl;
    Vector2D posBall = get_ballPos(pad);
    Vector2D posPad = pl.get_padPos(pad);
    if (posBall.y > posPad.y) 
    {
        return true;
    }
    else return false;
}