JianWei Lee
/
project_game
Meteor defense project
Spawn/Spawn.cpp@26:140515d80457, 2017-04-14 (annotated)
- Committer:
- jasper0712
- Date:
- Fri Apr 14 15:42:37 2017 +0000
- Revision:
- 26:140515d80457
- Parent:
- 25:edd6a95607b1
- Child:
- 27:d4926f19c12a
game almost done. theres still a few simple bug to fix.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jasper0712 | 1:f5cda0674f24 | 1 | #include "Spawn.h" |
jasper0712 | 0:5fbc0fbc9d30 | 2 | |
jasper0712 | 1:f5cda0674f24 | 3 | Spawn::Spawn() |
jasper0712 | 1:f5cda0674f24 | 4 | { |
jasper0712 | 1:f5cda0674f24 | 5 | |
jasper0712 | 1:f5cda0674f24 | 6 | } |
jasper0712 | 1:f5cda0674f24 | 7 | |
jasper0712 | 1:f5cda0674f24 | 8 | Spawn::~Spawn() |
jasper0712 | 1:f5cda0674f24 | 9 | { |
jasper0712 | 1:f5cda0674f24 | 10 | |
jasper0712 | 1:f5cda0674f24 | 11 | } |
jasper0712 | 18:d82059ce929b | 12 | void Spawn::init() { |
jasper0712 | 18:d82059ce929b | 13 | healthA = 10; |
jasper0712 | 26:140515d80457 | 14 | healthC = 150; |
jasper0712 | 26:140515d80457 | 15 | healthDE = 25; |
jasper0712 | 26:140515d80457 | 16 | spawnRateA = 32; //32 |
jasper0712 | 25:edd6a95607b1 | 17 | spawnRateB = 1; //1 |
jasper0712 | 25:edd6a95607b1 | 18 | spawnRateC = 2; //2 |
jasper0712 | 25:edd6a95607b1 | 19 | spawnRateDE = 1; //1 |
jasper0712 | 18:d82059ce929b | 20 | } |
jasper0712 | 20:32f115462bbc | 21 | void Spawn::randomizeSpawn(int Arr[][Rows], char cArr[][Rows]) { |
jasper0712 | 24:d6187d39f09b | 22 | //there is 4 kind of different spawn - a, b, c and d/e. |
jasper0712 | 23:6d197a452d7c | 23 | if (stopSpawnA == 1) { //spawn for this row |
jasper0712 | 25:edd6a95607b1 | 24 | spawnA(Arr, cArr); |
jasper0712 | 23:6d197a452d7c | 25 | stopSpawnA= 0; |
jasper0712 | 19:7ccbb19703f9 | 26 | } else { //stop spawn for the next row |
jasper0712 | 23:6d197a452d7c | 27 | stopSpawnA = 1; |
jasper0712 | 24:d6187d39f09b | 28 | spawnC(Arr, cArr); |
jasper0712 | 13:38cbce17d7d7 | 29 | } |
jasper0712 | 1:f5cda0674f24 | 30 | for ( int x = 0; x < Cols; x++) { |
jasper0712 | 20:32f115462bbc | 31 | spawnB(x, Arr, cArr); |
jasper0712 | 24:d6187d39f09b | 32 | spawnDE(x, Arr, cArr); |
jasper0712 | 1:f5cda0674f24 | 33 | } |
jasper0712 | 7:3be4369131c6 | 34 | //printf("at x=2, y=0. value = %d \n",Arr1[2][0]); |
jasper0712 | 1:f5cda0674f24 | 35 | } |
jasper0712 | 20:32f115462bbc | 36 | void Spawn::spawnA(int Arr[][Rows],char cArr[][Rows]) { |
jasper0712 | 19:7ccbb19703f9 | 37 | for ( int x = 0; x < Cols; x++) { |
jasper0712 | 19:7ccbb19703f9 | 38 | //32/200 chance of spawning for A. |
jasper0712 | 19:7ccbb19703f9 | 39 | //can depends on level |
jasper0712 | 19:7ccbb19703f9 | 40 | bool TrueFalseA = (rand() % 200) < spawnRateA; |
jasper0712 | 19:7ccbb19703f9 | 41 | if (TrueFalseA) { |
jasper0712 | 19:7ccbb19703f9 | 42 | //spawning from the top of the screen |
jasper0712 | 20:32f115462bbc | 43 | Arr[x][0] = healthA; |
jasper0712 | 20:32f115462bbc | 44 | cArr[x][0] = 'a'; |
jasper0712 | 19:7ccbb19703f9 | 45 | //printf("spawn set at x = is %d \n",x); |
jasper0712 | 19:7ccbb19703f9 | 46 | } |
jasper0712 | 19:7ccbb19703f9 | 47 | } |
jasper0712 | 19:7ccbb19703f9 | 48 | } |
jasper0712 | 20:32f115462bbc | 49 | void Spawn::spawnB(int x, int Arr[][Rows],char cArr[][Rows]) { |
jasper0712 | 19:7ccbb19703f9 | 50 | bool TrueFalseB = (rand() % 200) < spawnRateB; |
jasper0712 | 19:7ccbb19703f9 | 51 | if (TrueFalseB) { |
jasper0712 | 20:32f115462bbc | 52 | Arr[x][0] = 100000; |
jasper0712 | 20:32f115462bbc | 53 | cArr[x][0] = 'b'; |
jasper0712 | 19:7ccbb19703f9 | 54 | } |
jasper0712 | 19:7ccbb19703f9 | 55 | } |
jasper0712 | 23:6d197a452d7c | 56 | void Spawn::spawnC(int Arr[][Rows],char cArr[][Rows]) { |
jasper0712 | 23:6d197a452d7c | 57 | for ( int x = 0; x < Cols; x++) { |
jasper0712 | 19:7ccbb19703f9 | 58 | bool TrueFalseC = (rand() % 200) < spawnRateC; |
jasper0712 | 23:6d197a452d7c | 59 | if (TrueFalseC) { |
jasper0712 | 23:6d197a452d7c | 60 | Arr[x][1] = healthC; |
jasper0712 | 23:6d197a452d7c | 61 | cArr[x][1] = 'c'; |
jasper0712 | 23:6d197a452d7c | 62 | cArr[x-1][0] = 'l'; |
jasper0712 | 23:6d197a452d7c | 63 | cArr[x+1][0] = 'r'; |
jasper0712 | 23:6d197a452d7c | 64 | } |
jasper0712 | 19:7ccbb19703f9 | 65 | } |
jasper0712 | 19:7ccbb19703f9 | 66 | } |
jasper0712 | 24:d6187d39f09b | 67 | void Spawn::spawnDE(int x, int Arr[][Rows], char cArr[][Rows]) { |
jasper0712 | 24:d6187d39f09b | 68 | bool TrueFalseD = (rand() % 201) < spawnRateDE; |
jasper0712 | 23:6d197a452d7c | 69 | if (TrueFalseD) { |
jasper0712 | 23:6d197a452d7c | 70 | bool LeftRightD = (rand() % 10) <5; //its a 50-50 chance |
jasper0712 | 23:6d197a452d7c | 71 | if (LeftRightD) { |
jasper0712 | 24:d6187d39f09b | 72 | Arr[x][0] = healthDE; |
jasper0712 | 23:6d197a452d7c | 73 | cArr[x][0] = 'd'; //d denotes spawnD starts by moving left |
jasper0712 | 23:6d197a452d7c | 74 | } else { |
jasper0712 | 24:d6187d39f09b | 75 | Arr[x][0] = healthDE; |
jasper0712 | 23:6d197a452d7c | 76 | cArr[x][0] = 'e'; //e denotes spawnD starts by moving right |
jasper0712 | 23:6d197a452d7c | 77 | } |
jasper0712 | 23:6d197a452d7c | 78 | } |
jasper0712 | 22:2e75b50b26f0 | 79 | } |
jasper0712 | 20:32f115462bbc | 80 | void Spawn::moveSpawnABC(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { |
jasper0712 | 2:2c60c92bf65b | 81 | //printf("x=2,y =0. value = %d \n",array[2][0]); |
jasper0712 | 1:f5cda0674f24 | 82 | //scan through the whole array |
jasper0712 | 13:38cbce17d7d7 | 83 | for (int x = 0; x < Cols; x++) { |
jasper0712 | 13:38cbce17d7d7 | 84 | for (int y = 0; y < Rows; y++) { |
jasper0712 | 23:6d197a452d7c | 85 | if (cArr[x][y] == 'a' || cArr[x][y] == 'b' || cArr[x][y] == 'c' || cArr[x][y] == 'l' || cArr[x][y] == 'r') { |
jasper0712 | 23:6d197a452d7c | 86 | Arr2[x][y+1] = Arr[x][y]; cArr2[x][y+1] = cArr[x][y]; //move everything down a row |
jasper0712 | 2:2c60c92bf65b | 87 | } else { |
jasper0712 | 23:6d197a452d7c | 88 | Arr2[x][y+1] = 0; cArr2[x][y+1] = '\0'; |
jasper0712 | 1:f5cda0674f24 | 89 | } |
jasper0712 | 1:f5cda0674f24 | 90 | } |
jasper0712 | 1:f5cda0674f24 | 91 | } |
jasper0712 | 6:a554424e4517 | 92 | printf("at x=2, y=47. number is =%d \n",Arr2[2][47]); |
jasper0712 | 1:f5cda0674f24 | 93 | } |
jasper0712 | 24:d6187d39f09b | 94 | void Spawn::moveSpawnDE(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //spawnD moves in a zig zag behavior. |
jasper0712 | 23:6d197a452d7c | 95 | for (int x = 0; x < Cols; x++) { // moves other direction when hit 'L' or 'R' of spawnC and walls. |
jasper0712 | 23:6d197a452d7c | 96 | for (int y = 0; y < Rows; y++) { //jumps over spawnA and spawnB |
jasper0712 | 24:d6187d39f09b | 97 | if (cArr[x][y] == 'd') { //d = move left & down |
jasper0712 | 24:d6187d39f09b | 98 | movementD(x, y, Arr, Arr2, cArr, cArr2); |
jasper0712 | 24:d6187d39f09b | 99 | } else if ( cArr[x][y] == 'e') { //e = move right & down |
jasper0712 | 24:d6187d39f09b | 100 | movementE(x, y, Arr, Arr2, cArr, cArr2); |
jasper0712 | 23:6d197a452d7c | 101 | } |
jasper0712 | 23:6d197a452d7c | 102 | } |
jasper0712 | 23:6d197a452d7c | 103 | } |
jasper0712 | 23:6d197a452d7c | 104 | } |
jasper0712 | 24:d6187d39f09b | 105 | void Spawn::movementD(int x, int y, int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { |
jasper0712 | 24:d6187d39f09b | 106 | if (cArr[x-1][y+1] == 'a' || cArr[x-1][y+1] == 'b' ) { //if the path is blocked by spawnA or spawnB |
jasper0712 | 24:d6187d39f09b | 107 | cArr2[x-2][y+2] = 'd'; Arr2[x-2][y+2] = Arr[x][y]; //jumps over it |
jasper0712 | 24:d6187d39f09b | 108 | cArr2[x][y] = '\0'; Arr2[x][y] = 0; |
jasper0712 | 24:d6187d39f09b | 109 | //printf("jump over at [%d][%d] \n",x-1,y+1); |
jasper0712 | 24:d6187d39f09b | 110 | } 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'. |
jasper0712 | 24:d6187d39f09b | 111 | if (cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r') { |
jasper0712 | 25:edd6a95607b1 | 112 | //printf("char = %c \n",cArr[x-1][y+1]); |
jasper0712 | 24:d6187d39f09b | 113 | } |
jasper0712 | 24:d6187d39f09b | 114 | cArr2[x+1][y+1] = 'e'; Arr2[x+1][y+1] = Arr[x][y]; //start moving right & down |
jasper0712 | 24:d6187d39f09b | 115 | cArr2[x][y] = '\0'; Arr2[x][y] = 0; |
jasper0712 | 24:d6187d39f09b | 116 | //printf("spawn d at x = %d is shifted to e \n",x); |
jasper0712 | 24:d6187d39f09b | 117 | } else { //if nothing is blocking the path |
jasper0712 | 24:d6187d39f09b | 118 | cArr2[x-1][y+1] = cArr[x][y]; Arr2[x-1][y+1] = Arr[x][y]; //move as usual. |
jasper0712 | 24:d6187d39f09b | 119 | cArr2[x][y] = '\0'; Arr2[x][y] = 0; |
jasper0712 | 24:d6187d39f09b | 120 | //printf("work pls. \n"); |
jasper0712 | 24:d6187d39f09b | 121 | } |
jasper0712 | 24:d6187d39f09b | 122 | } |
jasper0712 | 24:d6187d39f09b | 123 | void Spawn::movementE(int x, int y, int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { |
jasper0712 | 24:d6187d39f09b | 124 | if (cArr[x+1][y+1] == 'a' || cArr[x+1][y+1] == 'b' ) { //if the path is blocked by spawnA or spawnB |
jasper0712 | 24:d6187d39f09b | 125 | cArr2[x+2][y+2] = 'e'; Arr2[x+2][y+2] = Arr[x][y]; //jumps over it |
jasper0712 | 24:d6187d39f09b | 126 | cArr2[x][y] = '\0'; Arr2[x][y] = 0; |
jasper0712 | 24:d6187d39f09b | 127 | //printf("jump over at [%d][%d] \n",x+1,y+1); |
jasper0712 | 24:d6187d39f09b | 128 | } 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'. |
jasper0712 | 24:d6187d39f09b | 129 | cArr2[x-1][y+1] = 'd'; Arr2[x-1][y+1] = Arr[x][y]; //start moving left & down |
jasper0712 | 24:d6187d39f09b | 130 | cArr2[x][y] = '\0'; Arr2[x][y] = 0; |
jasper0712 | 24:d6187d39f09b | 131 | //printf("spawn d at x = %d is shifted to e \n",x); |
jasper0712 | 24:d6187d39f09b | 132 | } else { //if nothing is blocking the path |
jasper0712 | 24:d6187d39f09b | 133 | cArr2[x+1][y+1] = cArr[x][y]; Arr2[x+1][y+1] = Arr[x][y]; //move as usual. |
jasper0712 | 24:d6187d39f09b | 134 | cArr2[x][y] = '\0'; Arr2[x][y] = 0; |
jasper0712 | 24:d6187d39f09b | 135 | //printf("work pls. \n"); |
jasper0712 | 24:d6187d39f09b | 136 | } |
jasper0712 | 24:d6187d39f09b | 137 | } |
jasper0712 | 20:32f115462bbc | 138 | void Spawn::moveSpawnB(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]){ |
jasper0712 | 13:38cbce17d7d7 | 139 | //printf("moving spawn B \n"); |
jasper0712 | 13:38cbce17d7d7 | 140 | for (int x = 0; x < Cols; x++) { |
jasper0712 | 13:38cbce17d7d7 | 141 | for (int y = 0; y < Rows; y++) { |
jasper0712 | 25:edd6a95607b1 | 142 | //deleteChar(x, y, Arr2, cArr2); |
jasper0712 | 20:32f115462bbc | 143 | if (cArr[x][y] == 'b') { |
jasper0712 | 20:32f115462bbc | 144 | Arr2[x][y+1] = Arr[x][y]; |
jasper0712 | 20:32f115462bbc | 145 | cArr2[x][y+1] = 'b'; |
jasper0712 | 20:32f115462bbc | 146 | cArr2[x][y] = '\0'; |
jasper0712 | 20:32f115462bbc | 147 | Arr2[x][y] = 0; |
jasper0712 | 13:38cbce17d7d7 | 148 | } |
jasper0712 | 13:38cbce17d7d7 | 149 | } |
jasper0712 | 13:38cbce17d7d7 | 150 | } |
jasper0712 | 13:38cbce17d7d7 | 151 | } |
jasper0712 | 20:32f115462bbc | 152 | void Spawn::updateSpawn(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows], N5110 &lcd) { |
jasper0712 | 24:d6187d39f09b | 153 | for ( int y = 0; y < Rows; y++) { |
jasper0712 | 24:d6187d39f09b | 154 | for ( int x = 0; x < Cols; x++) { |
jasper0712 | 21:e5585569a938 | 155 | deleteChar(x, y, Arr2, cArr2); //cleaning up the array |
jasper0712 | 24:d6187d39f09b | 156 | if (cArr2[x][y] == 'a') { |
jasper0712 | 1:f5cda0674f24 | 157 | lcd.setPixel(x,y); |
jasper0712 | 23:6d197a452d7c | 158 | Arr[x][y] = Arr2[x][y]; //to update the main array |
jasper0712 | 23:6d197a452d7c | 159 | cArr[x][y] = cArr2[x][y]; |
jasper0712 | 20:32f115462bbc | 160 | } else if (cArr2[x][y] == 'b') { |
jasper0712 | 18:d82059ce929b | 161 | lcd.setPixel(x,y); lcd.setPixel(x,y+1); |
jasper0712 | 23:6d197a452d7c | 162 | Arr[x][y] = Arr2[x][y]; //to update the main array |
jasper0712 | 20:32f115462bbc | 163 | cArr[x][y] = 'b'; |
jasper0712 | 24:d6187d39f09b | 164 | } else if (cArr2[x][y] == 'c') {// l and r (left and right) side of the spawn C for bigger hit box. |
jasper0712 | 24:d6187d39f09b | 165 | if (cArr2[x-1][y-1] == '\0'){ //this code to prevent b, d and e from getting replaced |
jasper0712 | 24:d6187d39f09b | 166 | cArr[x-1][y-1] = 'l'; cArr2[x-1][y-1] = 'l'; |
jasper0712 | 24:d6187d39f09b | 167 | } |
jasper0712 | 24:d6187d39f09b | 168 | if (cArr2[x+1][y-1] == '\0'){ //this code to prevent b, d and e from getting replaced |
jasper0712 | 24:d6187d39f09b | 169 | cArr[x+1][y-1] = 'r'; cArr2[x+1][y-1] = 'r'; |
jasper0712 | 24:d6187d39f09b | 170 | } |
jasper0712 | 24:d6187d39f09b | 171 | cArr[x][y] = cArr2[x][y]; Arr[x][y] = Arr2[x][y]; //to update the main array |
jasper0712 | 24:d6187d39f09b | 172 | lcd.setPixel(x,y); lcd.setPixel(x,y-1); |
jasper0712 | 24:d6187d39f09b | 173 | lcd.setPixel(x+1,y-1); lcd.setPixel(x-1,y-1); |
jasper0712 | 24:d6187d39f09b | 174 | } else if (cArr2[x][y] == 'd' || cArr2[x][y] == 'e') { |
jasper0712 | 24:d6187d39f09b | 175 | lcd.setPixel(x,y); |
jasper0712 | 23:6d197a452d7c | 176 | Arr[x][y] = Arr2[x][y]; //to update the main array |
jasper0712 | 24:d6187d39f09b | 177 | cArr[x][y] = cArr2[x][y]; |
jasper0712 | 24:d6187d39f09b | 178 | } else { |
jasper0712 | 20:32f115462bbc | 179 | Arr[x][y] = 0; |
jasper0712 | 20:32f115462bbc | 180 | cArr[x][y] = '\0'; |
jasper0712 | 1:f5cda0674f24 | 181 | } |
jasper0712 | 1:f5cda0674f24 | 182 | } |
jasper0712 | 1:f5cda0674f24 | 183 | } |
jasper0712 | 1:f5cda0674f24 | 184 | } |
jasper0712 | 21:e5585569a938 | 185 | void Spawn::deleteChar(int x, int y, int Arr2[][Rows], char cArr2[][Rows]) { // to clean up the array after killing the spawn. |
jasper0712 | 26:140515d80457 | 186 | if (Arr2[x][y] <= 0) { |
jasper0712 | 20:32f115462bbc | 187 | cArr2[x][y] = '\0'; |
jasper0712 | 24:d6187d39f09b | 188 | //printf("char at Arr2[%d][%d] cleared for good. \n",x,y); |
jasper0712 | 26:140515d80457 | 189 | } |
jasper0712 | 13:38cbce17d7d7 | 190 | } |
jasper0712 | 7:3be4369131c6 | 191 | |
jasper0712 | 3:6b50fe9d3848 | 192 |