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 5:0b31909caf7f 1 #include "GolfEngine.h"
ellisbhastroud 5:0b31909caf7f 2
ellisbhastroud 9:bc34f2243e43 3 Levels levels[2] = {{{15,31}, {68,31},
ellisbhastroud 9:bc34f2243e43 4 {{LEFT,{9,24},{9,39}},
ellisbhastroud 9:bc34f2243e43 5 {BOTTOM,{9,39},{74,39}},
ellisbhastroud 9:bc34f2243e43 6 {RIGHT,{74,24},{74,39}},
ellisbhastroud 9:bc34f2243e43 7 {TOP,{9,24},{74,24}}},4},
ellisbhastroud 9:bc34f2243e43 8
ellisbhastroud 9:bc34f2243e43 9 {{15,33}, {64,17},
ellisbhastroud 9:bc34f2243e43 10 {{LEFT,{9,26},{9,40}},
ellisbhastroud 9:bc34f2243e43 11 {BOTTOM,{9,40},{74,40}},
ellisbhastroud 9:bc34f2243e43 12 {RIGHT,{74,9},{74,40}},
ellisbhastroud 9:bc34f2243e43 13 {TOP,{56,9},{74,9}},
ellisbhastroud 9:bc34f2243e43 14 {LEFT,{56,9},{56,26}},
ellisbhastroud 9:bc34f2243e43 15 {TOP,{9,26},{56,26}}}, 6}};
ellisbhastroud 9:bc34f2243e43 16
ellisbhastroud 9:bc34f2243e43 17
ellisbhastroud 9:bc34f2243e43 18
ellisbhastroud 7:5a19dd9fe8a4 19 // constructor
ellisbhastroud 7:5a19dd9fe8a4 20
ellisbhastroud 7:5a19dd9fe8a4 21 GolfEngine::GolfEngine()
ellisbhastroud 7:5a19dd9fe8a4 22 {
ellisbhastroud 7:5a19dd9fe8a4 23
ellisbhastroud 7:5a19dd9fe8a4 24 }
ellisbhastroud 7:5a19dd9fe8a4 25
ellisbhastroud 7:5a19dd9fe8a4 26 //deconstructor
ellisbhastroud 7:5a19dd9fe8a4 27
ellisbhastroud 7:5a19dd9fe8a4 28 GolfEngine::~GolfEngine()
ellisbhastroud 7:5a19dd9fe8a4 29 {
ellisbhastroud 7:5a19dd9fe8a4 30
ellisbhastroud 7:5a19dd9fe8a4 31 }
ellisbhastroud 7:5a19dd9fe8a4 32
ellisbhastroud 5:0b31909caf7f 33 void GolfEngine::init()
ellisbhastroud 5:0b31909caf7f 34 {
ellisbhastroud 9:bc34f2243e43 35 _level = 0; //is level 1
ellisbhastroud 9:bc34f2243e43 36 _ball.set_total_shot_count(0);
ellisbhastroud 9:bc34f2243e43 37 _ball.init(levels[_level].ball);
ellisbhastroud 9:bc34f2243e43 38 }
ellisbhastroud 9:bc34f2243e43 39
ellisbhastroud 9:bc34f2243e43 40
ellisbhastroud 9:bc34f2243e43 41 void GolfEngine::new_level()
ellisbhastroud 9:bc34f2243e43 42 {
ellisbhastroud 9:bc34f2243e43 43 _level++; //increases level
ellisbhastroud 9:bc34f2243e43 44 _ball.init(levels[_level].ball); //initialises ball in position for current level
ellisbhastroud 5:0b31909caf7f 45 }
ellisbhastroud 5:0b31909caf7f 46
ellisbhastroud 8:d410856c6d04 47 void GolfEngine::drawGame(N5110 &lcd, Gamepad &pad)
ellisbhastroud 5:0b31909caf7f 48 {
ellisbhastroud 9:bc34f2243e43 49 drawHole(lcd, levels[_level].hole);
ellisbhastroud 5:0b31909caf7f 50 _ball.drawBall(lcd);
ellisbhastroud 8:d410856c6d04 51 _ball.drawPower(lcd, _mag);
ellisbhastroud 8:d410856c6d04 52 _ball.drawAim(lcd, _joy_coord, _angle);
ellisbhastroud 5:0b31909caf7f 53 _ball.printShotCount(lcd);
ellisbhastroud 9:bc34f2243e43 54 drawCourseWalls(lcd,levels[_level].walls,levels[_level].wall_count);
ellisbhastroud 8:d410856c6d04 55 }
ellisbhastroud 8:d410856c6d04 56
ellisbhastroud 8:d410856c6d04 57 void GolfEngine::read_input(Gamepad &pad)
ellisbhastroud 8:d410856c6d04 58 {
ellisbhastroud 8:d410856c6d04 59 _joy_coord = pad.get_mapped_coord();
ellisbhastroud 8:d410856c6d04 60 _mag = pad.get_mag();
ellisbhastroud 8:d410856c6d04 61 _angle = pad.get_angle();
ellisbhastroud 5:0b31909caf7f 62 }
ellisbhastroud 5:0b31909caf7f 63
ellisbhastroud 5:0b31909caf7f 64 void GolfEngine::update_ball(Gamepad &pad, int frame_rate)
ellisbhastroud 5:0b31909caf7f 65 {
ellisbhastroud 8:d410856c6d04 66 _ball.shoot_ball(pad, _joy_coord);
ellisbhastroud 9:bc34f2243e43 67 _ball.check_wall_bounce(levels[_level].walls, 6);
ellisbhastroud 5:0b31909caf7f 68 _ball.move_ball(frame_rate);
ellisbhastroud 9:bc34f2243e43 69 _ball.check_wall_bounce(levels[_level].walls, 6);
ellisbhastroud 9:bc34f2243e43 70 _hole_flag = _ball.check_hole(levels[_level].hole); //when ball is in hole this returns true and next level begins and flag reset for next level
ellisbhastroud 8:d410856c6d04 71
ellisbhastroud 8:d410856c6d04 72 }
ellisbhastroud 9:bc34f2243e43 73 void GolfEngine::drawCourseWalls(N5110 &lcd, WallMap map[], int size)
ellisbhastroud 8:d410856c6d04 74 {
ellisbhastroud 8:d410856c6d04 75 for(int i = 0; i < size; i++){
ellisbhastroud 8:d410856c6d04 76 lcd.drawLine(map[i].start.x,map[i].start.y,map[i].end.x,map[i].end.y,1);
ellisbhastroud 8:d410856c6d04 77 }
ellisbhastroud 8:d410856c6d04 78 }
ellisbhastroud 8:d410856c6d04 79
ellisbhastroud 9:bc34f2243e43 80 void GolfEngine::drawHole(N5110 &lcd, Coord hole) {
ellisbhastroud 9:bc34f2243e43 81 lcd.drawRect(hole.x,hole.y,3,3,FILL_TRANSPARENT); //draws ball
ellisbhastroud 9:bc34f2243e43 82 }
ellisbhastroud 9:bc34f2243e43 83
ellisbhastroud 9:bc34f2243e43 84 void GolfEngine::printLevel(N5110 &lcd)
ellisbhastroud 9:bc34f2243e43 85 {
ellisbhastroud 9:bc34f2243e43 86 char buffer[14];
ellisbhastroud 9:bc34f2243e43 87 sprintf(buffer,"Level %i",_level+1);
ellisbhastroud 9:bc34f2243e43 88 lcd.printString(buffer,24,2);
ellisbhastroud 9:bc34f2243e43 89 }
ellisbhastroud 9:bc34f2243e43 90
ellisbhastroud 9:bc34f2243e43 91
ellisbhastroud 8:d410856c6d04 92 void GolfEngine::set_level(int level)
ellisbhastroud 8:d410856c6d04 93 {
ellisbhastroud 8:d410856c6d04 94 _level = level;
ellisbhastroud 5:0b31909caf7f 95 }
ellisbhastroud 5:0b31909caf7f 96
ellisbhastroud 8:d410856c6d04 97 int GolfEngine::get_level()
ellisbhastroud 5:0b31909caf7f 98 {
ellisbhastroud 8:d410856c6d04 99 return _level;
ellisbhastroud 8:d410856c6d04 100 }
ellisbhastroud 9:bc34f2243e43 101
ellisbhastroud 9:bc34f2243e43 102 bool GolfEngine::get_hole_flag()
ellisbhastroud 9:bc34f2243e43 103 {
ellisbhastroud 9:bc34f2243e43 104 return _hole_flag;
ellisbhastroud 9:bc34f2243e43 105 }
ellisbhastroud 9:bc34f2243e43 106
ellisbhastroud 9:bc34f2243e43 107 void GolfEngine::reset_hole_flag()
ellisbhastroud 9:bc34f2243e43 108 {
ellisbhastroud 9:bc34f2243e43 109 _hole_flag = false;
ellisbhastroud 9:bc34f2243e43 110 }