Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Wed Apr 12 17:02:22 2017 +0000
Revision:
20:32f115462bbc
Parent:
19:7ccbb19703f9
Child:
21:e5585569a938
Learning to use STRUCT. Problem with bomb (spawn reviving case).

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 18:d82059ce929b 12 void Spawn::init() {
jasper0712 18:d82059ce929b 13 healthA = 10;
jasper0712 18:d82059ce929b 14 healthC = 100;
jasper0712 18:d82059ce929b 15 spawnRateA = 32;
jasper0712 18:d82059ce929b 16 spawnRateB = 1;
jasper0712 18:d82059ce929b 17 spawnRateC = 2;
jasper0712 18:d82059ce929b 18 }
jasper0712 20:32f115462bbc 19 void Spawn::randomizeSpawn(int Arr[][Rows], char cArr[][Rows]) {
jasper0712 13:38cbce17d7d7 20 //there is 3 kind of different spawn - a, b and c.
jasper0712 19:7ccbb19703f9 21 if (stopSpawnA == 0) { //spawn for this row
jasper0712 20:32f115462bbc 22 spawnA(Arr, cArr);
jasper0712 13:38cbce17d7d7 23 stopSpawnA= 1;
jasper0712 19:7ccbb19703f9 24 } else { //stop spawn for the next row
jasper0712 13:38cbce17d7d7 25 stopSpawnA = 0;
jasper0712 13:38cbce17d7d7 26 }
jasper0712 1:f5cda0674f24 27 for ( int x = 0; x < Cols; x++) {
jasper0712 20:32f115462bbc 28 spawnB(x, Arr, cArr);
jasper0712 20:32f115462bbc 29 spawnC(x, Arr, cArr);
jasper0712 1:f5cda0674f24 30 }
jasper0712 7:3be4369131c6 31 //printf("at x=2, y=0. value = %d \n",Arr1[2][0]);
jasper0712 1:f5cda0674f24 32 }
jasper0712 20:32f115462bbc 33 void Spawn::spawnA(int Arr[][Rows],char cArr[][Rows]) {
jasper0712 19:7ccbb19703f9 34 for ( int x = 0; x < Cols; x++) {
jasper0712 19:7ccbb19703f9 35 //32/200 chance of spawning for A.
jasper0712 19:7ccbb19703f9 36 //can depends on level
jasper0712 19:7ccbb19703f9 37 bool TrueFalseA = (rand() % 200) < spawnRateA;
jasper0712 19:7ccbb19703f9 38 if (TrueFalseA) {
jasper0712 19:7ccbb19703f9 39 //spawning from the top of the screen
jasper0712 20:32f115462bbc 40 Arr[x][0] = healthA;
jasper0712 20:32f115462bbc 41 cArr[x][0] = 'a';
jasper0712 19:7ccbb19703f9 42 //printf("spawn set at x = is %d \n",x);
jasper0712 19:7ccbb19703f9 43 }
jasper0712 19:7ccbb19703f9 44 }
jasper0712 19:7ccbb19703f9 45 }
jasper0712 20:32f115462bbc 46 void Spawn::spawnB(int x, int Arr[][Rows],char cArr[][Rows]) {
jasper0712 19:7ccbb19703f9 47 bool TrueFalseB = (rand() % 200) < spawnRateB;
jasper0712 19:7ccbb19703f9 48 if (TrueFalseB) {
jasper0712 20:32f115462bbc 49 Arr[x][0] = 100000;
jasper0712 20:32f115462bbc 50 cArr[x][0] = 'b';
jasper0712 19:7ccbb19703f9 51 }
jasper0712 19:7ccbb19703f9 52 }
jasper0712 20:32f115462bbc 53 void Spawn::spawnC(int x, int Arr[][Rows],char cArr[][Rows]) {
jasper0712 19:7ccbb19703f9 54 bool TrueFalseC = (rand() % 200) < spawnRateC;
jasper0712 19:7ccbb19703f9 55 if (TrueFalseC) {
jasper0712 20:32f115462bbc 56 Arr[x][0] = healthC;
jasper0712 20:32f115462bbc 57 cArr[x][0] = 'c';
jasper0712 19:7ccbb19703f9 58 }
jasper0712 19:7ccbb19703f9 59 }
jasper0712 20:32f115462bbc 60 void Spawn::moveSpawnABC(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) {
jasper0712 2:2c60c92bf65b 61 //printf("x=2,y =0. value = %d \n",array[2][0]);
jasper0712 1:f5cda0674f24 62 //scan through the whole array
jasper0712 13:38cbce17d7d7 63 for (int x = 0; x < Cols; x++) {
jasper0712 13:38cbce17d7d7 64 for (int y = 0; y < Rows; y++) {
jasper0712 20:32f115462bbc 65 if (cArr[x][y] == 'a' || cArr[x][y] == 'b' || cArr[x][y] == 'c') {
jasper0712 18:d82059ce929b 66 //move everything down a row
jasper0712 20:32f115462bbc 67
jasper0712 20:32f115462bbc 68 Arr2[x][y+1] = Arr[x][y];
jasper0712 20:32f115462bbc 69 cArr2[x][y+1] = cArr[x][y];
jasper0712 2:2c60c92bf65b 70 } else {
jasper0712 20:32f115462bbc 71 Arr2[x][y+1] = 0;
jasper0712 20:32f115462bbc 72 cArr2[x][y+1] = '\0';
jasper0712 1:f5cda0674f24 73 }
jasper0712 1:f5cda0674f24 74 }
jasper0712 1:f5cda0674f24 75 }
jasper0712 6:a554424e4517 76 printf("at x=2, y=47. number is =%d \n",Arr2[2][47]);
jasper0712 1:f5cda0674f24 77 }
jasper0712 20:32f115462bbc 78 void Spawn::moveSpawnB(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]){
jasper0712 13:38cbce17d7d7 79 //printf("moving spawn B \n");
jasper0712 13:38cbce17d7d7 80 for (int x = 0; x < Cols; x++) {
jasper0712 13:38cbce17d7d7 81 for (int y = 0; y < Rows; y++) {
jasper0712 20:32f115462bbc 82 deleteChar(x, y, Arr2, cArr2);
jasper0712 20:32f115462bbc 83 if (cArr[x][y] == 'b') {
jasper0712 20:32f115462bbc 84 Arr2[x][y+1] = Arr[x][y];
jasper0712 20:32f115462bbc 85 cArr2[x][y+1] = 'b';
jasper0712 20:32f115462bbc 86 cArr2[x][y] = '\0';
jasper0712 20:32f115462bbc 87 Arr2[x][y] = 0;
jasper0712 13:38cbce17d7d7 88 }
jasper0712 13:38cbce17d7d7 89 }
jasper0712 13:38cbce17d7d7 90 }
jasper0712 13:38cbce17d7d7 91 }
jasper0712 20:32f115462bbc 92 void Spawn::updateSpawn(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows], N5110 &lcd) {
jasper0712 2:2c60c92bf65b 93 for ( int x = 0; x < Cols; x++) {
jasper0712 2:2c60c92bf65b 94 for ( int y = 0; y < Rows; y++) {
jasper0712 20:32f115462bbc 95 deleteChar(x, y, Arr2, cArr2);
jasper0712 20:32f115462bbc 96 if (cArr2[x][y] == 'a') {
jasper0712 1:f5cda0674f24 97 lcd.setPixel(x,y);
jasper0712 20:32f115462bbc 98 //to update the main array
jasper0712 20:32f115462bbc 99 Arr[x][y] = Arr2[x][y];
jasper0712 20:32f115462bbc 100 cArr[x][y] = 'a';
jasper0712 20:32f115462bbc 101 } else if (cArr2[x][y] == 'b') {
jasper0712 18:d82059ce929b 102 lcd.setPixel(x,y); lcd.setPixel(x,y+1);
jasper0712 20:32f115462bbc 103 Arr[x][y] = Arr2[x][y];
jasper0712 20:32f115462bbc 104 cArr[x][y] = 'b';
jasper0712 20:32f115462bbc 105 } else if (cArr2[x][y] == 'c') {
jasper0712 20:32f115462bbc 106 cArr[x-1][y-1] = 'l'; // L and R (left and right) side of the spawn C for bigger hit box.
jasper0712 20:32f115462bbc 107 cArr[x+1][y-1] = 'r';
jasper0712 20:32f115462bbc 108 cArr[x][y] = 'c';
jasper0712 20:32f115462bbc 109 Arr[x][y] = Arr2[x][y];
jasper0712 18:d82059ce929b 110 lcd.setPixel(x,y); lcd.setPixel(x,y+1);
jasper0712 18:d82059ce929b 111 lcd.setPixel(x+1,y); lcd.setPixel(x-1,y);
jasper0712 13:38cbce17d7d7 112 }else {
jasper0712 20:32f115462bbc 113 Arr[x][y] = 0;
jasper0712 20:32f115462bbc 114 cArr[x][y] = '\0';
jasper0712 1:f5cda0674f24 115 }
jasper0712 1:f5cda0674f24 116 }
jasper0712 1:f5cda0674f24 117 }
jasper0712 1:f5cda0674f24 118 }
jasper0712 20:32f115462bbc 119 void Spawn::deleteChar(int x, int y, int Arr2[][Rows], char cArr2[][Rows]) { // to clean up the array killing the spawn.
jasper0712 20:32f115462bbc 120 if (Arr2[x][y] <= 0) {
jasper0712 20:32f115462bbc 121 cArr2[x][y] = '\0';
jasper0712 13:38cbce17d7d7 122 }
jasper0712 13:38cbce17d7d7 123 }
jasper0712 7:3be4369131c6 124
jasper0712 3:6b50fe9d3848 125