ELEC2645 (2018/19) / Mbed 2 deprecated el17cr

Dependencies:   mbed

Falldown/Falldown.cpp

Committer:
el17cr
Date:
2019-04-16
Revision:
3:5edefa83f8f0
Parent:
2:7f91a86b4dc0
Child:
4:03d13a53308c

File content as of revision 3:5edefa83f8f0:

#include "Falldown.h"

Falldown::Falldown()
{

}

Falldown::~Falldown()
{

}

void Falldown::init(int ground_height,int ball_size)
{
    // initialise the game parameters
    _ground_width1 = 50;
    _ground_height = ground_height;
    _ground_width2 = 70;
    //_ground_height2 = 2;
    _ball_size = ball_size;
    
    
    
    

    // x position on screen - WIDTH is defined in N5110.h
    //_p1x = GAP;
    //_p2x = WIDTH - GAP - _Ground_width;
    _bally = GAP;
    _g1x = 1;
    _g2x = 1;
    _g1y = 10;
    _g2y = 20;
    

    // puts Grounds and ball in middle
    _ball.init(_bally,_ball_size);
    
    _ground1.init(_g1x,_g1y,_ground_height,_ground_width1);
    _ground2.init(_g2x,_g2y,_ground_height,_ground_width2);
}

void Falldown::read_input(Gamepad &pad)
{
    _d = pad.get_direction();
    _mag = pad.get_mag();
}

void Falldown::draw(N5110 &lcd)
{
    // draw the elements in the LCD buffer
    // pitch
    //lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
    //lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2);
    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
   
    // Grounds
    _ground1.draw(lcd);
    _ground2.draw(lcd);
    _ball.draw(lcd);
}

void Falldown::update(Gamepad &pad)
{
    //check_goal(pad);
    // important to update Grounds and ball before checking collisions so can
    // correct for it before updating the display
    _ball.updateX(_d,_mag);
    _ball.update();
    
    //_ground.update();

    //check_wall_collision(pad);
    check_Ground_collisions(pad);
}
/*
void Falldown::check_wall_collision(Gamepad &pad)
{
    // read current ball attributes
    //Vector2D ball_pos = _ball.get_pos();
    //Vector2D ball_velocity = _ball.get_velocity();

    // check if hits bottom wall
    if (ball_pos.x >= 48) {  //  1 due to 1 pixel boundary
        ball_pos.y = 1;  // bounce off ceiling without going off screen
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(750.0,0.1);
    }
    // check if hit bottom wall
    else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47
        // hit bottom
        ball_pos.y = (HEIGHT-1) - _ball_size;  // stops ball going off screen
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(750.0,0.1);
    }

    // update ball parameters
    _ball.set_velocity(ball_velocity);
    //_ball.set_pos(ball_pos);
}*/

void Falldown::check_Ground_collisions(Gamepad &pad)
{
    // read current ball attributes
    Vector2D ball_pos = _ball.get_pos();
    Vector2D ball_velocity = _ball.get_velocity();

    // check p1 first
    //Vector2D ground_pos = _ground.get_pos();

    // see if ball has hit the Ground by checking for overlaps
    if (
        (ball_pos.y >= 7) && //top
        (ball_pos.y <= 10) //bottom
        //(ball_pos.x >= _p1x) && //left
        //(ball_pos.x <= _p1x + _Ground_width)  //right
    ) {
        // if it has, fix position and reflect x velocity
        ball_pos.y = 7;
        ball_velocity.y = 0;
        //ball_velocity.x = -ball_velocity.x;
        // audio feedback
        //pad.tone(1000.0,0.1);
    }
     
     _ball.set_velocity(ball_velocity);
     _ball.set_pos(ball_pos);
}
/*
    // check p2 next
    Vector2D p2_pos = _p2.get_pos();

    // see if ball has hit the Ground by checking for overlaps
    if (
        (ball_pos.y >= p2_pos.y) && //top
        (ball_pos.y <= p2_pos.y + _Ground_height) && //bottom
        (ball_pos.x + _ball_size >= _p2x) && //left
        (ball_pos.x + _ball_size <= _p2x + _Ground_width)  //right
    ) {
        // if it has, fix position and reflect x velocity
        ball_pos.x = _p2x - _ball_size;
        ball_velocity.x = -ball_velocity.x;
        // audio feedback
        pad.tone(1000.0,0.1);
    }

    // write new attributes
    _ball.set_velocity(ball_velocity);
    _ball.set_pos(ball_pos);
}

void Falldown::check_goal(Gamepad &pad)
{
    Vector2D ball_pos = _ball.get_pos();
    // P2 has scored
    if (ball_pos.x + _ball_size < 0) {
        _p2.add_score();
        _ball.init(_ball_size,_speed);
        pad.tone(1500.0,0.5);
        pad.leds_on();
        wait(0.5);
        pad.leds_off();
    }

    // P1 has scored
    if (ball_pos.x > WIDTH) {
        _p1.add_score();
        _ball.init(_ball_size,_speed);
        pad.tone(1500.0,0.5);
        pad.leds_on();
        wait(0.5);
        pad.leds_off();
    }
}

void Falldown::print_scores(N5110 &lcd)
{
    // get scores from Grounds
    int p1_score = _p1.get_score();
    int p2_score = _p2.get_score();

    // print to LCD i
    char buffer1[14];
    sprintf(buffer1,"%2d",p1_score);
    lcd.printString(buffer1,WIDTH/2 - 20,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
    char buffer2[14];
    sprintf(buffer2,"%2d",p2_score);
    lcd.printString(buffer2,WIDTH/2 + 4,1);
}*/