Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Ball/Ball.cpp

Committer:
kocemax
Date:
2019-04-08
Revision:
6:39bda45efeed
Parent:
5:12c179da4788
Child:
7:cd3cafda3dd4

File content as of revision 6:39bda45efeed:

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

// Constructor
Ball::Ball()
{
    ballpos.x = WIDTH/2;
    ballpos.y = HEIGHT - GAP - 2;
    velocity.x = randomize();
    velocity.y = -1;
    w = 1;
    h = 1;
}

// Destructor
Ball::~Ball()
{

}

//Ball screen edge detection
void Ball::move() 
{   
    GameObject::move();
    
    if (ballpos.x > WIDTH-1) 
    {   
        velocity.x = -1;
    }
    else if(ballpos.x < 1)
    {
        velocity.x = 1;
    }
    if (ballpos.y < 1)
    {   
        velocity.y = 1;
    }
    else if (ballpos.y > HEIGHT-1) 
    {
        velocity.y = -1;
    }
}

//Pad and Ball collision detection
void Ball::hitPad(Gamepad &pad, PlayerControl &pl) 
{
    Vector2D posPad = pl.get_padPos(pad);
    if (ballpos.y == posPad.y - 1 && (ballpos.x >= posPad.x && ballpos.x <= posPad.x + 12))
    {
        velocity.y = -1;
    }
    
    
}

void Ball::endCondition(Gamepad &pad, N5110 &lcd, PlayerControl &pl)
{
    Vector2D posPad = pl.get_padPos(pad);
    if (ballpos.y > posPad.y) 
    {
        while (1) 
        {
            lcd.clear();
            lcd.printString("You Lost",17,2);
            lcd.printString("Press A",20,3);
            lcd.printString("to restart",12,4);
            wait(0.1);
            lcd.refresh();
            if (pad.check_event(Gamepad::A_PRESSED) == true)
            {
                resetGame(pl);
                break;           
            }
        }
    }
}

void Ball::resetGame(PlayerControl &pl)
{
    ballpos.x = WIDTH/2;
    ballpos.y = HEIGHT - GAP - 2;
    velocity.x = randomize();
    velocity.y = -1; 
    pl.padReset();
}

int Ball::randomize()
{
    AnalogIn noisy(PTB0);
    int movement;
    srand(1000000*noisy.read());
    int direction = rand() % 2; // randomise initial direction. 
    if (direction == 0) 
    {
        movement = -1;
    } 
    else if (direction == 1) 
    {
        movement = 1;
    }
    return movement; 
}

void Ball::hitBrick(Gamepad &pad, N5110 &lcd)
{
     /*
     Vector2D posBall = get_ballPos(pad);
     int pixel_to_test1 = lcd.getPixel(posBall.x,posBall.y-2);
     int pixel_to_test2 = lcd.getPixel(posBall.x,posBall.y+2);
     int pixel_to_test3 = lcd.getPixel(posBall.x+2,posBall.y);
     int pixel_to_test4 = lcd.getPixel(posBall.x-2,posBall.y);
     
        if ( pixel_to_test1 ) 
        {
            velocity.y = -1;
        }
        if ( pixel_to_test2 ) 
        {
            velocity.y = 1;
        }
        if ( pixel_to_test3 ) 
        {
            velocity.x = -1;
        }
        if ( pixel_to_test4 ) 
        {
            velocity.x = 1;
        }
        */
}