Meteor defense project

Dependencies:   N5110 mbed

Revision:
24:d6187d39f09b
Parent:
23:6d197a452d7c
Child:
25:edd6a95607b1
--- a/Spawn/Spawn.cpp	Thu Apr 13 14:05:25 2017 +0000
+++ b/Spawn/Spawn.cpp	Thu Apr 13 17:05:45 2017 +0000
@@ -12,24 +12,24 @@
 void Spawn::init() {
     healthA = 10;
     healthC = 100;
-    healthD = 50;
+    healthDE = 9;
     spawnRateA = 32;
-    spawnRateB = 1;
-    spawnRateC = 2;
-    spawnRateD = 1;
+    spawnRateB = 5; //1
+    spawnRateC = 5; //2
+    spawnRateDE = 5; //1
 }
 void Spawn::randomizeSpawn(int Arr[][Rows], char cArr[][Rows]) {
-        //there is 3 kind of different spawn - a, b and c.
+        //there is 4 kind of different spawn - a, b, c and d/e.
         if (stopSpawnA == 1) { //spawn for this row
-            spawnA(Arr, cArr);
-            spawnC(Arr, cArr);
+            //spawnA(Arr, cArr);
             stopSpawnA= 0;
         } else { //stop spawn for the next row
             stopSpawnA = 1;
+            spawnC(Arr, cArr);
         }
         for ( int x = 0; x < Cols; x++) {
             spawnB(x, Arr, cArr);
-            spawnD(x, Arr, cArr);
+            spawnDE(x, Arr, cArr);
         }
         //printf("at x=2, y=0. value = %d \n",Arr1[2][0]);
 }
@@ -64,15 +64,15 @@
         }
     }
 }
-void Spawn::spawnD(int x, int Arr[][Rows], char cArr[][Rows]) {
-    bool TrueFalseD = (rand() % 201) < spawnRateD;
+void Spawn::spawnDE(int x, int Arr[][Rows], char cArr[][Rows]) {
+    bool TrueFalseD = (rand() % 201) < spawnRateDE;
     if (TrueFalseD) {
         bool LeftRightD = (rand() % 10) <5; //its a 50-50 chance 
         if (LeftRightD) {
-            Arr[x][0] = healthD;
+            Arr[x][0] = healthDE;
             cArr[x][0] = 'd'; //d denotes spawnD starts by moving left
         } else {
-            Arr[x][0] = healthD;
+            Arr[x][0] = healthDE;
             cArr[x][0] = 'e'; //e denotes spawnD starts by moving right          
         }
     }
@@ -91,35 +91,50 @@
     }
     printf("at x=2, y=47. number is =%d \n",Arr2[2][47]);  
 }
