ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Revision:
33:7a814c874c57
Parent:
32:098fbc1222cd
Child:
34:6d0786582d81
diff -r 098fbc1222cd -r 7a814c874c57 RosenEngine/RosenEngine.cpp
--- a/RosenEngine/RosenEngine.cpp	Sat Apr 20 09:05:10 2019 +0000
+++ b/RosenEngine/RosenEngine.cpp	Sat Apr 20 14:10:47 2019 +0000
@@ -18,7 +18,7 @@
     // initialise the game parameters
     _ship.init(ship_width,ship_height,ship_speed,ship_xpos,ship_ypos);
     // place seeker above the ship
-    _enemy.init(48,0,3);
+    _enemy.init(48,0,2);
     _menu.init(16);
     _health.init(_shipno);
 
@@ -43,8 +43,8 @@
     _health.draw_health(lcd,_shipno);
     _health.draw_shields(lcd);
     _enemy.draw_seeker(lcd);
-    _enemy.draw_shooter(lcd,3);
-    _enemy.draw_shw(lcd,pad,3);
+    _enemy.draw_shooter(lcd,2);
+    _enemy.draw_shw(lcd,pad,2);
     if(_shipno == 0) {
         _ship.set_dimensions(9,6);
         _ship.draw_ship(lcd,_shipno);
@@ -79,12 +79,12 @@
         _ship.update_ship(_xjoystick,_yjoystick);
         _weapons.update();
     }
-    if(shooter_weapon_collision(3) == true){
+    if(shooter_weapon_collision(3) == true) {
         _health.update(1,pad);
         pad.tone(500,0.05);
         wait(0.05);
-        }
-    if(enemy_ship_collision() == true) {
+    }
+    if(seeker_ship_collision() == true) {
         _health.update(1,pad);
         _enemy.reset_seeker();
         pad.tone(500,0.05);
@@ -98,7 +98,7 @@
         wait(0.05);
     }
     // printf("collision check complete");
-    
+
     // warning sound
     Vector2D hp = _health.get_hp();
     int health = hp.x;
@@ -107,8 +107,7 @@
             pad.tone(1300,0.05);
             wait(0.05);
         }
-    }
-    else if(_shipno == 2) {
+    } else if(_shipno == 2) {
         if(health <= 1 ) {
             pad.tone(1300,0.05);
             wait(0.05);
@@ -153,7 +152,7 @@
     _menu.update(_d);
     _menu.disp_ships(lcd);
 }
-bool RosenEngine::enemy_ship_collision()
+bool RosenEngine::seeker_ship_collision()
 {
     // Vector2D ship_pos = _ship.get_pos();
     Vector2D seeker_pos = _enemy.get_seekerpos();
@@ -198,6 +197,137 @@
         return false;
     }
 }
