JianWei Lee
/
project_game
Meteor defense project
GameEngine/GameEngine.cpp@38:c66a69c1a6de, 2017-04-24 (annotated)
- Committer:
- jasper0712
- Date:
- Mon Apr 24 23:30:54 2017 +0000
- Revision:
- 38:c66a69c1a6de
- Parent:
- 37:45da88e36d1d
- Child:
- 39:f92b93efbc72
new way of handling structs at Weapons. still developing cannon. project almost done
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 | 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 | 27:d4926f19c12a | 35 | _d1.droneScan(Array2, charArray2, lcd); |
jasper0712 | 27:d4926f19c12a | 36 | _d2.droneScan(Array2, charArray2, lcd); |
jasper0712 | 17:53aedd20155a | 37 | drawSpawn(lcd); |
jasper0712 | 38:c66a69c1a6de | 38 | laser_and_bomb(pad, lcd); |
jasper0712 | 17:53aedd20155a | 39 | bombAndShield(pad, lcd); |
jasper0712 | 32:580d74825810 | 40 | checkGameRule(pad, lcd); |
jasper0712 | 17:53aedd20155a | 41 | } |
jasper0712 | 28:450ab72fabdc | 42 | void GameEngine::weapUpgrade(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 28:450ab72fabdc | 43 | if (pad.check_event(Gamepad::START_PRESSED)) { |
jasper0712 | 29:6632dd9c48d8 | 44 | _gui.upgrading = 1; |
jasper0712 | 29:6632dd9c48d8 | 45 | _gui.preventDoubleClick(pad); |
jasper0712 | 29:6632dd9c48d8 | 46 | while (_gui.upgrading == 1) { |
jasper0712 | 37:45da88e36d1d | 47 | //printf("laserDamage = %i \n",weap._laserDamage); |
jasper0712 | 37:45da88e36d1d | 48 | //go to the main upgrade page with all the values (from Weapon) as reference. |
jasper0712 | 38:c66a69c1a6de | 49 | _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 | 50 | //printf("_gui.saveTheData = %d",_gui.saveTheData); |
jasper0712 | 29:6632dd9c48d8 | 51 | if (_gui.saveTheData == 1) { |
jasper0712 | 29:6632dd9c48d8 | 52 | Data _upgrades = _gui.savedata(); //saving data |
jasper0712 | 29:6632dd9c48d8 | 53 | //_gui.testingtest(_upgrades); //code used to test the struct |
jasper0712 | 29:6632dd9c48d8 | 54 | weap.setVar(_upgrades.up, _upgrades.up1); |
jasper0712 | 29:6632dd9c48d8 | 55 | weap.setVar(_upgrades.u1p, _upgrades.u1p1); |
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 | 38:c66a69c1a6de | 87 | void GameEngine::laser_and_bomb(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 19:7ccbb19703f9 | 88 | weap.drawPlayer(pad, lcd); |
jasper0712 | 38:c66a69c1a6de | 89 | weap.cannon_Cooldown(); |
jasper0712 | 38:c66a69c1a6de | 90 | weap.cannon_Meter(lcd); |
jasper0712 | 9:4c4d787c7a8b | 91 | //draw a line for the laser when button is pressed |
jasper0712 | 10:926b1f89c4f1 | 92 | if (pad.buttonhold_A == 1) { |
jasper0712 | 7:3be4369131c6 | 93 | //printf("hi, button is working \n"); |
jasper0712 | 5:c74bbdda06f4 | 94 | weap.weaponMath(pad); |
jasper0712 | 38:c66a69c1a6de | 95 | weap.laser_Main(Array2, charArray2, lcd); |
jasper0712 | 36:2608622e5018 | 96 | |
jasper0712 | 36:2608622e5018 | 97 | //cannon can only be fired when the laser is switched on. |
jasper0712 | 38:c66a69c1a6de | 98 | if ( weap._cannon > 320) { //when sufficient cost, check for button Y. |
jasper0712 | 38:c66a69c1a6de | 99 | if ( pad.check_event(Gamepad::Y_PRESSED)) { //press button to set flag (fire the cannon) |
jasper0712 | 38:c66a69c1a6de | 100 | fireInTheHole_flag = 1; |
jasper0712 | 38:c66a69c1a6de | 101 | } |
jasper0712 | 36:2608622e5018 | 102 | } |
jasper0712 | 36:2608622e5018 | 103 | } |
jasper0712 | 36:2608622e5018 | 104 | if ( fireInTheHole_flag == 1) { |
jasper0712 | 38:c66a69c1a6de | 105 | weap.cannon_main(lcd); |
jasper0712 | 36:2608622e5018 | 106 | } |
jasper0712 | 36:2608622e5018 | 107 | if ( weap.cannon_Firing == 0) { //clear flag when cannon is done |
jasper0712 | 36:2608622e5018 | 108 | fireInTheHole_flag = 0; |
jasper0712 | 5:c74bbdda06f4 | 109 | } |
jasper0712 | 5:c74bbdda06f4 | 110 | } |
jasper0712 | 32:580d74825810 | 111 | void GameEngine::checkGameRule(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 34:6ac9541d4c31 | 112 | //the only way to lose a life is when a spawn reaches y = 46. |
jasper0712 | 34:6ac9541d4c31 | 113 | numberOfLife_leds(pad); |
jasper0712 | 10:926b1f89c4f1 | 114 | for ( int x = 0; x < 84; x ++) { |
jasper0712 | 34:6ac9541d4c31 | 115 | if (Array[x][46] > 0) { //if anything reaches line y=46 |
jasper0712 | 34:6ac9541d4c31 | 116 | NumberOfLife --; |
jasper0712 | 34:6ac9541d4c31 | 117 | printf("opps you lost a life. NumberOfLife = %d \n",NumberOfLife); |
jasper0712 | 34:6ac9541d4c31 | 118 | if (NumberOfLife == 0) { |
jasper0712 | 34:6ac9541d4c31 | 119 | while (1) { |
jasper0712 | 34:6ac9541d4c31 | 120 | lcd.clear(); |
jasper0712 | 34:6ac9541d4c31 | 121 | lcd.printString("Game Over",10,3); |
jasper0712 | 34:6ac9541d4c31 | 122 | lcd.refresh(); |
jasper0712 | 34:6ac9541d4c31 | 123 | pad.leds_on(); |
jasper0712 | 34:6ac9541d4c31 | 124 | wait(0.1); |
jasper0712 | 34:6ac9541d4c31 | 125 | pad.leds_off(); |
jasper0712 | 34:6ac9541d4c31 | 126 | wait(0.1); |
jasper0712 | 34:6ac9541d4c31 | 127 | } |
jasper0712 | 10:926b1f89c4f1 | 128 | } |
jasper0712 | 10:926b1f89c4f1 | 129 | } |
jasper0712 | 10:926b1f89c4f1 | 130 | } |
jasper0712 | 5:c74bbdda06f4 | 131 | } |
jasper0712 | 34:6ac9541d4c31 | 132 | void GameEngine::numberOfLife_leds(Gamepad &pad) { |
jasper0712 | 34:6ac9541d4c31 | 133 | //light up LEDs depends on number of lifes left. |
jasper0712 | 34:6ac9541d4c31 | 134 | if (NumberOfLife == 3) { |
jasper0712 | 34:6ac9541d4c31 | 135 | pad.leds_on(); |
jasper0712 | 34:6ac9541d4c31 | 136 | } |
jasper0712 | 34:6ac9541d4c31 | 137 | if (NumberOfLife == 2) { |
jasper0712 | 34:6ac9541d4c31 | 138 | pad.led(1, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 139 | pad.led(2, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 140 | pad.led(3, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 141 | pad.led(4, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 142 | pad.led(5, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 143 | pad.led(6, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 144 | } |
jasper0712 | 34:6ac9541d4c31 | 145 | if (NumberOfLife == 1) { |
jasper0712 | 34:6ac9541d4c31 | 146 | pad.led(1, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 147 | pad.led(2, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 148 | pad.led(3, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 149 | pad.led(4, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 150 | pad.led(5, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 151 | pad.led(6, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 152 | } |
jasper0712 | 34:6ac9541d4c31 | 153 | } |
jasper0712 | 17:53aedd20155a | 154 | void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 21:e5585569a938 | 155 | weap.bombCooldown(Array2, pad, lcd); |
jasper0712 | 14:064b8d7f348d | 156 | if (pad.check_event(Gamepad::X_PRESSED)) { |
jasper0712 | 34:6ac9541d4c31 | 157 | //to prevent from detonating the bomb at instant when button X is accidentally pressed |
jasper0712 | 14:064b8d7f348d | 158 | } |
jasper0712 | 19:7ccbb19703f9 | 159 | if (pad.buttonhold_B == 1) { //if B pressed, generate shield. Shield doesnt have to be fully charged to be activated |
jasper0712 | 15:c68a5c22a2a2 | 160 | if (weap._shield > 0) { //shield will only activate if the shield has not depleted. |
jasper0712 | 20:32f115462bbc | 161 | weap.energyShield(Array, Array2, charArray, charArray2, lcd); |
jasper0712 | 15:c68a5c22a2a2 | 162 | } else { //or else, deactivate the shield and recharge the shield. |
jasper0712 | 15:c68a5c22a2a2 | 163 | pad.buttonhold_B = 0; |
jasper0712 | 15:c68a5c22a2a2 | 164 | } |
jasper0712 | 15:c68a5c22a2a2 | 165 | } |
jasper0712 | 15:c68a5c22a2a2 | 166 | if (pad.buttonhold_B == 0) { // recharge shield |
jasper0712 | 15:c68a5c22a2a2 | 167 | if (weap._shield < weap._shieldCapacity ) { |
jasper0712 | 15:c68a5c22a2a2 | 168 | weap._shield = weap._shield + weap._shieldRegenRate; |
jasper0712 | 15:c68a5c22a2a2 | 169 | //printf("current shield = %d \n",weap._shield); |
jasper0712 | 16:ddc4d5669a6e | 170 | //printf("shield regen rate = %d \n",weap._shieldRegenRate); |
jasper0712 | 19:7ccbb19703f9 | 171 | } else { //when the shield is fully charged. It will automatically activate the shield. |
jasper0712 | 27:d4926f19c12a | 172 | pad.buttonhold_B = 1; |
jasper0712 | 19:7ccbb19703f9 | 173 | } |
jasper0712 | 14:064b8d7f348d | 174 | } |
jasper0712 | 26:140515d80457 | 175 | weap.shieldMeter(lcd); |
jasper0712 | 26:140515d80457 | 176 | } |
jasper0712 | 26:140515d80457 | 177 | void GameEngine::updateArray() { //clean up tool |
jasper0712 | 26:140515d80457 | 178 | for (int x = 0; x < Cols; x++) { |
jasper0712 | 26:140515d80457 | 179 | for (int y = 0; y < Rows; y++) { |
jasper0712 | 26:140515d80457 | 180 | Array[x][y] = Array2[x][y]; |
jasper0712 | 26:140515d80457 | 181 | //printf(" Array[%d][%d] = %d \n",x,y,Array2[x][y]); |
jasper0712 | 26:140515d80457 | 182 | spa.deleteChar(x, y, Array, charArray2); |
jasper0712 | 26:140515d80457 | 183 | } |
jasper0712 | 26:140515d80457 | 184 | } |
jasper0712 | 10:926b1f89c4f1 | 185 | } |