JianWei Lee
/
project_game
Meteor defense project
GameEngine/GameEngine.cpp@40:3a0c66a0e10e, 2017-04-29 (annotated)
- Committer:
- jasper0712
- Date:
- Sat Apr 29 19:27:20 2017 +0000
- Revision:
- 40:3a0c66a0e10e
- Parent:
- 39:f92b93efbc72
- Child:
- 41:1b396334256b
done commenting. MAYBE. implemented RETRY function in the game. need further testing
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 | 40:3a0c66a0e10e | 35 | _d1.drone_main(scnd.Arr, scnd.cArr, lcd); |
jasper0712 | 40:3a0c66a0e10e | 36 | _d2.drone_main(scnd.Arr, scnd.cArr, lcd); |
jasper0712 | 17:53aedd20155a | 37 | drawSpawn(lcd); |
jasper0712 | 40:3a0c66a0e10e | 38 | laser_and_cannon(pad, lcd); |
jasper0712 | 40:3a0c66a0e10e | 39 | bomb_And_Shield(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 | 40:3a0c66a0e10e | 44 | _gui.upgrading_flag = 1; |
jasper0712 | 29:6632dd9c48d8 | 45 | _gui.preventDoubleClick(pad); |
jasper0712 | 40:3a0c66a0e10e | 46 | while (_gui.upgrading_flag == 1) { |
jasper0712 | 37:45da88e36d1d | 47 | //printf("laserDamage = %i \n",weap._laserDamage); |
jasper0712 | 40:3a0c66a0e10e | 48 | //go to the 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 | 40:3a0c66a0e10e | 51 | if (_gui.saveTheData_flag == 1) { |
jasper0712 | 40:3a0c66a0e10e | 52 | Data _upgrades = _gui.get_data(); //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 | 40:3a0c66a0e10e | 57 | _gui.saveTheData_flag = 0; |
jasper0712 | 40:3a0c66a0e10e | 58 | _gui.upgrading_flag = 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 | 40:3a0c66a0e10e | 69 | updateArray(); //clean up & update the main.Arr after the spawn took damage. |
jasper0712 | 36:2608622e5018 | 70 | if (drawit == 0) { // |
jasper0712 | 19:7ccbb19703f9 | 71 | drawit = 30; |
jasper0712 | 40:3a0c66a0e10e | 72 | spa.summon_spawn(main.Arr, main.cArr); |
jasper0712 | 40:3a0c66a0e10e | 73 | spa.moveSpawnABC(main.Arr, scnd.Arr, main.cArr, scnd.cArr); |
jasper0712 | 40:3a0c66a0e10e | 74 | spa.moveSpawnDE(main.Arr, scnd.Arr, main.cArr, scnd.cArr); |
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 | 40:3a0c66a0e10e | 81 | spa.moveSpawnDE(main.Arr, scnd.Arr, main.cArr, scnd.cArr); |
jasper0712 | 40:3a0c66a0e10e | 82 | spa.moveSpawnB(main.Arr, scnd.Arr, main.cArr, scnd.cArr); |
jasper0712 | 31:1c0e47931e84 | 83 | //printf("moving spawn b \n"); |
jasper0712 | 4:4145abd09075 | 84 | } |
jasper0712 | 40:3a0c66a0e10e | 85 | spa.updateSpawn(main.Arr, scnd.Arr, main.cArr, scnd.cArr, lcd); |
jasper0712 | 4:4145abd09075 | 86 | } |
jasper0712 | 40:3a0c66a0e10e | 87 | void GameEngine::laser_and_cannon(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 19:7ccbb19703f9 | 88 | weap.drawPlayer(pad, lcd); |
jasper0712 | 40:3a0c66a0e10e | 89 | weap.cannon_recharge(); |
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 | 40:3a0c66a0e10e | 95 | weap.laser_Main(scnd.Arr, scnd.cArr, 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 | 40:3a0c66a0e10e | 105 | weap.cannon_main(scnd.Arr, scnd.cArr, 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 | 40:3a0c66a0e10e | 115 | if (main.Arr[x][46] > 0) { //if anything reaches line y=46 |
jasper0712 | 34:6ac9541d4c31 | 116 | NumberOfLife --; |
jasper0712 | 40:3a0c66a0e10e | 117 | //printf("opps you lost a life. NumberOfLife = %d \n",NumberOfLife); |
jasper0712 | 34:6ac9541d4c31 | 118 | if (NumberOfLife == 0) { |
jasper0712 | 40:3a0c66a0e10e | 119 | gameOver(pad, lcd); |
jasper0712 | 10:926b1f89c4f1 | 120 | } |
jasper0712 | 10:926b1f89c4f1 | 121 | } |
jasper0712 | 10:926b1f89c4f1 | 122 | } |
jasper0712 | 5:c74bbdda06f4 | 123 | } |
jasper0712 | 40:3a0c66a0e10e | 124 | void GameEngine::gameOver(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 40:3a0c66a0e10e | 125 | while (retry_flag == 0 && gameMenu_flag == 0) { //while both are 0 |
jasper0712 | 40:3a0c66a0e10e | 126 | lcd.clear(); |
jasper0712 | 40:3a0c66a0e10e | 127 | lcd.printString("Game Over",10,3); |
jasper0712 | 40:3a0c66a0e10e | 128 | lcd.printString("GivUp Retry",0,5); |
jasper0712 | 40:3a0c66a0e10e | 129 | lcd.refresh(); |
jasper0712 | 40:3a0c66a0e10e | 130 | pad.leds_on(); |
jasper0712 | 40:3a0c66a0e10e | 131 | wait(0.1); |
jasper0712 | 40:3a0c66a0e10e | 132 | pad.leds_off(); |
jasper0712 | 40:3a0c66a0e10e | 133 | wait(0.1); |
jasper0712 | 40:3a0c66a0e10e | 134 | printf("press a button pls. you lost retry_flag = %d gameMenu_flag = %d\n",retry_flag,gameMenu_flag); |
jasper0712 | 40:3a0c66a0e10e | 135 | //must click atleast one button to continue |
jasper0712 | 40:3a0c66a0e10e | 136 | if (pad.check_event(Gamepad::START_PRESSED)) { |
jasper0712 | 40:3a0c66a0e10e | 137 | retry_flag = 1; |
jasper0712 | 40:3a0c66a0e10e | 138 | printf("retry pressed \n"); |
jasper0712 | 40:3a0c66a0e10e | 139 | update_RetryToMain(); |
jasper0712 | 40:3a0c66a0e10e | 140 | } |
jasper0712 | 40:3a0c66a0e10e | 141 | if (pad.check_event(Gamepad::BACK_PRESSED)) { |
jasper0712 | 40:3a0c66a0e10e | 142 | gameMenu_flag = 1; //bring the player to game menu |
jasper0712 | 40:3a0c66a0e10e | 143 | printf("player surrendered \n"); |
jasper0712 | 40:3a0c66a0e10e | 144 | } |
jasper0712 | 40:3a0c66a0e10e | 145 | |
jasper0712 | 40:3a0c66a0e10e | 146 | } |
jasper0712 | 40:3a0c66a0e10e | 147 | } |
jasper0712 | 34:6ac9541d4c31 | 148 | void GameEngine::numberOfLife_leds(Gamepad &pad) { |
jasper0712 | 34:6ac9541d4c31 | 149 | //light up LEDs depends on number of lifes left. |
jasper0712 | 34:6ac9541d4c31 | 150 | if (NumberOfLife == 3) { |
jasper0712 | 40:3a0c66a0e10e | 151 | pad.leds_on(); //turn on all leds |
jasper0712 | 34:6ac9541d4c31 | 152 | } |
jasper0712 | 34:6ac9541d4c31 | 153 | if (NumberOfLife == 2) { |
jasper0712 | 40:3a0c66a0e10e | 154 | pad.led(1, 1.0); //turn on led 1, 2, 5 and 6 |
jasper0712 | 34:6ac9541d4c31 | 155 | pad.led(2, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 156 | pad.led(3, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 157 | pad.led(4, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 158 | pad.led(5, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 159 | pad.led(6, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 160 | } |
jasper0712 | 34:6ac9541d4c31 | 161 | if (NumberOfLife == 1) { |
jasper0712 | 40:3a0c66a0e10e | 162 | pad.led(1, 1.0); //turn on led 1 and 6 |
jasper0712 | 34:6ac9541d4c31 | 163 | pad.led(2, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 164 | pad.led(3, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 165 | pad.led(4, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 166 | pad.led(5, 0.0); |
jasper0712 | 34:6ac9541d4c31 | 167 | pad.led(6, 1.0); |
jasper0712 | 34:6ac9541d4c31 | 168 | } |
jasper0712 | 34:6ac9541d4c31 | 169 | } |
jasper0712 | 40:3a0c66a0e10e | 170 | void GameEngine::bomb_And_Shield(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 40:3a0c66a0e10e | 171 | weap.bomb_main(scnd.Arr, pad, lcd); |
jasper0712 | 14:064b8d7f348d | 172 | if (pad.check_event(Gamepad::X_PRESSED)) { |
jasper0712 | 34:6ac9541d4c31 | 173 | //to prevent from detonating the bomb at instant when button X is accidentally pressed |
jasper0712 | 40:3a0c66a0e10e | 174 | //printf("instant bomb detonate prevented"); |
jasper0712 | 14:064b8d7f348d | 175 | } |
jasper0712 | 40:3a0c66a0e10e | 176 | weap.shield_main(scnd.Arr, scnd.cArr, pad, lcd); |
jasper0712 | 40:3a0c66a0e10e | 177 | if (pad.check_event(Gamepad::B_PRESSED)) { |
jasper0712 | 40:3a0c66a0e10e | 178 | //to prevent the button from clicking twice |
jasper0712 | 40:3a0c66a0e10e | 179 | printf("shield double click prevented"); |
jasper0712 | 14:064b8d7f348d | 180 | } |
jasper0712 | 26:140515d80457 | 181 | } |
jasper0712 | 26:140515d80457 | 182 | void GameEngine::updateArray() { //clean up tool |
jasper0712 | 26:140515d80457 | 183 | for (int x = 0; x < Cols; x++) { |
jasper0712 | 26:140515d80457 | 184 | for (int y = 0; y < Rows; y++) { |
jasper0712 | 40:3a0c66a0e10e | 185 | main.Arr[x][y] = scnd.Arr[x][y]; |
jasper0712 | 40:3a0c66a0e10e | 186 | //printf(" main.Arr[%d][%d] = %d \n",x,y,scnd.Arr[x][y]); |
jasper0712 | 40:3a0c66a0e10e | 187 | spa.deleteChar(x, y, main.Arr, scnd.cArr); |
jasper0712 | 26:140515d80457 | 188 | } |
jasper0712 | 26:140515d80457 | 189 | } |
jasper0712 | 40:3a0c66a0e10e | 190 | } |
jasper0712 | 40:3a0c66a0e10e | 191 | void GameEngine::update_MainToRetry() { |
jasper0712 | 40:3a0c66a0e10e | 192 | for (int x = 0; x < Cols; x ++) { |
jasper0712 | 40:3a0c66a0e10e | 193 | for (int y = 0; y < Rows; y ++) { |
jasper0712 | 40:3a0c66a0e10e | 194 | retry.Arr[x][y] = main.Arr[x][y]; |
jasper0712 | 40:3a0c66a0e10e | 195 | retry.cArr[x][y]= main.cArr[x][y]; |
jasper0712 | 40:3a0c66a0e10e | 196 | } |
jasper0712 | 40:3a0c66a0e10e | 197 | } |
jasper0712 | 40:3a0c66a0e10e | 198 | printf("main to retry array updated \n"); |
jasper0712 | 40:3a0c66a0e10e | 199 | } |
jasper0712 | 40:3a0c66a0e10e | 200 | void GameEngine::update_RetryToMain() { |
jasper0712 | 40:3a0c66a0e10e | 201 | for (int x = 0; x < Cols; x ++) { |
jasper0712 | 40:3a0c66a0e10e | 202 | for (int y = 0; y < Rows; y ++) { |
jasper0712 | 40:3a0c66a0e10e | 203 | main.Arr[x][y] = retry.Arr[x][y]; |
jasper0712 | 40:3a0c66a0e10e | 204 | main.cArr[x][y] = retry.cArr[x][y]; |
jasper0712 | 40:3a0c66a0e10e | 205 | scnd.Arr[x][y] = retry.Arr[x][y]; |
jasper0712 | 40:3a0c66a0e10e | 206 | scnd.cArr[x][y] = retry.cArr[x][y]; |
jasper0712 | 40:3a0c66a0e10e | 207 | } |
jasper0712 | 40:3a0c66a0e10e | 208 | } |
jasper0712 | 40:3a0c66a0e10e | 209 | printf("retry to main array updated \n"); |
jasper0712 | 10:926b1f89c4f1 | 210 | } |