Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Sun Apr 30 22:06:22 2017 +0000
Revision:
42:152e84494ad9
Parent:
41:1b396334256b
Child:
44:aa086363d09f
fixed a cannon bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasper0712 4:4145abd09075 1 #include "GameEngine.h"
jasper0712 4:4145abd09075 2
jasper0712 4:4145abd09075 3 GameEngine::GameEngine()
jasper0712 4:4145abd09075 4 {
jasper0712 4:4145abd09075 5
jasper0712 4:4145abd09075 6 }
jasper0712 4:4145abd09075 7
jasper0712 4:4145abd09075 8 GameEngine::~GameEngine()
jasper0712 4:4145abd09075 9 {
jasper0712 4:4145abd09075 10
jasper0712 4:4145abd09075 11 }
jasper0712 32:580d74825810 12 void GameEngine::startingMenu(Gamepad &pad, N5110 &lcd) {
jasper0712 32:580d74825810 13 _gui.startingMenu(lcd);
jasper0712 32:580d74825810 14 ledNumber ++;
jasper0712 32:580d74825810 15 pad.led(ledNumber, 1.0);
jasper0712 32:580d74825810 16 pad.led(ledNumber - 1, 0.0);
jasper0712 32:580d74825810 17 if (ledNumber == 6) {
jasper0712 32:580d74825810 18 ledNumber = 0;
jasper0712 32:580d74825810 19 } else {
jasper0712 32:580d74825810 20 pad.led(6, 0.0);
jasper0712 32:580d74825810 21 }
jasper0712 32:580d74825810 22 _gui.preventDoubleClick(pad);
jasper0712 32:580d74825810 23
jasper0712 32:580d74825810 24 }
jasper0712 32:580d74825810 25 void GameEngine::init(int w) {
jasper0712 34:6ac9541d4c31 26 NumberOfLife = 3; //3 life on every wave
jasper0712 38:c66a69c1a6de 27 weap.init();
jasper0712 26:140515d80457 28 _d1.droneInit(20, 46, 0, 42); //drone1 is taking left side pixel 0 to 42.
jasper0712 31:1c0e47931e84 29 _d2.droneInit(62, 46, 42, 84); //drone2 is taking right side pixel 42 to 83;
jasper0712 32:580d74825810 30 spa.init(w);
jasper0712 19:7ccbb19703f9 31 //printf("init completed \n");
jasper0712 17:53aedd20155a 32 }
jasper0712 17:53aedd20155a 33 void GameEngine::update(Gamepad &pad, N5110 &lcd) {
jasper0712 38:c66a69c1a6de 34 weap.init();
jasper0712 40:3a0c66a0e10e 35 _d1.drone_main(scnd.Arr, scnd.cArr, lcd);
jasper0712 40:3a0c66a0e10e 36 _d2.drone_main(scnd.Arr, scnd.cArr, lcd);
jasper0712 17:53aedd20155a 37 drawSpawn(lcd);
jasper0712 40:3a0c66a0e10e 38 laser_and_cannon(pad, lcd);
jasper0712 40:3a0c66a0e10e 39 bomb_And_Shield(pad, lcd);
jasper0712 32:580d74825810 40 checkGameRule(pad, lcd);
jasper0712 41:1b396334256b 41 //_gui.preventDoubleClick(pad);
jasper0712 17:53aedd20155a 42 }
jasper0712 28:450ab72fabdc 43 void GameEngine::weapUpgrade(Gamepad &pad, N5110 &lcd) {
jasper0712 28:450ab72fabdc 44 if (pad.check_event(Gamepad::START_PRESSED)) {
jasper0712 40:3a0c66a0e10e 45 _gui.upgrading_flag = 1;
jasper0712 29:6632dd9c48d8 46 _gui.preventDoubleClick(pad);
jasper0712 40:3a0c66a0e10e 47 while (_gui.upgrading_flag == 1) {
jasper0712 37:45da88e36d1d 48 //printf("laserDamage = %i \n",weap._laserDamage);
jasper0712 40:3a0c66a0e10e 49 //go to the upgrade page with all the values (from Weapon) as reference.
jasper0712 38:c66a69c1a6de 50 _gui.upgrades(weap._laserDamage, weap._bombDamage, weap._bombRange, weap._bombCooldown, weap._droneDamage, weap._droneRange, weap._shieldCapacity, weap._shieldRegenRate, weap._cannonDamage, weap._cannonCapacity, weap._cannonRegenRate, pad, lcd);
jasper0712 37:45da88e36d1d 51 //printf("_gui.saveTheData = %d",_gui.saveTheData);
jasper0712 40:3a0c66a0e10e 52 if (_gui.saveTheData_flag == 1) {
jasper0712 40:3a0c66a0e10e 53 Data _upgrades = _gui.get_data(); //saving data
jasper0712 29:6632dd9c48d8 54 //_gui.testingtest(_upgrades); //code used to test the struct
jasper0712 29:6632dd9c48d8 55 weap.setVar(_upgrades.up, _upgrades.up1);
jasper0712 29:6632dd9c48d8 56 weap.setVar(_upgrades.u1p, _upgrades.u1p1);
jasper0712 29:6632dd9c48d8 57 //printf("TESTING up = %d, up1 = %d, u1p = %d, u1p1 = %d \n",_upgrades.up,_upgrades.up1,_upgrades.u1p,_upgrades.u1p1);
jasper0712 40:3a0c66a0e10e 58 _gui.saveTheData_flag = 0;
jasper0712 40:3a0c66a0e10e 59 _gui.upgrading_flag = 0; //get out of while loop after done upgrading
jasper0712 28:450ab72fabdc 60 }
jasper0712 28:450ab72fabdc 61 lcd.refresh();
jasper0712 29:6632dd9c48d8 62 wait(0.2);
jasper0712 28:450ab72fabdc 63 lcd.clear();
jasper0712 28:450ab72fabdc 64 }
jasper0712 37:45da88e36d1d 65 doneUpgrade_flag = 1;
jasper0712 28:450ab72fabdc 66 //remember to re-initialise everything after a wave.
jasper0712 28:450ab72fabdc 67 }
jasper0712 28:450ab72fabdc 68 }
jasper0712 4:4145abd09075 69 void GameEngine::drawSpawn(N5110 &lcd){
jasper0712 40:3a0c66a0e10e 70 updateArray(); //clean up & update the main.Arr after the spawn took damage.
jasper0712 36:2608622e5018 71 if (drawit == 0) { //
jasper0712 19:7ccbb19703f9 72 drawit = 30;
jasper0712 40:3a0c66a0e10e 73 spa.summon_spawn(main.Arr, main.cArr);
jasper0712 40:3a0c66a0e10e 74 spa.moveSpawnABC(main.Arr, scnd.Arr, main.cArr, scnd.cArr);
jasper0712 40:3a0c66a0e10e 75 spa.moveSpawnDE(main.Arr, scnd.Arr, main.cArr, scnd.cArr);
jasper0712 42:152e84494ad9 76 //printf("why u do this to cannonFiring_flag?? cannonFiring_flag = %d \n",weap.cannonFiring_flag);
jasper0712 42:152e84494ad9 77 if ( fireInTheHole_flag ==1) { //this code because whenever spawn ABCDE moves, weap.cannonFiring_flag goes to 0 automatically (NO IDEA WHY)
jasper0712 42:152e84494ad9 78 weap.cannonFiring_flag = 1;
jasper0712 36:2608622e5018 79 }
jasper0712 4:4145abd09075 80 } else {
jasper0712 19:7ccbb19703f9 81 drawit -= 0.5;
jasper0712 40:3a0c66a0e10e 82 spa.moveSpawnDE(main.Arr, scnd.Arr, main.cArr, scnd.cArr);
jasper0712 40:3a0c66a0e10e 83 spa.moveSpawnB(main.Arr, scnd.Arr, main.cArr, scnd.cArr);
jasper0712 31:1c0e47931e84 84 //printf("moving spawn b \n");
jasper0712 4:4145abd09075 85 }
jasper0712 40:3a0c66a0e10e 86 spa.updateSpawn(main.Arr, scnd.Arr, main.cArr, scnd.cArr, lcd);
jasper0712 4:4145abd09075 87 }
jasper0712 40:3a0c66a0e10e 88 void GameEngine::laser_and_cannon(Gamepad &pad, N5110 &lcd) {
jasper0712 19:7ccbb19703f9 89 weap.drawPlayer(pad, lcd);
jasper0712 40:3a0c66a0e10e 90 weap.cannon_recharge();
jasper0712 38:c66a69c1a6de 91 weap.cannon_Meter(lcd);
jasper0712 9:4c4d787c7a8b 92 //draw a line for the laser when button is pressed
jasper0712 10:926b1f89c4f1 93 if (pad.buttonhold_A == 1) {
jasper0712 7:3be4369131c6 94 //printf("hi, button is working \n");
jasper0712 5:c74bbdda06f4 95 weap.weaponMath(pad);
jasper0712 40:3a0c66a0e10e 96 weap.laser_Main(scnd.Arr, scnd.cArr, lcd);
jasper0712 36:2608622e5018 97
jasper0712 36:2608622e5018 98 //cannon can only be fired when the laser is switched on.
jasper0712 41:1b396334256b 99 if ( weap._cannon > 320) { //when sufficient, check for button Y.
jasper0712 38:c66a69c1a6de 100 if ( pad.check_event(Gamepad::Y_PRESSED)) { //press button to set flag (fire the cannon)
jasper0712 38:c66a69c1a6de 101 fireInTheHole_flag = 1;
jasper0712 38:c66a69c1a6de 102 }
jasper0712 36:2608622e5018 103 }
jasper0712 36:2608622e5018 104 }
jasper0712 36:2608622e5018 105 if ( fireInTheHole_flag == 1) {
jasper0712 40:3a0c66a0e10e 106 weap.cannon_main(scnd.Arr, scnd.cArr, lcd);
jasper0712 36:2608622e5018 107 }
jasper0712 42:152e84494ad9 108 if ( weap.cannonFiring_flag == 0) { //clear flag when cannon is done
jasper0712 36:2608622e5018 109 fireInTheHole_flag = 0;
jasper0712 5:c74bbdda06f4 110 }
jasper0712 5:c74bbdda06f4 111 }
jasper0712 32:580d74825810 112 void GameEngine::checkGameRule(Gamepad &pad, N5110 &lcd) {
jasper0712 34:6ac9541d4c31 113 //the only way to lose a life is when a spawn reaches y = 46.
jasper0712 34:6ac9541d4c31 114 numberOfLife_leds(pad);
jasper0712 10:926b1f89c4f1 115 for ( int x = 0; x < 84; x ++) {
jasper0712 40:3a0c66a0e10e 116 if (main.Arr[x][46] > 0) { //if anything reaches line y=46
jasper0712 34:6ac9541d4c31 117 NumberOfLife --;
jasper0712 41:1b396334256b 118 pad.tone(750.0,0.5);
jasper0712 40:3a0c66a0e10e 119 //printf("opps you lost a life. NumberOfLife = %d \n",NumberOfLife);
jasper0712 34:6ac9541d4c31 120 if (NumberOfLife == 0) {
jasper0712 40:3a0c66a0e10e 121 gameOver(pad, lcd);
jasper0712 10:926b1f89c4f1 122 }
jasper0712 10:926b1f89c4f1 123 }
jasper0712 10:926b1f89c4f1 124 }
jasper0712 5:c74bbdda06f4 125 }
jasper0712 40:3a0c66a0e10e 126 void GameEngine::gameOver(Gamepad &pad, N5110 &lcd) {
jasper0712 40:3a0c66a0e10e 127 while (retry_flag == 0 && gameMenu_flag == 0) { //while both are 0
jasper0712 40:3a0c66a0e10e 128 lcd.clear();
jasper0712 40:3a0c66a0e10e 129 lcd.printString("Game Over",10,3);
jasper0712 42:152e84494ad9 130 lcd.printString("GiveUp Retry",0,5);
jasper0712 40:3a0c66a0e10e 131 lcd.refresh();
jasper0712 40:3a0c66a0e10e 132 pad.leds_on();
jasper0712 40:3a0c66a0e10e 133 wait(0.1);
jasper0712 40:3a0c66a0e10e 134 pad.leds_off();
jasper0712 40:3a0c66a0e10e 135 wait(0.1);
jasper0712 42:152e84494ad9 136 //printf("press a button pls. you lost retry_flag = %d gameMenu_flag = %d\n",retry_flag,gameMenu_flag);
jasper0712 40:3a0c66a0e10e 137 //must click atleast one button to continue
jasper0712 40:3a0c66a0e10e 138 if (pad.check_event(Gamepad::START_PRESSED)) {
jasper0712 40:3a0c66a0e10e 139 retry_flag = 1;
jasper0712 40:3a0c66a0e10e 140 printf("retry pressed \n");
jasper0712 40:3a0c66a0e10e 141 update_RetryToMain();
jasper0712 40:3a0c66a0e10e 142 }
jasper0712 40:3a0c66a0e10e 143 if (pad.check_event(Gamepad::BACK_PRESSED)) {
jasper0712 40:3a0c66a0e10e 144 gameMenu_flag = 1; //bring the player to game menu
jasper0712 40:3a0c66a0e10e 145 printf("player surrendered \n");
jasper0712 40:3a0c66a0e10e 146 }
jasper0712 40:3a0c66a0e10e 147
jasper0712 40:3a0c66a0e10e 148 }
jasper0712 40:3a0c66a0e10e 149 }
jasper0712 34:6ac9541d4c31 150 void GameEngine::numberOfLife_leds(Gamepad &pad) {
jasper0712 34:6ac9541d4c31 151 //light up LEDs depends on number of lifes left.
jasper0712 34:6ac9541d4c31 152 if (NumberOfLife == 3) {
jasper0712 40:3a0c66a0e10e 153 pad.leds_on(); //turn on all leds
jasper0712 34:6ac9541d4c31 154 }
jasper0712 34:6ac9541d4c31 155 if (NumberOfLife == 2) {
jasper0712 40:3a0c66a0e10e 156 pad.led(1, 1.0); //turn on led 1, 2, 5 and 6
jasper0712 34:6ac9541d4c31 157 pad.led(2, 1.0);
jasper0712 34:6ac9541d4c31 158 pad.led(3, 0.0);
jasper0712 34:6ac9541d4c31 159 pad.led(4, 0.0);
jasper0712 34:6ac9541d4c31 160 pad.led(5, 1.0);
jasper0712 34:6ac9541d4c31 161 pad.led(6, 1.0);
jasper0712 34:6ac9541d4c31 162 }
jasper0712 34:6ac9541d4c31 163 if (NumberOfLife == 1) {
jasper0712 40:3a0c66a0e10e 164 pad.led(1, 1.0); //turn on led 1 and 6
jasper0712 34:6ac9541d4c31 165 pad.led(2, 0.0);
jasper0712 34:6ac9541d4c31 166 pad.led(3, 0.0);
jasper0712 34:6ac9541d4c31 167 pad.led(4, 0.0);
jasper0712 34:6ac9541d4c31 168 pad.led(5, 0.0);
jasper0712 34:6ac9541d4c31 169 pad.led(6, 1.0);
jasper0712 34:6ac9541d4c31 170 }
jasper0712 34:6ac9541d4c31 171 }
jasper0712 40:3a0c66a0e10e 172 void GameEngine::bomb_And_Shield(Gamepad &pad, N5110 &lcd) {
jasper0712 40:3a0c66a0e10e 173 weap.bomb_main(scnd.Arr, pad, lcd);
jasper0712 14:064b8d7f348d 174 if (pad.check_event(Gamepad::X_PRESSED)) {
jasper0712 34:6ac9541d4c31 175 //to prevent from detonating the bomb at instant when button X is accidentally pressed
jasper0712 40:3a0c66a0e10e 176 //printf("instant bomb detonate prevented");
jasper0712 14:064b8d7f348d 177 }
jasper0712 40:3a0c66a0e10e 178 weap.shield_main(scnd.Arr, scnd.cArr, pad, lcd);
jasper0712 40:3a0c66a0e10e 179 if (pad.check_event(Gamepad::B_PRESSED)) {
jasper0712 40:3a0c66a0e10e 180 //to prevent the button from clicking twice
jasper0712 40:3a0c66a0e10e 181 printf("shield double click prevented");
jasper0712 14:064b8d7f348d 182 }
jasper0712 26:140515d80457 183 }
jasper0712 41:1b396334256b 184 void GameEngine::updateArray() { //update main array with secondary array and clean up the array
jasper0712 26:140515d80457 185 for (int x = 0; x < Cols; x++) {
jasper0712 26:140515d80457 186 for (int y = 0; y < Rows; y++) {
jasper0712 40:3a0c66a0e10e 187 main.Arr[x][y] = scnd.Arr[x][y];
jasper0712 40:3a0c66a0e10e 188 //printf(" main.Arr[%d][%d] = %d \n",x,y,scnd.Arr[x][y]);
jasper0712 40:3a0c66a0e10e 189 spa.deleteChar(x, y, main.Arr, scnd.cArr);
jasper0712 26:140515d80457 190 }
jasper0712 26:140515d80457 191 }
jasper0712 40:3a0c66a0e10e 192 }
jasper0712 41:1b396334256b 193 void GameEngine::update_MainToRetry() { //update Retry array with main array
jasper0712 40:3a0c66a0e10e 194 for (int x = 0; x < Cols; x ++) {
jasper0712 40:3a0c66a0e10e 195 for (int y = 0; y < Rows; y ++) {
jasper0712 40:3a0c66a0e10e 196 retry.Arr[x][y] = main.Arr[x][y];
jasper0712 40:3a0c66a0e10e 197 retry.cArr[x][y]= main.cArr[x][y];
jasper0712 40:3a0c66a0e10e 198 }
jasper0712 40:3a0c66a0e10e 199 }
jasper0712 40:3a0c66a0e10e 200 printf("main to retry array updated \n");
jasper0712 40:3a0c66a0e10e 201 }
jasper0712 41:1b396334256b 202 void GameEngine::update_RetryToMain() {//update main & secondary array with Retry array
jasper0712 40:3a0c66a0e10e 203 for (int x = 0; x < Cols; x ++) {
jasper0712 40:3a0c66a0e10e 204 for (int y = 0; y < Rows; y ++) {
jasper0712 40:3a0c66a0e10e 205 main.Arr[x][y] = retry.Arr[x][y];
jasper0712 40:3a0c66a0e10e 206 main.cArr[x][y] = retry.cArr[x][y];
jasper0712 40:3a0c66a0e10e 207 scnd.Arr[x][y] = retry.Arr[x][y];
jasper0712 40:3a0c66a0e10e 208 scnd.cArr[x][y] = retry.cArr[x][y];
jasper0712 40:3a0c66a0e10e 209 }
jasper0712 40:3a0c66a0e10e 210 }
jasper0712 40:3a0c66a0e10e 211 printf("retry to main array updated \n");
jasper0712 41:1b396334256b 212 }
jasper0712 41:1b396334256b 213 void GameEngine::reset_Game() { //clear everything - arrays and weapon
jasper0712 41:1b396334256b 214 for (int x = 0; x < Cols; x ++) {
jasper0712 41:1b396334256b 215 for (int y = 0; y < Rows; y ++) {
jasper0712 41:1b396334256b 216 main.Arr[x][y] = 0;
jasper0712 41:1b396334256b 217 main.cArr[x][y] = '\0';
jasper0712 41:1b396334256b 218 scnd.Arr[x][y] = 0;
jasper0712 41:1b396334256b 219 scnd.cArr[x][y] = '\0';
jasper0712 41:1b396334256b 220 }
jasper0712 41:1b396334256b 221 }
jasper0712 41:1b396334256b 222 weap.reset_WeaponVariables();
jasper0712 10:926b1f89c4f1 223 }