ELEC2645 (2018/19) / Mbed 2 deprecated el17ebs

Dependencies:   mbed FATFileSystem

Ball/Ball.cpp

Committer:
ellisbhastroud
Date:
2019-04-08
Revision:
4:035448357749
Parent:
3:a8960004d261
Child:
5:0b31909caf7f

File content as of revision 4:035448357749:

#include "Ball.h"

//constructor

Ball::Ball()
{

}

//deconstructor

Ball::~Ball()
{

}

//public methods

void Ball::init(float x, float y) //ball starts stationary in x and y positions given
{
    _x_pos = x;
    _y_pos = y;
    _x_vel = 0.0f;
    _y_vel = 0.0f;
    _shot_count = 0;

}

void Ball::draw_ball(N5110 &lcd)
{
    lcd.drawRect(_x_pos,_y_pos,3,3,FILL_BLACK); //draws ball 
}

void Ball::draw_aim(N5110 &lcd, Gamepad &pad)
{
    read_joy(pad);
    lcd.drawLine((_x_pos+1.0f),(_y_pos+1.0f),(_x_pos+1.0f)+(_joystick.x*16.0f),(_y_pos+1.0f)-(_joystick.y*16.0f),1); //draws ball 
}

void Ball::draw_course(N5110 &lcd) 
{
    lcd.drawRect(0,16,83,32,FILL_TRANSPARENT);    
}

void Ball::draw_screen(N5110 &lcd, Gamepad &pad) {
    
    draw_course(lcd);
    draw_ball(lcd);
    draw_aim(lcd, pad);
    print_shot_count(lcd); 
}

void Ball::move_ball(int frame_rate, N5110 &lcd)
{
    check_bounce(lcd);
    _x_pos = _x_pos + _x_vel*10.0f/frame_rate; //frame_rate used to scale ball movement so that it is the same for any fps  
    _y_pos = _y_pos + _y_vel*10.0f/frame_rate;   
    _x_vel = _x_vel*(1.0f-(0.8f/frame_rate)); //ball slows down 10% each loop (scaled by time between frames)
    _y_vel = _y_vel*(1.0f-(0.8f/frame_rate));
    check_bounce(lcd);
  
}

Vector2D Ball::get_ball_pos()
{   
    Vector2D pos = {_x_pos, _y_pos};
    return pos;
}

void Ball::shoot_ball(Gamepad &pad)
{
    
    if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < 0.05f && abs(_y_vel) < 0.05f){ //if ball stationary and a pressed then shoot
    _x_vel = 8.0f * _joystick.x; //scale x velocity by joystick direction and magnitude
    _y_vel = 8.0f * -_joystick.y; //scale y velocity by joystick direction and magnitude
    _shot_count ++; //increment shot count
    
    }

}

int Ball::get_shot_count()
{
    int shot_count = _shot_count;
    return shot_count;
}

void Ball::print_shot_count(N5110 &lcd) 
{
    char buffer[14];
    sprintf(buffer,"Shot Count = %i",_shot_count);
    lcd.printString(buffer,0,0);      
}

void Ball::set_vel(float x_vel, float y_vel) 
{
    _x_vel = x_vel;
    _y_vel = y_vel;
}

void Ball::check_bounce(N5110 &lcd) 
{
    
    if(_x_pos - 1 < 0 ) { //
        _x_pos = 1;
        _x_vel = -_x_vel;
    } 
    else if(_y_pos + 3 > 48) {
        _y_pos = 45;
        _y_vel = -_y_vel;
    }
    else if(_x_pos + 3 > 83 ) { 
        _x_pos = 80;
        _x_vel = -_x_vel;
    } 
    else if(_y_pos - 1 < 16) {
        _y_pos = 17;
        _y_vel = -_y_vel;
    }
    
    
}

void Ball::read_joy(Gamepad &pad)
{
                                    
    _joystick = pad.get_mapped_coord(); //x and y coordinates from joystick assigned 

    
}
//private methods