JianWei Lee
/
project_game
Meteor defense project
Spawn/Spawn.cpp@19:7ccbb19703f9, 2017-04-12 (annotated)
- Committer:
- jasper0712
- Date:
- Wed Apr 12 13:53:37 2017 +0000
- Revision:
- 19:7ccbb19703f9
- Parent:
- 18:d82059ce929b
- Child:
- 20:32f115462bbc
starting to change the way array is used in spawn
Who changed what in which revision?
User | Revision | Line number | New 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 | 19:7ccbb19703f9 | 19 | void Spawn::randomizeSpawn(char Arr2[][Rows], char Arr3[][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 | 19:7ccbb19703f9 | 22 | spawnA(Arr2, Arr3); |
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 | 19:7ccbb19703f9 | 28 | spawnB(Arr2, Arr3); |
jasper0712 | 19:7ccbb19703f9 | 29 | spawnC(Arr2, Arr3); |
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 | 19:7ccbb19703f9 | 33 | void Spawn::spawnA(int Arr2[][Rows],char Arr3[][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 | 19:7ccbb19703f9 | 40 | Arr2[x][0] = healthA; |
jasper0712 | 19:7ccbb19703f9 | 41 | Arr3[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 | 19:7ccbb19703f9 | 46 | void Spawn::spawnB(int Arr2[][Rows],char Arr3[][Rows]) { |
jasper0712 | 19:7ccbb19703f9 | 47 | bool TrueFalseB = (rand() % 200) < spawnRateB; |
jasper0712 | 19:7ccbb19703f9 | 48 | if (TrueFalseB) { |
jasper0712 | 19:7ccbb19703f9 | 49 | Arr2[x][0] = 100000; |
jasper0712 | 19:7ccbb19703f9 | 50 | Arr3[x][0] = 'b'; |
jasper0712 | 19:7ccbb19703f9 | 51 | } |
jasper0712 | 19:7ccbb19703f9 | 52 | } |
jasper0712 | 19:7ccbb19703f9 | 53 | void Spawn:spawnC(int Arr2[][Rows],char Arr3[][Rows]) { |
jasper0712 | 19:7ccbb19703f9 | 54 | bool TrueFalseC = (rand() % 200) < spawnRateC; |
jasper0712 | 19:7ccbb19703f9 | 55 | if (TrueFalseC) { |
jasper0712 | 19:7ccbb19703f9 | 56 | Arr2[x][0] = healthC; |
jasper0712 | 19:7ccbb19703f9 | 57 | Arr3[x][0] = 'c'; |
jasper0712 | 19:7ccbb19703f9 | 58 | } |
jasper0712 | 19:7ccbb19703f9 | 59 | } |
jasper0712 | 13:38cbce17d7d7 | 60 | void Spawn::moveSpawnABC(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][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 | 9:4c4d787c7a8b | 65 | if (Arr2[x][y] > 0) { |
jasper0712 | 18:d82059ce929b | 66 | //move everything down a row |
jasper0712 | 9:4c4d787c7a8b | 67 | Arr1[x][y+1] = Arr2[x][y]; |
jasper0712 | 13:38cbce17d7d7 | 68 | Arr3[x][y+1] = Arr3[x][y]; |
jasper0712 | 13:38cbce17d7d7 | 69 | Arr3[x][y] = '\0'; |
jasper0712 | 2:2c60c92bf65b | 70 | } else { |
jasper0712 | 9:4c4d787c7a8b | 71 | Arr1[x][y+1] = 0; |
jasper0712 | 1:f5cda0674f24 | 72 | } |
jasper0712 | 1:f5cda0674f24 | 73 | } |
jasper0712 | 1:f5cda0674f24 | 74 | } |
jasper0712 | 6:a554424e4517 | 75 | printf("at x=2, y=47. number is =%d \n",Arr2[2][47]); |
jasper0712 | 1:f5cda0674f24 | 76 | } |
jasper0712 | 13:38cbce17d7d7 | 77 | void Spawn::moveSpawnB(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][Rows]){ |
jasper0712 | 13:38cbce17d7d7 | 78 | //printf("moving spawn B \n"); |
jasper0712 | 13:38cbce17d7d7 | 79 | for (int x = 0; x < Cols; x++) { |
jasper0712 | 13:38cbce17d7d7 | 80 | for (int y = 0; y < Rows; y++) { |
jasper0712 | 18:d82059ce929b | 81 | if (Arr2[x][y] > 20000) { //any number larger than this must be spawn b. |
jasper0712 | 13:38cbce17d7d7 | 82 | Arr1[x][y+1] = Arr2[x][y]; |
jasper0712 | 13:38cbce17d7d7 | 83 | Arr1[x][y] = 0; |
jasper0712 | 13:38cbce17d7d7 | 84 | Arr3[x][y+1] = 'b'; |
jasper0712 | 13:38cbce17d7d7 | 85 | Arr3[x][y] = '\0'; |
jasper0712 | 13:38cbce17d7d7 | 86 | } |
jasper0712 | 13:38cbce17d7d7 | 87 | } |
jasper0712 | 13:38cbce17d7d7 | 88 | } |
jasper0712 | 13:38cbce17d7d7 | 89 | } |
jasper0712 | 13:38cbce17d7d7 | 90 | void Spawn::updateSpawn(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][Rows],N5110 &lcd) { |
jasper0712 | 2:2c60c92bf65b | 91 | for ( int x = 0; x < Cols; x++) { |
jasper0712 | 2:2c60c92bf65b | 92 | for ( int y = 0; y < Rows; y++) { |
jasper0712 | 16:ddc4d5669a6e | 93 | deleteChar(x, y, Arr1, Arr3); |
jasper0712 | 13:38cbce17d7d7 | 94 | if (Arr3[x][y] == 'a') { |
jasper0712 | 1:f5cda0674f24 | 95 | lcd.setPixel(x,y); |
jasper0712 | 2:2c60c92bf65b | 96 | //to update array1 |
jasper0712 | 9:4c4d787c7a8b | 97 | Arr2[x][y] = Arr1[x][y]; |
jasper0712 | 13:38cbce17d7d7 | 98 | } else if (Arr3[x][y] == 'b') { |
jasper0712 | 18:d82059ce929b | 99 | lcd.setPixel(x,y); lcd.setPixel(x,y+1); |
jasper0712 | 13:38cbce17d7d7 | 100 | Arr2[x][y] = Arr1[x][y]; |
jasper0712 | 13:38cbce17d7d7 | 101 | } else if (Arr3[x][y] == 'c') { |
jasper0712 | 16:ddc4d5669a6e | 102 | Arr3[x-1][y-1] = 'l'; |
jasper0712 | 16:ddc4d5669a6e | 103 | Arr3[x+1][y-1] = 'r'; |
jasper0712 | 13:38cbce17d7d7 | 104 | Arr2[x][y] = Arr1[x][y]; |
jasper0712 | 18:d82059ce929b | 105 | lcd.setPixel(x,y); lcd.setPixel(x,y+1); |
jasper0712 | 18:d82059ce929b | 106 | lcd.setPixel(x+1,y); lcd.setPixel(x-1,y); |
jasper0712 | 13:38cbce17d7d7 | 107 | }else { |
jasper0712 | 9:4c4d787c7a8b | 108 | Arr2[x][y] = 0; |
jasper0712 | 1:f5cda0674f24 | 109 | } |
jasper0712 | 1:f5cda0674f24 | 110 | } |
jasper0712 | 1:f5cda0674f24 | 111 | } |
jasper0712 | 1:f5cda0674f24 | 112 | } |
jasper0712 | 13:38cbce17d7d7 | 113 | void Spawn::deleteChar(int x, int y, int Arr1[][Rows], char Arr3[][Rows]) { |
jasper0712 | 13:38cbce17d7d7 | 114 | if (Arr1[x][y] == 0) { |
jasper0712 | 13:38cbce17d7d7 | 115 | Arr3[x][y] = '\0'; |
jasper0712 | 13:38cbce17d7d7 | 116 | } |
jasper0712 | 13:38cbce17d7d7 | 117 | } |
jasper0712 | 7:3be4369131c6 | 118 | |
jasper0712 | 3:6b50fe9d3848 | 119 |