ELEC2645 (2018/19) / Mbed 2 deprecated el17ntkv

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ghost.cpp Source File

Ghost.cpp

00001 #include "Ghost.h"
00002 
00003 //Default Ghost object constructor
00004 Ghost::Ghost()
00005 {
00006 }
00007 //Default Ghost object destructor
00008 Ghost::~Ghost()
00009 {
00010 }
00011 
00012 //Mutator method initialises the ghost character object by setting it's initial start position
00013 //and it's size to the value passed as the parameter in the function call and the
00014 //ghost character's start centre coordinates to the x and y value passed in the
00015 //method call.
00016 void Ghost::init(int size,int x,int y)
00017 {
00018     _size = size;
00019     _x = x;
00020     _y = y;
00021     _direction = rand()%4;
00022 
00023     int cleft = _x - size;
00024     int ctop     = _y - size;
00025     int cright= _x + size;
00026     int cbottom =  _y + size;
00027 
00028     _topl.x = cleft;
00029     _topl.y = ctop;
00030     _topr.x =ctop ;
00031     _topr.y = cright;
00032     _bottr.x =cbottom ;
00033     _bottr.y = cright;
00034     _bottl.x = cbottom;
00035     _bottl.y = cleft;
00036 
00037 
00038 }
00039 
00040 //Mutator method takes as a parameter a LCD reference object and displays the
00041 //ghost character on the screen dependent on the current value of it's x and y
00042 //coordinates
00043 void Ghost::draw(N5110 &lcd)
00044 {
00045     lcd.drawCircle(_x,_y,_size,FILL_TRANSPARENT);
00046     lcd.drawCircle(_x,_y,_size/2,FILL_BLACK);
00047 }
00048 
00049 //Accessor method checks whether the current ghost object has collided with the badman character
00050 //object present on the screen with the badman character's current x ,y centre
00051 // coordinates and radius passed as the method's parameters-returning true if a
00052 //collision between the respective ghost object and the badman character object
00053 //and false otherwise
00054 bool Ghost::collisiondetection(int x,int y,int r)
00055 {
00056     bool value = false;
00057     if((pow((x-_x),2.0) + pow((y-_y),2.0)) <= pow((r+_size),2.0)) {
00058         value = true;
00059     }
00060     return value;
00061 }
00062 
00063 
00064 
00065 
00066 //Accessor method return the ghost character's current x and y centre coordinates
00067 Vector2D Ghost::getPos()
00068 {
00069     Vector2D p = {_x,_y};
00070     return p;
00071 }
00072 
00073 //Mutator method takes the x and y integer values passed as the method's parameters
00074 //and sets the ghost's objects x and y coordinates to this value
00075 void Ghost::setPos(int x,int y)
00076 {
00077     _x =x;
00078     _y = y;
00079     int cleft = _x - _size;
00080     int ctop     = _y - _size;
00081     int cright= _x + _size;
00082     int cbottom =  _y + _size;
00083 
00084     _topl.x = cleft;
00085     _topl.y = ctop;
00086     _topr.x =ctop ;
00087     _topr.y = cright;
00088     _bottr.x =cbottom ;
00089     _bottr.y = cright;
00090     _bottl.x = cbottom;
00091     _bottl.y = cleft;
00092 }
00093 
00094 //Mutator method updates the ghost object by moving it by one place in the direction of
00095 //movement denoted by the _direction private variable every time this method is called
00096 //changing the ghosts movement randomly if the ghost collides with a wall pixel
00097 //denoted by the checkcollision() function returning a 1 within the maze
00098 //making use of the principle of recursion
00099 void Ghost::update(ScreenArray &screen)
00100 {
00101 
00102     {
00103         //Direction is N
00104         if(_direction == 0 && checkCollision(screen)!=1 ) {
00105             _y = _y - 1;
00106         }
00107 //Direction is E
00108         else if(_direction == 1 && checkCollision(screen)!=1) {
00109             _x = _x + 1;
00110         }
00111 //Direction is W
00112         else if(_direction == 2 && checkCollision(screen)!=1) {
00113             _x =_x -1;
00114         }
00115 //Direction is S
00116         else if(_direction == 3 && checkCollision(screen)!=1) {
00117             _y = _y + 1;
00118         }
00119 
00120         bool collision = false;
00121         if(checkCollision(screen) ==1) {
00122             collision = true;
00123         }
00124 
00125         if(collision == true) {
00126             _direction = rand()%4;
00127             update(screen);
00128 
00129         }
00130 
00131 
00132     }
00133 
00134 }
00135 
00136 //Accessor method uses the ghost's current x and y coordinates and direction returning the value of the
00137 //next screen array position dependent on its direction of movement -returning 0 if the position is empty
00138 //1 is the position if occupied a grid character pixel and 2 or 3 - if the postion is occupied by
00139 //a fruit character pixel
00140 int Ghost::checkCollision(ScreenArray &screen)
00141 {
00142     int value = 0;
00143     if(_direction == 1   ) {
00144         value = screen.get(_x + _size + 1,_y);
00145     }
00146     if(_direction == 0 ) {
00147         value = screen.get(_x,_y - _size - 1);
00148     }
00149 
00150     if(_direction == 2 ) {
00151         value = screen.get(_x - _size - 1,_y);
00152     }
00153 
00154     if(_direction == 3 ) {
00155         value = screen.get(_x,_y + _size + 1);
00156     }
00157 
00158 
00159 
00160     return value;
00161 }