ELEC2645 (2018/19) / Mbed 2 deprecated el17ntkv

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Badengine.cpp Source File

Badengine.cpp

00001 #include "Badengine.h"
00002 
00003 //Badengine default constructor used for object creation
00004 Badengine::Badengine(){
00005 }
00006 
00007 //Badengine default destructor used for object destruction
00008 Badengine::~Badengine(){
00009 }
00010 
00011 //Mutator method takes an integer number as it's parameter and sets the number of
00012 //ghosts for the respective level in the game denoted by the private variable
00013 //ghost number to this value
00014 void Badengine::setGhostNumber(int number){
00015     ghostnumber = number;
00016 }
00017 
00018 //Mutator method initialises the bad engine object by setting the game's status
00019 //variable to true(denoted that the player has not eaten all the fruit in the maze
00020 //yet) ,setting the ghoststatus private variable to true(denoting that no ghost-badman
00021 // character collisions have taken place yet),the score private variable is initialised to
00022 //zero as at the start no fruit have been eaten,the badman character is initialised with its
00023 //size set to the value passed in the call,the screen array object in use in the game is initialised
00024 //which entails filling the screen array with zeros,the grid object is then initialised with the parameters passed
00025 //being the screen array object and maximum number of ghosts that can be seen in the game which limits the level
00026 //for practicality sake and in consideration of processing power this value is 10
00027 void Badengine::init() {
00028     _status = true;
00029     _ghoststatus = true;
00030     _score = 0;
00031     _baddest.init(2);
00032     _screen.init();
00033     _grid.init(_screen,10);
00034 
00035 }
00036 
00037 //Mutator method reads the current position that the joystick is in at the point of
00038 //function call setting the private _d variable to this direction datatype value
00039 void Badengine::readInput(Gamepad &pad) {
00040     _d = pad.get_direction();
00041 }
00042 
00043 //Mutator method takes as input a reference to the LCD object subsequently calling the draw
00044 //methods of the grid and badman classes to display to the user the maze,fruits,ghosts and
00045 //badman character at their respective postions dependent on the point of call of this
00046 //method
00047 void Badengine::draw(N5110 &lcd) {
00048     _grid.drawGrid(lcd,ghostnumber);
00049     _baddest.draw(lcd);
00050 
00051 }
00052 
00053 //Mutator method which takes an input a reference gamepad object as the input parameter inorder to
00054 //actuate with the speaker component on the boar.The methods works by updating respective
00055 //flags within the game using methods from different classes inorder to control the
00056 //gameplay
00057 void Badengine::update(Gamepad &pad) {
00058     //In the event that the badman character controlled by the user has eaten
00059     //all the fruit in the maze which signals that the level is complete
00060     //the status flag is set to fault denoting that badman has eaten all the fruit
00061     if(_grid.getFruitNumber()== 0) {
00062         _status = false;
00063     }
00064 
00065     //The below line of code uses the direction set in the preceding call to the
00066     //read_input method in this instance east moving the badman character once
00067     //place to the east as long as the character has not collided with a wall within the
00068     //maze known by the call of the checkCollision()-collision detection method
00069     if(_d == E && checkCollision() == 0 ) {
00070         _baddest.setPos(_baddest.getPos().x + 1,_baddest.getPos().y);
00071     }
00072 
00073 
00074     //The below line of code uses the direction set in the preceding call to the value
00075     //returned by the preceding read_input call and in the event that the badman character has collided with
00076     //a fruit(eaten a fruit) - takes the coordinates of the collision,and removes the fruit from
00077     //the grid,updating the score by the value of the fruit collided with,playing a tone
00078     //in the process to notify to te gamer this collision
00079     if(_d == E && checkCollision() > 1) {
00080         _score = _score + checkCollision();
00081         pad.tone(750.0,0.1);
00082         _grid.updateFruit(_baddest.getPos().x + _baddest.getSize() + 1,_baddest.getPos().y,_screen);
00083     }
00084 
00085     //The below line of code uses the direction set in the preceding call to the
00086     //read_input method in this instance north moving the badman character once
00087     //place to the north as long as the character has not collided with a wall within the
00088     //maze known by the call of the checkCollision()-collision detection method
00089     if(_d == N && checkCollision() == 0) {
00090         _baddest.setPos(_baddest.getPos().x,_baddest.getPos().y -1);
00091     }
00092 
00093     //The below line of code uses the direction set in the preceding call to the value
00094     //returned by the preceding read_input call and in the event that the badman character has collided with
00095     //a fruit(eaten a fruit) - takes the coordinates of the collision,and removes the fruit from
00096     //the grid,updating the score by the value of the fruit collided with,playing a tone
00097     //in the process to notify to te gamer this collision
00098     if(_d == N && checkCollision() > 1) {
00099         _score = _score + checkCollision();
00100         pad.tone(750.0,0.1);
00101         _grid.updateFruit(_baddest.getPos().x,_baddest.getPos().y - _baddest.getSize() - 1,_screen);
00102     }
00103 
00104     //The below line of code uses the direction set in the preceding call to the
00105     //read_input method in this instance west moving the badman character once
00106     //place to the west as long as the character has not collided with a wall within the
00107     //maze known by the call of the checkCollision()-collision detection method
00108     if(_d == W && checkCollision() == 0) {
00109         _baddest.setPos(_baddest.getPos().x - 1,_baddest.getPos().y );
00110     }
00111 
00112     //The below line of code uses the direction set in the preceding call to the value
00113     //returned by the preceding read_input call and in the event that the badman character has collided with
00114     //a fruit(eaten a fruit) - takes the coordinates of the collision,and removes the fruit from
00115     //the grid,updating the score by the value of the fruit collided with,playing a tone
00116     //in the process to notify to te gamer this collision
00117     if(_d == W && checkCollision() > 1) {
00118         _score = _score + checkCollision();
00119         pad.tone(750.0,0.1);
00120         _grid.updateFruit(_baddest.getPos().x - _baddest.getSize() - 1,_baddest.getPos().y,_screen);
00121     }
00122 
00123     //The below line of code uses the direction set in the preceding call to the
00124     //read_input method in this instance south moving the badman character once
00125     //place to the south as long as the character has not collided with a wall within the
00126     //maze known by the call of the checkCollision()-collision detection method
00127     if(_d == S &&checkCollision() == 0) {
00128         _baddest.setPos(_baddest.getPos().x,_baddest.getPos().y +1);
00129     }
00130 
00131     //The below line of code uses the direction set in the preceding call to the value
00132     //returned by the preceding read_input call and in the event that the badman character has collided with
00133     //a fruit(eaten a fruit) - takes the coordinates of the collision,and removes the fruit from
00134     //the grid,updating the score by the value of the fruit collided with,playing a tone
00135     //in the process to notify to te gamer this collision
00136     if(_d == S &&checkCollision() > 1) {
00137         pad.tone(750.0,0.1);
00138         _score = _score + checkCollision();
00139         _grid.updateFruit(_baddest.getPos().x,_baddest.getPos().y + _baddest.getSize() + 1,_screen);
00140     }
00141 
00142     //The below lines of code takes the badman character's current x and y coordinates, the total number
00143     //of ghosts at the level and the screen array object to check whether any badman-ghost collisions
00144     //has taken place-setting the ghost collision flag to true in the instance that such a collision
00145     //has taken place
00146     if(_grid.update(_screen,ghostnumber,_baddest.getPos().x,_baddest.getPos().y,_baddest.getSize()) == true) {
00147         _ghoststatus = false;
00148     }
00149 }
00150 
00151 //Accessor method uses badman's current x and y coordinates and direction returning the value of the
00152 //next screen array position dependent on its direction of movement -returning 0 if the position is empty
00153 //1 is the position if occupied a grid character pixel and 2 or 3 - if the postion is occupied by
00154 //a fruit character pixel
00155 int Badengine::checkCollision() {
00156     int value = 0;
00157     if(_d == E   ) {
00158         value = _screen.get(_baddest.getPos().x + _baddest.getSize() + 1,_baddest.getPos().y);
00159     }
00160     if(_d == N ) {
00161         value = _screen.get(_baddest.getPos().x,_baddest.getPos().y - _baddest.getSize() - 1);
00162     }
00163 
00164     if(_d == W ) {
00165         value = _screen.get(_baddest.getPos().x - _baddest.getSize() - 1,_baddest.getPos().y);
00166     }
00167 
00168     if(_d == S ) {
00169         value = _screen.get(_baddest.getPos().x,_baddest.getPos().y + _baddest.getSize() + 1);
00170     }
00171 
00172     return value;
00173 }
00174 
00175 //The accessor method returns the status private variable value at the time of call
00176 bool Badengine::getStatus(){
00177     return _status;
00178 }
00179 
00180 //The accessor method returns the ghoststatus private variable value at the time of call
00181 bool Badengine::getGhostStatus() {
00182     return _ghoststatus;
00183 }
00184 
00185 //The accessor method returns the current score private variable value at the time of call
00186 int Badengine::getScore() {
00187     return _score;
00188 }