Haoyan Zhang / Mbed 2 deprecated el17h2z1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StarcraftEngine.cpp Source File

StarcraftEngine.cpp

00001 #include "StarcraftEngine.h"
00002 
00003 StarcraftEngine::StarcraftEngine()
00004 {
00005     
00006 }
00007 
00008 StarcraftEngine::~StarcraftEngine()
00009 {
00010     
00011 }
00012 
00013 void StarcraftEngine::init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width,int Boss_height, int Boss_width, int Acid_height, int Acid_width, int speed)
00014 {
00015      // initialise the game parameters
00016      _Battleship_height = Battleship_height;
00017      _Battleship_width = Battleship_width;
00018      _Laser_height = Laser_height;
00019      _Laser_width = Laser_width;
00020      _Swarm_height = Swarm_height;
00021      _Swarm_width = Swarm_width;
00022      _Boss_height = Boss_height;
00023      _Boss_width = Boss_width;
00024      _Acid_height = Acid_height;
00025      _Acid_width = Acid_width;
00026      _speed = speed;
00027      
00028      // x position on screen - WIDTH is defined in N5110.h
00029      _Battleshipx = GAP;
00030      
00031      // Initializing Battleship,Laser and Swarm
00032      _Battleship.init(_Battleshipx, _Battleship_height, _Battleship_width);
00033      _Laser.init(_Laser_height, _Laser_width, _speed);
00034      _Swarm.init(_Swarm_height, _Swarm_width, _speed);
00035      _Boss.init(_Boss_height, _Boss_width, _speed);
00036      _Acid.init(_Acid_height, _Acid_width, _speed);
00037      
00038 }
00039 
00040 void StarcraftEngine::read_input(Gamepad &pad)
00041 {
00042     _d = pad.get_direction();
00043     _mag = pad.get_mag();
00044 }     
00045  
00046 int StarcraftEngine::find_life()  //get battleship's life
00047 {   int find_life = _Battleship.get_life();
00048     return find_life;
00049 } 
00050 
00051 int StarcraftEngine::find_score()  //get player's score
00052 {   int find_score = _Battleship.get_score();
00053     return find_score;
00054 } 
00055  
00056 void StarcraftEngine::draw(N5110 &lcd)
00057 {
00058     // draw the elements in the LCD buffer
00059     // pitch
00060     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
00061     //score
00062     print_scores(lcd);
00063     // Battleship
00064     _Battleship.draw(lcd);
00065     // Swarm
00066     _Swarm.draw(lcd);
00067     // Boss
00068     _Boss.draw(lcd);
00069     // Laser
00070     _Laser.draw(lcd); 
00071     // Acid
00072     _Acid.draw(lcd);
00073     
00074 }
00075 
00076 void StarcraftEngine::update(Gamepad &pad)
00077 {
00078      check_goal(pad);
00079      
00080      // important to update battleship, laser,boss and swarm before checking collisions so can
00081      // correct for it before updating the display
00082      
00083      _Battleship.update(_d,_mag);
00084      _Swarm.update();
00085      _Boss.update();
00086      _Laser.update();
00087      _Acid.update();
00088      
00089      check_Swarm_collisions(pad);
00090      check_Boss_collisions(pad);
00091      check_Battleship_collisions(pad);
00092 }
00093 
00094 void StarcraftEngine::check_Swarm_collisions(Gamepad &pad)  //check if the swarm over the bottom line and minus battleship life
00095 {
00096      // read current swarm attributes
00097      Vector2D Swarm_pos = _Swarm.get_pos();
00098      Vector2D Swarm_velocity = _Swarm.get_velocity(); 
00099 
00100      // check if swarm over the battleship
00101      if (
00102           (Swarm_pos.y >= HEIGHT - 1) 
00103         )   {                 
00104          _Battleship.minus_life();
00105          Swarm_pos.x = rand() % 64;
00106          Swarm_pos.y = 2;
00107          pad.tone(800.0,0.1);  //  Audio feedback 
00108      }
00109      
00110      
00111      // write new attributes
00112     _Swarm.set_velocity(Swarm_velocity);
00113     _Swarm.set_pos(Swarm_pos);
00114 } 
00115 
00116 void StarcraftEngine::check_Boss_collisions(Gamepad &pad)  //check if the boss over the bottom line and minus battleship life
00117 {
00118      // read current Boss attributes
00119      Vector2D Boss_pos = _Boss.get_pos();
00120      Vector2D Boss_velocity = _Boss.get_velocity(); 
00121 
00122      // check if Boss over the battleship
00123      if (
00124           (Boss_pos.y >= HEIGHT - 1) 
00125         )   {                 
00126          _Battleship.minus_life();
00127          Boss_pos.x = rand() % 65;
00128          Boss_pos.y = 2;
00129          pad.tone(800.0,0.1);  //  Audio feedback 
00130      }
00131      
00132      
00133      // write new attributes
00134     _Boss.set_velocity(Boss_velocity);
00135     _Boss.set_pos(Boss_pos);
00136 } 
00137 
00138 void StarcraftEngine::check_Battleship_collisions(Gamepad &pad)  //check if the acid hit the battleship and minus life   
00139 {
00140      Vector2D Boss_pos = _Boss.get_pos();
00141      Vector2D Battleship_pos = _Battleship.get_pos();  
00142      Vector2D Acid_pos = _Acid.get_pos();
00143      
00144      Vector2D Acid_velocity = _Acid.get_velocity();
00145      Vector2D Boss_velocity = _Boss.get_velocity();
00146      
00147      // Player get score
00148      if ((Acid_pos.x >= Battleship_pos.x)&&
00149          (Acid_pos.x <= Battleship_pos.x + 6)&&
00150          (Acid_pos.y + 3 >= Battleship_pos.y)&&
00151          (Acid_pos.y <= Battleship_pos.y + 4))
00152      {
00153          _Battleship.minus_life();
00154          Acid_pos.x = Boss_pos.x + 3;
00155          Acid_pos.y = Boss_pos.y + 5; 
00156          pad.tone(800.0,0.1);  //  Audio feedback 
00157      }
00158      
00159       
00160      if ((Acid_pos.y > HEIGHT - 3)||(Acid_pos.y < 10))  //if acid over the bottom line, initialize acid's position
00161      {
00162         Acid_pos.x = Boss_pos.x + 3;
00163         Acid_pos.y = Boss_pos.y + 5; 
00164      }
00165      
00166       _Acid.set_velocity(Acid_velocity);
00167       _Boss.set_velocity(Boss_velocity);
00168       _Acid.set_pos(Acid_pos);   
00169       _Boss.set_pos(Boss_pos);  
00170 }                  
00171 
00172 void StarcraftEngine::check_goal(Gamepad &pad)  //check if the laser hit the swarn or boss and add score    
00173 {
00174      Vector2D Swarm_pos = _Swarm.get_pos();
00175      Vector2D Boss_pos = _Boss.get_pos();
00176      Vector2D Battleship_pos = _Battleship.get_pos();  
00177      Vector2D Laser_pos = _Laser.get_pos();
00178      
00179      Vector2D Laser_velocity = _Laser.get_velocity();
00180      Vector2D Swarm_velocity = _Swarm.get_velocity();
00181      Vector2D Boss_velocity = _Boss.get_velocity();
00182      
00183      // Player get score
00184      if ((Laser_pos.x >= Swarm_pos.x)&&
00185          (Laser_pos.x <= Swarm_pos.x + 6)&&
00186          (Laser_pos.y <= Swarm_pos.y + 6)&&
00187          (Laser_pos.y >= Swarm_pos.y))
00188      {
00189          _Battleship.add_score();
00190          Laser_pos.x = Battleship_pos.x + 3;    
00191          Laser_pos.y = Battleship_pos.y - 1; 
00192          Swarm_pos.x = rand() % 64;
00193          Swarm_pos.y = 2;
00194          
00195          pad.tone(800.0,0.25);
00196          wait(0.1);
00197      }
00198      
00199      if ((Laser_pos.x >= Boss_pos.x)&&
00200          (Laser_pos.x <= Boss_pos.x + 6)&&
00201          (Laser_pos.y <= Boss_pos.y + 4)&&
00202          (Laser_pos.y >= Boss_pos.y))
00203      {
00204          _Battleship.add_score();
00205          Laser_pos.x = Battleship_pos.x + 3;    
00206          Laser_pos.y = Battleship_pos.y - 1; 
00207          Boss_pos.x = rand() % 64;
00208          Boss_pos.y = 2;
00209          
00210          pad.tone(800.0,0.25);
00211          wait(0.1);
00212      }
00213       
00214      if ((Laser_pos.y < 6)||(Battleship_pos.y - Laser_pos.y >= 20))  //set the laser's range and initialize laser's position
00215      {
00216             Laser_pos.x = Battleship_pos.x + 3;    
00217             Laser_pos.y = Battleship_pos.y - 1;  
00218      }
00219      
00220       _Laser.set_velocity(Laser_velocity);
00221       _Swarm.set_velocity(Swarm_velocity);
00222       _Boss.set_velocity(Boss_velocity);
00223       _Laser.set_pos(Laser_pos); 
00224       _Swarm.set_pos(Swarm_pos);    
00225       _Boss.set_pos(Boss_pos);  
00226 }              
00227        
00228 void StarcraftEngine::print_scores(N5110 &lcd)
00229 {
00230      // get scores from battleship
00231      int Battleship_score = _Battleship.get_score();
00232      int Battleship_life = _Battleship.get_life();
00233      
00234      // print to LCD 
00235      char buffer1[14];
00236      sprintf(buffer1,"%2d",Battleship_score);
00237      lcd.printString("S",5,1);
00238      lcd.printString(buffer1,10,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
00239      
00240      char buffer2[14];
00241     sprintf(buffer2,"%2d",Battleship_life);
00242     lcd.printString("L",WIDTH - 25,1);
00243     lcd.printString(buffer2,WIDTH - 20,1);
00244 } 
00245  
00246  
00247  
00248      
00249