ELEC2645 (2018/19) / Mbed 2 deprecated henririgby98

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SpaceRebEngine.cpp Source File

SpaceRebEngine.cpp

00001 #include "SpaceRebEngine.h"
00002 
00003 SpaceRebEngine::SpaceRebEngine()
00004 {
00005 
00006 }
00007 
00008 SpaceRebEngine::~SpaceRebEngine()
00009 {
00010 
00011 }
00012 
00013 void SpaceRebEngine::init(int spaceinvader_width,int spaceinvader_height,int missiles_size,int speed)
00014 {
00015     _spaceinvader_width = spaceinvader_width;
00016     _spaceinvader_height = spaceinvader_height;
00017     _missiles_size = missiles_size;
00018     _speed = speed;
00019     _player.init(_spaceinvader_height,_spaceinvader_width);  //sets initial values of spaceinvader
00020     _missiles.init(_missiles_size);  //sets initial values of missilles
00021     _missiles.set_score();  //sets score of game in missiles.cpp
00022     _end = false;  //ensures _end = false for each time the game is reset
00023 }
00024 
00025 void SpaceRebEngine::read_input(Gamepad &pad)
00026 {
00027     _d = pad.get_direction();  //sets _d by takeing the direction from the joystick
00028     _mag = pad.get_mag();  //sets magnitude by takeing the magnitude from the joystick
00029 }
00030 
00031 void SpaceRebEngine::draw(N5110 &lcd)
00032 {
00033     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);  // draw the play area
00034     _player.draw(lcd);  //draw the spaceinvader
00035     _missiles.draw(lcd);  // draw the missiles
00036 }
00037 
00038 void SpaceRebEngine::update(Gamepad &pad)
00039 {
00040     _player.update(_d,_mag);  //updates the movement of the spaceinvader
00041     _missiles.update();  //updates position of missiles
00042     
00043     check_spaceinvader_collision(pad);  //checks to see if spaceinvader has been struck by missiles
00044     check_wall_collision(pad);  //checks to see if missiles has collided with the play area wall
00045 }
00046 
00047 void SpaceRebEngine::check_wall_collision(Gamepad &pad)
00048 {   
00049     Vector2D missiles_pos = _missiles.get_pos();
00050     Vector2D missiles_velocity = _missiles.get_velocity();
00051     if (missiles_pos.y <= 1) {  
00052         missiles_pos.y = 1;  //ensures missiles do not go beyond the y-axis play area at the top
00053         missiles_velocity.y = -missiles_velocity.y *1.1;  //used to bounce and increase the missiles_velocity.y with each bounce on the wall
00054         _missiles.add_score();  //incrases score by 1 each time that the missiles hits the wall
00055         if  (missiles_velocity.y >= 8){
00056         missiles_velocity.y = 8;  //sets a maximum missiles_velocity.y so missiles dont go 'supersonic'
00057         }
00058     } else if (missiles_pos.y + _missiles_size >= (HEIGHT-1) ) { 
00059         missiles_pos.y = (HEIGHT-1) - _missiles_size;  //ensures missiles do not go beyond the y-axis play area at the bottom
00060         missiles_velocity.y = -missiles_velocity.y * 1.1;
00061         _missiles.add_score();
00062         if  (missiles_velocity.y >= 8){
00063         missiles_velocity.y = 8;
00064         }
00065     } else if (missiles_pos.x + _missiles_size >= (WIDTH-1) ) {
00066         missiles_pos.x = (WIDTH-1) - _missiles_size;  //ensures missiles do not go beyond the x-axis play area to the right
00067         missiles_velocity.x = -missiles_velocity.x * 1.1 ; //used to bounce and increase the missiles_velocity.x with each bounce on the wall
00068         _missiles.add_score();
00069         if  (missiles_velocity.x >= 8){
00070         missiles_velocity.x = 8;  //sets a maximum missiles_velocity.x so missiles dont go 'supersonic'
00071         }
00072     } else if (missiles_pos.x <= 1) {
00073         missiles_pos.x = 1;  //ensures missiles do not go beyond the x-axis play area to the left
00074         missiles_velocity.x = -missiles_velocity.x * 1.1;
00075         _missiles.add_score();
00076         if  (missiles_velocity.x >= 8){
00077         missiles_velocity.x = 8;
00078         }
00079     }
00080     _missiles.set_velocity(missiles_velocity);  //used to set the velocity of the missiles (velocity.y & velocity.x)
00081     _missiles.set_pos(missiles_pos);  //used to set the position of the missiles (_y & _x)
00082 }
00083 
00084 void SpaceRebEngine::check_spaceinvader_collision(Gamepad &pad)
00085 {
00086     Vector2D missiles_pos = _missiles.get_pos();  //gets the position of missiles
00087     Vector2D missiles_velocity = _missiles.get_velocity();  //gets the velocity of the missiles
00088     Vector2D player_pos = _player.get_pos();  //gets the position of the spaceinvader
00089     if (
00090         (missiles_pos.y >= player_pos.y) &&
00091         (missiles_pos.y <= player_pos.y + _spaceinvader_height) && 
00092         (missiles_pos.x >= player_pos.x) &&
00093         (missiles_pos.x <= player_pos.x + _spaceinvader_width)  //checks if there is a collision between spaceinvader & missiles
00094     ) {
00095         pad.tone(1500.0,0.1);  //beep when spaceinvader collides with missiles
00096         _end = true;  //sets value to true and does this to end the play mechanic
00097     }
00098 }
00099 
00100 void SpaceRebEngine::print_scores(N5110 &lcd)
00101 {
00102     _score = _missiles.get_score();  //gets the game score from missiles
00103     char buffer1[14];
00104     sprintf(buffer1,"%2d",_score);
00105     lcd.printString(buffer1,WIDTH - 23,2);  //prints the score on the lcd display
00106 }
00107 
00108 bool SpaceRebEngine::game_end()
00109 {
00110  bool end = _end;
00111  return end;  //returns boolean value of end which is used to end play mechanic
00112  }
00113