Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Mon Apr 24 11:11:39 2017 +0000
Revision:
37:45da88e36d1d
Parent:
36:2608622e5018
Child:
38:c66a69c1a6de
linked weapon to gui. continue on cannon now

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 26:140515d80457 27 _d1.droneInit(20, 46, 0, 42); //drone1 is taking left side pixel 0 to 42.
jasper0712 31:1c0e47931e84 28 _d2.droneInit(62, 46, 42, 84); //drone2 is taking right side pixel 42 to 83;
jasper0712 18:d82059ce929b 29 weap.init();
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 27:d4926f19c12a 34 _d1.droneScan(Array2, charArray2, lcd);
jasper0712 27:d4926f19c12a 35 _d2.droneScan(Array2, charArray2, lcd);
jasper0712 17:53aedd20155a 36 drawSpawn(lcd);
jasper0712 17:53aedd20155a 37 drawLaserPlayer(pad, lcd);
jasper0712 17:53aedd20155a 38 bombAndShield(pad, lcd);
jasper0712 32:580d74825810 39 checkGameRule(pad, lcd);
jasper0712 17:53aedd20155a 40 }
jasper0712 28:450ab72fabdc 41 void GameEngine::weapUpgrade(Gamepad &pad, N5110 &lcd) {
jasper0712 28:450ab72fabdc 42 if (pad.check_event(Gamepad::START_PRESSED)) {
jasper0712 29:6632dd9c48d8 43 _gui.upgrading = 1;
jasper0712 29:6632dd9c48d8 44 _gui.preventDoubleClick(pad);
jasper0712 29:6632dd9c48d8 45 while (_gui.upgrading == 1) {
jasper0712 37:45da88e36d1d 46 //printf("laserDamage = %i \n",weap._laserDamage);
jasper0712 37:45da88e36d1d 47 //go to the main upgrade page with all the values (from Weapon) as reference.
jasper0712 37:45da88e36d1d 48 _gui.upgrades(weap._laserDamage, weap._bombDamage, weap._bombR, weap._bombCooldown, _d1._droneDamage, _d1._droneRange, weap._shieldCapacity, weap._shieldRegenRate, pad, lcd);
jasper0712 37:45da88e36d1d 49 //printf("_gui.saveTheData = %d",_gui.saveTheData);
jasper0712 29:6632dd9c48d8 50 if (_gui.saveTheData == 1) {
jasper0712 29:6632dd9c48d8 51 Data _upgrades = _gui.savedata(); //saving data
jasper0712 29:6632dd9c48d8 52 //_gui.testingtest(_upgrades); //code used to test the struct
jasper0712 29:6632dd9c48d8 53 weap.setVar(_upgrades.up, _upgrades.up1);
jasper0712 29:6632dd9c48d8 54 weap.setVar(_upgrades.u1p, _upgrades.u1p1);
jasper0712 37:45da88e36d1d 55 weap.testtest();
jasper0712 29:6632dd9c48d8 56 //printf("TESTING up = %d, up1 = %d, u1p = %d, u1p1 = %d \n",_upgrades.up,_upgrades.up1,_upgrades.u1p,_upgrades.u1p1);
jasper0712 29:6632dd9c48d8 57 _gui.saveTheData = 0;
jasper0712 29:6632dd9c48d8 58 _gui.upgrading = 0; //get out of while loop after done upgrading
jasper0712 28:450ab72fabdc 59 }
jasper0712 28:450ab72fabdc 60 lcd.refresh();
jasper0712 29:6632dd9c48d8 61 wait(0.2);
jasper0712 28:450ab72fabdc 62 lcd.clear();
jasper0712 28:450ab72fabdc 63 }
jasper0712 37:45da88e36d1d 64 doneUpgrade_flag = 1;
jasper0712 28:450ab72fabdc 65 //remember to re-initialise everything after a wave.
jasper0712 28:450ab72fabdc 66 }
jasper0712 28:450ab72fabdc 67 }
jasper0712 4:4145abd09075 68 void GameEngine::drawSpawn(N5110 &lcd){
jasper0712 26:140515d80457 69 updateArray(); //clean up & update the array after the spawn took damage.
jasper0712 36:2608622e5018 70 if (drawit == 0) { //
jasper0712 19:7ccbb19703f9 71 drawit = 30;
jasper0712 20:32f115462bbc 72 spa.randomizeSpawn(Array, charArray);
jasper0712 20:32f115462bbc 73 spa.moveSpawnABC(Array, Array2, charArray, charArray2);
jasper0712 24:d6187d39f09b 74 spa.moveSpawnDE(Array, Array2, charArray, charArray2);
jasper0712 36:2608622e5018 75 //printf("why u do this to cannon_Firing?? cannon_Firing = %d \n",weap.cannon_Firing);
jasper0712 36:2608622e5018 76 if ( fireInTheHole_flag ==1) { //this code because whenever spawn ABCDE moves, weap.cannon_Firing goes to 0 automatically (NO IDEA WHY)
jasper0712 36:2608622e5018 77 weap.cannon_Firing = 1;
jasper0712 36:2608622e5018 78 }
jasper0712 4:4145abd09075 79 } else {
jasper0712 19:7ccbb19703f9 80 drawit -= 0.5;
jasper0712 24:d6187d39f09b 81 spa.moveSpawnDE(Array, Array2, charArray, charArray2);
jasper0712 31:1c0e47931e84 82 spa.moveSpawnB(Array, Array2, charArray, charArray2);
jasper0712 31:1c0e47931e84 83 //printf("moving spawn b \n");
jasper0712 4:4145abd09075 84 }
jasper0712 20:32f115462bbc 85 spa.updateSpawn(Array, Array2, charArray, charArray2, lcd);
jasper0712 4:4145abd09075 86 }
jasper0712 5:c74bbdda06f4 87 void GameEngine::drawLaserPlayer(Gamepad &pad, N5110 &lcd) {
jasper0712 19:7ccbb19703f9 88 weap.drawPlayer(pad, lcd);
jasper0712 9:4c4d787c7a8b 89 //draw a line for the laser when button is pressed
jasper0712 10:926b1f89c4f1 90 if (pad.buttonhold_A == 1) {
jasper0712 7:3be4369131c6 91 //printf("hi, button is working \n");
jasper0712 5:c74bbdda06f4 92 weap.weaponMath(pad);
jasper0712 20:32f115462bbc 93 weap.drawLaser(Array2, charArray2, lcd);
jasper0712 36:2608622e5018 94
jasper0712 36:2608622e5018 95 //cannon can only be fired when the laser is switched on.
jasper0712 36:2608622e5018 96 if ( pad.check_event(Gamepad::Y_PRESSED)) { //press button to set flag (fire the cannon)
jasper0712 36:2608622e5018 97 fireInTheHole_flag = 1;
jasper0712 36:2608622e5018 98 }
jasper0712 36:2608622e5018 99 }
jasper0712 36:2608622e5018 100 if ( fireInTheHole_flag == 1) {
jasper0712 36:2608622e5018 101 weap.cannon(lcd);
jasper0712 36:2608622e5018 102 }
jasper0712 36:2608622e5018 103 if ( weap.cannon_Firing == 0) { //clear flag when cannon is done
jasper0712 36:2608622e5018 104 fireInTheHole_flag = 0;
jasper0712 5:c74bbdda06f4 105 }
jasper0712 5:c74bbdda06f4 106 }
jasper0712 32:580d74825810 107 void GameEngine::checkGameRule(Gamepad &pad, N5110 &lcd) {
jasper0712 34:6ac9541d4c31 108 //the only way to lose a life is when a spawn reaches y = 46.
jasper0712 34:6ac9541d4c31 109 numberOfLife_leds(pad);
jasper0712 10:926b1f89c4f1 110 for ( int x = 0; x < 84; x ++) {
jasper0712 34:6ac9541d4c31 111 if (Array[x][46] > 0) { //if anything reaches line y=46
jasper0712 34:6ac9541d4c31 112 NumberOfLife --;
jasper0712 34:6ac9541d4c31 113 printf("opps you lost a life. NumberOfLife = %d \n",NumberOfLife);
jasper0712 34:6ac9541d4c31 114 if (NumberOfLife == 0) {
jasper0712 34:6ac9541d4c31 115 while (1) {
jasper0712 34:6ac9541d4c31 116 lcd.clear();
jasper0712 34:6ac9541d4c31 117 lcd.printString("Game Over",10,3);
jasper0712 34:6ac9541d4c31 118 lcd.refresh();
jasper0712 34:6ac9541d4c31 119 pad.leds_on();
jasper0712 34:6ac9541d4c31 120 wait(0.1);
jasper0712 34:6ac9541d4c31 121 pad.leds_off();
jasper0712 34:6ac9541d4c31 122 wait(0.1);
jasper0712 34:6ac9541d4c31 123 }
jasper0712 10:926b1f89c4f1 124 }
jasper0712 10:926b1f89c4f1 125 }
jasper0712 10:926b1f89c4f1 126 }
jasper0712 5:c74bbdda06f4 127 }
jasper0712 34:6ac9541d4c31 128 void GameEngine::numberOfLife_leds(Gamepad &pad) {
jasper0712 34:6ac9541d4c31 129 //light up LEDs depends on number of lifes left.
jasper0712 34:6ac9541d4c31 130 if (NumberOfLife == 3) {
jasper0712 34:6ac9541d4c31 131 pad.leds_on();
jasper0712 34:6ac9541d4c31 132 }
jasper0712 34:6ac9541d4c31 133 if (NumberOfLife == 2) {
jasper0712 34:6ac9541d4c31 134 pad.led(1, 1.0);
jasper0712 34:6ac9541d4c31 135 pad.led(2, 1.0);
jasper0712 34:6ac9541d4c31 136 pad.led(3, 0.0);
jasper0712 34:6ac9541d4c31 137 pad.led(4, 0.0);
jasper0712 34:6ac9541d4c31 138 pad.led(5, 1.0);
jasper0712 34:6ac9541d4c31 139 pad.led(6, 1.0);
jasper0712 34:6ac9541d4c31 140 }
jasper0712 34:6ac9541d4c31 141 if (NumberOfLife == 1) {
jasper0712 34:6ac9541d4c31 142 pad.led(1, 1.0);
jasper0712 34:6ac9541d4c31 143 pad.led(2, 0.0);
jasper0712 34:6ac9541d4c31 144 pad.led(3, 0.0);
jasper0712 34:6ac9541d4c31 145 pad.led(4, 0.0);
jasper0712 34:6ac9541d4c31 146 pad.led(5, 0.0);
jasper0712 34:6ac9541d4c31 147 pad.led(6, 1.0);
jasper0712 34:6ac9541d4c31 148 }
jasper0712 34:6ac9541d4c31 149 }
jasper0712 17:53aedd20155a 150 void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) {
jasper0712 34:6ac9541d4c31 151 weap.init();
jasper0712 21:e5585569a938 152 weap.bombCooldown(Array2, pad, lcd);
jasper0712 14:064b8d7f348d 153 if (pad.check_event(Gamepad::X_PRESSED)) {
jasper0712 34:6ac9541d4c31 154 //to prevent from detonating the bomb at instant when button X is accidentally pressed
jasper0712 14:064b8d7f348d 155 }
jasper0712 19:7ccbb19703f9 156 if (pad.buttonhold_B == 1) { //if B pressed, generate shield. Shield doesnt have to be fully charged to be activated
jasper0712 15:c68a5c22a2a2 157 if (weap._shield > 0) { //shield will only activate if the shield has not depleted.
jasper0712 20:32f115462bbc 158 weap.energyShield(Array, Array2, charArray, charArray2, lcd);
jasper0712 15:c68a5c22a2a2 159 } else { //or else, deactivate the shield and recharge the shield.
jasper0712 15:c68a5c22a2a2 160 pad.buttonhold_B = 0;
jasper0712 15:c68a5c22a2a2 161 }
jasper0712 15:c68a5c22a2a2 162 }
jasper0712 15:c68a5c22a2a2 163 if (pad.buttonhold_B == 0) { // recharge shield
jasper0712 15:c68a5c22a2a2 164 if (weap._shield < weap._shieldCapacity ) {
jasper0712 15:c68a5c22a2a2 165 weap._shield = weap._shield + weap._shieldRegenRate;
jasper0712 15:c68a5c22a2a2 166 //printf("current shield = %d \n",weap._shield);
jasper0712 16:ddc4d5669a6e 167 //printf("shield regen rate = %d \n",weap._shieldRegenRate);
jasper0712 19:7ccbb19703f9 168 } else { //when the shield is fully charged. It will automatically activate the shield.
jasper0712 27:d4926f19c12a 169 pad.buttonhold_B = 1;
jasper0712 19:7ccbb19703f9 170 }
jasper0712 14:064b8d7f348d 171 }
jasper0712 26:140515d80457 172 weap.shieldMeter(lcd);
jasper0712 26:140515d80457 173 }
jasper0712 26:140515d80457 174 void GameEngine::updateArray() { //clean up tool
jasper0712 26:140515d80457 175 for (int x = 0; x < Cols; x++) {
jasper0712 26:140515d80457 176 for (int y = 0; y < Rows; y++) {
jasper0712 26:140515d80457 177 Array[x][y] = Array2[x][y];
jasper0712 26:140515d80457 178 //printf(" Array[%d][%d] = %d \n",x,y,Array2[x][y]);
jasper0712 26:140515d80457 179 spa.deleteChar(x, y, Array, charArray2);
jasper0712 26:140515d80457 180 }
jasper0712 26:140515d80457 181 }
jasper0712 10:926b1f89c4f1 182 }