Nemesis game, engine
Engine.cpp@14:94011e4a0624, 2017-05-04 (annotated)
- Committer:
- musallambseiso
- Date:
- Thu May 04 11:29:47 2017 +0000
- Revision:
- 14:94011e4a0624
- Parent:
- 13:f3560c403397
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
musallambseiso | 4:e84724dcad49 | 1 | #include "Engine.h" |
musallambseiso | 4:e84724dcad49 | 2 | |
musallambseiso | 11:10c01766f774 | 3 | #define LEVEL_ONE 2 |
musallambseiso | 11:10c01766f774 | 4 | #define LEVEL_TWO 3 |
musallambseiso | 11:10c01766f774 | 5 | #define LEVEL_THREE 4 |
musallambseiso | 11:10c01766f774 | 6 | #define LEVEL_FOUR 5 |
musallambseiso | 11:10c01766f774 | 7 | #define LEVEL_FIVE 6 |
musallambseiso | 11:10c01766f774 | 8 | |
musallambseiso | 4:e84724dcad49 | 9 | Engine::Engine() |
musallambseiso | 4:e84724dcad49 | 10 | { |
musallambseiso | 13:f3560c403397 | 11 | collisions = 0; // Collisions variable. Maximum value of 5. |
musallambseiso | 13:f3560c403397 | 12 | wave_counter = 0; // Wave counter variable. |
musallambseiso | 13:f3560c403397 | 13 | ammo = 3; // Ammo variable. Shows if rockets (first weapon choice) are available. Maximum value of 3. |
musallambseiso | 13:f3560c403397 | 14 | star = true; // Star variable. Shows if a star (second weapon choice) is available. |
musallambseiso | 13:f3560c403397 | 15 | pauses = 3; // Pauses variable. Shows how many pauses the player has left. |
musallambseiso | 4:e84724dcad49 | 16 | } |
musallambseiso | 4:e84724dcad49 | 17 | |
musallambseiso | 4:e84724dcad49 | 18 | Engine::~Engine() |
musallambseiso | 4:e84724dcad49 | 19 | { |
musallambseiso | 11:10c01766f774 | 20 | } |
musallambseiso | 11:10c01766f774 | 21 | |
musallambseiso | 8:1c0bc1a08153 | 22 | |
musallambseiso | 13:f3560c403397 | 23 | // Initialization method: |
musallambseiso | 11:10c01766f774 | 24 | |
musallambseiso | 13:f3560c403397 | 25 | void Engine::init(int speed, N5110 &lcd, Gamepad &pad, int _collisions, int _wave_counter, int _ammo, bool _star, int _pauses) |
musallambseiso | 4:e84724dcad49 | 26 | { |
musallambseiso | 4:e84724dcad49 | 27 | _speed = speed; |
musallambseiso | 13:f3560c403397 | 28 | _friendly.init(); // Friendly ship initialization. |
musallambseiso | 13:f3560c403397 | 29 | |
musallambseiso | 13:f3560c403397 | 30 | // Enemy ships initialization: |
musallambseiso | 13:f3560c403397 | 31 | _enemy1.init(_speed); |
musallambseiso | 12:d68c757d829a | 32 | _enemy2.init(_speed); |
musallambseiso | 12:d68c757d829a | 33 | _enemy3.init(_speed); |
musallambseiso | 12:d68c757d829a | 34 | _enemy4.init(_speed); |
musallambseiso | 12:d68c757d829a | 35 | _enemy5.init(_speed); |
musallambseiso | 12:d68c757d829a | 36 | _enemy6.init(_speed); |
musallambseiso | 13:f3560c403397 | 37 | printf("%s \n", "Friendly and enemy ships initialized."); |
musallambseiso | 13:f3560c403397 | 38 | |
musallambseiso | 13:f3560c403397 | 39 | // Initializing variable values: |
musallambseiso | 13:f3560c403397 | 40 | collisions = _collisions; |
musallambseiso | 13:f3560c403397 | 41 | wave_counter = _wave_counter; |
musallambseiso | 13:f3560c403397 | 42 | ammo = _ammo; |
musallambseiso | 13:f3560c403397 | 43 | star = _star; |
musallambseiso | 13:f3560c403397 | 44 | pauses = _pauses; |
musallambseiso | 4:e84724dcad49 | 45 | } |
musallambseiso | 4:e84724dcad49 | 46 | |
musallambseiso | 13:f3560c403397 | 47 | |
musallambseiso | 13:f3560c403397 | 48 | // Method for reading the input from the gamepad analog stick: |
musallambseiso | 13:f3560c403397 | 49 | |
musallambseiso | 4:e84724dcad49 | 50 | void Engine::read_input(Gamepad &pad) |
musallambseiso | 4:e84724dcad49 | 51 | { |
musallambseiso | 4:e84724dcad49 | 52 | _d = pad.get_direction(); |
musallambseiso | 4:e84724dcad49 | 53 | _mag = pad.get_mag(); |
musallambseiso | 4:e84724dcad49 | 54 | } |
musallambseiso | 4:e84724dcad49 | 55 | |
musallambseiso | 11:10c01766f774 | 56 | |
musallambseiso | 13:f3560c403397 | 57 | // Drawing method: |
musallambseiso | 9:0efc8d7cdcf7 | 58 | |
musallambseiso | 12:d68c757d829a | 59 | void Engine::draw_all(N5110 &lcd) |
musallambseiso | 4:e84724dcad49 | 60 | { |
musallambseiso | 13:f3560c403397 | 61 | _stats.draw_grid(lcd); // Draws initial grid and border. |
musallambseiso | 13:f3560c403397 | 62 | _stats.draw_health(lcd); // Draws health counter (initial state). |
musallambseiso | 13:f3560c403397 | 63 | _stats.check_health_high(lcd, collisions); // Checks and draws health counter (later states, high health). |
musallambseiso | 13:f3560c403397 | 64 | _stats.check_health_low(lcd, collisions); // Checks and draws health counter (later states, low health). |
musallambseiso | 13:f3560c403397 | 65 | _stats.check_star(lcd, star); // Checks and draws star. |
musallambseiso | 13:f3560c403397 | 66 | _stats.check_rocket(lcd, ammo); // Checks and draws rocket. |
musallambseiso | 13:f3560c403397 | 67 | _stats.draw_wave_counter(lcd, wave_counter); // Draws wave counter. |
musallambseiso | 13:f3560c403397 | 68 | _friendly.draw(lcd); // Draws friendly ship. |
musallambseiso | 13:f3560c403397 | 69 | |
musallambseiso | 13:f3560c403397 | 70 | // Draws enemy ships: |
musallambseiso | 13:f3560c403397 | 71 | _enemy1.draw(lcd); |
musallambseiso | 12:d68c757d829a | 72 | _enemy2.draw(lcd); |
musallambseiso | 12:d68c757d829a | 73 | _enemy3.draw(lcd); |
musallambseiso | 12:d68c757d829a | 74 | _enemy4.draw(lcd); |
musallambseiso | 12:d68c757d829a | 75 | _enemy5.draw(lcd); |
musallambseiso | 13:f3560c403397 | 76 | _enemy6.draw(lcd); |
musallambseiso | 4:e84724dcad49 | 77 | } |
musallambseiso | 4:e84724dcad49 | 78 | |
musallambseiso | 13:f3560c403397 | 79 | |
musallambseiso | 13:f3560c403397 | 80 | // Checks for changes in the game: |
musallambseiso | 11:10c01766f774 | 81 | |
musallambseiso | 12:d68c757d829a | 82 | void Engine::check_all(N5110 &lcd, Gamepad &pad) |
musallambseiso | 4:e84724dcad49 | 83 | { |
musallambseiso | 13:f3560c403397 | 84 | check_enemy_pass_high(pad); // Checks if upper enemy ships passed screen border and generates others. |
musallambseiso | 13:f3560c403397 | 85 | check_enemy_pass_low(pad); // Checks if lower enemy ships passed screen border and generates others. |
musallambseiso | 13:f3560c403397 | 86 | check_friendly_death_all(lcd, pad); // Checks for collisions and eventual friendly death. |
musallambseiso | 13:f3560c403397 | 87 | check_health_replenish(); // Checks if health should be replenished. |
musallambseiso | 13:f3560c403397 | 88 | check_rocket_replenish(); // Checks if rockets should be replenished. |
musallambseiso | 13:f3560c403397 | 89 | check_star_replenish(); // Checks if star should be replenished. |
musallambseiso | 13:f3560c403397 | 90 | check_level_two(lcd, pad); // Checks if level 2 has been reached. |
musallambseiso | 13:f3560c403397 | 91 | check_level_three(lcd, pad); // Checks if level 3 has been reached. |
musallambseiso | 13:f3560c403397 | 92 | check_level_four(lcd, pad); // Checks if level 4 has been reached. |
musallambseiso | 13:f3560c403397 | 93 | check_level_five(lcd, pad); // Checks if level 5 has been reached. |
musallambseiso | 13:f3560c403397 | 94 | check_pause(lcd, pad); // Checks if the game has been paused/unpaused. |
musallambseiso | 13:f3560c403397 | 95 | check_mode_toggle(lcd, pad); // Checks if the LCD mode has been toggled. |
musallambseiso | 6:cae24a40ab34 | 96 | } |
musallambseiso | 6:cae24a40ab34 | 97 | |
musallambseiso | 11:10c01766f774 | 98 | |
musallambseiso | 11:10c01766f774 | 99 | // Dynamic object position/velocity updater: |
musallambseiso | 11:10c01766f774 | 100 | |
musallambseiso | 12:d68c757d829a | 101 | void Engine::update_all(N5110 &lcd, Gamepad &pad) |
musallambseiso | 4:e84724dcad49 | 102 | { |
musallambseiso | 13:f3560c403397 | 103 | _friendly.update_basic(_d,_mag); // Updates position of friendly ship based on basic horizontal/vertical movements. |
musallambseiso | 13:f3560c403397 | 104 | _friendly.update_diagonal(_d,_mag); // Updates position of friendly ship based on diagonal movements. |
musallambseiso | 13:f3560c403397 | 105 | _friendly.check_pos(); // Prevents the friendly ship from going off-screen. |
musallambseiso | 13:f3560c403397 | 106 | |
musallambseiso | 13:f3560c403397 | 107 | // Updates enemy position: |
musallambseiso | 13:f3560c403397 | 108 | _enemy1.update(); |
musallambseiso | 12:d68c757d829a | 109 | _enemy2.update(); |
musallambseiso | 12:d68c757d829a | 110 | _enemy3.update(); |
musallambseiso | 12:d68c757d829a | 111 | _enemy4.update(); |
musallambseiso | 12:d68c757d829a | 112 | _enemy5.update(); |
musallambseiso | 12:d68c757d829a | 113 | _enemy6.update(); |
musallambseiso | 7:4c19433600c8 | 114 | } |
musallambseiso | 7:4c19433600c8 | 115 | |
musallambseiso | 11:10c01766f774 | 116 | |
musallambseiso | 13:f3560c403397 | 117 | // Method to enable shooting rockets: |
musallambseiso | 11:10c01766f774 | 118 | |
musallambseiso | 12:d68c757d829a | 119 | void Engine::shoot_rocket(N5110 &lcd, Gamepad &pad) |
musallambseiso | 7:4c19433600c8 | 120 | { |
musallambseiso | 13:f3560c403397 | 121 | Vector2D friendly_pos = _friendly.get_pos(); // Obtains friendly ship's position. |
musallambseiso | 13:f3560c403397 | 122 | int _x = friendly_pos.x+6; // Sets the variables as the nose of the friendly ship. |
musallambseiso | 12:d68c757d829a | 123 | int _y = friendly_pos.y+3; |
musallambseiso | 13:f3560c403397 | 124 | _rocket.init(_x, _y); // Initializes the rocket at those variables. |
musallambseiso | 4:e84724dcad49 | 125 | |
musallambseiso | 13:f3560c403397 | 126 | if ((pad.check_event(Gamepad::B_PRESSED) == true) && |
musallambseiso | 11:10c01766f774 | 127 | (ammo > 0)) |
musallambseiso | 11:10c01766f774 | 128 | { |
musallambseiso | 13:f3560c403397 | 129 | _rocket.draw(lcd); // Draws the rocket beam. |
musallambseiso | 13:f3560c403397 | 130 | check_enemy_death_high(pad); // Checks for upper enemy ship death. |
musallambseiso | 13:f3560c403397 | 131 | check_enemy_death_low(pad); // Checks for lower enemy ship death. |
musallambseiso | 13:f3560c403397 | 132 | pad.tone(1500.0,0.1); // Outputs a tone. |
musallambseiso | 13:f3560c403397 | 133 | ammo = ammo - 1; // Removes a rocket from the inventory. |
musallambseiso | 13:f3560c403397 | 134 | printf("A rocket has been shot. Remaining ammo: %d \n", ammo); |
musallambseiso | 12:d68c757d829a | 135 | } |
musallambseiso | 12:d68c757d829a | 136 | } |
musallambseiso | 12:d68c757d829a | 137 | |
musallambseiso | 12:d68c757d829a | 138 | |
musallambseiso | 13:f3560c403397 | 139 | // Method to enable shooting rockets: |
musallambseiso | 13:f3560c403397 | 140 | |
musallambseiso | 12:d68c757d829a | 141 | void Engine::shoot_star(N5110 &lcd, Gamepad &pad) |
musallambseiso | 12:d68c757d829a | 142 | { |
musallambseiso | 12:d68c757d829a | 143 | if ((pad.check_event(Gamepad::A_PRESSED) == true) && |
musallambseiso | 12:d68c757d829a | 144 | (star == true)) |
musallambseiso | 12:d68c757d829a | 145 | { |
musallambseiso | 13:f3560c403397 | 146 | // Reinitializes all enemies when a star is shot: |
musallambseiso | 12:d68c757d829a | 147 | _enemy1.init(_speed); |
musallambseiso | 12:d68c757d829a | 148 | _enemy2.init(_speed); |
musallambseiso | 12:d68c757d829a | 149 | _enemy3.init(_speed); |
musallambseiso | 12:d68c757d829a | 150 | _enemy4.init(_speed); |
musallambseiso | 12:d68c757d829a | 151 | _enemy5.init(_speed); |
musallambseiso | 12:d68c757d829a | 152 | _enemy6.init(_speed); |
musallambseiso | 13:f3560c403397 | 153 | |
musallambseiso | 13:f3560c403397 | 154 | wave_counter = wave_counter + 1; // Sends the player to the next wave. |
musallambseiso | 13:f3560c403397 | 155 | pad.tone(2000.0,0.5); |
musallambseiso | 13:f3560c403397 | 156 | star = false; // Removes the star from the inventory. |
musallambseiso | 13:f3560c403397 | 157 | printf("%s \n", "A star has been shot."); |
musallambseiso | 11:10c01766f774 | 158 | } |
musallambseiso | 10:b2dd5f484f98 | 159 | } |
musallambseiso | 4:e84724dcad49 | 160 | |
musallambseiso | 11:10c01766f774 | 161 | |
musallambseiso | 13:f3560c403397 | 162 | // Checks if upper enemy ships passed off screen and re-initializes ones that have: |
musallambseiso | 11:10c01766f774 | 163 | |
musallambseiso | 13:f3560c403397 | 164 | void Engine::check_enemy_pass_high(Gamepad &pad) |
musallambseiso | 4:e84724dcad49 | 165 | { |
musallambseiso | 12:d68c757d829a | 166 | Vector2D enemy1_pos = _enemy1.get_pos(); |
musallambseiso | 13:f3560c403397 | 167 | Vector2D enemy2_pos = _enemy2.get_pos(); |
musallambseiso | 13:f3560c403397 | 168 | Vector2D enemy3_pos = _enemy3.get_pos(); |
musallambseiso | 4:e84724dcad49 | 169 | |
musallambseiso | 13:f3560c403397 | 170 | if (enemy1_pos.x < 0) { // Checks if the Enemy1 ship's x position is negative (off-screen). |
musallambseiso | 13:f3560c403397 | 171 | _enemy1.init(_speed); // Reinitializes Enemy1. |
musallambseiso | 13:f3560c403397 | 172 | wave_counter = wave_counter + 1; // Wave counter is associated to Enemy1. |
musallambseiso | 13:f3560c403397 | 173 | health_regen_trigger = false; // Sets the regen triggers to false to allow for further regeneration in next waves. |
musallambseiso | 13:f3560c403397 | 174 | rocket_regen_trigger = false; |
musallambseiso | 12:d68c757d829a | 175 | star_regen_trigger = false; |
musallambseiso | 13:f3560c403397 | 176 | printf("A wave has passed. Current wave: %d \n", wave_counter); |
musallambseiso | 4:e84724dcad49 | 177 | } |
musallambseiso | 13:f3560c403397 | 178 | if (enemy2_pos.x < 0) { |
musallambseiso | 12:d68c757d829a | 179 | _enemy2.init(_speed); |
musallambseiso | 4:e84724dcad49 | 180 | } |
musallambseiso | 13:f3560c403397 | 181 | if (enemy3_pos.x < 0) { |
musallambseiso | 12:d68c757d829a | 182 | _enemy3.init(_speed); |
musallambseiso | 4:e84724dcad49 | 183 | } |
musallambseiso | 13:f3560c403397 | 184 | } |
musallambseiso | 13:f3560c403397 | 185 | |
musallambseiso | 13:f3560c403397 | 186 | |
musallambseiso | 13:f3560c403397 | 187 | // Checks if lower enemy ships passed off screen and re-initializes ones that have: |
musallambseiso | 13:f3560c403397 | 188 | |
musallambseiso | 13:f3560c403397 | 189 | void Engine::check_enemy_pass_low(Gamepad &pad) |
musallambseiso | 13:f3560c403397 | 190 | { |
musallambseiso | 12:d68c757d829a | 191 | Vector2D enemy4_pos = _enemy4.get_pos(); |
musallambseiso | 13:f3560c403397 | 192 | Vector2D enemy5_pos = _enemy5.get_pos(); |
musallambseiso | 13:f3560c403397 | 193 | Vector2D enemy6_pos = _enemy6.get_pos(); |
musallambseiso | 4:e84724dcad49 | 194 | |
musallambseiso | 13:f3560c403397 | 195 | if (enemy4_pos.x < 0) { |
musallambseiso | 12:d68c757d829a | 196 | _enemy4.init(_speed); |
musallambseiso | 4:e84724dcad49 | 197 | } |
musallambseiso | 4:e84724dcad49 | 198 | |
musallambseiso | 13:f3560c403397 | 199 | if (enemy5_pos.x < 0) { |
musallambseiso | 12:d68c757d829a | 200 | _enemy5.init(_speed); |
musallambseiso | 4:e84724dcad49 | 201 | } |
musallambseiso | 7:4c19433600c8 | 202 | |
musallambseiso | 13:f3560c403397 | 203 | if (enemy6_pos.x < 0) { |
musallambseiso | 12:d68c757d829a | 204 | _enemy6.init(_speed); |
musallambseiso | 7:4c19433600c8 | 205 | } |
musallambseiso | 4:e84724dcad49 | 206 | } |
musallambseiso | 4:e84724dcad49 | 207 | |
musallambseiso | 11:10c01766f774 | 208 | |
musallambseiso | 13:f3560c403397 | 209 | // Checks if upper enemy ships have been destroyed: |
musallambseiso | 11:10c01766f774 | 210 | |
musallambseiso | 13:f3560c403397 | 211 | void Engine::check_enemy_death_high(Gamepad &pad) |
musallambseiso | 4:e84724dcad49 | 212 | { |
musallambseiso | 4:e84724dcad49 | 213 | Vector2D friendly_pos = _friendly.get_pos(); |
musallambseiso | 5:88886c1bd2c1 | 214 | |
musallambseiso | 13:f3560c403397 | 215 | if (friendly_pos.y < 5) { // Checks if the friendly ship is opposite the Enemy1 ship. |
musallambseiso | 13:f3560c403397 | 216 | _enemy1.init(_speed); // Reinitializes the Enemy1 ship. |
musallambseiso | 13:f3560c403397 | 217 | printf("%s \n", "Enemy1 has been destroyed by a rocket and reinitialized."); |
musallambseiso | 11:10c01766f774 | 218 | } |
musallambseiso | 11:10c01766f774 | 219 | else if ((friendly_pos.y < 11) && |
musallambseiso | 13:f3560c403397 | 220 | (friendly_pos.y > 4)) { |
musallambseiso | 12:d68c757d829a | 221 | _enemy2.init(_speed); |
musallambseiso | 13:f3560c403397 | 222 | printf("%s \n", "Enemy2 has been destroyed by a rocket and reinitialized."); |
musallambseiso | 11:10c01766f774 | 223 | } |
musallambseiso | 11:10c01766f774 | 224 | else if ((friendly_pos.y < 17) && |
musallambseiso | 13:f3560c403397 | 225 | (friendly_pos.y > 10)) { |
musallambseiso | 12:d68c757d829a | 226 | _enemy3.init(_speed); |
musallambseiso | 13:f3560c403397 | 227 | printf("%s \n", "Enemy3 has been destroyed by a rocket and reinitialized."); |
musallambseiso | 13:f3560c403397 | 228 | } |
musallambseiso | 13:f3560c403397 | 229 | } |
musallambseiso | 13:f3560c403397 | 230 | |
musallambseiso | 13:f3560c403397 | 231 | |
musallambseiso | 13:f3560c403397 | 232 | // Checks if lower enemy ships have been destroyed: |
musallambseiso | 13:f3560c403397 | 233 | |
musallambseiso | 13:f3560c403397 | 234 | void Engine::check_enemy_death_low(Gamepad &pad) |
musallambseiso | 13:f3560c403397 | 235 | { |
musallambseiso | 13:f3560c403397 | 236 | Vector2D friendly_pos = _friendly.get_pos(); |
musallambseiso | 13:f3560c403397 | 237 | |
musallambseiso | 13:f3560c403397 | 238 | if ((friendly_pos.y < 25) && |
musallambseiso | 13:f3560c403397 | 239 | (friendly_pos.y > 16)) { |
musallambseiso | 12:d68c757d829a | 240 | _enemy4.init(_speed); |
musallambseiso | 13:f3560c403397 | 241 | printf("%s \n", "Enemy4 has been destroyed by a rocket and reinitialized."); |
musallambseiso | 11:10c01766f774 | 242 | } |
musallambseiso | 11:10c01766f774 | 243 | else if ((friendly_pos.y < 31) && |
musallambseiso | 13:f3560c403397 | 244 | (friendly_pos.y > 24)) { |
musallambseiso | 12:d68c757d829a | 245 | _enemy5.init(_speed); |
musallambseiso | 13:f3560c403397 | 246 | printf("%s \n", "Enemy5 has been destroyed by a rocket and reinitialized."); |
musallambseiso | 11:10c01766f774 | 247 | } |
musallambseiso | 13:f3560c403397 | 248 | else if (friendly_pos.y > 30) { |
musallambseiso | 12:d68c757d829a | 249 | _enemy6.init(_speed); |
musallambseiso | 13:f3560c403397 | 250 | printf("%s \n", "Enemy6 has been destroyed by a rocket and reinitialized."); |
musallambseiso | 11:10c01766f774 | 251 | } |
musallambseiso | 11:10c01766f774 | 252 | } |
musallambseiso | 11:10c01766f774 | 253 | |
musallambseiso | 11:10c01766f774 | 254 | |
musallambseiso | 13:f3560c403397 | 255 | // Checks for collisions between friendly and an enemy ship: |
musallambseiso | 11:10c01766f774 | 256 | |
musallambseiso | 12:d68c757d829a | 257 | void Engine::check_friendly_death(Gamepad &pad, Vector2D enemy_pos) |
musallambseiso | 11:10c01766f774 | 258 | { |
musallambseiso | 11:10c01766f774 | 259 | Vector2D friendly_pos = _friendly.get_pos(); |
musallambseiso | 11:10c01766f774 | 260 | |
musallambseiso | 13:f3560c403397 | 261 | if ((friendly_pos.y >= enemy_pos.y-5) && // Checks if x and y coordinates of friendly and enemy ships |
musallambseiso | 13:f3560c403397 | 262 | (friendly_pos.y <= enemy_pos.y+5) && // match, including the friendly and enemy size. |
musallambseiso | 12:d68c757d829a | 263 | (friendly_pos.x+6 >= enemy_pos.x) && |
musallambseiso | 12:d68c757d829a | 264 | (friendly_pos.x+6 <= enemy_pos.x+5)) |
musallambseiso | 10:b2dd5f484f98 | 265 | { |
musallambseiso | 5:88886c1bd2c1 | 266 | pad.tone(800.0,0.1); |
musallambseiso | 13:f3560c403397 | 267 | collisions = collisions + 1; // Registers a collision. |
musallambseiso | 5:88886c1bd2c1 | 268 | } |
musallambseiso | 5:88886c1bd2c1 | 269 | } |
musallambseiso | 5:88886c1bd2c1 | 270 | |
musallambseiso | 11:10c01766f774 | 271 | |
musallambseiso | 11:10c01766f774 | 272 | // Checks for collisions between friendly and each enemy ship: |
musallambseiso | 11:10c01766f774 | 273 | |
musallambseiso | 13:f3560c403397 | 274 | void Engine::check_friendly_death_all(N5110 &lcd, Gamepad &pad) |
musallambseiso | 5:88886c1bd2c1 | 275 | { |
musallambseiso | 12:d68c757d829a | 276 | Vector2D enemy1_pos = _enemy1.get_pos(); |
musallambseiso | 12:d68c757d829a | 277 | Vector2D enemy2_pos = _enemy2.get_pos(); |
musallambseiso | 12:d68c757d829a | 278 | Vector2D enemy3_pos = _enemy3.get_pos(); |
musallambseiso | 12:d68c757d829a | 279 | Vector2D enemy4_pos = _enemy4.get_pos(); |
musallambseiso | 12:d68c757d829a | 280 | Vector2D enemy5_pos = _enemy5.get_pos(); |
musallambseiso | 12:d68c757d829a | 281 | Vector2D enemy6_pos = _enemy6.get_pos(); |
musallambseiso | 5:88886c1bd2c1 | 282 | |
musallambseiso | 13:f3560c403397 | 283 | // Executes the "check_friendly_death" method for each enemy ship: |
musallambseiso | 12:d68c757d829a | 284 | check_friendly_death(pad, enemy1_pos); |
musallambseiso | 12:d68c757d829a | 285 | check_friendly_death(pad, enemy2_pos); |
musallambseiso | 12:d68c757d829a | 286 | check_friendly_death(pad, enemy3_pos); |
musallambseiso | 12:d68c757d829a | 287 | check_friendly_death(pad, enemy4_pos); |
musallambseiso | 12:d68c757d829a | 288 | check_friendly_death(pad, enemy5_pos); |
musallambseiso | 12:d68c757d829a | 289 | check_friendly_death(pad, enemy6_pos); |
musallambseiso | 13:f3560c403397 | 290 | |
musallambseiso | 13:f3560c403397 | 291 | check_gameover(lcd, pad); // Game over sequence, runs if health subzero. |
musallambseiso | 5:88886c1bd2c1 | 292 | } |
musallambseiso | 5:88886c1bd2c1 | 293 | |
musallambseiso | 12:d68c757d829a | 294 | |
musallambseiso | 13:f3560c403397 | 295 | // Checks if it is time for health to be replenished: |
musallambseiso | 13:f3560c403397 | 296 | |
musallambseiso | 13:f3560c403397 | 297 | void Engine::check_health_replenish() |
musallambseiso | 12:d68c757d829a | 298 | { |
musallambseiso | 13:f3560c403397 | 299 | if (!(collisions == 0) && // Checks if player isnt already at full health. |
musallambseiso | 14:94011e4a0624 | 300 | (health_regen_trigger == false)) { // Checks if health hasn't already been replenished this wave. (to prevent spamming) |
musallambseiso | 13:f3560c403397 | 301 | if ((wave_counter == 20) || // Checks if the game is at a certain wave: |
musallambseiso | 13:f3560c403397 | 302 | (wave_counter == 40) || |
musallambseiso | 13:f3560c403397 | 303 | (wave_counter == 60) || |
musallambseiso | 13:f3560c403397 | 304 | (wave_counter == 80) || |
musallambseiso | 13:f3560c403397 | 305 | (wave_counter == 100) || |
musallambseiso | 13:f3560c403397 | 306 | (wave_counter == 120) || |
musallambseiso | 13:f3560c403397 | 307 | (wave_counter == 140) || |
musallambseiso | 13:f3560c403397 | 308 | (wave_counter == 160) || |
musallambseiso | 13:f3560c403397 | 309 | (wave_counter == 180) || |
musallambseiso | 13:f3560c403397 | 310 | (wave_counter == 200)) { |
musallambseiso | 13:f3560c403397 | 311 | collisions = collisions - 1; // Adds health to the health bar. |
musallambseiso | 13:f3560c403397 | 312 | health_regen_trigger = true; // Sets the trigger to show health has already been replenished this wave. |
musallambseiso | 13:f3560c403397 | 313 | printf("%s \n", "Health has been regenerated."); |
musallambseiso | 13:f3560c403397 | 314 | } |
musallambseiso | 13:f3560c403397 | 315 | } |
musallambseiso | 13:f3560c403397 | 316 | } |
musallambseiso | 13:f3560c403397 | 317 | |
musallambseiso | 13:f3560c403397 | 318 | |
musallambseiso | 13:f3560c403397 | 319 | // Checks if it is time for a rocket to be replenished: |
musallambseiso | 13:f3560c403397 | 320 | |
musallambseiso | 13:f3560c403397 | 321 | void Engine::check_rocket_replenish() |
musallambseiso | 13:f3560c403397 | 322 | { |
musallambseiso | 13:f3560c403397 | 323 | if ((ammo < 3) && // Checks if player doesn't already have 3 rockets. |
musallambseiso | 13:f3560c403397 | 324 | (rocket_regen_trigger == false)) { // Checks if a rocket hasn't already been replenished this wave. (to prevent spamming) |
musallambseiso | 13:f3560c403397 | 325 | if ((wave_counter == 5) || // Checks if the game is at a certain wave: |
musallambseiso | 12:d68c757d829a | 326 | (wave_counter == 10) || |
musallambseiso | 12:d68c757d829a | 327 | (wave_counter == 15) || |
musallambseiso | 12:d68c757d829a | 328 | (wave_counter == 20) || |
musallambseiso | 12:d68c757d829a | 329 | (wave_counter == 25) || |
musallambseiso | 12:d68c757d829a | 330 | (wave_counter == 30) || |
musallambseiso | 12:d68c757d829a | 331 | (wave_counter == 50) || |
musallambseiso | 12:d68c757d829a | 332 | (wave_counter == 75) || |
musallambseiso | 12:d68c757d829a | 333 | (wave_counter == 100) || |
musallambseiso | 12:d68c757d829a | 334 | (wave_counter == 150) || |
musallambseiso | 13:f3560c403397 | 335 | (wave_counter == 200)) { |
musallambseiso | 13:f3560c403397 | 336 | ammo = ammo + 1; // Adds a rocket to the inventory. |
musallambseiso | 13:f3560c403397 | 337 | rocket_regen_trigger = true; // Sets the trigger to show a rocket has already been replenished this wave. |
musallambseiso | 13:f3560c403397 | 338 | printf("A rocket has been regenerated. Current number of rockets: %d \n", ammo); |
musallambseiso | 12:d68c757d829a | 339 | } |
musallambseiso | 12:d68c757d829a | 340 | } |
musallambseiso | 12:d68c757d829a | 341 | } |
musallambseiso | 12:d68c757d829a | 342 | |
musallambseiso | 13:f3560c403397 | 343 | |
musallambseiso | 13:f3560c403397 | 344 | // Checks if it is time for the star to be replenished: |
musallambseiso | 13:f3560c403397 | 345 | |
musallambseiso | 13:f3560c403397 | 346 | void Engine::check_star_replenish() |
musallambseiso | 12:d68c757d829a | 347 | { |
musallambseiso | 13:f3560c403397 | 348 | if ((star == false) && // Checks if player doesn't already have a star. |
musallambseiso | 13:f3560c403397 | 349 | (star_regen_trigger == false)) // Checks if a star hasn't already been replenished this wave. (to prevent spamming) |
musallambseiso | 12:d68c757d829a | 350 | { |
musallambseiso | 13:f3560c403397 | 351 | if ((wave_counter == 5) || // Checks if the game is at a certain wave: |
musallambseiso | 12:d68c757d829a | 352 | (wave_counter == 25) || |
musallambseiso | 12:d68c757d829a | 353 | (wave_counter == 50) || |
musallambseiso | 12:d68c757d829a | 354 | (wave_counter == 100) || |
musallambseiso | 12:d68c757d829a | 355 | (wave_counter == 150) || |
musallambseiso | 12:d68c757d829a | 356 | (wave_counter == 200)) |
musallambseiso | 12:d68c757d829a | 357 | { |
musallambseiso | 13:f3560c403397 | 358 | star = true; // Adds the star to the inventory. |
musallambseiso | 13:f3560c403397 | 359 | star_regen_trigger = true; // Sets the trigger to show the star has already been replenished this wave. |
musallambseiso | 13:f3560c403397 | 360 | printf("%s \n", "A star has been regenerated."); |
musallambseiso | 12:d68c757d829a | 361 | } |
musallambseiso | 12:d68c757d829a | 362 | } |
musallambseiso | 12:d68c757d829a | 363 | } |
musallambseiso | 12:d68c757d829a | 364 | |
musallambseiso | 12:d68c757d829a | 365 | |
musallambseiso | 11:10c01766f774 | 366 | // Level two sequence, occurs if wave 5 is reached: |
musallambseiso | 11:10c01766f774 | 367 | |
musallambseiso | 12:d68c757d829a | 368 | void Engine::check_level_two(N5110 &lcd, Gamepad &pad) |
musallambseiso | 5:88886c1bd2c1 | 369 | { |
musallambseiso | 11:10c01766f774 | 370 | if (wave_counter == 5) |
musallambseiso | 10:b2dd5f484f98 | 371 | { |
musallambseiso | 11:10c01766f774 | 372 | lcd.drawRect(0,0,84,48,FILL_WHITE); |
musallambseiso | 12:d68c757d829a | 373 | lcd.printString(" Nice! Level 2",0,1); |
musallambseiso | 11:10c01766f774 | 374 | lcd.printString(" Press Back! ",0,4); |
musallambseiso | 11:10c01766f774 | 375 | lcd.refresh(); |
musallambseiso | 11:10c01766f774 | 376 | wait(1); |
musallambseiso | 11:10c01766f774 | 377 | |
musallambseiso | 13:f3560c403397 | 378 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { // Waits until the back button is pressed. |
musallambseiso | 11:10c01766f774 | 379 | wait(0.1); |
musallambseiso | 11:10c01766f774 | 380 | } |
musallambseiso | 11:10c01766f774 | 381 | |
musallambseiso | 13:f3560c403397 | 382 | init(LEVEL_TWO, lcd, pad, collisions, wave_counter, ammo, star, pauses); // Initializes the ships at an elevated speed. |
musallambseiso | 13:f3560c403397 | 383 | wave_counter = 6; // Increments the wave counter. |
musallambseiso | 13:f3560c403397 | 384 | printf("%s \n", "Level two has been reached."); |
musallambseiso | 4:e84724dcad49 | 385 | } |
musallambseiso | 5:88886c1bd2c1 | 386 | } |
musallambseiso | 5:88886c1bd2c1 | 387 | |
musallambseiso | 11:10c01766f774 | 388 | |
musallambseiso | 11:10c01766f774 | 389 | // Level three sequence, occurs if wave 25 is reached: |
musallambseiso | 11:10c01766f774 | 390 | |
musallambseiso | 12:d68c757d829a | 391 | void Engine::check_level_three(N5110 &lcd, Gamepad &pad) |
musallambseiso | 5:88886c1bd2c1 | 392 | { |
musallambseiso | 11:10c01766f774 | 393 | if (wave_counter == 25) |
musallambseiso | 10:b2dd5f484f98 | 394 | { |
musallambseiso | 11:10c01766f774 | 395 | lcd.drawRect(0,0,84,48,FILL_WHITE); |
musallambseiso | 12:d68c757d829a | 396 | lcd.printString(" Nice! Level 3",0,1); |
musallambseiso | 11:10c01766f774 | 397 | lcd.printString(" Press Back! ",0,4); |
musallambseiso | 11:10c01766f774 | 398 | lcd.refresh(); |
musallambseiso | 11:10c01766f774 | 399 | wait(1); |
musallambseiso | 11:10c01766f774 | 400 | |
musallambseiso | 11:10c01766f774 | 401 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { |
musallambseiso | 11:10c01766f774 | 402 | wait(0.1); |
musallambseiso | 11:10c01766f774 | 403 | } |
musallambseiso | 11:10c01766f774 | 404 | |
musallambseiso | 13:f3560c403397 | 405 | init(LEVEL_THREE, lcd, pad, collisions, wave_counter, ammo, star, pauses); |
musallambseiso | 11:10c01766f774 | 406 | wave_counter = 26; |
musallambseiso | 13:f3560c403397 | 407 | printf("%s \n", "Level three has been reached."); |
musallambseiso | 5:88886c1bd2c1 | 408 | } |
musallambseiso | 5:88886c1bd2c1 | 409 | } |
musallambseiso | 5:88886c1bd2c1 | 410 | |
musallambseiso | 11:10c01766f774 | 411 | |
musallambseiso | 11:10c01766f774 | 412 | // Level three sequence, occurs if wave 50 is reached: |
musallambseiso | 11:10c01766f774 | 413 | |
musallambseiso | 12:d68c757d829a | 414 | void Engine::check_level_four(N5110 &lcd, Gamepad &pad) |
musallambseiso | 5:88886c1bd2c1 | 415 | { |
musallambseiso | 11:10c01766f774 | 416 | if (wave_counter == 50) |
musallambseiso | 10:b2dd5f484f98 | 417 | { |
musallambseiso | 11:10c01766f774 | 418 | lcd.drawRect(0,0,84,48,FILL_WHITE); |
musallambseiso | 12:d68c757d829a | 419 | lcd.printString(" Nice! Level 4",0,1); |
musallambseiso | 11:10c01766f774 | 420 | lcd.printString(" Press Back! ",0,4); |
musallambseiso | 11:10c01766f774 | 421 | lcd.refresh(); |
musallambseiso | 11:10c01766f774 | 422 | wait(1); |
musallambseiso | 11:10c01766f774 | 423 | |
musallambseiso | 11:10c01766f774 | 424 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { |
musallambseiso | 11:10c01766f774 | 425 | wait(0.1); |
musallambseiso | 11:10c01766f774 | 426 | } |
musallambseiso | 11:10c01766f774 | 427 | |
musallambseiso | 13:f3560c403397 | 428 | init(LEVEL_FOUR, lcd, pad, collisions, wave_counter, ammo, star, pauses); |
musallambseiso | 11:10c01766f774 | 429 | wave_counter = 51; |
musallambseiso | 13:f3560c403397 | 430 | printf("%s \n", "Level four has been reached."); |
musallambseiso | 5:88886c1bd2c1 | 431 | } |
musallambseiso | 5:88886c1bd2c1 | 432 | } |
musallambseiso | 5:88886c1bd2c1 | 433 | |
musallambseiso | 11:10c01766f774 | 434 | |
musallambseiso | 11:10c01766f774 | 435 | // Level five sequence, occurs if wave 100 is reached: |
musallambseiso | 11:10c01766f774 | 436 | |
musallambseiso | 12:d68c757d829a | 437 | void Engine::check_level_five(N5110 &lcd, Gamepad &pad) |
musallambseiso | 7:4c19433600c8 | 438 | { |
musallambseiso | 11:10c01766f774 | 439 | if (wave_counter == 100) |
musallambseiso | 10:b2dd5f484f98 | 440 | { |
musallambseiso | 11:10c01766f774 | 441 | lcd.drawRect(0,0,84,48,FILL_WHITE); |
musallambseiso | 12:d68c757d829a | 442 | lcd.printString(" 5. Good Luck!",0,1); |
musallambseiso | 11:10c01766f774 | 443 | lcd.printString(" Press Back! ",0,4); |
musallambseiso | 11:10c01766f774 | 444 | lcd.refresh(); |
musallambseiso | 11:10c01766f774 | 445 | wait(1); |
musallambseiso | 11:10c01766f774 | 446 | |
musallambseiso | 11:10c01766f774 | 447 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { |
musallambseiso | 11:10c01766f774 | 448 | wait(0.1); |
musallambseiso | 11:10c01766f774 | 449 | } |
musallambseiso | 11:10c01766f774 | 450 | |
musallambseiso | 13:f3560c403397 | 451 | init(LEVEL_FIVE, lcd, pad, collisions, wave_counter, ammo, star, pauses); |
musallambseiso | 11:10c01766f774 | 452 | wave_counter = 101; |
musallambseiso | 13:f3560c403397 | 453 | printf("%s \n", "Level five has been reached."); |
musallambseiso | 7:4c19433600c8 | 454 | } |
musallambseiso | 7:4c19433600c8 | 455 | } |
musallambseiso | 8:1c0bc1a08153 | 456 | |
musallambseiso | 11:10c01766f774 | 457 | |
musallambseiso | 12:d68c757d829a | 458 | // Pause sequence, checks if the game has been paused/unpaused |
musallambseiso | 12:d68c757d829a | 459 | |
musallambseiso | 12:d68c757d829a | 460 | void Engine::check_pause(N5110 &lcd, Gamepad &pad) |
musallambseiso | 12:d68c757d829a | 461 | { |
musallambseiso | 13:f3560c403397 | 462 | if ((pad.check_event(Gamepad::START_PRESSED) == true) && // Checks if the start button has been pressed. |
musallambseiso | 13:f3560c403397 | 463 | (pauses > 0)) // Checks if the player has remaining pauses. |
musallambseiso | 13:f3560c403397 | 464 | { |
musallambseiso | 13:f3560c403397 | 465 | printf("%s \n", "The game has been paused."); |
musallambseiso | 13:f3560c403397 | 466 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
musallambseiso | 13:f3560c403397 | 467 | // Flashes LEDs, switches between inverse and normal LCD modes, and waits until start button is pressed (unpause): |
musallambseiso | 13:f3560c403397 | 468 | pad.leds_on(); |
musallambseiso | 12:d68c757d829a | 469 | lcd.inverseMode(); |
musallambseiso | 12:d68c757d829a | 470 | wait(0.5); |
musallambseiso | 12:d68c757d829a | 471 | pad.leds_off(); |
musallambseiso | 12:d68c757d829a | 472 | lcd.normalMode(); |
musallambseiso | 12:d68c757d829a | 473 | wait(0.5); |
musallambseiso | 12:d68c757d829a | 474 | } |
musallambseiso | 13:f3560c403397 | 475 | pauses = pauses - 1; // Takes a pause from the player. |
musallambseiso | 13:f3560c403397 | 476 | printf("%s \n", "The game has been unpaused."); |
musallambseiso | 12:d68c757d829a | 477 | } |
musallambseiso | 12:d68c757d829a | 478 | } |
musallambseiso | 12:d68c757d829a | 479 | |
musallambseiso | 12:d68c757d829a | 480 | |
musallambseiso | 13:f3560c403397 | 481 | // Checks if the LCD's display mode has been toggled: |
musallambseiso | 13:f3560c403397 | 482 | |
musallambseiso | 12:d68c757d829a | 483 | void Engine::check_mode_toggle(N5110 &lcd, Gamepad &pad) |
musallambseiso | 12:d68c757d829a | 484 | { |
musallambseiso | 13:f3560c403397 | 485 | if (pad.check_event(Gamepad::R_PRESSED) == true) { // Checks if right button is pressed. |
musallambseiso | 13:f3560c403397 | 486 | lcd.normalMode(); // Sets the LCD to normal mode. |
musallambseiso | 12:d68c757d829a | 487 | pad.tone(1000.0,0.2); |
musallambseiso | 13:f3560c403397 | 488 | printf("%s \n", "Normal LCD mode has been toggled on."); |
musallambseiso | 12:d68c757d829a | 489 | } |
musallambseiso | 12:d68c757d829a | 490 | |
musallambseiso | 13:f3560c403397 | 491 | if (pad.check_event(Gamepad::L_PRESSED) == true) { // Checks if left button is pressed. |
musallambseiso | 13:f3560c403397 | 492 | lcd.inverseMode(); // Sets the LCD to inverse mode. |
musallambseiso | 12:d68c757d829a | 493 | pad.tone(1000.0,0.2); |
musallambseiso | 13:f3560c403397 | 494 | printf("%s \n", "Inverse LCD mode has been toggled on."); |
musallambseiso | 12:d68c757d829a | 495 | } |
musallambseiso | 12:d68c757d829a | 496 | } |
musallambseiso | 12:d68c757d829a | 497 | |
musallambseiso | 12:d68c757d829a | 498 | |
musallambseiso | 11:10c01766f774 | 499 | // Game over sequence, ends game if health is below zero: |
musallambseiso | 8:1c0bc1a08153 | 500 | |
musallambseiso | 12:d68c757d829a | 501 | void Engine::check_gameover(N5110 &lcd, Gamepad &pad) |
musallambseiso | 8:1c0bc1a08153 | 502 | { |
musallambseiso | 13:f3560c403397 | 503 | if (collisions >= 6) { // Checks if collisions are above 6 (health zero). |
musallambseiso | 13:f3560c403397 | 504 | lcd.drawRect(0,0,84,48,FILL_WHITE); // Draws a dialog box. |
musallambseiso | 12:d68c757d829a | 505 | char buffer[14]; |
musallambseiso | 12:d68c757d829a | 506 | int length = sprintf(buffer," Score: %2d",wave_counter); |
musallambseiso | 13:f3560c403397 | 507 | lcd.printString(buffer,0,2); // Displays how many waves the player survived. |
musallambseiso | 11:10c01766f774 | 508 | lcd.printString(" You Lose! ",0,1); |
musallambseiso | 11:10c01766f774 | 509 | lcd.printString(" Press Back! ",0,4); |
musallambseiso | 8:1c0bc1a08153 | 510 | lcd.refresh(); |
musallambseiso | 13:f3560c403397 | 511 | |
musallambseiso | 13:f3560c403397 | 512 | printf("%s \n", "The player has lost."); |
musallambseiso | 13:f3560c403397 | 513 | |
musallambseiso | 13:f3560c403397 | 514 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { // Waits until the back button is pressed. |
musallambseiso | 8:1c0bc1a08153 | 515 | wait(0.1); |
musallambseiso | 8:1c0bc1a08153 | 516 | } |
musallambseiso | 13:f3560c403397 | 517 | |
musallambseiso | 13:f3560c403397 | 518 | printf("%s \n", "The player has restarted the game."); |
musallambseiso | 13:f3560c403397 | 519 | init(LEVEL_ONE, lcd, pad, 0, 0, 3, true, 3); // Reinitializes the game at the lowest speed. Reinitializes variables. |
musallambseiso | 8:1c0bc1a08153 | 520 | } |
musallambseiso | 10:b2dd5f484f98 | 521 | } |