Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Mon Apr 10 11:13:10 2017 +0000
Revision:
18:d82059ce929b
Parent:
16:ddc4d5669a6e
Child:
19:7ccbb19703f9
done grouping every variable to init(). can start balancing everything or making levels

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 13:38cbce17d7d7 19 void Spawn::randomizeSpawn(int Arr2[][Rows], char Arr3[][Rows]) {
jasper0712 13:38cbce17d7d7 20 //there is 3 kind of different spawn - a, b and c.
jasper0712 13:38cbce17d7d7 21 if (stopSpawnA == 0) {
jasper0712 13:38cbce17d7d7 22 for ( int x = 0; x < Cols; x++) {
jasper0712 16:ddc4d5669a6e 23 //32/200 chance of spawning for A.
jasper0712 13:38cbce17d7d7 24 //can depends on level
jasper0712 18:d82059ce929b 25 bool TrueFalseA = (rand() % 200) < spawnRateA;
jasper0712 13:38cbce17d7d7 26 if (TrueFalseA) {
jasper0712 13:38cbce17d7d7 27 //spawning from the top of the screen
jasper0712 18:d82059ce929b 28 Arr2[x][0] = healthA;
jasper0712 13:38cbce17d7d7 29 Arr3[x][0] = 'a';
jasper0712 13:38cbce17d7d7 30 //printf("spawn set at x = is %d \n",x);
jasper0712 13:38cbce17d7d7 31 }
jasper0712 13:38cbce17d7d7 32 }
jasper0712 13:38cbce17d7d7 33 stopSpawnA= 1;
jasper0712 13:38cbce17d7d7 34 } else {
jasper0712 13:38cbce17d7d7 35 stopSpawnA = 0;
jasper0712 13:38cbce17d7d7 36 }
jasper0712 1:f5cda0674f24 37 for ( int x = 0; x < Cols; x++) {
jasper0712 18:d82059ce929b 38 bool TrueFalseB = (rand() % 200) < spawnRateB;
jasper0712 13:38cbce17d7d7 39 if (TrueFalseB) {
jasper0712 16:ddc4d5669a6e 40 Arr2[x][0] = 100000;
jasper0712 13:38cbce17d7d7 41 Arr3[x][0] = 'b';
jasper0712 13:38cbce17d7d7 42 }
jasper0712 18:d82059ce929b 43 bool TrueFalseC = (rand() % 200) < spawnRateC;
jasper0712 13:38cbce17d7d7 44 if (TrueFalseC) {
jasper0712 18:d82059ce929b 45 Arr2[x][0] = healthC;
jasper0712 13:38cbce17d7d7 46 Arr3[x][0] = 'c';
jasper0712 1:f5cda0674f24 47 }
jasper0712 1:f5cda0674f24 48 }
jasper0712 7:3be4369131c6 49 //printf("at x=2, y=0. value = %d \n",Arr1[2][0]);
jasper0712 1:f5cda0674f24 50 }
jasper0712 13:38cbce17d7d7 51 void Spawn::moveSpawnABC(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][Rows]) {
jasper0712 2:2c60c92bf65b 52 //printf("x=2,y =0. value = %d \n",array[2][0]);
jasper0712 1:f5cda0674f24 53 //scan through the whole array
jasper0712 13:38cbce17d7d7 54 for (int x = 0; x < Cols; x++) {
jasper0712 13:38cbce17d7d7 55 for (int y = 0; y < Rows; y++) {
jasper0712 9:4c4d787c7a8b 56 if (Arr2[x][y] > 0) {
jasper0712 18:d82059ce929b 57 //move everything down a row
jasper0712 9:4c4d787c7a8b 58 Arr1[x][y+1] = Arr2[x][y];
jasper0712 13:38cbce17d7d7 59 Arr3[x][y+1] = Arr3[x][y];
jasper0712 13:38cbce17d7d7 60 Arr3[x][y] = '\0';
jasper0712 2:2c60c92bf65b 61 } else {
jasper0712 9:4c4d787c7a8b 62 Arr1[x][y+1] = 0;
jasper0712 1:f5cda0674f24 63 }
jasper0712 1:f5cda0674f24 64 }
jasper0712 1:f5cda0674f24 65 }
jasper0712 6:a554424e4517 66 printf("at x=2, y=47. number is =%d \n",Arr2[2][47]);
jasper0712 1:f5cda0674f24 67 }
jasper0712 13:38cbce17d7d7 68 void Spawn::moveSpawnB(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][Rows]){
jasper0712 13:38cbce17d7d7 69 //printf("moving spawn B \n");
jasper0712 13:38cbce17d7d7 70 for (int x = 0; x < Cols; x++) {
jasper0712 13:38cbce17d7d7 71 for (int y = 0; y < Rows; y++) {
jasper0712 18:d82059ce929b 72 if (Arr2[x][y] > 20000) { //any number larger than this must be spawn b.
jasper0712 13:38cbce17d7d7 73 Arr1[x][y+1] = Arr2[x][y];
jasper0712 13:38cbce17d7d7 74 Arr1[x][y] = 0;
jasper0712 13:38cbce17d7d7 75 Arr3[x][y+1] = 'b';
jasper0712 13:38cbce17d7d7 76 Arr3[x][y] = '\0';
jasper0712 13:38cbce17d7d7 77 }
jasper0712 13:38cbce17d7d7 78 }
jasper0712 13:38cbce17d7d7 79 }
jasper0712 13:38cbce17d7d7 80 }
jasper0712 13:38cbce17d7d7 81 void Spawn::updateSpawn(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][Rows],N5110 &lcd) {
jasper0712 2:2c60c92bf65b 82 for ( int x = 0; x < Cols; x++) {
jasper0712 2:2c60c92bf65b 83 for ( int y = 0; y < Rows; y++) {
jasper0712 16:ddc4d5669a6e 84 deleteChar(x, y, Arr1, Arr3);
jasper0712 13:38cbce17d7d7 85 if (Arr3[x][y] == 'a') {
jasper0712 1:f5cda0674f24 86 lcd.setPixel(x,y);
jasper0712 2:2c60c92bf65b 87 //to update array1
jasper0712 9:4c4d787c7a8b 88 Arr2[x][y] = Arr1[x][y];
jasper0712 13:38cbce17d7d7 89 } else if (Arr3[x][y] == 'b') {
jasper0712 18:d82059ce929b 90 lcd.setPixel(x,y); lcd.setPixel(x,y+1);
jasper0712 13:38cbce17d7d7 91 Arr2[x][y] = Arr1[x][y];
jasper0712 13:38cbce17d7d7 92 } else if (Arr3[x][y] == 'c') {
jasper0712 16:ddc4d5669a6e 93 Arr3[x-1][y-1] = 'l';
jasper0712 16:ddc4d5669a6e 94 Arr3[x+1][y-1] = 'r';
jasper0712 13:38cbce17d7d7 95 Arr2[x][y] = Arr1[x][y];
jasper0712 18:d82059ce929b 96 lcd.setPixel(x,y); lcd.setPixel(x,y+1);
jasper0712 18:d82059ce929b 97 lcd.setPixel(x+1,y); lcd.setPixel(x-1,y);
jasper0712 13:38cbce17d7d7 98 }else {
jasper0712 9:4c4d787c7a8b 99 Arr2[x][y] = 0;
jasper0712 1:f5cda0674f24 100 }
jasper0712 1:f5cda0674f24 101 }
jasper0712 1:f5cda0674f24 102 }
jasper0712 1:f5cda0674f24 103 }
jasper0712 13:38cbce17d7d7 104 void Spawn::deleteChar(int x, int y, int Arr1[][Rows], char Arr3[][Rows]) {
jasper0712 13:38cbce17d7d7 105 if (Arr1[x][y] == 0) {
jasper0712 13:38cbce17d7d7 106 Arr3[x][y] = '\0';
jasper0712 13:38cbce17d7d7 107 }
jasper0712 13:38cbce17d7d7 108 }
jasper0712 7:3be4369131c6 109
jasper0712 3:6b50fe9d3848 110