+Vector2D RosenEngine::shooter_projectile_collision()
+{
+    Vector2D shooter1_pos = _enemy.get_sh1pos();
+    Vector2D shooter2_pos = _enemy.get_sh2pos();
+    Vector2D shooter3_pos = _enemy.get_sh3pos();
+    int shooter1_x[11],shooter1_y[10],shooter2_x[11],shooter2_y[10],shooter3_x[11],shooter3_y[10];
+    int wx1col = 0;
+    int wx2col = 0;
+    int wx3col = 0;
+    int wy1col = 0;
+    int wy2col = 0;
+    int wy3col = 0;
+    
+
+    // get shooters x any y values and put into arrays
+    for(int cx = 0; cx<=11; cx=cx+1) {
+        shooter1_x[cx]= shooter1_pos.x + cx;
+    }
+    for(int cy = 0; cy<=10; cy=cy+1) {
+        shooter1_y[cy]= shooter1_pos.y + cy;
+    }
+    for(int cx = 0; cx<=11; cx=cx+1) {
+        shooter2_x[cx]= shooter2_pos.x + cx;
+    }
+    for(int cy = 0; cy<=10; cy=cy+1) {
+        shooter2_y[cy]= shooter2_pos.y + cy;
+    }
+    for(int cx = 0; cx<=11; cx=cx+1) {
+        shooter3_x[cx]= shooter3_pos.x + cx;
+    }
+    for(int cy = 0; cy<=10; cy=cy+1) {
+        shooter3_y[cy]= shooter3_pos.y + cy;
+    }
+
+    if(_shipno == 0) {
+        Vector2D missle_pos = _weapons.get_pos(_shipno);
+
+        for(int cx = 0; cx<=11; cx=cx+1) {
+            if(shooter1_x[cx] == missle_pos.x) {
+                wx1col = 1;
+            }
+            if(shooter2_x[cx] == missle_pos.x) {
+                wx2col = 1;
+            }
+            if(shooter3_x[cx] == missle_pos.x) {
+                wx3col = 1;
+            }
+        }
+
+        for(int cy = 0; cy<=10; cy=cy+1) {
+            if(shooter1_y[cy] == missle_pos.y + 4) {
+                wy1col = 1;
+            }
+            if(shooter2_y[cy] == missle_pos.y + 4) {
+                wy2col = 1;
+            }
+            if(shooter3_y[cy] == missle_pos.y + 4) {
+                wy3col = 1;
+            }
+        }
+        if(wx1col == 1 && wy1col == 1) {
+            _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
+            return {1,1};
+        } else if(wx2col == 1 && wy2col == 1) {
+            _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
+            return {1,2};
+        } else if(wx3col == 1 && wy3col == 1) {
+            _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
+            return {1,3};
+        } else {
+            return {0,0};
+        }
+    }
+
+
+    // imperion lazer
+    if(_shipno == 1) {
+        for(int cx = 0; cx<=3; cx=cx+1) {
+            lazer_x[cx] = (ship_xpos + 2) + cx;
+        }
+        // ony register collision if seeker is above ship
+        if(shooter1_pos.y + 6 < ship_ypos) {
+            // check all values of ship position against all values of seekers x position
+            for(int nx = 0; nx<=3; nx=nx+1) {
+                for(int mx = 0; mx<=11; mx=mx+1) {
+                    if(lazer_x[nx] == shooter1_x[mx]) {
+                        wx1col = 1;
+                        printf("lazer_x = %f,seeker_x = %d\n",lazer_x[nx],seeker_x[mx]);
+                    }
+                }
+            }
+        }
+        if(shooter2_pos.y + 6 < ship_ypos) {
+            // check all values of ship position against all values of seekers x position
+            for(int nx = 0; nx<=3; nx=nx+1) {
+                for(int mx = 0; mx<=11; mx=mx+1) {
+                    if(lazer_x[nx] == shooter2_x[mx]) {
+                        wx2col = 1;
+                        printf("lazer_x = %f,seeker_x = %d\n",lazer_x[nx],seeker_x[mx]);
+                    }
+                }
+            }
+        }
+        if(shooter3_pos.y + 6 < ship_ypos) {
+            // check all values of ship position against all values of seekers x position
+            for(int nx = 0; nx<=3; nx=nx+1) {
+                for(int mx = 0; mx<=11; mx=mx+1) {
+                    if(lazer_x[nx] == shooter3_x[mx]) {
+                        wx3col = 1;
+                        printf("lazer_x = %f,seeker_x = %d\n",lazer_x[nx],seeker_x[mx]);
+                    }
+                }
+            }
+        }
+        if(wx1col == 1 && A == true) {
+            return {1,1};
+        else if(wx2col == 1 && A == true){
+            return {1,2};
+            }
+        else if(wx2col == 1 && A == true){
+            return {1,3};
+            }
+        else{
+            return {0,0};
+            }
+    }
+    if(_shipno == 2) {
+        return {0,0};
+    }
+
+}
 bool RosenEngine::enemy_projectile_collision()
 {
     Vector2D seeker_pos = _enemy.get_seekerpos();
@@ -250,7 +380,7 @@
                 for(int mx = 0; mx<=9; mx=mx+1) {
                     if(lazer_x[nx] == seeker_x[mx]) {
                         wxcol = 1;
-                        printf("lazer_x = %d,seeker_x = %f\n");
+                        printf("lazer_x = %d,seeker_x = %f\n",lazer_x[nx],seeker_x[mx]);
                     }
                 }
             }
@@ -270,59 +400,59 @@
     Vector2D shw1_pos = _enemy.get_sh1pos();
     Vector2D shw2_pos = _enemy.get_sh2pos();
     Vector2D shw3_pos = _enemy.get_sh3pos();
-    
+
     bool shw1xcol = false;
     bool shw1ycol = false;
     bool shw2xcol = false;
     bool shw2ycol = false;
     bool shw3xcol = false;
     bool shw3ycol = false;
-    
+
     int ship_x[9];
     int ship_y[6];
-    
+
     for(int cx = 0; cx<=9; cx=cx+1) {
         ship_x[cx] = ship_xpos + cx;
-        if(ship_x[cx] == shw1_pos.x){
+        if(ship_x[cx] == shw1_pos.x) {
             shw1xcol = true;
-        } 
-        if(ship_x[cx] == shw2_pos.x){
+        }
+        if(ship_x[cx] == shw2_pos.x) {
             shw2xcol = true;
-        } 
-        if(ship_x[cx] == shw3_pos.x){
+        }
+        if(ship_x[cx] == shw3_pos.x) {
             shw3xcol = true;
-        } 
+        }
     }
     for(int cy = 0; cy<=6; cy=cy+1) {
         ship_y[cy] = (ship_ypos ) + cy ;
-        if(ship_y[cy] == shw1_pos.y){
+        if(ship_y[cy] == shw1_pos.y) {
             shw1ycol = true;
         }
-        if(ship_y[cy] == shw2_pos.y){
+        if(ship_y[cy] == shw2_pos.y) {
             shw2ycol = true;
-        } 
-        if(ship_y[cy] == shw3_pos.y){
+        }
+        if(ship_y[cy] == shw3_pos.y) {
             shw3ycol = true;
-        } 
+        }
     }
-    if(sh_no == 1){
-        shw2xcol = false; 
+    if(sh_no == 1) {
+        shw2xcol = false;
         shw2ycol = false;
-        shw3xcol = false; 
+        shw3xcol = false;
         shw3ycol = false;
-        }  
-    if(sh_no == 2){
-        shw3xcol = false; 
+    }
+    if(sh_no == 2) {
+        shw3xcol = false;
         shw3ycol = false;
-        }
-    
-    if(shw1xcol && shw1ycol == true || shw2xcol && shw2ycol == true || shw3xcol && shw3ycol == true){
+    }
+
+    if(shw1xcol && shw1ycol == true || shw2xcol && shw2ycol == true || shw3xcol && shw3ycol == true) {
         return true;
-        }
-    else{
+    } else {
         return false;
-        }
+    }
 }
-void RosenEngine::score(int points){
+void RosenEngine::score(int points)
+{
     _score = _score + points;
 }
\ No newline at end of file