ELEC2645 (2018/19) / Mbed 2 deprecated el17apb

Dependencies:   mbed Gamepad N5110

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Blockhead.cpp Source File

Blockhead.cpp

00001 #include "Blockhead.h"
00002 
00003 
00004 
00005 
00006 // nothing doing in the constructor and destructor
00007 Blockhead::Blockhead()
00008 {
00009 
00010 }
00011 
00012 Blockhead::~Blockhead()
00013 {
00014 
00015 }
00016 
00017 
00018 //initialise all vairables
00019 void Blockhead::init()              
00020 {
00021 
00022     _x = 35;                        //x cordinate of blockhead
00023     _y = 5;                         //y cordinate of bloc
00024     _jump = 0;                      //jump counter
00025     _wjr = 0;                       //wall jump right counter
00026     _wjl = 0;                       //wall jump left counter
00027     _fall = 0;                      //fall counter
00028     _sprintright = 0;               //right sprint counter
00029     _sprintleft = 0;                //left sprint counter
00030     _level = 1;                     //dont initialise every time so doesnt restart at beggining each time..
00031     _runani = 0;                    //run counter
00032     _walkani = 0;                   //wak animation counter
00033     _death = 0;                     //death counter
00034 }
00035 
00036 
00037 //initialise all variables, except level
00038 void Blockhead::continue_init()         
00039 {
00040 
00041 
00042     _x = 0;                             //x cordiante of blockhead
00043     if (_level == 6 || _level == 7 || _level == 9) {   //start point differs depending on what level your on
00044         _y = -3;                         //y cordiante either 5,
00045     } else {
00046         _y = 25;                        //or 25
00047     }
00048     _jump = 0;                          //jump counter
00049     _wjr = 0;                           //wall jump right counter
00050     _wjl = 0;                           //wall jump left counter
00051     _fall = 0;                          //fall counter
00052     _sprintright = 0;                   //sprint right counter
00053     _sprintleft = 0;                    //sprint left counter
00054     _runani = 0;                        //run counter
00055     _walkani = 0;                       //walk animation counter
00056     _death = 0;                         //death counter
00057 
00058 
00059 }
00060 
00061 
00062 //all mechanics controling blockhead (movement from user input, movement from platforms, and gameover)
00063 void Blockhead::blockhead(Pos pos, N5110 &lcd, Gamepad &pad)    
00064 {
00065     movement(lcd, pad);                                         //contolls movement of blockhead depending on input
00066     platform_check(pos);                                        //checks if blochead is on or by a moving platform,
00067     on_platform(lcd);                                           //if so, moves blockhead acordingly
00068     gameover(pos, lcd);                                         //intiates death sequence if spike hit or crushed by platform
00069     
00070     
00071 
00072 }
00073 
00074 
00075 //all the functions which take an input and controll movement of blockhead (in correct order)
00076 void Blockhead::movement(N5110 &lcd, Gamepad &pad)              
00077 {
00078     button_press(lcd, pad);                                     //checks if A button is pressed at correct moment (on ground for example), and starts correct counter
00079     _speed = get_speed(pad);                                    //gets speed value according to joystick position
00080     jump(lcd);                                                  //performes a jump when jump counter is 1
00081     wall_jump_left(lcd, _speed);                                //peformes a wall jumo when wjl counter is 1
00082     wall_jump_right(lcd, _speed);                               //peformes a wall jump when wjr counter is 1
00083     run_left(lcd, _speed);                                      //moves left if speed is less than 0, lower speed is quicker blockhead moves
00084     run_right(lcd, _speed);                                     //moves righ if speed is more than 0, higher speed is quicker blockhead moves
00085     cancel_sprint(lcd, _speed);                                 //cancels sprint if hits wall or joystick is 0
00086     fall(lcd);                                                  //falls when no ground below
00087     runner_state(lcd, pad, _speed);                             //displays different sprites depending on what state runner is in (animating him)
00088 }
00089 
00090 
00091 //when game over, returns 1
00092 int Blockhead::gameover_flag(Gamepad &pad)                     
00093 {
00094     if (_death > 8 || fallen(pad) == 1) {                      //will return 1 when character falls of screen, or death sequence completes,
00095         _gameover = 1;
00096     } else {
00097         _gameover = 0;
00098     }
00099     
00100     //printf("_gameove = %d", _gameover);  //uncoment to print for testing
00101 
00102     return _gameover;
00103 }
00104 
00105 //starts death sequence if crushed by moving platform or if hit spikes
00106 void Blockhead::gameover(Pos pos, N5110 &lcd)                 
00107 {
00108     if ((crushed_by_ver(pos) + crushed_by_ver_2(pos) + spike_hit(lcd, pos))>0 && _death==0) {
00109         _death = 1;
00110     }
00111     
00112     //printf("_death = %d", _death);  //uncoment to print for testing
00113 }
00114 
00115 
00116 //change increment depending on what button is pressed and where the runner is
00117 void Blockhead::button_press(N5110 &lcd, Gamepad &pad)                                                  
00118 {
00119 
00120     if (pad.check_event(Gamepad::A_PRESSED)) {
00121         if (_ani.pixelsDown(_x,_y,lcd) || _d3 == 3 || _d4 == 3) {                                       //if standing, or if on vertical platform,
00122             _jump = 1;                                                                                  //jump increment begins.
00123             pad.tone(880, 0.1);
00124         } else if (_ani.pixelsLeft(_x,_y,lcd) && _ani.pixelsDown(_x,_y,lcd)==false && _wjr == 0) {      //if in air and by wall to left, wall jump right = 1
00125             _wjr++;
00126             pad.tone(1046.502, 0.1);
00127         } else if (_ani.pixelsRight(_x,_y,lcd) && _ani.pixelsDown(_x,_y,lcd) == false && _wjl == 0) {   //if in air, and by wall to right, wall jump left = 1
00128             _wjl++;
00129             pad.tone(1046.502, 0.1);
00130         }
00131     }
00132 }
00133 
00134 
00135  //move n pixels up if no collision
00136 void Blockhead::move_up(N5110 &lcd, int n)                     
00137 {
00138 
00139     for(int i = 0; i < n; i++) {
00140         if (_ani.pixelsUp(_x,_y,lcd) == false) {
00141             _y--;
00142         }
00143     }
00144     
00145     //printf("_y = %d", _y);  //uncoment to print for testing
00146 }
00147 
00148 
00149 //move n pixels down if no collision
00150 void Blockhead::move_down(N5110 &lcd, int n)                    
00151 {
00152     for(int i = 0; i < n; i++) {
00153         if (_ani.pixelsDown(_x,_y,lcd) == false) {
00154             _y++;
00155         }
00156     }
00157     
00158     //printf("_y = %d", _y);  //uncoment to print for testing
00159 }
00160 
00161 
00162 //move n pixels right if no collision
00163 void Blockhead::move_right(N5110 &lcd, int n)                   
00164 {
00165     for(int i = 0; i < n; i++) {
00166         if (_ani.pixelsRight(_x,_y,lcd) == false) {
00167             _x++;
00168         }
00169     }
00170     
00171     //printf("_x = %d", _x);  //uncoment to print for testing
00172 }
00173 
00174 
00175 //move n pixels left if no collision
00176 void Blockhead::move_left(N5110 &lcd, int n)                    
00177 {
00178 
00179     for(int i = 0; i < n; i++) {
00180         if (_ani.pixelsLeft(_x,_y,lcd) == false) {
00181             _x--;
00182         }
00183     }
00184     
00185     //printf("_x = %d", _x);  //uncoment to print for testing
00186 
00187 }
00188 
00189 
00190 //peforms jump sequence when _jump is 1
00191 void Blockhead::jump(N5110 &lcd)
00192 {
00193 
00194     if (_jump <= 4 && _ani.pixelsUp(_x,_y,lcd) == false) {              //if nothing above, and jump counter is below 4
00195         if (_jump == 1) {                                               //four pixels per refresh for first two jump increments
00196             _jump++;
00197             move_up(lcd, 4);
00198         } else if (_jump == 2) {                                        // two pixels per refresh for next jump incrmements
00199             _jump++;
00200             move_up(lcd, 2);
00201         } else if (_jump > 2 && _jump <= 4) {                           // one pixel per refresh for final jump increments
00202             move_up(lcd, 1);
00203             _jump++;
00204         }
00205     } else {
00206         _jump = 0;                                                      //jump counter reset to 0 once jump cycle finished or interupted
00207     }
00208     
00209     //printf("_jump = %d, _x = %d, _y = d%", _jump, _x, _y);  //uncoment to print for testing
00210 
00211 }
00212 
00213 
00214 //peforms wall jump right sequence when _wjr = 1
00215 void Blockhead::wall_jump_right(N5110 &lcd, float speed)
00216 {
00217     if (_wjr == 1 && _ani.pixelsUp(_x,_y,lcd) == false) {                       //if wjr counter == 1 and nothing above
00218         _wjr++;
00219         _wjl = 0;                                                               //set wjl counter to 0, so can jump from opposite wall
00220         move_up(lcd, 4);                                                        //move four pixels up
00221         if (speed < 0.5f) {                                                     //and two pixels right, unless joy stick is driving right anyway
00222             move_right(lcd, 2);
00223         }
00224     } else if (_wjr == 2 && _ani.pixelsUp(_x,_y,lcd) == false) {
00225         _wjr++;
00226         move_up(lcd,2);                                                         //move two pixels up
00227         if (speed < 0.5f) {                                                     //and a pixel right, unless joy stick is driving right anyway
00228             move_right(lcd, 1);
00229         }
00230     } else if (_wjr > 2 && _wjr <= 4 && _ani.pixelsUp(_x,_y,lcd) == false) {
00231         _wjr++;
00232         move_up(lcd, 1);                                                        //move one pixel up
00233         if (speed < 0.5f) {                                                     //and a pixel right, unless joy stick is driving right anyway
00234             move_right(lcd, 1);
00235         }
00236     } else if (_ani.pixelsDown(_x,_y,lcd)) {
00237         _wjr = 0;                                                               //only reset wall jump count when on ground, or when
00238     } else if (_ani.pixelsUp(_x,_y,lcd)) {                                      //left wall jump activated, so you cant wall jump continuously up one wall.
00239         _wjr = 0;                                                               //or when ceiling hit to avoid blockhead sticking
00240     }
00241     
00242     //printf("_wjr = %d, _x = %d, _y = d%", _wjr, _x, _y);  //uncoment to print for testing
00243 
00244 
00245 }
00246 
00247 
00248 //peforms wall jump left sequence when _wjl = 1 
00249 void Blockhead::wall_jump_left(N5110 &lcd, float speed)
00250 {
00251 
00252     if (_wjl == 1 && _ani.pixelsUp(_x,_y,lcd) == false) {           //if wjl counter == 1 and nothing above
00253         _wjl++;
00254         _wjr = 0;                                                               //set wjr counter to 0, so can jump from opposite wall
00255         move_up(lcd, 4);                                                        //move four pixels up
00256         if (speed > -0.5f) {                                                    //and two pixels left, unless joy stick is driving left anyway
00257             move_left(lcd, 2);
00258         }
00259     } else if (_wjl == 2 && _ani.pixelsUp(_x,_y,lcd) == false) {
00260         _wjl++;
00261         move_up(lcd, 2);                                                        //move two pixels up
00262         if (speed > -0.5f) {                                                    //and a pixel left, unless joy stick is driving left anyway
00263             move_left(lcd, 1);
00264         }
00265     } else if (_wjl > 2 && _wjl <= 4 && _ani.pixelsUp(_x,_y,lcd) == false) {
00266         _wjl++;
00267         move_up(lcd, 1);                                                        // move one pixel up
00268         if (speed > -0.5f) {                                                    //and a pixel left, unless joy stick is driving left anyway
00269             move_left(lcd, 1);
00270         }
00271     } else if (_ani.pixelsDown(_x,_y,lcd)) {
00272         _wjl = 0;                                                               //only reset wall jump count when on ground, or when
00273     } else if (_ani.pixelsUp(_x,_y,lcd)) {                                      //right wall jump activated, so you cant wall jump continuously up one wall.
00274         _wjl = 0;
00275     }
00276 
00277     //printf("_wjl = %d, _x = %d, _y = d%", _wjl, _x, _y);  //uncoment to print for testing
00278 
00279 }
00280 
00281 
00282 //peforms fall when NOT in a jump cycle, wall jump cycle, and no ground below.
00283 void Blockhead::fall(N5110 &lcd)
00284 {
00285     if (_jump == 0 && (_wjr == 0 || _wjr == 5) && (_wjl == 0 || _wjl == 5) && _ani.pixelsDown(_x,_y,lcd) == false) {
00286         if (_fall == 0) {
00287             move_down(lcd, 1);                                          //one pixel down per refresh to start
00288             _fall++;
00289         } else if (_fall == 1) {
00290             _fall++;
00291             move_down(lcd, 2);                                          //then two pixels down per refresh
00292         } else if (_fall == 2) {
00293             _fall++;
00294             move_down(lcd, 3);                                          //then three..
00295         } else if (_fall >= 3) {
00296             _fall++;
00297             move_down(lcd, 4);                                          //then four pixels per fresh (Maximum velocity!)
00298         }
00299     } else {
00300         _fall = 0;                                                      //rest fall counter when no longer falling
00301 
00302     }
00303     
00304     //printf("_fall = %d, _x = %d, _y = d%", _fall, _x, _y);  //uncoment to print for testing
00305 
00306 }
00307 
00308 
00309 //gets direction from joystick, and returns as speed
00310 float Blockhead::get_speed(Gamepad &pad)
00311 {
00312 
00313     Vector2D coord = pad.get_coord();
00314     float speed = coord.x;                                              //set speed to the cordinates of the joystick's x value
00315 
00316     return speed;
00317     
00318     //printf("speed = %d, _x = %d, _y = d%", speed);  //uncoment to print for testing
00319 }
00320 
00321 
00322 //moves blockhead right according to speed
00323 void Blockhead::run_right(N5110 &lcd, float speed)
00324 {
00325     if (_ani.pixelsRight(_x,_y,lcd) == false && (_wjl == 0 || _wjl == 5)) {     //run right if no obstical
00326         if (speed > 0.1f && speed <= 0.25f) {                                   //depending on speed, move...
00327             move_right(lcd, 1);                                                 //1 pixel right
00328         } else if (speed > 0.25f && speed <= 0.5f) {
00329             move_right(lcd, 2);                                                 //2 pixels right
00330         } else if (speed > 0.5f && speed <= 0.75f) {
00331             move_right(lcd, 3);                                                 //3 pixels right
00332         } else if (speed > 0.75f) {
00333             move_right(lcd, 4);                                                 //4 pixels right
00334             _sprintright++;
00335             if (_sprintright > 20) {                                            //if at top speed for more than 20 cycles, sprint! (6 pix per refresh);
00336                 move_right(lcd, 2);
00337             }
00338         }
00339     }
00340     
00341     //printf("speed = %d, _x = %d, _y = d%", speed, _x, _y);  //uncoment to print for testing
00342 
00343 }
00344 
00345 
00346 //moves blockhead left according to speed
00347 void Blockhead::run_left(N5110 &lcd, float speed)
00348 {
00349 
00350 
00351     if (_ani.pixelsLeft(_x,_y,lcd) == false && (_wjr == 0 || _wjr == 5)) {      //run left if no obstical
00352         if (speed < -0.1f && speed >= -0.25f) {                                 //depending on speed move..
00353             move_left(lcd, 1);                                                  //1 pixel left
00354         } else if (speed < -0.25f && speed >= -0.5f) {
00355             move_left(lcd, 2);                                                  //2 pixels right
00356         } else if (speed < -0.5f && speed >= -0.75f) {
00357             move_left(lcd, 3);                                                  //3 pixels right
00358         } else if (speed < -0.75f) {
00359             move_left(lcd, 4);                                                  //4 pixels right
00360             _sprintleft++;
00361             if (_sprintleft > 20) {                                             //if at top speed for more than 20 cycles, sprint!
00362                 move_left(lcd, 2);
00363             }
00364 
00365         }
00366     }
00367 
00368     //printf("speed = %d, _x = %d, _y = d%", speed, _x, _y);  //uncoment to print for testing
00369 
00370 }
00371 
00372 
00373 //restets sprint incrment if collision or joystick idle
00374 void Blockhead::cancel_sprint(N5110 &lcd, float speed)
00375 {
00376 
00377     if (speed < 0.5f || _ani.pixelsRight(_x,_y,lcd) ) {
00378         _sprintright = 0;                                                   //if collision or speed drops below 0.5 cancell sprint
00379     }
00380 
00381     if (speed > -0.5f || _ani.pixelsLeft(_x,_y,lcd) ) {
00382         _sprintleft = 0;                                                   //if collision or speed drops below 0.5 cancell sprint
00383     }
00384     
00385     //printf("_sprintleft = %d, _sprintright = %d", _jump;  //uncoment to print for testing
00386 
00387 }
00388 
00389 //runs blockhead's alive sequnce untill _death flag turns to one (as a result of death)
00390 void Blockhead::runner_state(N5110 &lcd, Gamepad &pad, float speed)
00391 {
00392 
00393     if (_death == 0) {
00394         alive_sequence(lcd, speed);                         //whilst game over flag is 0, runs alive sequence animation
00395     } else {
00396         death_sequence(lcd, pad);                           //when game over flag turns 1, run death sequence animation)
00397     }
00398 
00399 
00400 
00401 }
00402 
00403 
00404 //displays the correct animation depending on runner state  (for example when moving slowly walking animation is displayed)
00405 void Blockhead::alive_sequence(N5110 &lcd, float _speed)    
00406 {
00407     if (_ani.pixelsRight(_x, _y, lcd) && (_fall > 0 || _jump > 0 ) && (_d3 != 3 && _d4 !=3)) {                                  //if by right wall in air,
00408         _ani.wallclingRight(_x, _y, lcd);                                                                                       //display wall cling right spite
00409     } else if (_ani.pixelsLeft(_x, _y, lcd) && (_fall > 0 || _jump > 0 ) && (_d3 != 3 && _d4 !=3)) {                            //if by left wall in air,
00410         _ani.wallclingLeft(_x, _y, lcd);                                                                                        //display wall cling left sprite
00411     } else if ( ( (_wjr > 0 && _wjr < 5) || (_speed >= 0.1f && (_fall > 0 || _jump > 0)) ) && (_d3 != 3 && _d4 != 3) ) {        //if falling right,
00412         _ani.fallingRight(_x, _y, lcd);                                                                                         //display falling left sprite
00413     } else if ( ( (_wjl > 0 && _wjl < 5) || (_speed < -0.1f && (_fall > 0 || _jump > 0 )) ) && (_d3 != 3 && _d4 != 3) ) {       //if fallig left,
00414         _ani.fallingLeft(_x, _y, lcd);                                                                                          //display falling left sprite
00415     } else if ( (_wjr ==9 || _fall > 0 || _jump > 0 ) && (_d3 != 3 && _d4 !=3) )  {                                             //if falling,
00416         _ani.falling(_x, _y, lcd);                                                                                              //display falling sprite
00417     } else if (_speed > 0.5f) {                                                                                                 //if speed is more than 0.5
00418         run_sequence_right(lcd);                                                                                                //peform run right animation
00419     } else if (_speed > 0.1f) {                                                                                                 //if only more than 0
00420         walk_sequence_right(lcd);                                                                                               //peform walk right animation
00421     } else if (_speed < -0.5f) {                                                                                                //if speed less than -0.5
00422         run_sequence_left(lcd);                                                                                                 //peform run left animation
00423     } else if (_speed <  -0.1f) {                                                                                               //if only less than 0
00424         walk_sequence_left(lcd);                                                                                                //peform walk left animation
00425     } else {                                                                                                                    //if none of the above
00426         _ani.standing(_x, _y, lcd);                                                                                             //display standing sprite!
00427     }
00428 
00429 
00430 }
00431 
00432 
00433 //displays running animation (right)
00434 void Blockhead::run_sequence_right(N5110 &lcd)
00435 {
00436     _runani++;
00437     if (_runani == 1) {                                                 //when running right, cr alterntes between 1 and 2,
00438         _ani.runRightOne(_x, _y, lcd);                                  //displaying the run right sprites consecutively thus creating a running animation
00439     } else if (_runani == 2) {
00440         _ani.runRightTwo(_x, _y, lcd);
00441         _runani = 0;
00442     }
00443 
00444 
00445 
00446 }
00447 
00448 
00449 //displays running animation (left)
00450 void Blockhead::run_sequence_left(N5110 &lcd)
00451 {
00452     _runani++;
00453     if (_runani == 1) {                                                 //when running left, cr alterntes between 1 and 2,
00454         _ani.runLeftOne(_x, _y, lcd);                                   //displaying the run left sprites consecutively thus creating a running animation
00455     } else if (_runani == 2) {
00456         _ani.runLeftTwo(_x, _y, lcd);
00457         _runani = 0;
00458     }
00459 
00460 
00461 
00462 }
00463 
00464 
00465 //displays walking animation (right)
00466 void Blockhead::walk_sequence_right(N5110 &lcd)
00467 {
00468     _walkani++;
00469     if (_walkani == 1) {
00470         _ani.walkRightOne(_x, _y, lcd);                                     //when walking right, cw runs between 1 2 3 4,
00471     } else if (_walkani == 2) {                                             //displaying the walk right sprites consecutively thus creating a walking animation
00472         _ani.walkRightTwo(_x, _y, lcd);
00473     } else if (_walkani == 3) {
00474         _ani.walkRightThree(_x, _y, lcd);
00475     } else if (_walkani == 4) {
00476         _ani.walkRightFour(_x, _y, lcd);
00477         _walkani = 0;
00478     }
00479 
00480 
00481 }
00482 
00483 
00484 //displays walking animation (left)
00485 void Blockhead::walk_sequence_left(N5110 &lcd)
00486 {
00487     _walkani++;
00488     if (_walkani == 1) {                                                 //when walking left, cw runs between 1 2 3 4,
00489         _ani.walkLeftOne(_x, _y, lcd);                                   //displaying the walk right sprites consecutively thus creating a walking animation
00490     } else if (_walkani == 2) {
00491         _ani.walkLeftTwo(_x, _y, lcd);
00492     } else if (_walkani == 3) {
00493         _ani.walkLeftThree(_x, _y, lcd);
00494     } else if (_walkani == 4) {
00495         _ani.walkLeftFour(_x, _y, lcd);
00496         _walkani = 0;
00497     }
00498 
00499 
00500 }
00501 
00502 
00503 //displays death animation 
00504 void Blockhead::death_sequence(N5110 &lcd, Gamepad &pad)
00505 {
00506 
00507     if (_death == 1) {
00508         _ani.deathOne(_x,_y,lcd);                                       //when cd activated by gameover, run death sequence animation
00509         pad.tone(261.62, 0.1); //c3                                     //a tone is played each fram, creating a disonant tune along with the animation
00510     } else if (_death == 2) {
00511         _ani.deathTwo(_x,_y,lcd);
00512         pad.tone(97.999, 0.1); //g2
00513     } else if (_death == 3) {
00514         _ani.deathThree(_x,_y,lcd);
00515         pad.tone(207.652, 0.1);  //g1#
00516     } else if (_death == 4) {
00517         _ani.deathFour(_x,_y,lcd);
00518         pad.tone(155.563, 0.1);   //d1#
00519     }
00520     _death++;
00521 
00522 
00523 }
00524 
00525 
00526 //changes level when either side of screen
00527 int Blockhead::next_level()
00528 {
00529     if (_x > 76) {  
00530         _x = -1;                                                        //when blockhead goes off right side of screen,
00531         _level++;                                                       //level increments, and blockhead appears at left side
00532     } else if (_x < -1) {
00533         _x = 76;                                                        //when blockhead goes off left side of screen,
00534         _level--;                                                       //level deccrements, and block head appears at right side
00535     }
00536     
00537     //printf("_level = %d", _level);  //uncoment to print for testing
00538 
00539     return _level;
00540 
00541 }
00542 
00543 
00544 //checks if blockhead is on horizontal platform
00545 int Blockhead::on_hoz_check(Pos pos)
00546 {
00547     int d;
00548     int c = 0;
00549 
00550     for (int i = 0; i < pos.l; i++) {                                                       //for the entire lenth of moving platform one
00551         if (((_x+2 == pos.x+i) || (_x+6 == pos.x+i)) &&(_y+12 == pos.y)) {                  //checks if cordinates of horizontal moving platform one,
00552             c++;                                                                            //are the same as blockheads's underside cordinates, if so c++
00553         }
00554     }
00555     if (c > 0) {                                                                            //if c > 0 blockhead is on the moving platform,
00556         d = pos.d;                                                                          //then d = direction of moving platform (0 = right, 1 - left)
00557     } else {                                                                                //if not then d = 2 (insignificant number)
00558         d = 2;
00559     }
00560     
00561     //printf("d = %d", d);  //uncoment to print for testing
00562 
00563     return d;                                                                               //d is then used to move blcokhead in the direction of the platform he is on
00564 }
00565 
00566 
00567 //checks if blockhead is by right of horizontal platform
00568 int Blockhead::by_hoz_check_right(Pos pos)
00569 {
00570     int c = 0;
00571     int d;
00572 
00573     for (int i = 0; i < 12; i ++) {                                                                                             //for 12 (height of blockhead)
00574         if ((_x-1 == pos.x + pos.l) && ((_y + i == pos.y) || (_y + i == pos.y+1) || (_y + i == pos.y+2))) {                     //check if cordinates of right side horizontal moving platform one
00575             c++;                                                                                                                //are the same as blockhead's left side
00576         }
00577     }
00578 
00579     if (c > 0 && pos.d == 0) {                                                              // if c > 0 and the platform is moving right
00580         d = 1;                                                                              // then d = 1,
00581     } else {                                                                                // otherwise d = 0 (insignificant number)
00582         d = 0;
00583     }
00584     
00585     //printf("d = %d", d);  //uncoment to print for testing
00586 
00587     return d;                                                                               //d is then used to move blockhead in the direction the platform is pushing against him
00588 }
00589 
00590 
00591 //checks if blockhead is by left of horizontal platform
00592 int Blockhead::by_hoz_check_left(Pos pos)
00593 {
00594     int c = 0;
00595     int d;
00596 
00597     for (int i = 0; i < 12; i ++) {                                                                                             //for 12 (height of blockhead)
00598         if ((_x+9 == pos.x) && ((_y + i == pos.y) || (_y + i == pos.y+1) || (_y + i == pos.y+2))) {                             //check if cordinates of left side horizontal moving platform one
00599             c++;                                                                                                                //are the same as blockhead's right side
00600         }
00601     }
00602 
00603     if (c > 0 && pos.d == 1) {                                                              //if c > 0 and the platform is moving right
00604         d = 1;                                                                              //then d = 1,
00605     } else {                                                                                //otherwise d = 0 (insignificant number)
00606         d = 0;
00607     }
00608     
00609     //printf("d = %d", d);  //uncoment to print for testing
00610 
00611     return d;                                                                               //d is then used to move blockhead in the dicrection the platform is pushing against him
00612 }
00613 
00614 
00615 //checks if blockhead is on horizontal platform (2)
00616 int Blockhead::on_hoz_2_check(Pos pos)
00617 {
00618     int d;
00619     int c = 0;
00620 
00621     for (int i = 0; i < pos.l2; i++) {
00622         if (((_x+2 == pos.x2+i) || (_x+6 == pos.x2+i)) &&(_y+12 == pos.y2)) {               //for the entire lenth of moving platform one
00623             c++;                                                                            //checks if cordinates of horizontal moving platform one,
00624         }                                                                                   //are the same as blockheads's underside cordinates, if so c++
00625     }
00626     if (c > 0) {
00627         d = pos.d2;                                                                         //if c > 0 blockhead is on the moving platform,
00628     } else {                                                                                //then d = direction of moving platform (0 = right, 1 - left)
00629         d = 2;                                                                              //if not then d = 2 (insignificant number)
00630     }
00631     
00632     //printf("d = %d", d);  //uncoment to print for testing
00633 
00634     return d;                                                                               //d is then used to move blcokhead in the direction of the platform he is on
00635 }
00636 
00637 
00638 //checks if blockhead is by right of horizontal platform (2)
00639 int Blockhead::by_hoz_2_check_right(Pos pos)
00640 {
00641     int c = 0;
00642     int d;
00643 
00644     for (int i = 0; i < 12; i ++) {                                                                                             //for 12 (height of blockhead)
00645         if ((_x-1 == pos.x2 + pos.l2) && ((_y+i == pos.y2) || (_y+i == pos.y2+1) || (_y + i == pos.y2+2))) {                    //check if cordinates of right side horizontal moving platform one
00646             c++;                                                                                                                //are the same as blockhead's left side
00647         }
00648     }
00649     //if c > 0 and the platform is moving right
00650     if (c > 0 && pos.d2 == 0) {                                                             //then d = 1,
00651         d = 1;                                                                              //otherwise d = 0 (insignificant number)
00652     } else {
00653         d = 0;
00654     }
00655     
00656     //printf("d = %d", d);  //uncoment to print for testing
00657 
00658     return d;                                                                               //d is then used to move blockhead in the direction the platform is pushing against him
00659 }
00660 
00661 
00662 //checks if blockhead is by left of horizontal platform (2)
00663 int Blockhead::by_hoz_2_check_left(Pos pos)
00664 {
00665     int c = 0;
00666     int d;
00667 
00668     for (int i = 0; i < 12; i ++) {                                                                                             //for 12 (height of blockhead)
00669         if ((_x+9 == pos.x2) && ((_y + i == pos.y2) || (_y + i == pos.y2+1) || (_y + i == pos.y2+2))) {                         //check if cordinates of left side horizontal moving platform one
00670             c++;                                                                                                                //are the same as blockhead's right side
00671         }
00672     }
00673 
00674     if (c > 0 && pos.d2 == 1) {                                                             //if c > 0 and the platform is moving right
00675         d = 1;                                                                              //then d = 1,
00676     } else {                                                                                //otherwise d = 0 (insignificant number)
00677         d = 0;
00678     }
00679     
00680     //printf("d = %d", d);  //uncoment to print for testing
00681 
00682     return d;                                                                               //d is then used to move blockhead in the dicrection the platform is pushing against him
00683 }
00684 
00685 
00686 //check is blockhead is on vertical moving platform
00687 int Blockhead::on_ver_check(Pos pos) // add the _x part..
00688 {
00689     int d;
00690     int c = 0;
00691 
00692     for (int i = 0; i < pos.vl+1; i++) {                                                    //for width of vertical moving platform
00693         if ((_y+11 == pos.vy-1) && ((_x == pos.vx+i) || (_x+1 == pos.vx+i) || (_x+2 == pos.vx+i) || (_x+6 == pos.vx+i) || (_x+7 == pos.vx+i) || (_x+8 == pos.vx+i))) {            //check if cordinates of top of vertical moving platform,
00694             c++;                                                                            //are the same as blockheads underside
00695         }
00696     }
00697     
00698     if (c > 0) {                                                                            //if c > 0 blockhead is on the moving platform,
00699         d = pos.vd+3; //3 = down 4 = up                                                     //then d = direction of moving platform (3 = down, 4 = up)
00700     } else {                                                                                //if not then d = 2 (insignificant number)
00701         d = 2; //not moving
00702     }
00703     
00704     //printf("d = %d", d);  //uncoment to print for testing
00705 
00706     return d;
00707 }
00708 
00709 
00710 //check is blockhead is on vertical moving platform (2)
00711 int Blockhead::on_ver_2_check(Pos pos)
00712 {
00713     int d;
00714     int c = 0;
00715 
00716     for (int i = 0; i < pos.vl2+1; i++) {                                                   //for width of vertical moving platform
00717         if ((_y+11 == pos.vy2-1) &&  ((_x == pos.vx2+i) || (_x+1 == pos.vx2+i) || (_x+2 == pos.vx2+i) || (_x+6 == pos.vx2+i) || (_x+7 == pos.vx2+i) || (_x+8 == pos.vx2+i)) ) {   //check if cordinates of top of vertical moving platform
00718             c++;                                                                            //are the same as blockheads underside
00719         }
00720     }
00721 
00722     if (c > 0) {                                                                            //if c > 0 blockhead is on the moving platform,
00723         d = pos.vd2+3; //3 = down 4 = up                                                    //then d = direction of moving platform (3 = down, 4 = up)
00724     } else {                                                                                //if not then d = 2 (insignificant number)
00725         d = 2; //not moving
00726     }
00727     
00728     //printf("d = %d", d);  //uncoment to print for testing
00729 
00730     return d;
00731 }
00732 
00733 
00734 //checks if blockhead is being crushed by vertical moving platform 
00735 int Blockhead::crushed_by_ver(Pos pos)  
00736 {
00737     int c = 0;
00738     int g = 0;
00739 
00740     for (int a = 0; a < 9; a++) {                                                                  //for the height of blockhead,
00741         for (int i = 0; i < pos.vl+1; i++) {                                                       //and the width of the vertical moving platform,
00742             if ((_y+10-a == pos.vy-1) &&  ((_x+2 == pos.vx + i) || (_x+6 == pos.vx + i)) ) {       //check if the cordinates of width of vertical moving platform
00743                 c++;                                                                               //are the same as blockehads, starting from bottom -1 and up.
00744             }
00745         }
00746     }
00747 
00748     for (int a = 0; a < 9; a++) {                                                                   //for the height of blockhead
00749         for (int i = 0; i < pos.vl+1; i++) {                                                        //and the width of the vertical moving platform
00750             if ((_y+a == pos.vy+pos.vh) &&  ((_x+2 == pos.vx + i) || (_x+6 == pos.vx + i)) ) {      //check if the cordinates of width of vertical moving platform
00751                 c++;                                                                                //are the same as blockhead's, starting from top -1, and down.
00752             }
00753         }
00754     }
00755 
00756     if (c > 0) {                                                                                    //if so, g = 1 .. this means that blockhead his being crushed
00757         g = 1;
00758     } else {
00759         g = 0;
00760     }
00761     
00762     //printf("g = %d", g);  //uncoment to print for testing
00763 
00764     return g;
00765 }
00766 
00767 
00768 //checks if blockhead is being crushed by vertical moving platform (2)
00769 int Blockhead::crushed_by_ver_2(Pos pos)
00770 {
00771     int c = 0;
00772     int g = 0;
00773 
00774     for (int a = 0; a < 9; a++) {                                                                   //for the height of blockhead,
00775         for (int i = 0; i < pos.vl2+1; i++) {                                                       //and the width of the vertical moving platform two,
00776             if ((_y+10-a == pos.vy2-1) &&  ((_x+2 == pos.vx2 + i) || (_x+6 == pos.vx2 + i)) ) {     //check if the cordinates of width of vertical moving platform
00777                 c++;                                                                                //are the same as blockehads, starting from bottom -1 and up.
00778             }
00779         }
00780     }
00781 
00782     for (int a = 0; a < 9; a++) {                                                                   //for the height of blockhead
00783         for (int i = 0; i < pos.vl2+1; i++) {                                                       //and the width of the vertical moving platform two
00784             if ((_y+a == pos.vy2+pos.vh2) &&  ((_x+2 == pos.vx2 + i) || (_x+6 == pos.vx2 + i)) ) {  //check if the cordinates of width of vertical moving platform
00785                 c++;                                                                                //are the same as blockhead's, starting from top -1, and down.
00786             }
00787         }
00788     }
00789 
00790     if (c > 0) {
00791         g = 1;                                                                                      //if so, g = 1 .. this means that blockhead is being crushed
00792     } else {
00793         g = 0;
00794     }
00795     
00796     //printf("g = %d", g);  //uncoment to print for testing
00797 
00798     return g;
00799 }
00800 
00801 
00802 //checks if blockhead has hit spikes
00803 int Blockhead::spike_hit(N5110 &lcd, Pos pos)
00804 {
00805     int c = 0;
00806     int g;
00807 
00808     for (int i = 0; i < pos.sl; i++) {                                                                                                      //for length of spikes,
00809         if (lcd.getPixel(pos.sx+i, pos.sy-3)) {                                                                                             //check if pixels above,
00810             c++;                                                                                                                            //if plus c
00811         }
00812     }
00813     if (lcd.getPixel(pos.sx-1, pos.sy) || lcd.getPixel(pos.sx, pos.sy-1) || lcd.getPixel(pos.sx, pos.sy-2)) {                               //check if pixels to left side of spikes
00814         c++;                                                                                                                                //if so plus c
00815     }
00816 
00817     if (lcd.getPixel(pos.sx+pos.sl+1, pos.sy) || lcd.getPixel(pos.sx+pos.sl, pos.sy-1) || lcd.getPixel(pos.sx+pos.sl, pos.sy-2)) {          //checl if pixels to right side of spikes
00818         c++;                                                                                                                                //if so plus c
00819     }
00820 
00821 
00822     if (c > 0) {                                                                                                //if c > 0, g = 1.. this meand that blockhead has hit the spikes!
00823         g = 1;
00824     } else {
00825         g = 0;
00826     }
00827     
00828     //printf("g = %d", g);  //uncoment to print for testing
00829 
00830     return g;
00831 }
00832 
00833 
00834 //returns 1 if blockhead falls of screen
00835 int Blockhead::fallen(Gamepad &pad)
00836 {
00837     int g;
00838 
00839     if (_y > 51) {                                                      //if blockhead falls of screen,
00840         g = 1;                                                          //g = 1
00841         fall_tune(pad);
00842     } else {
00843         g = 0;
00844     }
00845 
00846     //printf("g = %d", g);  //uncoment to print for testing
00847 
00848     return g;
00849 
00850 }
00851 
00852 
00853 //plays desending tune to play once blockhead falls off screen.
00854 void Blockhead::fall_tune(Gamepad &pad)
00855 {
00856 
00857     pad.tone(1046.502, 0.1);                                            //desending tune to play once blockhead falls off screen.
00858     wait(0.1);
00859     pad.tone(932.328, 0.1);
00860     wait(0.1);
00861     pad.tone(622.254, 0.1);
00862     wait(0.1);
00863     pad.tone(523.251, 0.1);
00864     wait(0.1);
00865     pad.tone(466.164, 0.1);
00866     wait(0.1);
00867     pad.tone(369.994, 0.1);
00868     wait(0.1);
00869     pad.tone(293.665, 0.1);
00870     wait(0.1);
00871     pad.tone(261.626, 0.1);
00872     wait(0.1);
00873 }
00874 
00875 
00876 //checks if blcokhead is on or by any of the platforms
00877 void Blockhead::platform_check(Pos pos)
00878 {
00879     _d = on_hoz_check(pos);                                     //checks if blochead on horizontal (hoz) moving platform
00880     _r = by_hoz_check_right(pos);                               //checks if blockhead by hoz moving platform
00881     _l = by_hoz_check_left(pos);
00882     _d2 = on_hoz_2_check(pos);                                  //checks if blockhead on second horizontal moving platform
00883     _r2 = by_hoz_2_check_right(pos);
00884     _l2 = by_hoz_2_check_left(pos);
00885     _d3 = on_ver_check(pos);                                    //checks if blockhead on vertical moving platform
00886     _d4 = on_ver_2_check(pos);                                  //checks if blockhead on second vertical moving platform
00887 }
00888 
00889 
00890 //moves blockhead according to what platform he is on
00891 void Blockhead::on_platform(N5110 &lcd)                                 //if on platform, move blockhead in the  direction the platform is moving
00892 {
00893 
00894     if (_d == 0 || _r == 1 || _d2 == 0 || _r2 == 1) {                   //move blockhead right if on/by a horizontal platform moving right
00895         move_right(lcd, 1);
00896     } else if (_d == 1 || _l == 1 || _d2 == 1 || _l2 == 1) {            //move blockhead left if on/by a horizontal platform moving left
00897         move_left(lcd, 1);
00898     } else if (_d3 == 4 || _d4 == 4) {                                  //move blockhead up if on a vertical moving platform
00899         move_up(lcd, 1);
00900     }
00901 
00902 }
00903 
00904