Joe Body 201215898

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Eng.cpp Source File

Eng.cpp

00001 
00002 #include "Eng.h"
00003 Eng::Eng()
00004 {
00005 
00006 }
00007 
00008 Eng::~Eng()
00009 {
00010 
00011 }
00012 // objects
00013 //Gamepad pad;
00014 //N5110 lcd;
00015 Aim aim;
00016 Heston heston;
00017 Pup pup;
00018 Spikes spikes;
00019 
00020 
00021 void Eng::init()
00022 {
00023     
00024     aim.init();
00025     heston.init();
00026     pup.init();
00027     spikes.init();
00028     pupon = 0;
00029     _tok = 0;
00030     flag = true;
00031     spike_f =false;
00032 
00033 
00034 }
00035 
00036 //void Eng::read_input(Gamepad &pad);
00037 void Eng::update(Gamepad &pad, int fire, N5110 &lcd)
00038 {
00039         int shot = fire;
00040         if (shot == 1){
00041             aim.draw(lcd, fire);
00042             lcd.refresh();
00043             check_hit(pad, lcd);   
00044         }
00045         
00046         int c_score = heston.checkscore();
00047         if (c_score%5 == 0 && flag == false){
00048             flag = true;
00049             powerup(lcd, pad);
00050         }
00051         if (c_score%5 == 1){
00052             flag = false;
00053         }
00054         
00055         int strikes = heston.checkstrike();
00056             
00057         
00058         heston.update(pad);
00059         aim.update(pad);
00060         
00061         if (spike_f == true){
00062             //spikes.update();
00063             _s1.update();
00064             _s2.update();
00065             check_s_hit(pad);
00066         }
00067 }
00068 
00069 void Eng::draw(N5110 &lcd)
00070 {
00071     heston.draw(lcd);
00072     aim.draw(lcd, 0);
00073     if (pupon == 1){
00074         pup.draw(lcd, 0);
00075     }
00076     if (spike_f == true){
00077         _s1.draw(lcd);
00078         _s2.draw(lcd);
00079     }
00080     
00081 }
00082 
00083 void Eng::check_hit(Gamepad &pad, N5110 &lcd)
00084 {
00085     Vector2D aim_pos = aim.get_pos();
00086     Vector2D h1_pos = heston.get_pos();
00087     Vector2D pup_pos = pup.get_pos();
00088     
00089     if (
00090         (aim_pos.y >= h1_pos.y) && //top
00091         (aim_pos.y <= h1_pos.y + 12) && //bottom
00092         (aim_pos.x >= h1_pos.x) && //left
00093         (aim_pos.x <= h1_pos.x + 11)  //right
00094     ) {
00095         heston.hit(pad);
00096         print_score(lcd);
00097         lcd.refresh();
00098     }
00099     else if (
00100         (pupon == 1) &&
00101         (aim_pos.y >= pup_pos.y) && //top
00102         (aim_pos.y <= pup_pos.y + 8) && //bottom
00103         (aim_pos.x >= pup_pos.x) && //left
00104         (aim_pos.x <= pup_pos.x + 8)  //right
00105     ) {
00106         pupon = 0;
00107         heston.set_speed(0);
00108         pad.led(2,1);
00109         pad.led(5,1);
00110     }
00111     else {
00112         heston.miss(pad);
00113         print_strike(lcd);
00114         lcd.refresh();
00115         }
00116 wait(0.4);
00117 pad.leds_off();
00118 }
00119 
00120 void Eng::powerup(N5110 &lcd, Gamepad &pad)
00121 {
00122     Vector2D aim_pos = aim.get_pos();
00123     int x = aim_pos.x;
00124     int y = aim_pos.y;
00125     pup.position(x, y);
00126     pupon = 1;
00127 }
00128 
00129 void Eng::tik()
00130 {
00131     _tok = _tok + 1;
00132     int rem = _tok%50;
00133     if (rem == 0){
00134         heston.set_speed(1);
00135         spike_f = true;
00136         _s1.position(1);
00137         _s2.position(2);
00138     }
00139 }
00140 
00141 void Eng::print_strike(N5110 &lcd)
00142 {
00143     // get scores from paddles
00144     int g_score = heston.checkstrike();
00145  
00146     // print to LCD i
00147     char buffer1[14];
00148     sprintf(buffer1,"%2d",g_score);
00149     lcd.printString(buffer1,1,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
00150 }   
00151 
00152 void Eng::print_score(N5110 &lcd)
00153 {
00154     // get scores from paddles
00155     int g_score = heston.checkscore();
00156  
00157     // print to LCD i
00158     char buffer1[14];
00159     sprintf(buffer1,"%2d",g_score);
00160     lcd.printString(buffer1,1,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
00161 }   
00162 
00163 void Eng::check_s_hit(Gamepad &pad){
00164     Vector2D s1_pos = _s1.get_pos();
00165     Vector2D s2_pos = _s2.get_pos();
00166     Vector2D aim_pos = aim.get_pos();
00167     
00168     if (
00169         (aim_pos.y >= s1_pos.y) && //top
00170         (aim_pos.y <= s1_pos.y + 4) && //bottom
00171         (aim_pos.x >= s1_pos.x) && //left
00172         (aim_pos.x <= s1_pos.x + 4)  //right
00173         ) {
00174          spike_f = false;
00175          spikes.hit(pad);
00176     }
00177     if (
00178         (aim_pos.y >= s2_pos.y) && //top
00179         (aim_pos.y <= s2_pos.y + 4) && //bottom
00180         (aim_pos.x >= s2_pos.x) && //left
00181         (aim_pos.x <= s2_pos.x + 4)  //right
00182         ) {
00183          spike_f = false;
00184          spikes.hit(pad);
00185     }
00186          
00187          
00188 }