ELEC2645 (2018/19) / Mbed 2 deprecated el17ebs

Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Sat Apr 20 10:42:17 2019 +0000
Revision:
9:bc34f2243e43
Parent:
8:d410856c6d04
Child:
10:9f54a6366e94
Levels addition fully functioning

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellisbhastroud 3:a8960004d261 1 #include "Ball.h"
ellisbhastroud 3:a8960004d261 2
ellisbhastroud 3:a8960004d261 3 //constructor
ellisbhastroud 3:a8960004d261 4
ellisbhastroud 3:a8960004d261 5 Ball::Ball()
ellisbhastroud 3:a8960004d261 6 {
ellisbhastroud 3:a8960004d261 7
ellisbhastroud 3:a8960004d261 8 }
ellisbhastroud 3:a8960004d261 9
ellisbhastroud 3:a8960004d261 10 //deconstructor
ellisbhastroud 3:a8960004d261 11
ellisbhastroud 3:a8960004d261 12 Ball::~Ball()
ellisbhastroud 3:a8960004d261 13 {
ellisbhastroud 3:a8960004d261 14
ellisbhastroud 3:a8960004d261 15 }
ellisbhastroud 3:a8960004d261 16
ellisbhastroud 3:a8960004d261 17 //public methods
ellisbhastroud 3:a8960004d261 18
ellisbhastroud 9:bc34f2243e43 19 void Ball::init(Coord start_pos) //ball starts new level stationary in x and y positions given
ellisbhastroud 3:a8960004d261 20 {
ellisbhastroud 9:bc34f2243e43 21 _x_pos = start_pos.x;
ellisbhastroud 9:bc34f2243e43 22 _y_pos = start_pos.y;
ellisbhastroud 4:035448357749 23 _x_vel = 0.0f;
ellisbhastroud 4:035448357749 24 _y_vel = 0.0f;
ellisbhastroud 9:bc34f2243e43 25 _total_shot_count = _total_shot_count + _shot_count;
ellisbhastroud 4:035448357749 26 _shot_count = 0;
ellisbhastroud 3:a8960004d261 27 }
ellisbhastroud 3:a8960004d261 28
ellisbhastroud 5:0b31909caf7f 29 void Ball::drawBall(N5110 &lcd)
ellisbhastroud 3:a8960004d261 30 {
ellisbhastroud 5:0b31909caf7f 31 lcd.drawRect(_x_pos,_y_pos,2,2,FILL_BLACK); //draws ball
ellisbhastroud 3:a8960004d261 32 }
ellisbhastroud 3:a8960004d261 33
ellisbhastroud 5:0b31909caf7f 34 void Ball::printShotCount(N5110 &lcd)
ellisbhastroud 3:a8960004d261 35 {
ellisbhastroud 5:0b31909caf7f 36 char buffer[14];
ellisbhastroud 5:0b31909caf7f 37 sprintf(buffer,"Shot %i",_shot_count);
ellisbhastroud 5:0b31909caf7f 38 lcd.printString(buffer,40,0);
ellisbhastroud 4:035448357749 39 }
ellisbhastroud 4:035448357749 40
ellisbhastroud 8:d410856c6d04 41 void Ball::drawPower(N5110 &lcd, float mag)
ellisbhastroud 5:0b31909caf7f 42 {
ellisbhastroud 9:bc34f2243e43 43 lcd.drawRect(0,10,36,6,FILL_TRANSPARENT);
ellisbhastroud 9:bc34f2243e43 44 lcd.drawRect(0,10,36*mag,6,FILL_BLACK);
ellisbhastroud 3:a8960004d261 45 }
ellisbhastroud 3:a8960004d261 46
ellisbhastroud 3:a8960004d261 47
ellisbhastroud 8:d410856c6d04 48 void Ball::drawAim(N5110 &lcd, Vector2D joy_coord, float angle)
ellisbhastroud 8:d410856c6d04 49 {
ellisbhastroud 8:d410856c6d04 50 if(angle != -1.0f) {
ellisbhastroud 8:d410856c6d04 51 lcd.drawLine(10,16,10+12*joy_coord.x,16+-12*joy_coord.y,1);
ellisbhastroud 8:d410856c6d04 52 }
ellisbhastroud 8:d410856c6d04 53
ellisbhastroud 8:d410856c6d04 54 }
ellisbhastroud 8:d410856c6d04 55
ellisbhastroud 5:0b31909caf7f 56 void Ball::move_ball(int frame_rate)
ellisbhastroud 3:a8960004d261 57 {
ellisbhastroud 8:d410856c6d04 58 _x_pos = _x_pos + _x_vel*10.0f/frame_rate; //move ball position at rate proportional to velocity in each direction
ellisbhastroud 4:035448357749 59 _y_pos = _y_pos + _y_vel*10.0f/frame_rate;
ellisbhastroud 8:d410856c6d04 60 _x_vel = _x_vel*(1.0f-(0.6f/frame_rate)); //ball slows down each loop caled by time between frames to ensure same movement at different frame rates
ellisbhastroud 8:d410856c6d04 61 _y_vel = _y_vel*(1.0f-(0.6f/frame_rate));
ellisbhastroud 8:d410856c6d04 62 if(_x_vel != 0 && _y_vel != 0 && abs(_x_vel) < 0.2f && abs(_y_vel) < 0.2f) { //to make ball come to complete stop once velocity is nearly 0
ellisbhastroud 8:d410856c6d04 63 _x_vel = 0;
ellisbhastroud 8:d410856c6d04 64 _y_vel = 0;
ellisbhastroud 8:d410856c6d04 65 }
ellisbhastroud 3:a8960004d261 66 }
ellisbhastroud 3:a8960004d261 67
ellisbhastroud 4:035448357749 68 Vector2D Ball::get_ball_pos()
ellisbhastroud 4:035448357749 69 {
ellisbhastroud 4:035448357749 70 Vector2D pos = {_x_pos, _y_pos};
ellisbhastroud 4:035448357749 71 return pos;
ellisbhastroud 4:035448357749 72 }
ellisbhastroud 4:035448357749 73
ellisbhastroud 8:d410856c6d04 74 void Ball::shoot_ball(Gamepad &pad, Vector2D _joy_coord)
ellisbhastroud 8:d410856c6d04 75 {
ellisbhastroud 8:d410856c6d04 76 if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < TOL && abs(_y_vel) < TOL){ //if ball stationary and a pressed then shoot
ellisbhastroud 8:d410856c6d04 77 _x_vel = 6.0f * _joy_coord.x; //scale x velocity by joystick direction and magnitude
ellisbhastroud 8:d410856c6d04 78 _y_vel = 6.0f * -_joy_coord.y; //scale y velocity by joystick direction and magnitude
ellisbhastroud 5:0b31909caf7f 79 _shot_count ++; //increment shot count
ellisbhastroud 3:a8960004d261 80 }
ellisbhastroud 3:a8960004d261 81
ellisbhastroud 3:a8960004d261 82 }
ellisbhastroud 3:a8960004d261 83
ellisbhastroud 4:035448357749 84 int Ball::get_shot_count()
ellisbhastroud 4:035448357749 85 {
ellisbhastroud 4:035448357749 86 int shot_count = _shot_count;
ellisbhastroud 4:035448357749 87 return shot_count;
ellisbhastroud 4:035448357749 88 }
ellisbhastroud 4:035448357749 89
ellisbhastroud 9:bc34f2243e43 90 int Ball::get_total_shot_count()
ellisbhastroud 3:a8960004d261 91 {
ellisbhastroud 9:bc34f2243e43 92 int total_shot_count = _total_shot_count;
ellisbhastroud 9:bc34f2243e43 93 return total_shot_count;
ellisbhastroud 9:bc34f2243e43 94 }
ellisbhastroud 9:bc34f2243e43 95
ellisbhastroud 9:bc34f2243e43 96 void Ball::set_total_shot_count(int total_shot_count)
ellisbhastroud 9:bc34f2243e43 97 {
ellisbhastroud 9:bc34f2243e43 98 int _total_shot_count = total_shot_count;
ellisbhastroud 3:a8960004d261 99 }
ellisbhastroud 3:a8960004d261 100
ellisbhastroud 9:bc34f2243e43 101 bool Ball::check_hole(Coord hole) //returns true when ball is hit in hole and next level begins
ellisbhastroud 9:bc34f2243e43 102 {
ellisbhastroud 9:bc34f2243e43 103 if(_x_pos > hole.x - 1 && _x_pos < hole.x + 2 && _y_pos > hole.y - 1 && _y_pos < hole.y + 2) {
ellisbhastroud 9:bc34f2243e43 104 _x_vel = 0; //stop ball moving
ellisbhastroud 9:bc34f2243e43 105 _y_vel = 0;
ellisbhastroud 9:bc34f2243e43 106 _x_pos = hole.x;
ellisbhastroud 9:bc34f2243e43 107 _y_pos = hole.y;
ellisbhastroud 9:bc34f2243e43 108 return true; //causes next level process to begin
ellisbhastroud 9:bc34f2243e43 109 }
ellisbhastroud 9:bc34f2243e43 110 else {
ellisbhastroud 9:bc34f2243e43 111 return false;
ellisbhastroud 9:bc34f2243e43 112 }
ellisbhastroud 9:bc34f2243e43 113 }
ellisbhastroud 9:bc34f2243e43 114
ellisbhastroud 9:bc34f2243e43 115 void Ball::check_wall_bounce(WallMap map[], int size) //uses information from WallMap array for each level to check for bounces
ellisbhastroud 3:a8960004d261 116 {
ellisbhastroud 8:d410856c6d04 117 for(int i = 0; i < size; i ++) {
ellisbhastroud 3:a8960004d261 118
ellisbhastroud 8:d410856c6d04 119 if(map[i].wall == LEFT) {
ellisbhastroud 8:d410856c6d04 120 left_bounce(map[i].start,map[i].end);
ellisbhastroud 8:d410856c6d04 121 }
ellisbhastroud 8:d410856c6d04 122 else if(map[i].wall == RIGHT) {
ellisbhastroud 8:d410856c6d04 123 right_bounce(map[i].start,map[i].end);
ellisbhastroud 8:d410856c6d04 124 }
ellisbhastroud 8:d410856c6d04 125 else if(map[i].wall == TOP) {
ellisbhastroud 8:d410856c6d04 126 top_bounce(map[i].start,map[i].end);
ellisbhastroud 8:d410856c6d04 127 }
ellisbhastroud 8:d410856c6d04 128 else if(map[i].wall == BOTTOM) {
ellisbhastroud 8:d410856c6d04 129 bottom_bounce(map[i].start,map[i].end);
ellisbhastroud 8:d410856c6d04 130 }
ellisbhastroud 8:d410856c6d04 131
ellisbhastroud 8:d410856c6d04 132 }
ellisbhastroud 3:a8960004d261 133 }
ellisbhastroud 5:0b31909caf7f 134
ellisbhastroud 8:d410856c6d04 135 void Ball::left_bounce(Coord start, Coord end) //top check for left wall collision
ellisbhastroud 5:0b31909caf7f 136 {
ellisbhastroud 8:d410856c6d04 137 if(_x_pos+_x_vel - 1 < start.x && _y_pos >= start.y && _y_pos <= end.y && _x_vel < 0){ // left wall (x=9 ,26<=y<=40)
ellisbhastroud 8:d410856c6d04 138 _x_pos = start.x + 1;
ellisbhastroud 8:d410856c6d04 139 _x_vel = -_x_vel; //5% velocity lost on impact and bounce
ellisbhastroud 5:0b31909caf7f 140 }
ellisbhastroud 8:d410856c6d04 141 }
ellisbhastroud 8:d410856c6d04 142
ellisbhastroud 8:d410856c6d04 143 void Ball::right_bounce(Coord start, Coord end)
ellisbhastroud 8:d410856c6d04 144 {
ellisbhastroud 8:d410856c6d04 145 if(_x_pos+_x_vel + 1 > start.x && _y_pos >= start.y && _y_pos <= end.y && _x_vel > 0){ //right wall x + 1
ellisbhastroud 8:d410856c6d04 146 _x_pos = start.x - 1;
ellisbhastroud 8:d410856c6d04 147 _x_vel = -_x_vel;
ellisbhastroud 5:0b31909caf7f 148 }
ellisbhastroud 8:d410856c6d04 149 }
ellisbhastroud 8:d410856c6d04 150
ellisbhastroud 8:d410856c6d04 151 void Ball::top_bounce(Coord start, Coord end)
ellisbhastroud 8:d410856c6d04 152 {
ellisbhastroud 8:d410856c6d04 153 if(_y_pos+_y_vel - 1 < start.y && _x_pos >= start.x && _x_pos <= end.x && _y_vel < 0){ //top wall y -1
ellisbhastroud 8:d410856c6d04 154 _y_pos = start.y + 1;
ellisbhastroud 8:d410856c6d04 155 _y_vel = -_y_vel;
ellisbhastroud 5:0b31909caf7f 156 }
ellisbhastroud 8:d410856c6d04 157 }
ellisbhastroud 8:d410856c6d04 158
ellisbhastroud 8:d410856c6d04 159 void Ball::bottom_bounce(Coord start, Coord end)
ellisbhastroud 8:d410856c6d04 160 {
ellisbhastroud 8:d410856c6d04 161 if(_y_pos+_y_vel + 1 > start.y && _x_pos >= start.x && _x_pos <= end.x && _y_vel > 0){ //bottom wall y + 2
ellisbhastroud 8:d410856c6d04 162 _y_pos = start.y - 1;
ellisbhastroud 8:d410856c6d04 163 _y_vel = -_y_vel;
ellisbhastroud 8:d410856c6d04 164 }
ellisbhastroud 5:0b31909caf7f 165 }
ellisbhastroud 3:a8960004d261 166 //private methods
ellisbhastroud 3:a8960004d261 167