Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Sun Apr 23 21:09:32 2017 +0000
Revision:
34:6ac9541d4c31
Parent:
33:61c69c46a941
Child:
39:f92b93efbc72
did pretty much nothing dont know what to do now

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