Yudong Xiao / Mbed OS pokemon

Dependencies:   Tone

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PokeEngine.cpp Source File

PokeEngine.cpp

00001 
00002 #include "PokeEngine.h"
00003 
00004 PokeEngine::PokeEngine(){ _lives = 4; }    
00005 
00006 void PokeEngine::init(int ball_x, int ball_y, int ball_radius, int pokemon_x, int pokemon_y, int pokemon_size, int speed){
00007     _pokeball.init(ball_x, ball_y, ball_radius);
00008     _pokemon.init(pokemon_x, pokemon_y, pokemon_size, speed);
00009     _score = 0;
00010 }
00011 
00012 void PokeEngine::init_rocket(int x, int y, int size, int speed){
00013     _rocket.init(x,y,size, speed);    
00014 }
00015 
00016 int PokeEngine::update(UserInput input){
00017     _pokeball.update(input);
00018     Position2D ball_pos = _pokeball.get_pos();
00019     _pokemon.update();
00020     _rocket.update(ball_pos);
00021    
00022     check_wall_collision();
00023     check_catching_collision();
00024     check_crash();
00025     return _lives;
00026 }
00027 
00028 void PokeEngine::draw(N5110 &lcd) {
00029     //printf("Pong Engine: Draw\n");
00030     // draw the elements in the LCD buffer
00031     // pitch
00032     lcd.drawLine(0,0,WIDTH-1,0,1);  // top
00033     lcd.drawLine(WIDTH-1,0,WIDTH-1,HEIGHT-1,1);  // back wall
00034     lcd.drawLine(0,HEIGHT-1,WIDTH-1,HEIGHT-1,1); // bottom
00035     lcd.drawLine(0,0,0,HEIGHT-1,1);
00036     _pokeball.draw(lcd);
00037     _pokemon.draw(lcd);
00038     _rocket.draw(lcd);
00039 }
00040 
00041 void PokeEngine::check_wall_collision() {
00042     //printf("Pong Engine: Check Wall Collision\n");
00043     // read current ball attributes
00044     Position2D pokemon_pos = _pokemon.get_pos();
00045     Position2D pokemon_velocity = _pokemon.get_velocity();
00046     int size = _pokemon.get_size();
00047 
00048     // check if hit top wall
00049     if (pokemon_pos.y <= 1) {  //  1 due to 1 pixel boundary
00050         pokemon_pos.y = 1;  // bounce off ceiling without going off screen
00051         pokemon_velocity.y = -pokemon_velocity.y;  // flip velocity
00052     } else if (pokemon_pos.y + size >= (HEIGHT-1) ) {
00053         // hit bottom
00054         pokemon_pos.y = (HEIGHT-1) - size;  // stops ball going off screen
00055         pokemon_velocity.y = -pokemon_velocity.y;    // flip velcoity 
00056     } else if (pokemon_pos.x + size >= (WIDTH-1) ) {
00057         // hit right wall
00058         pokemon_pos.x = (WIDTH-1) - size;  // stops ball going off screen
00059         pokemon_velocity.x = -pokemon_velocity.x;    // flip velcoity 
00060     } else if (pokemon_pos.x <= 1) {  //  1 due to 1 pixel boundary
00061         pokemon_pos.x = 1;  // bounce off ceiling without going off screen
00062         pokemon_velocity.x = -pokemon_velocity.x;  // flip velocity
00063     }
00064 
00065     // update ball parameters
00066     _pokemon.set_velocity(pokemon_velocity);
00067     _pokemon.set_pos(pokemon_pos);
00068 }
00069 
00070 void PokeEngine::check_catching_collision() {
00071     Position2D pokemon_pos = _pokemon.get_pos();
00072     Position2D pokeball_pos = _pokeball.get_pos();  // paddle
00073 
00074     // see if ball has hit the paddle by checking for overlaps
00075     if (
00076         (pokemon_pos.y >= pokeball_pos.y - _pokeball.get_radius()) && //top
00077         (pokemon_pos.y <= pokeball_pos.y + _pokeball.get_radius() ) && //bottom
00078         (pokemon_pos.x >= pokeball_pos.x - _pokeball.get_radius()) && //left
00079         (pokemon_pos.x <= pokeball_pos.x + _pokeball.get_radius() )  //right
00080     ) {
00081         // if it has,the pokemon is caught.
00082         _pokemon.pokemon_caught();
00083         _score++;
00084     }
00085 }
00086 
00087 
00088 void PokeEngine::check_crash() {
00089     int left_lives = _lives;
00090     Position2D rocket_pos = _rocket.get_pos();
00091     int rocket_size = _rocket.get_size();
00092     Position2D pokeball_pos = _pokeball.get_pos();  // paddle
00093 
00094      // see if ball has hit the paddle by checking for overlaps
00095     if (
00096         (rocket_pos.y >= pokeball_pos.y - _pokeball.get_radius()) && //top
00097         (rocket_pos.y <= pokeball_pos.y + _pokeball.get_radius() ) && //bottom
00098         (rocket_pos.x >= pokeball_pos.x - _pokeball.get_radius()) && //left
00099         (rocket_pos.x <= pokeball_pos.x + _pokeball.get_radius() )  //right
00100     ) {
00101         // if it has,the pokemon is caught.
00102         _rocket.rocket_crash();
00103         _lives--;
00104         }
00105 }
00106 
00107 
00108     int PokeEngine::get_lives() { return _lives;}
00109     
00110     int PokeEngine::get_score() { return _score; }
00111     
00112     void  PokeEngine::check_lives(int lives) { _lives = lives ;}
00113