Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Tue Apr 18 11:21:35 2017 +0000
Revision:
29:6632dd9c48d8
Parent:
28:450ab72fabdc
Child:
30:2e2d48cbfec3
successfully linked weaponUpgrades to weapon. ; Now starting on linking spawn.

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 17:53aedd20155a 12 void GameEngine::init() {
jasper0712 26:140515d80457 13 _d1.droneInit(20, 46, 0, 42); //drone1 is taking left side pixel 0 to 42.
jasper0712 26:140515d80457 14 _d2.droneInit(62, 46, 42, 84); //drone2 is taking right side pixel 42 to 84;
jasper0712 18:d82059ce929b 15 weap.init();
jasper0712 18:d82059ce929b 16 spa.init();
jasper0712 19:7ccbb19703f9 17 //printf("init completed \n");
jasper0712 17:53aedd20155a 18 }
jasper0712 17:53aedd20155a 19 void GameEngine::update(Gamepad &pad, N5110 &lcd) {
jasper0712 27:d4926f19c12a 20 _d1.droneScan(Array2, charArray2, lcd);
jasper0712 27:d4926f19c12a 21 _d2.droneScan(Array2, charArray2, lcd);
jasper0712 17:53aedd20155a 22 drawSpawn(lcd);
jasper0712 17:53aedd20155a 23 drawLaserPlayer(pad, lcd);
jasper0712 17:53aedd20155a 24 bombAndShield(pad, lcd);
jasper0712 17:53aedd20155a 25 }
jasper0712 28:450ab72fabdc 26 void GameEngine::weapUpgrade(Gamepad &pad, N5110 &lcd) {
jasper0712 28:450ab72fabdc 27 if (pad.check_event(Gamepad::START_PRESSED)) {
jasper0712 29:6632dd9c48d8 28 _gui.upgrading = 1;
jasper0712 29:6632dd9c48d8 29 _gui.preventDoubleClick(pad);
jasper0712 29:6632dd9c48d8 30 while (_gui.upgrading == 1) {
jasper0712 29:6632dd9c48d8 31 _gui.main_Upgrades(pad, lcd); //go to the main upgrade page
jasper0712 29:6632dd9c48d8 32 if (_gui.saveTheData == 1) {
jasper0712 29:6632dd9c48d8 33 Data _upgrades = _gui.savedata(); //saving data
jasper0712 29:6632dd9c48d8 34 //_gui.testingtest(_upgrades); //code used to test the struct
jasper0712 29:6632dd9c48d8 35 weap.setVar(_upgrades.up, _upgrades.up1);
jasper0712 29:6632dd9c48d8 36 weap.setVar(_upgrades.u1p, _upgrades.u1p1);
jasper0712 29:6632dd9c48d8 37 //weap.testtest();
jasper0712 29:6632dd9c48d8 38 //printf("TESTING up = %d, up1 = %d, u1p = %d, u1p1 = %d \n",_upgrades.up,_upgrades.up1,_upgrades.u1p,_upgrades.u1p1);
jasper0712 29:6632dd9c48d8 39 _gui.saveTheData = 0;
jasper0712 29:6632dd9c48d8 40 _gui.upgrading = 0; //get out of while loop after done upgrading
jasper0712 28:450ab72fabdc 41 }
jasper0712 28:450ab72fabdc 42 lcd.refresh();
jasper0712 29:6632dd9c48d8 43 wait(0.2);
jasper0712 28:450ab72fabdc 44 lcd.clear();
jasper0712 28:450ab72fabdc 45 }
jasper0712 28:450ab72fabdc 46 done = 1;
jasper0712 28:450ab72fabdc 47 //remember to re-initialise everything after a wave.
jasper0712 28:450ab72fabdc 48 }
jasper0712 28:450ab72fabdc 49 }
jasper0712 4:4145abd09075 50 void GameEngine::drawSpawn(N5110 &lcd){
jasper0712 26:140515d80457 51 updateArray(); //clean up & update the array after the spawn took damage.
jasper0712 22:2e75b50b26f0 52 if (drawit == 0) { //DONT FORGET TO CHANGE THIS TO TICKER. MORE EFFICIENT.
jasper0712 19:7ccbb19703f9 53 drawit = 30;
jasper0712 20:32f115462bbc 54 spa.randomizeSpawn(Array, charArray);
jasper0712 20:32f115462bbc 55 spa.moveSpawnABC(Array, Array2, charArray, charArray2);
jasper0712 24:d6187d39f09b 56 spa.moveSpawnDE(Array, Array2, charArray, charArray2);
jasper0712 4:4145abd09075 57 } else {
jasper0712 19:7ccbb19703f9 58 drawit -= 0.5;
jasper0712 24:d6187d39f09b 59 spa.moveSpawnDE(Array, Array2, charArray, charArray2);
jasper0712 20:32f115462bbc 60 spa.moveSpawnB(Array, Array2, charArray, charArray2);
jasper0712 19:7ccbb19703f9 61 //printf("moving spawn b \n");
jasper0712 4:4145abd09075 62 }
jasper0712 20:32f115462bbc 63 spa.updateSpawn(Array, Array2, charArray, charArray2, lcd);
jasper0712 14:064b8d7f348d 64 //checkGameRule(lcd);
jasper0712 4:4145abd09075 65 }
jasper0712 5:c74bbdda06f4 66 void GameEngine::drawLaserPlayer(Gamepad &pad, N5110 &lcd) {
jasper0712 19:7ccbb19703f9 67 weap.drawPlayer(pad, lcd);
jasper0712 9:4c4d787c7a8b 68 //draw a line for the laser when button is pressed
jasper0712 10:926b1f89c4f1 69 if (pad.buttonhold_A == 1) {
jasper0712 7:3be4369131c6 70 //printf("hi, button is working \n");
jasper0712 5:c74bbdda06f4 71 weap.weaponMath(pad);
jasper0712 20:32f115462bbc 72 weap.drawLaser(Array2, charArray2, lcd);
jasper0712 5:c74bbdda06f4 73 }
jasper0712 5:c74bbdda06f4 74 }
jasper0712 10:926b1f89c4f1 75 void GameEngine::checkGameRule(N5110 &lcd) {
jasper0712 9:4c4d787c7a8b 76 //the only way to lose is when the spawn reaches y = 46.
jasper0712 10:926b1f89c4f1 77 for ( int x = 0; x < 84; x ++) {
jasper0712 20:32f115462bbc 78 if (Array[x][46] > 0) {
jasper0712 10:926b1f89c4f1 79 while (1) {
jasper0712 10:926b1f89c4f1 80 lcd.clear();
jasper0712 10:926b1f89c4f1 81 lcd.printString("Game Over",10,3);
jasper0712 10:926b1f89c4f1 82 lcd.refresh();
jasper0712 10:926b1f89c4f1 83 wait(1.0);
jasper0712 10:926b1f89c4f1 84 }
jasper0712 10:926b1f89c4f1 85 }
jasper0712 10:926b1f89c4f1 86 }
jasper0712 5:c74bbdda06f4 87 }
jasper0712 17:53aedd20155a 88 void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) {
jasper0712 21:e5585569a938 89 weap.bombCooldown(Array2, pad, lcd);
jasper0712 14:064b8d7f348d 90 if (pad.check_event(Gamepad::X_PRESSED)) {
jasper0712 28:450ab72fabdc 91 //to prevent from detonating the bomb at instant when button X is ay1identally pressed
jasper0712 14:064b8d7f348d 92 }
jasper0712 19:7ccbb19703f9 93 if (pad.buttonhold_B == 1) { //if B pressed, generate shield. Shield doesnt have to be fully charged to be activated
jasper0712 15:c68a5c22a2a2 94 if (weap._shield > 0) { //shield will only activate if the shield has not depleted.
jasper0712 20:32f115462bbc 95 weap.energyShield(Array, Array2, charArray, charArray2, lcd);
jasper0712 15:c68a5c22a2a2 96 } else { //or else, deactivate the shield and recharge the shield.
jasper0712 15:c68a5c22a2a2 97 pad.buttonhold_B = 0;
jasper0712 15:c68a5c22a2a2 98 }
jasper0712 15:c68a5c22a2a2 99 }
jasper0712 15:c68a5c22a2a2 100 if (pad.buttonhold_B == 0) { // recharge shield
jasper0712 15:c68a5c22a2a2 101 if (weap._shield < weap._shieldCapacity ) {
jasper0712 15:c68a5c22a2a2 102 weap._shield = weap._shield + weap._shieldRegenRate;
jasper0712 15:c68a5c22a2a2 103 //printf("current shield = %d \n",weap._shield);
jasper0712 16:ddc4d5669a6e 104 //printf("shield regen rate = %d \n",weap._shieldRegenRate);
jasper0712 19:7ccbb19703f9 105 } else { //when the shield is fully charged. It will automatically activate the shield.
jasper0712 27:d4926f19c12a 106 pad.buttonhold_B = 1;
jasper0712 19:7ccbb19703f9 107 }
jasper0712 14:064b8d7f348d 108 }
jasper0712 26:140515d80457 109 weap.shieldMeter(lcd);
jasper0712 26:140515d80457 110 }
jasper0712 26:140515d80457 111 void GameEngine::updateArray() { //clean up tool
jasper0712 26:140515d80457 112 for (int x = 0; x < Cols; x++) {
jasper0712 26:140515d80457 113 for (int y = 0; y < Rows; y++) {
jasper0712 26:140515d80457 114 Array[x][y] = Array2[x][y];
jasper0712 26:140515d80457 115 //printf(" Array[%d][%d] = %d \n",x,y,Array2[x][y]);
jasper0712 26:140515d80457 116 spa.deleteChar(x, y, Array, charArray2);
jasper0712 26:140515d80457 117 }
jasper0712 26:140515d80457 118 }
jasper0712 10:926b1f89c4f1 119 }