JianWei Lee
/
project_game
Meteor defense project
GameEngine/GameEngine.cpp@36:2608622e5018, 2017-04-23 (annotated)
- Committer:
- jasper0712
- Date:
- Sun Apr 23 22:52:48 2017 +0000
- Revision:
- 36:2608622e5018
- Parent:
- 34:6ac9541d4c31
- Child:
- 37:45da88e36d1d
in the progress of making cannon
Who changed what in which revision?
User | Revision | Line number | New 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 | 36:2608622e5018 | 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 | 36:2608622e5018 | 69 | if (drawit == 0) { // |
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 | 36:2608622e5018 | 74 | //printf("why u do this to cannon_Firing?? cannon_Firing = %d \n",weap.cannon_Firing); |
jasper0712 | 36:2608622e5018 | 75 | if ( fireInTheHole_flag ==1) { //this code because whenever spawn ABCDE moves, weap.cannon_Firing goes to 0 automatically (NO IDEA WHY) |
jasper0712 | 36:2608622e5018 | 76 | weap.cannon_Firing = 1; |
jasper0712 | 36:2608622e5018 | 77 | } |
jasper0712 | 4:4145abd09075 | 78 | } else { |
jasper0712 | 19:7ccbb19703f9 | 79 | drawit -= 0.5; |
jasper0712 | 24:d6187d39f09b | 80 | spa.moveSpawnDE(Array, Array2, charArray, charArray2); |
jasper0712 | 31:1c0e47931e84 | 81 | spa.moveSpawnB(Array, Array2, charArray, charArray2); |
jasper0712 | 31:1c0e47931e84 | 82 | //printf("moving spawn b \n"); |
jasper0712 | 4:4145abd09075 | 83 | } |
jasper0712 | 20:32f115462bbc | 84 | spa.updateSpawn(Array, Array2, charArray, charArray2, lcd); |
jasper0712 | 4:4145abd09075 | 85 | } |
jasper0712 | 5:c74bbdda06f4 | 86 | void GameEngine::drawLaserPlayer(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 19:7ccbb19703f9 | 87 | weap.drawPlayer(pad, lcd); |
jasper0712 | 9:4c4d787c7a8b | 88 | //draw a line for the laser when button is pressed |
jasper0712 | 10:926b1f89c4f1 | 89 | if (pad.buttonhold_A == 1) { |
jasper0712 | 7:3be4369131c6 | 90 | //printf("hi, button is working \n"); |
jasper0712 | 5:c74bbdda06f4 | 91 | weap.weaponMath(pad); |
jasper0712 | 20:32f115462bbc | 92 | weap.drawLaser(Array2, charArray2, lcd); |
jasper0712 | 36:2608622e5018 | 93 | |
jasper0712 | 36:2608622e5018 | 94 | //cannon can only be fired when the laser is switched on. |
jasper0712 | 36:2608622e5018 | 95 | if ( pad.check_event(Gamepad::Y_PRESSED)) { //press button to set flag (fire the cannon) |
jasper0712 | 36:2608622e5018 | 96 | fireInTheHole_flag = 1; |
jasper0712 | 36:2608622e5018 | 97 | } |
jasper0712 | 36:2608622e5018 | 98 | } |
jasper0712 | 36:2608622e5018 | 99 | if ( fireInTheHole_flag == 1) { |
jasper0712 | 36:2608622e5018 | 100 | weap.cannon(lcd); |
jasper0712 | 36:2608622e5018 | 101 | } |
jasper0712 | 36:2608622e5018 | 102 | if ( weap.cannon_Firing == 0) { //clear flag when cannon is done |
jasper0712 | 36:2608622e5018 | 103 | fireInTheHole_flag = 0; |
jasper0712 | 5:c74bbdda06f4 | 104 | } |
jasper0712 | 5:c74bbdda06f4 | 105 | } |
jasper0712 | 32:580d74825810 | 106 | void GameEngine::checkGameRule(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 34:6ac9541d4c31 | 107 | //the only way to lose a life is when a spawn reaches y = 46. |
jasper0712 | 34:6ac9541d4c31 | 108 | numberOfLife_leds(pad); |
jasper0712 | 10:926b1f89c4f1 | 109 | for ( int x = 0; x < 84; x ++) { |
jasper0712 | 34:6ac9541d4c31 | 110 | if (Array[x][46] > 0) { //if anything reaches line y=46 |
jasper0712 | 34:6ac9541d4c31 | 111 | NumberOfLife --; |
jasper0712 | 34:6ac9541d4c31 | 112 | printf("opps you lost a life. NumberOfLife = %d \n",NumberOfLife); |
jasper0712 | 34:6ac9541d4c31 | 113 | if (NumberOfLife == 0) { |
jasper0712 | 34:6ac9541d4c31 | 114 | while (1) { |
jasper0712 | 34:6ac9541d4c31 | 115 | lcd.clear(); |
jasper0712 | 34:6ac9541d4c31 | 116 | lcd.printString("Game Over",10,3); |
jasper0712 | 34:6ac9541d4c31 | 117 | lcd.refresh(); |
jasper0712 | 34:6ac9541d4c31 | 118 | pad.leds_on(); |
jasper0712 | 34:6ac9541d4c31 | 119 | wait(0.1); |
jasper0712 | 34:6ac9541d4c31 | 120 | pad.leds_off(); |
jasper0712 | 34:6ac9541d4c31 | 121 | wait(0.1); |
jasper0712 | 34:6ac9541d4c31 | 122 | } |
jasper0712 | 10:926b1f89c4f1 | 123 | } |
jasper0712 | 10:926b1f89c4f1 | 124 | } |
jasper0712 | 10:926b1f89c4f1 | 125 | } |
jasper0712 | 5:c74bbdda06f4 | 126 | } |
jasper0712 | 34:6ac9541d4c31 | 127 | void GameEngine::numberOfLife_leds(Gamepad &pad) { |
jasper0712 | 34:6ac9541d4c31 | 128 | //light up LEDs depends on number of lifes left. |
jasper0712 | 34:6ac9541d4c31 | 129 | if (NumberOfLife == 3) { |
jasper0712 | 34:6ac9541d4c31 | 130 | pad.leds_on(); |
jasper0712 | 34:6ac9541d4c31 | 131 | } |
jasper0712 | 34:6ac9541d4c31 | 132 | if (NumberOfLife == 2) { |
jasper0712 | 34:6ac9541d4c31 | 133 | pad.led(1, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 134 | pad.led(2, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 135 | pad.led(3, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 136 | pad.led(4, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 137 | pad.led(5, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 138 | pad.led(6, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 139 | } |
jasper0712 | 34:6ac9541d4c31 | 140 | if (NumberOfLife == 1) { |
jasper0712 | 34:6ac9541d4c31 | 141 | pad.led(1, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 142 | pad.led(2, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 143 | pad.led(3, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 144 | pad.led(4, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 145 | pad.led(5, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 146 | pad.led(6, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 147 | } |
jasper0712 | 34:6ac9541d4c31 | 148 | } |
jasper0712 | 17:53aedd20155a | 149 | void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 34:6ac9541d4c31 | 150 | weap.init(); |
jasper0712 | 21:e5585569a938 | 151 | weap.bombCooldown(Array2, pad, lcd); |
jasper0712 | 14:064b8d7f348d | 152 | if (pad.check_event(Gamepad::X_PRESSED)) { |
jasper0712 | 34:6ac9541d4c31 | 153 | //to prevent from detonating the bomb at instant when button X is accidentally pressed |
jasper0712 | 14:064b8d7f348d | 154 | } |
jasper0712 | 19:7ccbb19703f9 | 155 | if (pad.buttonhold_B == 1) { //if B pressed, generate shield. Shield doesnt have to be fully charged to be activated |
jasper0712 | 15:c68a5c22a2a2 | 156 | if (weap._shield > 0) { //shield will only activate if the shield has not depleted. |
jasper0712 | 20:32f115462bbc | 157 | weap.energyShield(Array, Array2, charArray, charArray2, lcd); |
jasper0712 | 15:c68a5c22a2a2 | 158 | } else { //or else, deactivate the shield and recharge the shield. |
jasper0712 | 15:c68a5c22a2a2 | 159 | pad.buttonhold_B = 0; |
jasper0712 | 15:c68a5c22a2a2 | 160 | } |
jasper0712 | 15:c68a5c22a2a2 | 161 | } |
jasper0712 | 15:c68a5c22a2a2 | 162 | if (pad.buttonhold_B == 0) { // recharge shield |
jasper0712 | 15:c68a5c22a2a2 | 163 | if (weap._shield < weap._shieldCapacity ) { |
jasper0712 | 15:c68a5c22a2a2 | 164 | weap._shield = weap._shield + weap._shieldRegenRate; |
jasper0712 | 15:c68a5c22a2a2 | 165 | //printf("current shield = %d \n",weap._shield); |
jasper0712 | 16:ddc4d5669a6e | 166 | //printf("shield regen rate = %d \n",weap._shieldRegenRate); |
jasper0712 | 19:7ccbb19703f9 | 167 | } else { //when the shield is fully charged. It will automatically activate the shield. |
jasper0712 | 27:d4926f19c12a | 168 | pad.buttonhold_B = 1; |
jasper0712 | 19:7ccbb19703f9 | 169 | } |
jasper0712 | 14:064b8d7f348d | 170 | } |
jasper0712 | 26:140515d80457 | 171 | weap.shieldMeter(lcd); |
jasper0712 | 26:140515d80457 | 172 | } |
jasper0712 | 26:140515d80457 | 173 | void GameEngine::updateArray() { //clean up tool |
jasper0712 | 26:140515d80457 | 174 | for (int x = 0; x < Cols; x++) { |
jasper0712 | 26:140515d80457 | 175 | for (int y = 0; y < Rows; y++) { |
jasper0712 | 26:140515d80457 | 176 | Array[x][y] = Array2[x][y]; |
jasper0712 | 26:140515d80457 | 177 | //printf(" Array[%d][%d] = %d \n",x,y,Array2[x][y]); |
jasper0712 | 26:140515d80457 | 178 | spa.deleteChar(x, y, Array, charArray2); |
jasper0712 | 26:140515d80457 | 179 | } |
jasper0712 | 26:140515d80457 | 180 | } |
jasper0712 | 10:926b1f89c4f1 | 181 | } |