JianWei Lee
/
project_game
Meteor defense project
Diff: Spawn/Spawn.cpp
- Revision:
- 23:6d197a452d7c
- Parent:
- 22:2e75b50b26f0
- Child:
- 24:d6187d39f09b
--- a/Spawn/Spawn.cpp Thu Apr 13 10:49:27 2017 +0000 +++ b/Spawn/Spawn.cpp Thu Apr 13 14:05:25 2017 +0000 @@ -12,6 +12,7 @@ void Spawn::init() { healthA = 10; healthC = 100; + healthD = 50; spawnRateA = 32; spawnRateB = 1; spawnRateC = 2; @@ -19,15 +20,16 @@ } void Spawn::randomizeSpawn(int Arr[][Rows], char cArr[][Rows]) { //there is 3 kind of different spawn - a, b and c. - if (stopSpawnA == 0) { //spawn for this row + if (stopSpawnA == 1) { //spawn for this row spawnA(Arr, cArr); - stopSpawnA= 1; + spawnC(Arr, cArr); + stopSpawnA= 0; } else { //stop spawn for the next row - stopSpawnA = 0; + stopSpawnA = 1; } for ( int x = 0; x < Cols; x++) { spawnB(x, Arr, cArr); - spawnC(x, Arr, cArr); + spawnD(x, Arr, cArr); } //printf("at x=2, y=0. value = %d \n",Arr1[2][0]); } @@ -51,33 +53,73 @@ cArr[x][0] = 'b'; } } -void Spawn::spawnC(int x, int Arr[][Rows],char cArr[][Rows]) { +void Spawn::spawnC(int Arr[][Rows],char cArr[][Rows]) { + for ( int x = 0; x < Cols; x++) { bool TrueFalseC = (rand() % 200) < spawnRateC; - if (TrueFalseC) { - Arr[x][0] = healthC; - cArr[x][0] = 'c'; + if (TrueFalseC) { + Arr[x][1] = healthC; + cArr[x][1] = 'c'; + cArr[x-1][0] = 'l'; + cArr[x+1][0] = 'r'; + } } } -void Spawn::spawnD(int Arr[][Rows], char cArr[][Rows]) { - +void Spawn::spawnD(int x, int Arr[][Rows], char cArr[][Rows]) { + bool TrueFalseD = (rand() % 201) < spawnRateD; + if (TrueFalseD) { + bool LeftRightD = (rand() % 10) <5; //its a 50-50 chance + if (LeftRightD) { + Arr[x][0] = healthD; + cArr[x][0] = 'd'; //d denotes spawnD starts by moving left + } else { + Arr[x][0] = healthD; + cArr[x][0] = 'e'; //e denotes spawnD starts by moving right + } + } } void Spawn::moveSpawnABC(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //printf("x=2,y =0. value = %d \n",array[2][0]); //scan through the whole array for (int x = 0; x < Cols; x++) { for (int y = 0; y < Rows; y++) { - if (cArr[x][y] == 'a' || cArr[x][y] == 'b' || cArr[x][y] == 'c') { - //move everything down a row - Arr2[x][y+1] = Arr[x][y]; - cArr2[x][y+1] = cArr[x][y]; + if (cArr[x][y] == 'a' || cArr[x][y] == 'b' || cArr[x][y] == 'c' || cArr[x][y] == 'l' || cArr[x][y] == 'r') { + Arr2[x][y+1] = Arr[x][y]; cArr2[x][y+1] = cArr[x][y]; //move everything down a row } else { - Arr2[x][y+1] = 0; - cArr2[x][y+1] = '\0'; + Arr2[x][y+1] = 0; cArr2[x][y+1] = '\0'; } } } 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. + 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; + } + } + } + } +} 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++) { @@ -96,20 +138,18 @@ for ( int x = 0; x < Cols; x++) { for ( int y = 0; y < Rows; y++) { deleteChar(x, y, Arr2, cArr2); //cleaning up the array - if (cArr2[x][y] == 'a') { + if (cArr2[x][y] == 'a' || cArr2[x][y] == 'd' || cArr2[x][y] == 'e') { lcd.setPixel(x,y); - //to update the main array - Arr[x][y] = Arr2[x][y]; - cArr[x][y] = 'a'; + Arr[x][y] = Arr2[x][y]; //to update the main array + cArr[x][y] = cArr2[x][y]; } else if (cArr2[x][y] == 'b') { lcd.setPixel(x,y); lcd.setPixel(x,y+1); - Arr[x][y] = Arr2[x][y]; + Arr[x][y] = Arr2[x][y]; //to update the main array cArr[x][y] = 'b'; - } else if (cArr2[x][y] == 'c') { - cArr[x-1][y-1] = 'l'; // L and R (left and right) side of the spawn C for bigger hit box. - cArr[x+1][y-1] = 'r'; - cArr[x][y] = 'c'; - Arr[x][y] = Arr2[x][y]; + } 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]; + 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 {