Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Thu Apr 13 10:49:27 2017 +0000
Revision:
22:2e75b50b26f0
Parent:
21:e5585569a938
Child:
23:6d197a452d7c
starting on spawnD

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