-void Spawn::moveSpawnD(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //spawnD moves in a zig zag behavior.
+void Spawn::moveSpawnDE(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //spawnD moves in a zig zag behavior.
     for (int x = 0; x < Cols; x++) {                                                               // moves other direction when hit 'L' or 'R' of spawnC and walls.
         for (int y = 0; y < Rows; y++) {                                                           //jumps over spawnA and spawnB
-            if (cArr[x][y] == 'd') { //d1 = move left & down
-                if (cArr[x-1][y+1] == 'a' || cArr[x-1][y+1] == 'b' ) { //if the path is blocked by spawnA or spawnB 
-                    cArr2[x-2][y+2] = 'd'; Arr2[x-2][y+2] = Arr[x][y]; //jumps over it
-                    cArr2[x][y] = '\0'; Arr2[x][y] = 0;
-                } else if (x == 0 || x == 83 || cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r') { //if it hits the wall or 'l' or 'r'.
-                    cArr2[x+1][y+1] = 'e'; Arr2[x+1][y+1] = Arr[x][y]; //start moving right & down
-                    cArr2[x][y] = '\0'; Arr2[x][y] = 0;
-                } else { //if nothing is blocking the path
-                    cArr2[x-1][y+1] = cArr[x][y]; Arr2[x-1][y+1] = Arr[x][y]; //move as usual.
-                    cArr2[x][y] = '\0'; Arr2[x][y] = 0;
-                }   
-            } else if ( cArr[x][y] == 'e') { //move right & down
-                if (cArr[x+1][y+1] == 'a' || cArr[x+1][y+1] == 'b' ) { //if the path is blocked by spawnA or spawnB 
-                    cArr2[x+2][y+2] = 'e'; Arr2[x+2][y+2] = Arr[x][y]; //jumps over it
-                    cArr2[x][y] = '\0'; Arr2[x][y] = 0;
-                } else if (x == 0 || x == 83 || cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r') { //if it hits the wall or 'l' or 'r'.
-                    cArr2[x-1][y+1] = 'd'; Arr2[x-1][y+1] = Arr[x][y]; //start moving left & down
-                    cArr2[x][y] = '\0'; Arr2[x][y] = 0;
-                } else { //if nothing is blocking the path
-                    cArr2[x+1][y+1] = cArr[x][y]; Arr2[x+1][y+1] = Arr[x][y]; //move as usual.
-                    cArr2[x][y] = '\0'; Arr2[x][y] = 0;
-                }
+            if (cArr[x][y] == 'd') { //d = move left & down
+                movementD(x, y, Arr, Arr2, cArr, cArr2);       
+            } else if ( cArr[x][y] == 'e') { //e = move right & down
+                movementE(x, y, Arr, Arr2, cArr, cArr2);
             }
         }
     }        
 }
+void Spawn::movementD(int x, int y, int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) {
+    if (cArr[x-1][y+1] == 'a' || cArr[x-1][y+1] == 'b' ) { //if the path is blocked by spawnA or spawnB 
+        cArr2[x-2][y+2] = 'd'; Arr2[x-2][y+2] = Arr[x][y]; //jumps over it
+        cArr2[x][y] = '\0'; Arr2[x][y] = 0;
+        //printf("jump over at [%d][%d] \n",x-1,y+1);
+    } else if (x == 0 || x == 83 || cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r') { //if it hits the wall or 'l' or 'r'.
+        if (cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r') {
+            printf("char = %c \n",cArr[x-1][y+1]);
+        }
+        cArr2[x+1][y+1] = 'e'; Arr2[x+1][y+1] = Arr[x][y]; //start moving right & down
+        cArr2[x][y] = '\0'; Arr2[x][y] = 0;
+        //printf("spawn d at x = %d is shifted to e \n",x);
+    } else { //if nothing is blocking the path
+        cArr2[x-1][y+1] = cArr[x][y]; Arr2[x-1][y+1] = Arr[x][y]; //move as usual.
+        cArr2[x][y] = '\0'; Arr2[x][y] = 0;
+        //printf("work pls. \n");
+    }
+}
+void Spawn::movementE(int x, int y, int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) {
+    if (cArr[x+1][y+1] == 'a' || cArr[x+1][y+1] == 'b' ) { //if the path is blocked by spawnA or spawnB 
+        cArr2[x+2][y+2] = 'e'; Arr2[x+2][y+2] = Arr[x][y]; //jumps over it
+        cArr2[x][y] = '\0'; Arr2[x][y] = 0;
+        //printf("jump over at [%d][%d] \n",x+1,y+1);
+    } else if (x == 0 || x == 83 || cArr[x+1][y+1] == 'l' || cArr[x+1][y+1] == 'r') { //if it hits the wall or 'l' or 'r'.
+        cArr2[x-1][y+1] = 'd'; Arr2[x-1][y+1] = Arr[x][y]; //start moving left & down
+        cArr2[x][y] = '\0'; Arr2[x][y] = 0;
+        //printf("spawn d at x = %d is shifted to e \n",x);
+    } else { //if nothing is blocking the path
+        cArr2[x+1][y+1] = cArr[x][y]; Arr2[x+1][y+1] = Arr[x][y]; //move as usual.
+        cArr2[x][y] = '\0'; Arr2[x][y] = 0;
+        //printf("work pls. \n");
+    }    
+}
 void Spawn::moveSpawnB(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]){
     //printf("moving spawn B \n");
     for (int x = 0; x < Cols; x++) {
@@ -135,10 +150,10 @@
     }            
 }
 void Spawn::updateSpawn(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows], N5110 &lcd) {
-    for ( int x = 0; x < Cols; x++) {
-        for ( int y = 0; y < Rows; y++) {
+    for ( int y = 0; y < Rows; y++) {
+        for ( int x = 0; x < Cols; x++) {
             deleteChar(x, y, Arr2, cArr2); //cleaning up the array
-            if (cArr2[x][y] == 'a' || cArr2[x][y] == 'd' || cArr2[x][y] == 'e') {
+            if (cArr2[x][y] == 'a') {
                 lcd.setPixel(x,y);
                 Arr[x][y] = Arr2[x][y]; //to update the main array
                 cArr[x][y] = cArr2[x][y];
@@ -146,13 +161,21 @@
                 lcd.setPixel(x,y); lcd.setPixel(x,y+1);
                 Arr[x][y] = Arr2[x][y];  //to update the main array
                 cArr[x][y] = 'b';             
-            } else if (cArr2[x][y] == 'c' || cArr2[x][y] == 'l' || cArr2[x][y] == 'r') {
-                //cArr[x-1][y-1] = 'l'; cArr[x+1][y-1] = 'r'; // L and R (left and right) side of the spawn C for bigger hit box.
-                cArr[x][y] = cArr2[x][y];
+            } else if (cArr2[x][y] == 'c') {// l and r (left and right) side of the spawn C for bigger hit box.
+                if (cArr2[x-1][y-1] == '\0'){ //this code to prevent b, d and e from getting replaced 
+                    cArr[x-1][y-1] = 'l'; cArr2[x-1][y-1] = 'l';
+                } 
+                if (cArr2[x+1][y-1] == '\0'){ //this code to prevent b, d and e from getting replaced
+                    cArr[x+1][y-1] = 'r'; cArr2[x+1][y-1] = 'r';
+                }
+                cArr[x][y] = cArr2[x][y]; Arr[x][y] = Arr2[x][y]; //to update the main array
+                lcd.setPixel(x,y); lcd.setPixel(x,y-1);
+                lcd.setPixel(x+1,y-1); lcd.setPixel(x-1,y-1);
+            } else if (cArr2[x][y] == 'd' || cArr2[x][y] == 'e') {
+                lcd.setPixel(x,y);
                 Arr[x][y] = Arr2[x][y]; //to update the main array
-                lcd.setPixel(x,y); lcd.setPixel(x,y+1);
-                lcd.setPixel(x+1,y); lcd.setPixel(x-1,y);
-            }else {
+                cArr[x][y] = cArr2[x][y];
+            } else {
                 Arr[x][y] = 0;
                 cArr[x][y] = '\0';
             }
@@ -162,6 +185,7 @@
 void Spawn::deleteChar(int x, int y, int Arr2[][Rows], char cArr2[][Rows]) { // to clean up the array after killing the spawn.
     if (Arr2[x][y] <= 0) { 
         cArr2[x][y] = '\0';
+    //printf("char at Arr2[%d][%d] cleared for good. \n",x,y);
     }   
 }