JianWei Lee
/
project_game
Meteor defense project
GameEngine/GameEngine.cpp@50:082feedcdd22, 2017-05-04 (annotated)
- Committer:
- jasper0712
- Date:
- Thu May 04 16:55:26 2017 +0000
- Revision:
- 50:082feedcdd22
- Parent:
- 49:bf802df54786
converted library to folder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jasper0712 | 49:bf802df54786 | 1 | #include "GameEngine.h" |
jasper0712 | 49:bf802df54786 | 2 | |
jasper0712 | 49:bf802df54786 | 3 | GameEngine::GameEngine() |
jasper0712 | 49:bf802df54786 | 4 | { |
jasper0712 | 49:bf802df54786 | 5 | |
jasper0712 | 49:bf802df54786 | 6 | } |
jasper0712 | 49:bf802df54786 | 7 | |
jasper0712 | 49:bf802df54786 | 8 | GameEngine::~GameEngine() |
jasper0712 | 49:bf802df54786 | 9 | { |
jasper0712 | 49:bf802df54786 | 10 | |
jasper0712 | 49:bf802df54786 | 11 | } |
jasper0712 | 49:bf802df54786 | 12 | void GameEngine::startingMenu(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 49:bf802df54786 | 13 | _gui.startingMenu(lcd); |
jasper0712 | 49:bf802df54786 | 14 | ledNumber ++; |
jasper0712 | 49:bf802df54786 | 15 | pad.led(ledNumber, 1.0); |
jasper0712 | 49:bf802df54786 | 16 | pad.led(ledNumber - 1, 0.0); |
jasper0712 | 49:bf802df54786 | 17 | if (ledNumber == 6) { |
jasper0712 | 49:bf802df54786 | 18 | ledNumber = 0; |
jasper0712 | 49:bf802df54786 | 19 | } else { |
jasper0712 | 49:bf802df54786 | 20 | pad.led(6, 0.0); |
jasper0712 | 49:bf802df54786 | 21 | } |
jasper0712 | 49:bf802df54786 | 22 | _gui.preventDoubleClick(pad); |
jasper0712 | 49:bf802df54786 | 23 | |
jasper0712 | 49:bf802df54786 | 24 | } |
jasper0712 | 49:bf802df54786 | 25 | void GameEngine::init(int w) { |
jasper0712 | 49:bf802df54786 | 26 | NumberOfLife = 3; //3 life on every wave |
jasper0712 | 49:bf802df54786 | 27 | weap.init(); |
jasper0712 | 49:bf802df54786 | 28 | _d1.droneInit(20, 46, 0, 42, weap._droneDamage, weap._droneRange); //drone1 is taking left side pixel 0 to 42. |
jasper0712 | 49:bf802df54786 | 29 | _d2.droneInit(62, 46, 42, 84, weap._droneDamage, weap._droneRange); //drone2 is taking right side pixel 42 to 83; |
jasper0712 | 49:bf802df54786 | 30 | spa.init(w); |
jasper0712 | 49:bf802df54786 | 31 | //printf("init completed \n"); |
jasper0712 | 49:bf802df54786 | 32 | } |
jasper0712 | 49:bf802df54786 | 33 | void GameEngine::update(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 49:bf802df54786 | 34 | weap.init(); |
jasper0712 | 49:bf802df54786 | 35 | _d1.drone_main(scnd.Arr, scnd.cArr, lcd); |
jasper0712 | 49:bf802df54786 | 36 | _d2.drone_main(scnd.Arr, scnd.cArr, lcd); |
jasper0712 | 49:bf802df54786 | 37 | drawSpawn(lcd); |
jasper0712 | 49:bf802df54786 | 38 | laser_and_cannon(pad, lcd); |
jasper0712 | 49:bf802df54786 | 39 | bomb_And_Shield(pad, lcd); |
jasper0712 | 49:bf802df54786 | 40 | checkGameRule(pad, lcd); |
jasper0712 | 49:bf802df54786 | 41 | //_gui.preventDoubleClick(pad); |
jasper0712 | 49:bf802df54786 | 42 | } |
jasper0712 | 49:bf802df54786 | 43 | void GameEngine::weapUpgrade(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 49:bf802df54786 | 44 | if (pad.check_event(Gamepad::START_PRESSED)) { |
jasper0712 | 49:bf802df54786 | 45 | _gui.upgrading_flag = 1; |
jasper0712 | 49:bf802df54786 | 46 | _gui.preventDoubleClick(pad); |
jasper0712 | 49:bf802df54786 | 47 | while (_gui.upgrading_flag == 1) { |
jasper0712 | 49:bf802df54786 | 48 | //printf("laserDamage = %i \n",weap._laserDamage); |
jasper0712 | 49:bf802df54786 | 49 | //go to the upgrade page with all the values (from Weapon) as reference. |
jasper0712 | 49:bf802df54786 | 50 | _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 | 49:bf802df54786 | 51 | //printf("_gui.saveTheData = %d",_gui.saveTheData); |
jasper0712 | 49:bf802df54786 | 52 | if (_gui.saveTheData_flag == 1) { |
jasper0712 | 49:bf802df54786 | 53 | Data _upgrades = _gui.get_data(); //saving data |
jasper0712 | 49:bf802df54786 | 54 | //_gui.testingtest(_upgrades); //code used to test the struct |
jasper0712 | 49:bf802df54786 | 55 | weap.setVar(_upgrades.up, _upgrades.up1); |
jasper0712 | 49:bf802df54786 | 56 | weap.setVar(_upgrades.u1p, _upgrades.u1p1); |
jasper0712 | 49:bf802df54786 | 57 | //printf("TESTING up = %d, up1 = %d, u1p = %d, u1p1 = %d \n",_upgrades.up,_upgrades.up1,_upgrades.u1p,_upgrades.u1p1); |
jasper0712 | 49:bf802df54786 | 58 | _gui.saveTheData_flag = 0; |
jasper0712 | 49:bf802df54786 | 59 | _gui.upgrading_flag = 0; //get out of while loop after done upgrading |
jasper0712 | 49:bf802df54786 | 60 | } |
jasper0712 | 49:bf802df54786 | 61 | lcd.refresh(); |
jasper0712 | 49:bf802df54786 | 62 | wait(0.2); |
jasper0712 | 49:bf802df54786 | 63 | lcd.clear(); |
jasper0712 | 49:bf802df54786 | 64 | } |
jasper0712 | 49:bf802df54786 | 65 | doneUpgrade_flag = 1; |
jasper0712 | 49:bf802df54786 | 66 | //remember to re-initialise everything after a wave. |
jasper0712 | 49:bf802df54786 | 67 | } |
jasper0712 | 49:bf802df54786 | 68 | } |
jasper0712 | 49:bf802df54786 | 69 | void GameEngine::drawSpawn(N5110 &lcd){ |
jasper0712 | 49:bf802df54786 | 70 | updateArray(); //clean up & update the main.Arr after the spawn took damage. |
jasper0712 | 49:bf802df54786 | 71 | if (drawit == 0) { |
jasper0712 | 49:bf802df54786 | 72 | drawit = 30; |
jasper0712 | 49:bf802df54786 | 73 | spa.summon_spawn(main.Arr, main.cArr);//summon spawn when drawit == 0 |
jasper0712 | 49:bf802df54786 | 74 | spa.moveSpawnABC(main.Arr, scnd.Arr, main.cArr, scnd.cArr); |
jasper0712 | 49:bf802df54786 | 75 | spa.moveSpawnDE(main.Arr, scnd.Arr, main.cArr, scnd.cArr); |
jasper0712 | 49:bf802df54786 | 76 | //printf("why u do this to cannonFiring_flag?? cannonFiring_flag = %d \n",weap.cannonFiring_flag); |
jasper0712 | 49:bf802df54786 | 77 | if ( fireInTheHole_flag ==1) { //this code because whenever spawn ABCDE moves, weap.cannonFiring_flag goes to 0 automatically (NO IDEA WHY) |
jasper0712 | 49:bf802df54786 | 78 | weap.cannonFiring_flag = 1; |
jasper0712 | 49:bf802df54786 | 79 | } |
jasper0712 | 49:bf802df54786 | 80 | } else { |
jasper0712 | 49:bf802df54786 | 81 | drawit -= 0.5; |
jasper0712 | 49:bf802df54786 | 82 | spa.moveSpawnDE(main.Arr, scnd.Arr, main.cArr, scnd.cArr); |
jasper0712 | 49:bf802df54786 | 83 | spa.moveSpawnB(main.Arr, scnd.Arr, main.cArr, scnd.cArr); |
jasper0712 | 49:bf802df54786 | 84 | //printf("moving spawn b \n"); |
jasper0712 | 49:bf802df54786 | 85 | } |
jasper0712 | 49:bf802df54786 | 86 | spa.updateSpawn(main.Arr, scnd.Arr, main.cArr, scnd.cArr, lcd); |
jasper0712 | 49:bf802df54786 | 87 | } |
jasper0712 | 49:bf802df54786 | 88 | void GameEngine::laser_and_cannon(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 49:bf802df54786 | 89 | weap.drawPlayer(pad, lcd); |
jasper0712 | 49:bf802df54786 | 90 | weap.cannon_recharge(); |
jasper0712 | 49:bf802df54786 | 91 | weap.cannon_Meter(lcd); |
jasper0712 | 49:bf802df54786 | 92 | //draw a line for the laser when button is pressed |
jasper0712 | 49:bf802df54786 | 93 | if (pad.buttonhold_A == 1) { |
jasper0712 | 49:bf802df54786 | 94 | //printf("hi, button is working \n"); |
jasper0712 | 49:bf802df54786 | 95 | weap.weaponMath(pad); |
jasper0712 | 49:bf802df54786 | 96 | weap.laser_Main(scnd.Arr, scnd.cArr, lcd); |
jasper0712 | 49:bf802df54786 | 97 | |
jasper0712 | 49:bf802df54786 | 98 | //cannon can only be fired when the laser is switched on. |
jasper0712 | 49:bf802df54786 | 99 | if ( weap._cannon > 320) { //when sufficient, check for button Y. |
jasper0712 | 49:bf802df54786 | 100 | if ( pad.check_event(Gamepad::Y_PRESSED)) { //press button to set flag (fire the cannon) |
jasper0712 | 49:bf802df54786 | 101 | fireInTheHole_flag = 1; |
jasper0712 | 49:bf802df54786 | 102 | } |
jasper0712 | 49:bf802df54786 | 103 | } |
jasper0712 | 49:bf802df54786 | 104 | } |
jasper0712 | 49:bf802df54786 | 105 | if ( fireInTheHole_flag == 1) { //if flag toggled |
jasper0712 | 49:bf802df54786 | 106 | weap.cannon_main(scnd.Arr, scnd.cArr, lcd); |
jasper0712 | 49:bf802df54786 | 107 | } |
jasper0712 | 49:bf802df54786 | 108 | if ( weap.cannonFiring_flag == 0) { //clear flag when cannon is done |
jasper0712 | 49:bf802df54786 | 109 | fireInTheHole_flag = 0; |
jasper0712 | 49:bf802df54786 | 110 | } |
jasper0712 | 49:bf802df54786 | 111 | } |
jasper0712 | 49:bf802df54786 | 112 | void GameEngine::checkGameRule(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 49:bf802df54786 | 113 | //the only way to lose a life is when a spawn reaches y = 46. |
jasper0712 | 49:bf802df54786 | 114 | numberOfLife_leds(pad); |
jasper0712 | 49:bf802df54786 | 115 | for ( int x = 0; x < 84; x ++) { |
jasper0712 | 49:bf802df54786 | 116 | if (main.Arr[x][46] > 0) { //if anything reaches line y=46 |
jasper0712 | 49:bf802df54786 | 117 | NumberOfLife --; |
jasper0712 | 49:bf802df54786 | 118 | pad.tone(750.0,0.5); |
jasper0712 | 49:bf802df54786 | 119 | //printf("opps you lost a life. NumberOfLife = %d \n",NumberOfLife); |
jasper0712 | 49:bf802df54786 | 120 | if (NumberOfLife == 0) { |
jasper0712 | 49:bf802df54786 | 121 | gameOver(pad, lcd); |
jasper0712 | 49:bf802df54786 | 122 | } |
jasper0712 | 49:bf802df54786 | 123 | } |
jasper0712 | 49:bf802df54786 | 124 | } |
jasper0712 | 49:bf802df54786 | 125 | } |
jasper0712 | 49:bf802df54786 | 126 | void GameEngine::gameOver(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 49:bf802df54786 | 127 | while (retry_flag == 0 && gameMenu_flag == 0) { //while both are 0 |
jasper0712 | 49:bf802df54786 | 128 | lcd.clear(); |
jasper0712 | 49:bf802df54786 | 129 | lcd.printString("Game Over",10,3); |
jasper0712 | 49:bf802df54786 | 130 | lcd.printString("GiveUp Retry",0,5); |
jasper0712 | 49:bf802df54786 | 131 | lcd.refresh(); |
jasper0712 | 49:bf802df54786 | 132 | pad.leds_on(); |
jasper0712 | 49:bf802df54786 | 133 | wait(0.1); |
jasper0712 | 49:bf802df54786 | 134 | pad.leds_off(); |
jasper0712 | 49:bf802df54786 | 135 | wait(0.1); |
jasper0712 | 49:bf802df54786 | 136 | //printf("press a button pls. you lost retry_flag = %d gameMenu_flag = %d\n",retry_flag,gameMenu_flag); |
jasper0712 | 49:bf802df54786 | 137 | //must click atleast one button to continue |
jasper0712 | 49:bf802df54786 | 138 | if (pad.check_event(Gamepad::START_PRESSED)) { |
jasper0712 | 49:bf802df54786 | 139 | retry_flag = 1; |
jasper0712 | 49:bf802df54786 | 140 | printf("retry pressed \n"); |
jasper0712 | 49:bf802df54786 | 141 | update_RetryToMain(); |
jasper0712 | 49:bf802df54786 | 142 | } |
jasper0712 | 49:bf802df54786 | 143 | if (pad.check_event(Gamepad::BACK_PRESSED)) { |
jasper0712 | 49:bf802df54786 | 144 | gameMenu_flag = 1; //bring the player to game menu |
jasper0712 | 49:bf802df54786 | 145 | printf("player surrendered \n"); |
jasper0712 | 49:bf802df54786 | 146 | } |
jasper0712 | 49:bf802df54786 | 147 | |
jasper0712 | 49:bf802df54786 | 148 | } |
jasper0712 | 49:bf802df54786 | 149 | } |
jasper0712 | 49:bf802df54786 | 150 | void GameEngine::numberOfLife_leds(Gamepad &pad) { |
jasper0712 | 49:bf802df54786 | 151 | //light up LEDs depends on number of lifes left. |
jasper0712 | 49:bf802df54786 | 152 | if (NumberOfLife == 3) { |
jasper0712 | 49:bf802df54786 | 153 | pad.leds_on(); //turn on all leds |
jasper0712 | 49:bf802df54786 | 154 | } |
jasper0712 | 49:bf802df54786 | 155 | if (NumberOfLife == 2) { |
jasper0712 | 49:bf802df54786 | 156 | pad.led(1, 1.0); //turn on led 1, 2, 5 and 6 |
jasper0712 | 49:bf802df54786 | 157 | pad.led(2, 1.0); |
jasper0712 | 49:bf802df54786 | 158 | pad.led(3, 0.0); |
jasper0712 | 49:bf802df54786 | 159 | pad.led(4, 0.0); |
jasper0712 | 49:bf802df54786 | 160 | pad.led(5, 1.0); |
jasper0712 | 49:bf802df54786 | 161 | pad.led(6, 1.0); |
jasper0712 | 49:bf802df54786 | 162 | } |
jasper0712 | 49:bf802df54786 | 163 | if (NumberOfLife == 1) { |
jasper0712 | 49:bf802df54786 | 164 | pad.led(1, 1.0); //turn on led 1 and 6 |
jasper0712 | 49:bf802df54786 | 165 | pad.led(2, 0.0); |
jasper0712 | 49:bf802df54786 | 166 | pad.led(3, 0.0); |
jasper0712 | 49:bf802df54786 | 167 | pad.led(4, 0.0); |
jasper0712 | 49:bf802df54786 | 168 | pad.led(5, 0.0); |
jasper0712 | 49:bf802df54786 | 169 | pad.led(6, 1.0); |
jasper0712 | 49:bf802df54786 | 170 | } |
jasper0712 | 49:bf802df54786 | 171 | } |
jasper0712 | 49:bf802df54786 | 172 | void GameEngine::bomb_And_Shield(Gamepad &pad, N5110 &lcd) { |
jasper0712 | 49:bf802df54786 | 173 | weap.bomb_main(scnd.Arr, pad, lcd); |
jasper0712 | 49:bf802df54786 | 174 | if (pad.check_event(Gamepad::X_PRESSED)) { |
jasper0712 | 49:bf802df54786 | 175 | //to prevent from detonating the bomb at instant when button X is accidentally pressed |
jasper0712 | 49:bf802df54786 | 176 | //printf("instant bomb detonate prevented"); |
jasper0712 | 49:bf802df54786 | 177 | } |
jasper0712 | 49:bf802df54786 | 178 | weap.shield_main(scnd.Arr, scnd.cArr, pad, lcd); |
jasper0712 | 49:bf802df54786 | 179 | if (pad.check_event(Gamepad::B_PRESSED)) { |
jasper0712 | 49:bf802df54786 | 180 | //to prevent the button from clicking twice |
jasper0712 | 49:bf802df54786 | 181 | printf("shield double click prevented"); |
jasper0712 | 49:bf802df54786 | 182 | } |
jasper0712 | 49:bf802df54786 | 183 | } |
jasper0712 | 49:bf802df54786 | 184 | void GameEngine::updateArray() { //update main array with secondary array and clean up the array |
jasper0712 | 49:bf802df54786 | 185 | for (int x = 0; x < Cols; x++) { |
jasper0712 | 49:bf802df54786 | 186 | for (int y = 0; y < Rows; y++) { |
jasper0712 | 49:bf802df54786 | 187 | main.Arr[x][y] = scnd.Arr[x][y]; |
jasper0712 | 49:bf802df54786 | 188 | //printf(" main.Arr[%d][%d] = %d \n",x,y,scnd.Arr[x][y]); |
jasper0712 | 49:bf802df54786 | 189 | spa.deleteChar(x, y, main.Arr, scnd.cArr); |
jasper0712 | 49:bf802df54786 | 190 | } |
jasper0712 | 49:bf802df54786 | 191 | } |
jasper0712 | 49:bf802df54786 | 192 | } |
jasper0712 | 49:bf802df54786 | 193 | void GameEngine::update_MainToRetry() { //update Retry array with main array |
jasper0712 | 49:bf802df54786 | 194 | for (int x = 0; x < Cols; x ++) { |
jasper0712 | 49:bf802df54786 | 195 | for (int y = 0; y < Rows; y ++) { |
jasper0712 | 49:bf802df54786 | 196 | retry.Arr[x][y] = main.Arr[x][y]; |
jasper0712 | 49:bf802df54786 | 197 | retry.cArr[x][y]= main.cArr[x][y]; |
jasper0712 | 49:bf802df54786 | 198 | } |
jasper0712 | 49:bf802df54786 | 199 | } |
jasper0712 | 49:bf802df54786 | 200 | printf("main to retry array updated \n"); |
jasper0712 | 49:bf802df54786 | 201 | } |
jasper0712 | 49:bf802df54786 | 202 | void GameEngine::update_RetryToMain() {//update main & secondary array with Retry array |
jasper0712 | 49:bf802df54786 | 203 | for (int x = 0; x < Cols; x ++) { |
jasper0712 | 49:bf802df54786 | 204 | for (int y = 0; y < Rows; y ++) { |
jasper0712 | 49:bf802df54786 | 205 | main.Arr[x][y] = retry.Arr[x][y]; |
jasper0712 | 49:bf802df54786 | 206 | main.cArr[x][y] = retry.cArr[x][y]; |
jasper0712 | 49:bf802df54786 | 207 | scnd.Arr[x][y] = retry.Arr[x][y]; |
jasper0712 | 49:bf802df54786 | 208 | scnd.cArr[x][y] = retry.cArr[x][y]; |
jasper0712 | 49:bf802df54786 | 209 | } |
jasper0712 | 49:bf802df54786 | 210 | } |
jasper0712 | 49:bf802df54786 | 211 | printf("retry to main array updated \n"); |
jasper0712 | 49:bf802df54786 | 212 | } |
jasper0712 | 49:bf802df54786 | 213 | void GameEngine::reset_Game() { //reset everything in the game |
jasper0712 | 49:bf802df54786 | 214 | for (int x = 0; x < Cols; x ++) { |
jasper0712 | 49:bf802df54786 | 215 | for (int y = 0; y < Rows; y ++) { //clear array |
jasper0712 | 49:bf802df54786 | 216 | main.Arr[x][y] = 0; |
jasper0712 | 49:bf802df54786 | 217 | main.cArr[x][y] = '\0'; |
jasper0712 | 49:bf802df54786 | 218 | scnd.Arr[x][y] = 0; |
jasper0712 | 49:bf802df54786 | 219 | scnd.cArr[x][y] = '\0'; |
jasper0712 | 49:bf802df54786 | 220 | } |
jasper0712 | 49:bf802df54786 | 221 | } |
jasper0712 | 49:bf802df54786 | 222 | weap.reset_WeaponVariables(); //clear weapon variables |
jasper0712 | 49:bf802df54786 | 223 | } |