Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Sun Apr 23 21:09:32 2017 +0000
Revision:
34:6ac9541d4c31
Parent:
32:580d74825810
Child:
36:2608622e5018
did pretty much nothing dont know what to do 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 34:6ac9541d4c31 46 Variables copyVar = weap.getVar(); //this shit is not working.
jasper0712 34:6ac9541d4c31 47 printf("copyvar %d \n",copyVar.laserDamage);
jasper0712 34:6ac9541d4c31 48 _gui.upgrades(copyVar, pad, lcd); //go to the main upgrade page
jasper0712 29:6632dd9c48d8 49 if (_gui.saveTheData == 1) {
jasper0712 29:6632dd9c48d8 50 Data _upgrades = _gui.savedata(); //saving data
jasper0712 29:6632dd9c48d8 51 //_gui.testingtest(_upgrades); //code used to test the struct
jasper0712 29:6632dd9c48d8 52 weap.setVar(_upgrades.up, _upgrades.up1);
jasper0712 29:6632dd9c48d8 53 weap.setVar(_upgrades.u1p, _upgrades.u1p1);
jasper0712 29:6632dd9c48d8 54 //weap.testtest();
jasper0712 29:6632dd9c48d8 55 //printf("TESTING up = %d, up1 = %d, u1p = %d, u1p1 = %d \n",_upgrades.up,_upgrades.up1,_upgrades.u1p,_upgrades.u1p1);
jasper0712 29:6632dd9c48d8 56 _gui.saveTheData = 0;
jasper0712 29:6632dd9c48d8 57 _gui.upgrading = 0; //get out of while loop after done upgrading
jasper0712 28:450ab72fabdc 58 }
jasper0712 28:450ab72fabdc 59 lcd.refresh();
jasper0712 29:6632dd9c48d8 60 wait(0.2);
jasper0712 28:450ab72fabdc 61 lcd.clear();
jasper0712 28:450ab72fabdc 62 }
jasper0712 32:580d74825810 63 doneUpgrade = 1;
jasper0712 28:450ab72fabdc 64 //remember to re-initialise everything after a wave.
jasper0712 28:450ab72fabdc 65 }
jasper0712 28:450ab72fabdc 66 }
jasper0712 4:4145abd09075 67 void GameEngine::drawSpawn(N5110 &lcd){
jasper0712 26:140515d80457 68 updateArray(); //clean up & update the array after the spawn took damage.
jasper0712 22:2e75b50b26f0 69 if (drawit == 0) { //DONT FORGET TO CHANGE THIS TO TICKER. MORE EFFICIENT.
jasper0712 19:7ccbb19703f9 70 drawit = 30;
jasper0712 20:32f115462bbc 71 spa.randomizeSpawn(Array, charArray);
jasper0712 20:32f115462bbc 72 spa.moveSpawnABC(Array, Array2, charArray, charArray2);
jasper0712 24:d6187d39f09b 73 spa.moveSpawnDE(Array, Array2, charArray, charArray2);
jasper0712 4:4145abd09075 74 } else {
jasper0712 19:7ccbb19703f9 75 drawit -= 0.5;
jasper0712 24:d6187d39f09b 76 spa.moveSpawnDE(Array, Array2, charArray, charArray2);
jasper0712 31:1c0e47931e84 77 spa.moveSpawnB(Array, Array2, charArray, charArray2);
jasper0712 31:1c0e47931e84 78 //printf("moving spawn b \n");
jasper0712 4:4145abd09075 79 }
jasper0712 20:32f115462bbc 80 spa.updateSpawn(Array, Array2, charArray, charArray2, lcd);
jasper0712 4:4145abd09075 81 }
jasper0712 5:c74bbdda06f4 82 void GameEngine::drawLaserPlayer(Gamepad &pad, N5110 &lcd) {
jasper0712 19:7ccbb19703f9 83 weap.drawPlayer(pad, lcd);
jasper0712 9:4c4d787c7a8b 84 //draw a line for the laser when button is pressed
jasper0712 10:926b1f89c4f1 85 if (pad.buttonhold_A == 1) {
jasper0712 7:3be4369131c6 86 //printf("hi, button is working \n");
jasper0712 5:c74bbdda06f4 87 weap.weaponMath(pad);
jasper0712 20:32f115462bbc 88 weap.drawLaser(Array2, charArray2, lcd);
jasper0712 5:c74bbdda06f4 89 }
jasper0712 5:c74bbdda06f4 90 }
jasper0712 32:580d74825810 91 void GameEngine::checkGameRule(Gamepad &pad, N5110 &lcd) {
jasper0712 34:6ac9541d4c31 92 //the only way to lose a life is when a spawn reaches y = 46.
jasper0712 34:6ac9541d4c31 93 numberOfLife_leds(pad);
jasper0712 10:926b1f89c4f1 94 for ( int x = 0; x < 84; x ++) {
jasper0712 34:6ac9541d4c31 95 if (Array[x][46] > 0) { //if anything reaches line y=46
jasper0712 34:6ac9541d4c31 96 NumberOfLife --;
jasper0712 34:6ac9541d4c31 97 printf("opps you lost a life. NumberOfLife = %d \n",NumberOfLife);
jasper0712 34:6ac9541d4c31 98 if (NumberOfLife == 0) {
jasper0712 34:6ac9541d4c31 99 while (1) {
jasper0712 34:6ac9541d4c31 100 lcd.clear();
jasper0712 34:6ac9541d4c31 101 lcd.printString("Game Over",10,3);
jasper0712 34:6ac9541d4c31 102 lcd.refresh();
jasper0712 34:6ac9541d4c31 103 pad.leds_on();
jasper0712 34:6ac9541d4c31 104 wait(0.1);
jasper0712 34:6ac9541d4c31 105 pad.leds_off();
jasper0712 34:6ac9541d4c31 106 wait(0.1);
jasper0712 34:6ac9541d4c31 107 }
jasper0712 10:926b1f89c4f1 108 }
jasper0712 10:926b1f89c4f1 109 }
jasper0712 10:926b1f89c4f1 110 }
jasper0712 5:c74bbdda06f4 111 }
jasper0712 34:6ac9541d4c31 112 void GameEngine::numberOfLife_leds(Gamepad &pad) {
jasper0712 34:6ac9541d4c31 113 //light up LEDs depends on number of lifes left.
jasper0712 34:6ac9541d4c31 114 if (NumberOfLife == 3) {
jasper0712 34:6ac9541d4c31 115 pad.leds_on();
jasper0712 34:6ac9541d4c31 116 }
jasper0712 34:6ac9541d4c31 117 if (NumberOfLife == 2) {
jasper0712 34:6ac9541d4c31 118 pad.led(1, 1.0);
jasper0712 34:6ac9541d4c31 119 pad.led(2, 1.0);
jasper0712 34:6ac9541d4c31 120 pad.led(3, 0.0);
jasper0712 34:6ac9541d4c31 121 pad.led(4, 0.0);
jasper0712 34:6ac9541d4c31 122 pad.led(5, 1.0);
jasper0712 34:6ac9541d4c31 123 pad.led(6, 1.0);
jasper0712 34:6ac9541d4c31 124 }
jasper0712 34:6ac9541d4c31 125 if (NumberOfLife == 1) {
jasper0712 34:6ac9541d4c31 126 pad.led(1, 1.0);
jasper0712 34:6ac9541d4c31 127 pad.led(2, 0.0);
jasper0712 34:6ac9541d4c31 128 pad.led(3, 0.0);
jasper0712 34:6ac9541d4c31 129 pad.led(4, 0.0);
jasper0712 34:6ac9541d4c31 130 pad.led(5, 0.0);
jasper0712 34:6ac9541d4c31 131 pad.led(6, 1.0);
jasper0712 34:6ac9541d4c31 132 }
jasper0712 34:6ac9541d4c31 133 }
jasper0712 17:53aedd20155a 134 void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) {
jasper0712 34:6ac9541d4c31 135 weap.init();
jasper0712 21:e5585569a938 136 weap.bombCooldown(Array2, pad, lcd);
jasper0712 14:064b8d7f348d 137 if (pad.check_event(Gamepad::X_PRESSED)) {
jasper0712 34:6ac9541d4c31 138 //to prevent from detonating the bomb at instant when button X is accidentally pressed
jasper0712 14:064b8d7f348d 139 }
jasper0712 19:7ccbb19703f9 140 if (pad.buttonhold_B == 1) { //if B pressed, generate shield. Shield doesnt have to be fully charged to be activated
jasper0712 15:c68a5c22a2a2 141 if (weap._shield > 0) { //shield will only activate if the shield has not depleted.
jasper0712 20:32f115462bbc 142 weap.energyShield(Array, Array2, charArray, charArray2, lcd);
jasper0712 15:c68a5c22a2a2 143 } else { //or else, deactivate the shield and recharge the shield.
jasper0712 15:c68a5c22a2a2 144 pad.buttonhold_B = 0;
jasper0712 15:c68a5c22a2a2 145 }
jasper0712 15:c68a5c22a2a2 146 }
jasper0712 15:c68a5c22a2a2 147 if (pad.buttonhold_B == 0) { // recharge shield
jasper0712 15:c68a5c22a2a2 148 if (weap._shield < weap._shieldCapacity ) {
jasper0712 15:c68a5c22a2a2 149 weap._shield = weap._shield + weap._shieldRegenRate;
jasper0712 15:c68a5c22a2a2 150 //printf("current shield = %d \n",weap._shield);
jasper0712 16:ddc4d5669a6e 151 //printf("shield regen rate = %d \n",weap._shieldRegenRate);
jasper0712 19:7ccbb19703f9 152 } else { //when the shield is fully charged. It will automatically activate the shield.
jasper0712 27:d4926f19c12a 153 pad.buttonhold_B = 1;
jasper0712 19:7ccbb19703f9 154 }
jasper0712 14:064b8d7f348d 155 }
jasper0712 26:140515d80457 156 weap.shieldMeter(lcd);
jasper0712 26:140515d80457 157 }
jasper0712 26:140515d80457 158 void GameEngine::updateArray() { //clean up tool
jasper0712 26:140515d80457 159 for (int x = 0; x < Cols; x++) {
jasper0712 26:140515d80457 160 for (int y = 0; y < Rows; y++) {
jasper0712 26:140515d80457 161 Array[x][y] = Array2[x][y];
jasper0712 26:140515d80457 162 //printf(" Array[%d][%d] = %d \n",x,y,Array2[x][y]);
jasper0712 26:140515d80457 163 spa.deleteChar(x, y, Array, charArray2);
jasper0712 26:140515d80457 164 }
jasper0712 26:140515d80457 165 }
jasper0712 10:926b1f89c4f1 166 }