ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Ball/Ball.cpp

Committer:
shahidsajid
Date:
2019-04-20
Revision:
8:7b7e1a5b8200
Parent:
7:a1a6bff238c1
Child:
33:9d34ef219fff

File content as of revision 8:7b7e1a5b8200:

#include "Ball.h"


Ball::Ball()
{

}

Ball::~Ball()
{

}

void Ball::init(int size,int speed)
{
    _size = size;
    _x1=42;
    _y1=3;
    _x = 42;
    _y = 15;
    bat.init(3,4);

    srand(time(NULL));
    int direction = rand() % 4; // randomise initial direction. 

    // 4 possibilities. Get random modulo and set velocities accordingly
    if (direction == 0) {
        _velocity.x = speed;
        _velocity.y = speed;
    } else if (direction == 1) {
        _velocity.x = speed;
        _velocity.y = -speed;
    } else if (direction == 2) {
        _velocity.x = speed;
        _velocity.y = speed;
    } else {
        _velocity.x = -speed;
        _velocity.y = -speed;
    }
}
int Ball::bowler_start(){
    if(_y1!=15){
        _y1++;
        return 0;
    }
    else {
        return 1;
    }
}
void Ball::start(N5110 &lcd,int checkHit, Direction dir){
   int ball_start_check=bowler_start();
   if (ball_start_check==1){
        printf("HELLO\n");
        if (bowled==1){
            update_ball(checkHit, dir);
        }
        else if (_y!=36){
            _y+=1;        
        }
        else {
            bowled=1;
        } 
    }   
}
void Ball::draw(N5110 &lcd)
{
    lcd.drawCircle(_x1,_y1,2,FILL_BLACK);
    lcd.drawCircle(_x,_y,1,FILL_BLACK);
    //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
}
void Ball::update_ball(int checkHit, Direction dir){
     if (ballHit!=1 && checkHit==1){
        ballHit=1;
    }
    if(bowled==1 && ballHit==1){
        printf("TEST\n");
        if (dir==NE){
              printf("x=%i  y=%i \n",_x,_y);
              if (!(_y == 2 )){
                  _y--;
              }   
              if (_x!=65){
                  _x++;
              }
              printf("HERE");        
    }
    
}
    
}


Vector2D Ball::get_velocity()
{
    Vector2D v = {_velocity.x,_velocity.y};
    return v;
}


void Ball::get_direction(Gamepad &pad){
    _d = pad.get_direction();
}

void Ball::draw_field(N5110 &lcd){
    lcd.drawCircle(field[0].x,field[0].y,2,FILL_BLACK);
    lcd.drawCircle(field[1].x,field[1].y,2,FILL_BLACK);
    lcd.drawCircle(field[2].x,field[2].y,2,FILL_BLACK);
    lcd.drawCircle(field[3].x,field[3].y,2,FILL_BLACK);
}
void Ball::set_field(N5110 &lcd){
    lcd.drawLine(40,11,40,14,1);
    lcd.drawLine(42,11,42,14,1);
    lcd.drawLine(44,11,44,14,1);
    lcd.drawLine(40,14,44,14,2);
    /*
    lcd.drawCircle(25,2,2,FILL_BLACK); //NE
    lcd.drawCircle(65,2,2,FILL_BLACK);  //NW
    lcd.drawCircle(65,40,2,FILL_BLACK);  //SE
    lcd.drawCircle(25,40,2,FILL_BLACK); //SW
    lcd.drawCircle(3,20,2,FILL_BLACK); //E
    lcd.drawCircle(80,20,2,FILL_BLACK);
    */
    int d=0;
    int pos=0;
    srand(time(NULL));
    for(int i=0;i<3;i++){
        d = 1+ rand() % 6;
        pos=rand()%2;
        //printf("Direction %i \n",d);  
        //while (d==N||d==S){
            //get_direction(pad);
        //}
        /*
        if (d==1){ //NW
            if (pos==1){
              field[i].x=30;
              field[i].y=9;
              field[i].position=1;
            }
            else{
              field[i].x=25;
              field[i].y=2;
              field[i].position=0;
            }
        }
        
        
        if (d==2){ //NE
            if (pos==1){
            field[i].x=55;
            field[i].y=9;
            field[i].position=1;
            }
            else{
                field[i].x=65;
                 field[i].y=2;
                 field[i].position=0;
            }
        }
        
                
        if (d==3){ //SW
            if (pos==1){
                field[i].x=33;
                field[i].y=40;
                field[i].position=1;
            }
                
            else{
              field[i].x=25;
              field[i].y=44;
            field[i].position=0;
            }
        }
        
        if (d==4){ //SE
            if (pos==1){
              field[i].x=55;
              field[i].y=40;
              field[i].position=1;
            }
            else{
              field[i].x=65;
              field[i].y=44;
              field[i].position=0;
            }
        }
        */
        if (d==5){ //W
            if (pos==1){
              field[i].x=3;
              field[i].y=25;
              field[i].position=1;
            }
            else{
              field[i].x=25;
              field[i].y=25;
              field[i].position=0;
            }
        }
        /*
        if (d==6){ //W
            field[i].x=80;
            field[i].y=20;
            field[i].position=1;
        }
        */
  }
  
}