Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Sat Apr 29 19:27:20 2017 +0000
Revision:
40:3a0c66a0e10e
Parent:
39:f92b93efbc72
Child:
41:1b396334256b
done commenting. MAYBE. implemented RETRY function in the game. need further testing

Who changed what in which revision?

UserRevisionLine numberNew 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 32:580d74825810 12 void Spawn::init(int w) {
jasper0712 34:6ac9541d4c31 13 A.health = 7 + 2 * w;
jasper0712 34:6ac9541d4c31 14 A.spawnRate = 35 + w;
jasper0712 39:f92b93efbc72 15 B.health = 1000; //health B wont change
jasper0712 34:6ac9541d4c31 16 B.spawnRate = 1 + w / 2;
jasper0712 34:6ac9541d4c31 17 C.health = 150 + w * 5;
jasper0712 34:6ac9541d4c31 18 C.spawnRate = 1 + w;
jasper0712 34:6ac9541d4c31 19 DE.health = 20 + w;
jasper0712 34:6ac9541d4c31 20 DE.spawnRate = 1 + w / 2;
jasper0712 32:580d74825810 21 //randomise the seed. To get different spawn pattern all the time.
jasper0712 32:580d74825810 22 int t = time(NULL);
jasper0712 32:580d74825810 23 //printf("t = %d \n",t);
jasper0712 39:f92b93efbc72 24 srand(t);
jasper0712 18:d82059ce929b 25 }
jasper0712 40:3a0c66a0e10e 26 void Spawn::summon_spawn(int Arr[][Rows], char cArr[][Rows]) {
jasper0712 24:d6187d39f09b 27 //there is 4 kind of different spawn - a, b, c and d/e.
jasper0712 34:6ac9541d4c31 28 if (stopSpawnA == 1) { //spawn spawnA for this row
jasper0712 25:edd6a95607b1 29 spawnA(Arr, cArr);
jasper0712 23:6d197a452d7c 30 stopSpawnA= 0;
jasper0712 34:6ac9541d4c31 31 } else { //spawn spawnC for this next row
jasper0712 23:6d197a452d7c 32 stopSpawnA = 1;
jasper0712 24:d6187d39f09b 33 spawnC(Arr, cArr);
jasper0712 13:38cbce17d7d7 34 }
jasper0712 1:f5cda0674f24 35 for ( int x = 0; x < Cols; x++) {
jasper0712 20:32f115462bbc 36 spawnB(x, Arr, cArr);
jasper0712 24:d6187d39f09b 37 spawnDE(x, Arr, cArr);
jasper0712 1:f5cda0674f24 38 }
jasper0712 7:3be4369131c6 39 //printf("at x=2, y=0. value = %d \n",Arr1[2][0]);
jasper0712 1:f5cda0674f24 40 }
jasper0712 20:32f115462bbc 41 void Spawn::spawnA(int Arr[][Rows],char cArr[][Rows]) {
jasper0712 19:7ccbb19703f9 42 for ( int x = 0; x < Cols; x++) {
jasper0712 34:6ac9541d4c31 43 //35/220 chance of spawning for A.
jasper0712 34:6ac9541d4c31 44 //depends on wave number
jasper0712 34:6ac9541d4c31 45 bool TrueFalseA = (rand() % 220) < A.spawnRate;
jasper0712 19:7ccbb19703f9 46 if (TrueFalseA) {
jasper0712 19:7ccbb19703f9 47 //spawning from the top of the screen
jasper0712 34:6ac9541d4c31 48 Arr[x][0] = A.health;
jasper0712 20:32f115462bbc 49 cArr[x][0] = 'a';
jasper0712 19:7ccbb19703f9 50 //printf("spawn set at x = is %d \n",x);
jasper0712 19:7ccbb19703f9 51 }
jasper0712 19:7ccbb19703f9 52 }
jasper0712 19:7ccbb19703f9 53 }
jasper0712 20:32f115462bbc 54 void Spawn::spawnB(int x, int Arr[][Rows],char cArr[][Rows]) {
jasper0712 34:6ac9541d4c31 55 bool TrueFalseB = (rand() % 220) < B.spawnRate;
jasper0712 19:7ccbb19703f9 56 if (TrueFalseB) {
jasper0712 39:f92b93efbc72 57 Arr[x][0] = B.health;
jasper0712 20:32f115462bbc 58 cArr[x][0] = 'b';
jasper0712 19:7ccbb19703f9 59 }
jasper0712 19:7ccbb19703f9 60 }
jasper0712 23:6d197a452d7c 61 void Spawn::spawnC(int Arr[][Rows],char cArr[][Rows]) {
jasper0712 23:6d197a452d7c 62 for ( int x = 0; x < Cols; x++) {
jasper0712 34:6ac9541d4c31 63 bool TrueFalseC = (rand() % 220) < C.spawnRate;
jasper0712 23:6d197a452d7c 64 if (TrueFalseC) {
jasper0712 34:6ac9541d4c31 65 Arr[x][1] = C.health;
jasper0712 23:6d197a452d7c 66 cArr[x][1] = 'c';
jasper0712 23:6d197a452d7c 67 cArr[x-1][0] = 'l';
jasper0712 23:6d197a452d7c 68 cArr[x+1][0] = 'r';
jasper0712 23:6d197a452d7c 69 }
jasper0712 19:7ccbb19703f9 70 }
jasper0712 19:7ccbb19703f9 71 }
jasper0712 24:d6187d39f09b 72 void Spawn::spawnDE(int x, int Arr[][Rows], char cArr[][Rows]) {
jasper0712 34:6ac9541d4c31 73 bool TrueFalseD = (rand() % 220) < DE.spawnRate;
jasper0712 23:6d197a452d7c 74 if (TrueFalseD) {
jasper0712 23:6d197a452d7c 75 bool LeftRightD = (rand() % 10) <5; //its a 50-50 chance
jasper0712 23:6d197a452d7c 76 if (LeftRightD) {
jasper0712 34:6ac9541d4c31 77 Arr[x][0] = DE.health;
jasper0712 23:6d197a452d7c 78 cArr[x][0] = 'd'; //d denotes spawnD starts by moving left
jasper0712 23:6d197a452d7c 79 } else {
jasper0712 34:6ac9541d4c31 80 Arr[x][0] = DE.health;
jasper0712 23:6d197a452d7c 81 cArr[x][0] = 'e'; //e denotes spawnD starts by moving right
jasper0712 23:6d197a452d7c 82 }
jasper0712 23:6d197a452d7c 83 }
jasper0712 22:2e75b50b26f0 84 }
jasper0712 20:32f115462bbc 85 void Spawn::moveSpawnABC(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) {
jasper0712 2:2c60c92bf65b 86 //printf("x=2,y =0. value = %d \n",array[2][0]);
jasper0712 1:f5cda0674f24 87 //scan through the whole array
jasper0712 13:38cbce17d7d7 88 for (int x = 0; x < Cols; x++) {
jasper0712 13:38cbce17d7d7 89 for (int y = 0; y < Rows; y++) {
jasper0712 27:d4926f19c12a 90 if (cArr[x][y] == 'a' || cArr[x][y] == 'b' || cArr[x][y] == 'c' || cArr[x][y] == 'l' || cArr[x][y] == 'r' || cArr[x][y] == 'm') {
jasper0712 23:6d197a452d7c 91 Arr2[x][y+1] = Arr[x][y]; cArr2[x][y+1] = cArr[x][y]; //move everything down a row
jasper0712 2:2c60c92bf65b 92 } else {
jasper0712 23:6d197a452d7c 93 Arr2[x][y+1] = 0; cArr2[x][y+1] = '\0';
jasper0712 1:f5cda0674f24 94 }
jasper0712 1:f5cda0674f24 95 }
jasper0712 1:f5cda0674f24 96 }
jasper0712 6:a554424e4517 97 printf("at x=2, y=47. number is =%d \n",Arr2[2][47]);
jasper0712 1:f5cda0674f24 98 }
jasper0712 40:3a0c66a0e10e 99 void Spawn::moveSpawnDE(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) {
jasper0712 40:3a0c66a0e10e 100 for (int x = 0; x < Cols; x++) {
jasper0712 40:3a0c66a0e10e 101 for (int y = 0; y < Rows; y++) {
jasper0712 24:d6187d39f09b 102 if (cArr[x][y] == 'd') { //d = move left & down
jasper0712 24:d6187d39f09b 103 movementD(x, y, Arr, Arr2, cArr, cArr2);
jasper0712 24:d6187d39f09b 104 } else if ( cArr[x][y] == 'e') { //e = move right & down
jasper0712 24:d6187d39f09b 105 movementE(x, y, Arr, Arr2, cArr, cArr2);
jasper0712 23:6d197a452d7c 106 }
jasper0712 23:6d197a452d7c 107 }
jasper0712 23:6d197a452d7c 108 }
jasper0712 23:6d197a452d7c 109 }
jasper0712 27:d4926f19c12a 110 void Spawn::movementD(int x, int y, int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //d = move left & down
jasper0712 27:d4926f19c12a 111 if (cArr[x-1][y+1] == 'a' || cArr[x-1][y] == 'b' ) { //if the path is blocked by spawnA or spawnB
jasper0712 24:d6187d39f09b 112 cArr2[x-2][y+2] = 'd'; Arr2[x-2][y+2] = Arr[x][y]; //jumps over it
jasper0712 24:d6187d39f09b 113 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 114 //printf("jump over at [%d][%d] \n",x-1,y+1);
jasper0712 27:d4926f19c12a 115 } else if (x == 0 || x == 83 || cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r' || cArr[x-1][y+1] == 'm') { //if it hits the wall or 'l' or 'r' or 'm'.
jasper0712 24:d6187d39f09b 116 if (cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r') {
jasper0712 25:edd6a95607b1 117 //printf("char = %c \n",cArr[x-1][y+1]);
jasper0712 24:d6187d39f09b 118 }
jasper0712 24:d6187d39f09b 119 cArr2[x+1][y+1] = 'e'; Arr2[x+1][y+1] = Arr[x][y]; //start moving right & down
jasper0712 24:d6187d39f09b 120 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 121 //printf("spawn d at x = %d is shifted to e \n",x);
jasper0712 24:d6187d39f09b 122 } else { //if nothing is blocking the path
jasper0712 24:d6187d39f09b 123 cArr2[x-1][y+1] = cArr[x][y]; Arr2[x-1][y+1] = Arr[x][y]; //move as usual.
jasper0712 24:d6187d39f09b 124 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 125 //printf("work pls. \n");
jasper0712 24:d6187d39f09b 126 }
jasper0712 24:d6187d39f09b 127 }
jasper0712 27:d4926f19c12a 128 void Spawn::movementE(int x, int y, int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //e = move right & down
jasper0712 27:d4926f19c12a 129 if (cArr[x+1][y+1] == 'a' || cArr[x+1][y] == 'b' ) { //if the path is blocked by spawnA or spawnB
jasper0712 24:d6187d39f09b 130 cArr2[x+2][y+2] = 'e'; Arr2[x+2][y+2] = Arr[x][y]; //jumps over it
jasper0712 24:d6187d39f09b 131 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 132 //printf("jump over at [%d][%d] \n",x+1,y+1);
jasper0712 39:f92b93efbc72 133 } else if (x == 0 || x == 83 || cArr[x+1][y+1] == 'l' || cArr[x+1][y+1] == 'r' || cArr[x+1][y+1] == 'm') { //if it hits either side of the wall or 'l' or 'r' or 'm'.
jasper0712 24:d6187d39f09b 134 cArr2[x-1][y+1] = 'd'; Arr2[x-1][y+1] = Arr[x][y]; //start moving left & down
jasper0712 24:d6187d39f09b 135 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 136 //printf("spawn d at x = %d is shifted to e \n",x);
jasper0712 24:d6187d39f09b 137 } else { //if nothing is blocking the path
jasper0712 24:d6187d39f09b 138 cArr2[x+1][y+1] = cArr[x][y]; Arr2[x+1][y+1] = Arr[x][y]; //move as usual.
jasper0712 24:d6187d39f09b 139 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 140 //printf("work pls. \n");
jasper0712 24:d6187d39f09b 141 }
jasper0712 24:d6187d39f09b 142 }
jasper0712 20:32f115462bbc 143 void Spawn::moveSpawnB(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]){
jasper0712 13:38cbce17d7d7 144 //printf("moving spawn B \n");
jasper0712 13:38cbce17d7d7 145 for (int x = 0; x < Cols; x++) {
jasper0712 13:38cbce17d7d7 146 for (int y = 0; y < Rows; y++) {
jasper0712 25:edd6a95607b1 147 //deleteChar(x, y, Arr2, cArr2);
jasper0712 20:32f115462bbc 148 if (cArr[x][y] == 'b') {
jasper0712 20:32f115462bbc 149 Arr2[x][y+1] = Arr[x][y];
jasper0712 20:32f115462bbc 150 cArr2[x][y+1] = 'b';
jasper0712 20:32f115462bbc 151 cArr2[x][y] = '\0';
jasper0712 20:32f115462bbc 152 Arr2[x][y] = 0;
jasper0712 13:38cbce17d7d7 153 }
jasper0712 13:38cbce17d7d7 154 }
jasper0712 13:38cbce17d7d7 155 }
jasper0712 13:38cbce17d7d7 156 }
jasper0712 20:32f115462bbc 157 void Spawn::updateSpawn(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows], N5110 &lcd) {
jasper0712 24:d6187d39f09b 158 for ( int y = 0; y < Rows; y++) {
jasper0712 24:d6187d39f09b 159 for ( int x = 0; x < Cols; x++) {
jasper0712 21:e5585569a938 160 deleteChar(x, y, Arr2, cArr2); //cleaning up the array
jasper0712 27:d4926f19c12a 161 if (cArr2[x][y] == 'a' || cArr2[x][y] == 'd' || cArr2[x][y] == 'e') {
jasper0712 1:f5cda0674f24 162 lcd.setPixel(x,y);
jasper0712 23:6d197a452d7c 163 Arr[x][y] = Arr2[x][y]; //to update the main array
jasper0712 23:6d197a452d7c 164 cArr[x][y] = cArr2[x][y];
jasper0712 20:32f115462bbc 165 } else if (cArr2[x][y] == 'b') {
jasper0712 27:d4926f19c12a 166 lcd.setPixel(x,y); lcd.setPixel(x,y-1);
jasper0712 23:6d197a452d7c 167 Arr[x][y] = Arr2[x][y]; //to update the main array
jasper0712 20:32f115462bbc 168 cArr[x][y] = 'b';
jasper0712 27:d4926f19c12a 169 } else if (cArr2[x][y] == 'c') {// l and r (left and right) side of the spawn C for bigger hit box & to deflect spawnDE.
jasper0712 24:d6187d39f09b 170 if (cArr2[x-1][y-1] == '\0'){ //this code to prevent b, d and e from getting replaced
jasper0712 24:d6187d39f09b 171 cArr[x-1][y-1] = 'l'; cArr2[x-1][y-1] = 'l';
jasper0712 24:d6187d39f09b 172 }
jasper0712 24:d6187d39f09b 173 if (cArr2[x+1][y-1] == '\0'){ //this code to prevent b, d and e from getting replaced
jasper0712 24:d6187d39f09b 174 cArr[x+1][y-1] = 'r'; cArr2[x+1][y-1] = 'r';
jasper0712 24:d6187d39f09b 175 }
jasper0712 27:d4926f19c12a 176 cArr[x][y-1] = 'm'; //to deflect spawnDE.
jasper0712 24:d6187d39f09b 177 cArr[x][y] = cArr2[x][y]; Arr[x][y] = Arr2[x][y]; //to update the main array
jasper0712 24:d6187d39f09b 178 lcd.setPixel(x,y); lcd.setPixel(x,y-1);
jasper0712 24:d6187d39f09b 179 lcd.setPixel(x+1,y-1); lcd.setPixel(x-1,y-1);
jasper0712 24:d6187d39f09b 180 } else {
jasper0712 34:6ac9541d4c31 181 Arr[x][y] = 0; cArr[x][y] = '\0';
jasper0712 1:f5cda0674f24 182 }
jasper0712 1:f5cda0674f24 183 }
jasper0712 1:f5cda0674f24 184 }
jasper0712 1:f5cda0674f24 185 }
jasper0712 21:e5585569a938 186 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 187 if (Arr2[x][y] <= 0) {
jasper0712 20:32f115462bbc 188 cArr2[x][y] = '\0';
jasper0712 24:d6187d39f09b 189 //printf("char at Arr2[%d][%d] cleared for good. \n",x,y);
jasper0712 26:140515d80457 190 }
jasper0712 34:6ac9541d4c31 191 }