Ben Evans University Second Year Project. Game Called Defender.
Hello, soldier, you have been specially selected as the defender of planet earth.
Your mission, if you choose to accept it. Fly around the planet and pulverise invading alien ships for as long as you can. Stop the aliens abducting the innocent people on the ground. Be warned if an alien ship manages to abduct a person and take them to top of the screen, they will no longer move randomly and will begin to hunt you down. This sounds like a challenge you were trained for.
But don’t worry soldier you’re not going into battle empty-handed. Your ship is equipped with a state of the art laser beam that has unlimited ammo and four smart bombs that will destroy anything on the screen. The ship also has three lives so use them wisely.
As time goes on more alien ships will arrive on planet earth increasing the difficulty of your mission. And remember the landscape bellow loops around so if you continually fly in the same direction you go to your original position. Good luck soldier.
GameEngine/PlayEngine.cpp@87:832ca78426b5, 2020-05-27 (annotated)
- Committer:
- evanso
- Date:
- Wed May 27 02:06:05 2020 +0000
- Revision:
- 87:832ca78426b5
- Parent:
- 86:eecd168c3a23
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evanso | 80:870bc6b4bf08 | 1 | #include "PlayEngine.h" |
evanso | 80:870bc6b4bf08 | 2 | |
evanso | 82:3211b31e9421 | 3 | // Spaceship control ----------------------------------------------------------- |
evanso | 80:870bc6b4bf08 | 4 | |
evanso | 82:3211b31e9421 | 5 | void PlayEngine::read_joystick_direction() { |
evanso | 80:870bc6b4bf08 | 6 | d_ = pad.get_direction(); |
evanso | 80:870bc6b4bf08 | 7 | } |
evanso | 80:870bc6b4bf08 | 8 | |
evanso | 82:3211b31e9421 | 9 | void PlayEngine::read_accelerometer_direction(float roll,float pitch) { |
evanso | 80:870bc6b4bf08 | 10 | // printf("Pitch = %f Roll = %f\n", pitch, roll); |
evanso | 80:870bc6b4bf08 | 11 | |
evanso | 82:3211b31e9421 | 12 | // North |
evanso | 85:87bc28b151d8 | 13 | if (pitch >= -35) { |
evanso | 85:87bc28b151d8 | 14 | if (roll >= 10) { |
evanso | 80:870bc6b4bf08 | 15 | d_ = NE; |
evanso | 82:3211b31e9421 | 16 | }else if (roll <= -10) { |
evanso | 80:870bc6b4bf08 | 17 | d_ = NW; |
evanso | 80:870bc6b4bf08 | 18 | }else { |
evanso | 80:870bc6b4bf08 | 19 | d_ = N; |
evanso | 80:870bc6b4bf08 | 20 | } |
evanso | 82:3211b31e9421 | 21 | // South |
evanso | 85:87bc28b151d8 | 22 | }else if (pitch <= -55) { |
evanso | 82:3211b31e9421 | 23 | if (roll >= 10) { |
evanso | 80:870bc6b4bf08 | 24 | d_ = SE; |
evanso | 85:87bc28b151d8 | 25 | }else if (roll <= -10) { |
evanso | 80:870bc6b4bf08 | 26 | d_ = SW; |
evanso | 80:870bc6b4bf08 | 27 | }else { |
evanso | 80:870bc6b4bf08 | 28 | d_ = S; |
evanso | 80:870bc6b4bf08 | 29 | } |
evanso | 80:870bc6b4bf08 | 30 | // East/west |
evanso | 80:870bc6b4bf08 | 31 | }else { |
evanso | 82:3211b31e9421 | 32 | if (roll >= 10) { |
evanso | 80:870bc6b4bf08 | 33 | d_ = E; |
evanso | 85:87bc28b151d8 | 34 | }else if (roll <= -10) { |
evanso | 80:870bc6b4bf08 | 35 | d_ = W; |
evanso | 80:870bc6b4bf08 | 36 | }else{ |
evanso | 80:870bc6b4bf08 | 37 | d_ = CENTRE; |
evanso | 80:870bc6b4bf08 | 38 | } |
evanso | 80:870bc6b4bf08 | 39 | } |
evanso | 80:870bc6b4bf08 | 40 | } |
evanso | 80:870bc6b4bf08 | 41 | |
evanso | 82:3211b31e9421 | 42 | void PlayEngine::spaceship_lives_leds() { |
evanso | 80:870bc6b4bf08 | 43 | pad.leds_off(); |
evanso | 80:870bc6b4bf08 | 44 | |
evanso | 82:3211b31e9421 | 45 | // Top two red leds on if 1 lives left |
evanso | 85:87bc28b151d8 | 46 | if (spaceship_lives_ >= 1) { |
evanso | 80:870bc6b4bf08 | 47 | pad.led(1,1); |
evanso | 80:870bc6b4bf08 | 48 | pad.led(4,1); |
evanso | 80:870bc6b4bf08 | 49 | } |
evanso | 80:870bc6b4bf08 | 50 | |
evanso | 82:3211b31e9421 | 51 | // Middle two orange leds also on if 2 lives left |
evanso | 85:87bc28b151d8 | 52 | if (spaceship_lives_ >= 2) { |
evanso | 80:870bc6b4bf08 | 53 | pad.led(2,1); |
evanso | 80:870bc6b4bf08 | 54 | pad.led(5,1); |
evanso | 80:870bc6b4bf08 | 55 | } |
evanso | 80:870bc6b4bf08 | 56 | |
evanso | 82:3211b31e9421 | 57 | // Bottom two red leds also on if 3 lives left |
evanso | 85:87bc28b151d8 | 58 | if (spaceship_lives_ == 3) { |
evanso | 80:870bc6b4bf08 | 59 | pad.led(3,1); |
evanso | 80:870bc6b4bf08 | 60 | pad.led(6,1); |
evanso | 80:870bc6b4bf08 | 61 | } |
evanso | 80:870bc6b4bf08 | 62 | } |
evanso | 80:870bc6b4bf08 | 63 | |
evanso | 80:870bc6b4bf08 | 64 | |
evanso | 80:870bc6b4bf08 | 65 | |
evanso | 80:870bc6b4bf08 | 66 | |
evanso | 82:3211b31e9421 | 67 | // Weapon control -------------------------------------------------------------- |
evanso | 80:870bc6b4bf08 | 68 | |
evanso | 82:3211b31e9421 | 69 | void PlayEngine::create_weapons_bullets() { |
evanso | 82:3211b31e9421 | 70 | // Controlls speed that bullet can be fired |
evanso | 82:3211b31e9421 | 71 | if (pad.A_pressed() && bullet_timer_ <=0) { |
evanso | 81:78c461e6770b | 72 | // Bullet object |
evanso | 81:78c461e6770b | 73 | Weapons new_bullet; |
evanso | 81:78c461e6770b | 74 | |
evanso | 81:78c461e6770b | 75 | new_bullet.init(spaceship.get_pos(), |
evanso | 81:78c461e6770b | 76 | spaceship.get_spaceship_sprite_direction(), true); |
evanso | 81:78c461e6770b | 77 | |
evanso | 81:78c461e6770b | 78 | // Stores bullet object in vector |
evanso | 81:78c461e6770b | 79 | bullet_vector.push_back(new_bullet); |
evanso | 81:78c461e6770b | 80 | bullet_timer_ = 10; |
evanso | 81:78c461e6770b | 81 | } |
evanso | 81:78c461e6770b | 82 | bullet_timer_--; |
evanso | 80:870bc6b4bf08 | 83 | } |
evanso | 80:870bc6b4bf08 | 84 | |
evanso | 82:3211b31e9421 | 85 | void PlayEngine::create_weapons_smart_bomb() { |
evanso | 82:3211b31e9421 | 86 | if (pad.B_pressed() && smart_bomb_counter_ > 0 && smart_bomb_timer_ <=0) { |
evanso | 80:870bc6b4bf08 | 87 | weapons.smart_bomb(lcd); |
evanso | 80:870bc6b4bf08 | 88 | smart_bomb_counter_--; |
evanso | 80:870bc6b4bf08 | 89 | |
evanso | 82:3211b31e9421 | 90 | // Deletes alien object if on screen and person object if abducted |
evanso | 82:3211b31e9421 | 91 | for (int i = (alien_vector.size() -1) ; i >= 0 ; i--) { |
evanso | 80:870bc6b4bf08 | 92 | Vector2D alien_pos = alien_vector[i].get_pos(); |
evanso | 85:87bc28b151d8 | 93 | |
evanso | 80:870bc6b4bf08 | 94 | // Creats explosion if alien was on the screen |
evanso | 86:eecd168c3a23 | 95 | if(alien_pos.x <= 84 && alien_pos.x >= -6){ |
evanso | 86:eecd168c3a23 | 96 | create_explosion(alien_pos); |
evanso | 86:eecd168c3a23 | 97 | |
evanso | 86:eecd168c3a23 | 98 | // Delete person object if was being abducted by destroyed alien |
evanso | 86:eecd168c3a23 | 99 | if(alien_vector[i].get_collision_people_element() > -1){ |
evanso | 86:eecd168c3a23 | 100 | people_vector.erase(people_vector.begin() + |
evanso | 86:eecd168c3a23 | 101 | alien_vector[i].get_collision_people_element()); |
evanso | 86:eecd168c3a23 | 102 | } |
evanso | 86:eecd168c3a23 | 103 | |
evanso | 80:870bc6b4bf08 | 104 | alien_vector.erase(alien_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 105 | points_ ++; |
evanso | 86:eecd168c3a23 | 106 | } |
evanso | 80:870bc6b4bf08 | 107 | } |
evanso | 81:78c461e6770b | 108 | |
evanso | 82:3211b31e9421 | 109 | // Timer to stop smart bomb button be accidently pressed twice or spammed |
evanso | 80:870bc6b4bf08 | 110 | smart_bomb_timer_ = 30; |
evanso | 80:870bc6b4bf08 | 111 | } |
evanso | 81:78c461e6770b | 112 | smart_bomb_timer_--; |
evanso | 80:870bc6b4bf08 | 113 | } |
evanso | 80:870bc6b4bf08 | 114 | |
evanso | 82:3211b31e9421 | 115 | void PlayEngine::draw_bullets() { |
evanso | 82:3211b31e9421 | 116 | // Interates over bullet vectors, moves and draws bullet objects |
evanso | 80:870bc6b4bf08 | 117 | |
evanso | 80:870bc6b4bf08 | 118 | // Alien bullets |
evanso | 82:3211b31e9421 | 119 | for (int i = 0; i < alien_bullet_vector.size(); i++) { |
evanso | 80:870bc6b4bf08 | 120 | alien_bullet_vector[i].draw_alien_bullet(lcd, d_); |
evanso | 80:870bc6b4bf08 | 121 | |
evanso | 82:3211b31e9421 | 122 | // Deletes alien bullet object after bullet has moved set distance |
evanso | 85:87bc28b151d8 | 123 | if (alien_bullet_vector[i].get_bullet_delete_counter() >> 5) { |
evanso | 80:870bc6b4bf08 | 124 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 125 | } |
evanso | 80:870bc6b4bf08 | 126 | |
evanso | 85:87bc28b151d8 | 127 | // Deletes bullet and spaceship if collision detected |
evanso | 80:870bc6b4bf08 | 128 | if (spaceship.check_collision(alien_bullet_vector[i]) && |
evanso | 82:3211b31e9421 | 129 | !spaceship_destroyed_ ) { |
evanso | 80:870bc6b4bf08 | 130 | create_explosion(spaceship.get_pos()); |
evanso | 80:870bc6b4bf08 | 131 | spaceship_destroyed_ = true; |
evanso | 80:870bc6b4bf08 | 132 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 133 | } |
evanso | 80:870bc6b4bf08 | 134 | } |
evanso | 80:870bc6b4bf08 | 135 | |
evanso | 80:870bc6b4bf08 | 136 | // Spaceship bullets |
evanso | 82:3211b31e9421 | 137 | for (int i = 0; i < bullet_vector.size(); i++) { |
evanso | 80:870bc6b4bf08 | 138 | bullet_vector[i].draw_bullet(lcd); |
evanso | 80:870bc6b4bf08 | 139 | |
evanso | 82:3211b31e9421 | 140 | // Deletes bullet object after bullet has moved set distance |
evanso | 85:87bc28b151d8 | 141 | if (bullet_vector[i].get_bullet_delete_counter() >> 5) { |
evanso | 80:870bc6b4bf08 | 142 | bullet_vector.erase(bullet_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 143 | } |
evanso | 80:870bc6b4bf08 | 144 | } |
evanso | 80:870bc6b4bf08 | 145 | } |
evanso | 80:870bc6b4bf08 | 146 | |
evanso | 80:870bc6b4bf08 | 147 | |
evanso | 80:870bc6b4bf08 | 148 | |
evanso | 80:870bc6b4bf08 | 149 | |
evanso | 82:3211b31e9421 | 150 | // Alien Control --------------------------------------------------------------- |
evanso | 80:870bc6b4bf08 | 151 | |
evanso | 82:3211b31e9421 | 152 | void PlayEngine::spawn_aliens() { |
evanso | 85:87bc28b151d8 | 153 | if (alien_vector.size() <= alien_number_) { |
evanso | 80:870bc6b4bf08 | 154 | create_alien(); |
evanso | 80:870bc6b4bf08 | 155 | } |
evanso | 80:870bc6b4bf08 | 156 | |
evanso | 82:3211b31e9421 | 157 | // printf( " alien_number_counter_ = %d\n",alien_number_counter_); |
evanso | 80:870bc6b4bf08 | 158 | |
evanso | 85:87bc28b151d8 | 159 | // Slowly increasing the alien counter as game goes on to make harder |
evanso | 85:87bc28b151d8 | 160 | if (spawn_alien_counter_%(375*spawn_time_multipler_) == 0) { |
evanso | 80:870bc6b4bf08 | 161 | alien_number_++; |
evanso | 86:eecd168c3a23 | 162 | spawn_time_multipler_*= 1.2; |
evanso | 86:eecd168c3a23 | 163 | |
evanso | 86:eecd168c3a23 | 164 | // printf("alien_number_ %d\n",alien_number_); |
evanso | 80:870bc6b4bf08 | 165 | } |
evanso | 80:870bc6b4bf08 | 166 | |
evanso | 80:870bc6b4bf08 | 167 | spawn_alien_counter_++; |
evanso | 80:870bc6b4bf08 | 168 | } |
evanso | 80:870bc6b4bf08 | 169 | |
evanso | 82:3211b31e9421 | 170 | void PlayEngine::create_alien() { |
evanso | 80:870bc6b4bf08 | 171 | int position_x_start = 0; |
evanso | 80:870bc6b4bf08 | 172 | |
evanso | 80:870bc6b4bf08 | 173 | // Alien object |
evanso | 80:870bc6b4bf08 | 174 | Alien new_alien; |
evanso | 80:870bc6b4bf08 | 175 | |
evanso | 82:3211b31e9421 | 176 | // Spawns aliens between x > 84 and x <0 |
evanso | 85:87bc28b151d8 | 177 | if (rand() % 2) { |
evanso | 80:870bc6b4bf08 | 178 | position_x_start = rand() % 84 + 84; |
evanso | 80:870bc6b4bf08 | 179 | }else{ |
evanso | 80:870bc6b4bf08 | 180 | position_x_start =rand() % 83 - 84; |
evanso | 80:870bc6b4bf08 | 181 | } |
evanso | 80:870bc6b4bf08 | 182 | new_alien.init(pad, position_x_start, (rand() % 33 + 9)); |
evanso | 80:870bc6b4bf08 | 183 | |
evanso | 80:870bc6b4bf08 | 184 | // Stores alien object in vector |
evanso | 80:870bc6b4bf08 | 185 | alien_vector.push_back(new_alien); |
evanso | 80:870bc6b4bf08 | 186 | } |
evanso | 80:870bc6b4bf08 | 187 | |
evanso | 82:3211b31e9421 | 188 | void PlayEngine::check_alien_people_collision(int i) { |
evanso | 82:3211b31e9421 | 189 | // Only a collision if all three of theses conditions are met: |
evanso | 85:87bc28b151d8 | 190 | // Checks all these condition for every alien to every person |
evanso | 80:870bc6b4bf08 | 191 | // 1)person hasn't already had a collision |
evanso | 85:87bc28b151d8 | 192 | // 2)There is an actual collision of people and alien |
evanso | 82:3211b31e9421 | 193 | for (int x = 0; x < people_vector.size(); x++) { |
evanso | 80:870bc6b4bf08 | 194 | if (!people_vector[x].get_alien_collision_flag() && |
evanso | 82:3211b31e9421 | 195 | people_vector[x].check_alien_collision(alien_vector[i])) { |
evanso | 80:870bc6b4bf08 | 196 | alien_vector[i].set_collision_people_element(x); |
evanso | 80:870bc6b4bf08 | 197 | } |
evanso | 80:870bc6b4bf08 | 198 | } |
evanso | 80:870bc6b4bf08 | 199 | |
evanso | 82:3211b31e9421 | 200 | // Draws collision if detected |
evanso | 85:87bc28b151d8 | 201 | if (alien_vector[i].get_collision_people_element() > -1) { |
evanso | 80:870bc6b4bf08 | 202 | alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, |
evanso | 80:870bc6b4bf08 | 203 | map.get_length_map(), map.get_position_x_map(),true); |
evanso | 80:870bc6b4bf08 | 204 | }else{ |
evanso | 80:870bc6b4bf08 | 205 | alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, |
evanso | 80:870bc6b4bf08 | 206 | map.get_length_map(), map.get_position_x_map(),false); |
evanso | 80:870bc6b4bf08 | 207 | } |
evanso | 80:870bc6b4bf08 | 208 | } |
evanso | 80:870bc6b4bf08 | 209 | |
evanso | 82:3211b31e9421 | 210 | void PlayEngine::alliens_fire_bullets(int i) { |
evanso | 82:3211b31e9421 | 211 | if (alien_vector[i].get_alien_fire_counter()%60 == 0) { |
evanso | 80:870bc6b4bf08 | 212 | Weapons alien_bullet; |
evanso | 80:870bc6b4bf08 | 213 | |
evanso | 82:3211b31e9421 | 214 | // Fires bullet towards direction of spaceship |
evanso | 80:870bc6b4bf08 | 215 | bool alien_bullet_direction = false; |
evanso | 80:870bc6b4bf08 | 216 | Vector2D spaceship_pos = spaceship.get_pos(); |
evanso | 80:870bc6b4bf08 | 217 | Vector2D alien_pos = alien_vector[i].get_pos(); |
evanso | 85:87bc28b151d8 | 218 | if (spaceship_pos.x > alien_pos.x) { |
evanso | 80:870bc6b4bf08 | 219 | alien_bullet_direction = true; |
evanso | 80:870bc6b4bf08 | 220 | } |
evanso | 80:870bc6b4bf08 | 221 | |
evanso | 80:870bc6b4bf08 | 222 | alien_bullet.init(alien_vector[i].get_pos(), |
evanso | 80:870bc6b4bf08 | 223 | alien_bullet_direction , false); |
evanso | 80:870bc6b4bf08 | 224 | |
evanso | 80:870bc6b4bf08 | 225 | // Stores bullet object in vector |
evanso | 80:870bc6b4bf08 | 226 | alien_bullet_vector.push_back(alien_bullet); |
evanso | 80:870bc6b4bf08 | 227 | } |
evanso | 80:870bc6b4bf08 | 228 | } |
evanso | 80:870bc6b4bf08 | 229 | |
evanso | 82:3211b31e9421 | 230 | void PlayEngine::delete_aliens(int i) { |
evanso | 82:3211b31e9421 | 231 | for (int x = 0; x < bullet_vector.size(); x++) { |
evanso | 86:eecd168c3a23 | 232 | if (alien_vector[i].check_collision(bullet_vector[x])){ |
evanso | 80:870bc6b4bf08 | 233 | create_explosion(alien_vector[i].get_pos()); |
evanso | 86:eecd168c3a23 | 234 | |
evanso | 86:eecd168c3a23 | 235 | //Delete person object if was carried by destroyed alien |
evanso | 86:eecd168c3a23 | 236 | if(alien_vector[i].get_collision_people_element() > -1){ |
evanso | 86:eecd168c3a23 | 237 | people_vector.erase(people_vector.begin() + |
evanso | 86:eecd168c3a23 | 238 | alien_vector[i].get_collision_people_element()); |
evanso | 86:eecd168c3a23 | 239 | } |
evanso | 86:eecd168c3a23 | 240 | |
evanso | 80:870bc6b4bf08 | 241 | bullet_vector.erase(bullet_vector.begin()+ x); |
evanso | 80:870bc6b4bf08 | 242 | alien_vector.erase(alien_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 243 | points_ ++; |
evanso | 80:870bc6b4bf08 | 244 | } |
evanso | 80:870bc6b4bf08 | 245 | } |
evanso | 80:870bc6b4bf08 | 246 | } |
evanso | 80:870bc6b4bf08 | 247 | |
evanso | 82:3211b31e9421 | 248 | void PlayEngine::draw_aliens() { |
evanso | 85:87bc28b151d8 | 249 | // Integrates over alien vector and draws each new_alien object |
evanso | 82:3211b31e9421 | 250 | for (int i = 0; i < alien_vector.size(); i++) { |
evanso | 80:870bc6b4bf08 | 251 | |
evanso | 80:870bc6b4bf08 | 252 | check_alien_people_collision(i); |
evanso | 80:870bc6b4bf08 | 253 | |
evanso | 82:3211b31e9421 | 254 | // Deleted spaceship if alien ship touches spaceship |
evanso | 80:870bc6b4bf08 | 255 | if (spaceship.check_alien_collision(alien_vector[i]) && |
evanso | 82:3211b31e9421 | 256 | !spaceship_destroyed_) { |
evanso | 80:870bc6b4bf08 | 257 | create_explosion(spaceship.get_pos()); |
evanso | 80:870bc6b4bf08 | 258 | spaceship_destroyed_ = true; |
evanso | 80:870bc6b4bf08 | 259 | } |
evanso | 80:870bc6b4bf08 | 260 | |
evanso | 80:870bc6b4bf08 | 261 | alliens_fire_bullets(i); |
evanso | 80:870bc6b4bf08 | 262 | |
evanso | 80:870bc6b4bf08 | 263 | delete_aliens(i); |
evanso | 80:870bc6b4bf08 | 264 | } |
evanso | 80:870bc6b4bf08 | 265 | } |
evanso | 80:870bc6b4bf08 | 266 | |
evanso | 80:870bc6b4bf08 | 267 | |
evanso | 80:870bc6b4bf08 | 268 | |
evanso | 80:870bc6b4bf08 | 269 | |
evanso | 85:87bc28b151d8 | 270 | // Explosion Control ----------------------------------------------------------- |
evanso | 80:870bc6b4bf08 | 271 | |
evanso | 82:3211b31e9421 | 272 | void PlayEngine::create_explosion(Vector2D destroyed_position) { |
evanso | 82:3211b31e9421 | 273 | // Explosion object |
evanso | 80:870bc6b4bf08 | 274 | Explosion new_explosion; |
evanso | 80:870bc6b4bf08 | 275 | |
evanso | 80:870bc6b4bf08 | 276 | new_explosion.init(destroyed_position); |
evanso | 80:870bc6b4bf08 | 277 | |
evanso | 80:870bc6b4bf08 | 278 | // Stores explosion object in vector |
evanso | 80:870bc6b4bf08 | 279 | explosion_vector.push_back(new_explosion); |
evanso | 80:870bc6b4bf08 | 280 | |
evanso | 80:870bc6b4bf08 | 281 | |
evanso | 82:3211b31e9421 | 282 | // Plays explosion sound if sound fx on |
evanso | 82:3211b31e9421 | 283 | if (sound_fx_ == on) { |
evanso | 80:870bc6b4bf08 | 284 | pad.tone(40,0.1); |
evanso | 86:eecd168c3a23 | 285 | // printf("playing Explosion\n"); |
evanso | 81:78c461e6770b | 286 | } |
evanso | 80:870bc6b4bf08 | 287 | } |
evanso | 80:870bc6b4bf08 | 288 | |
evanso | 82:3211b31e9421 | 289 | void PlayEngine::draw_explosions() { |
evanso | 85:87bc28b151d8 | 290 | // Integrates over explosion vector and draws each explosion object |
evanso | 85:87bc28b151d8 | 291 | // then deleted object after set size |
evanso | 82:3211b31e9421 | 292 | for (int i = 0; i < explosion_vector.size(); i++) { |
evanso | 80:870bc6b4bf08 | 293 | explosion_vector[i].draw_explosion(lcd); |
evanso | 80:870bc6b4bf08 | 294 | |
evanso | 82:3211b31e9421 | 295 | // Delete explosion after reaches set size |
evanso | 85:87bc28b151d8 | 296 | if (explosion_vector[i].get_explosion_radius() == 8) { |
evanso | 80:870bc6b4bf08 | 297 | explosion_vector.erase(explosion_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 298 | } |
evanso | 80:870bc6b4bf08 | 299 | } |
evanso | 80:870bc6b4bf08 | 300 | } |
evanso | 80:870bc6b4bf08 | 301 | |
evanso | 80:870bc6b4bf08 | 302 | |
evanso | 80:870bc6b4bf08 | 303 | |
evanso | 80:870bc6b4bf08 | 304 | |
evanso | 82:3211b31e9421 | 305 | // People Control -------------------------------------------------------------- |
evanso | 80:870bc6b4bf08 | 306 | |
evanso | 82:3211b31e9421 | 307 | void PlayEngine::spawn_people() { |
evanso | 85:87bc28b151d8 | 308 | // Keeps number of people objects constant as people objects are erased |
evanso | 85:87bc28b151d8 | 309 | if (people_vector.size() <= 5) { |
evanso | 80:870bc6b4bf08 | 310 | create_people(); |
evanso | 80:870bc6b4bf08 | 311 | } |
evanso | 80:870bc6b4bf08 | 312 | } |
evanso | 80:870bc6b4bf08 | 313 | |
evanso | 82:3211b31e9421 | 314 | void PlayEngine::create_people() { |
evanso | 80:870bc6b4bf08 | 315 | // People object |
evanso | 80:870bc6b4bf08 | 316 | People people; |
evanso | 80:870bc6b4bf08 | 317 | |
evanso | 80:870bc6b4bf08 | 318 | people.init(pad, (rand() % 168 - 84)); |
evanso | 80:870bc6b4bf08 | 319 | |
evanso | 80:870bc6b4bf08 | 320 | // Stores alien object in vector |
evanso | 80:870bc6b4bf08 | 321 | people_vector.push_back(people); |
evanso | 80:870bc6b4bf08 | 322 | } |
evanso | 80:870bc6b4bf08 | 323 | |
evanso | 82:3211b31e9421 | 324 | void PlayEngine::draw_people() { |
evanso | 82:3211b31e9421 | 325 | for (int i = 0; i < people_vector.size(); i++) { |
evanso | 80:870bc6b4bf08 | 326 | people_vector[i].draw_people(lcd, d_, |
evanso | 80:870bc6b4bf08 | 327 | map.get_length_map(), map.get_position_x_map()); |
evanso | 80:870bc6b4bf08 | 328 | |
evanso | 85:87bc28b151d8 | 329 | // Erase person if at top of screen as captured by alien and alien is |
evanso | 82:3211b31e9421 | 330 | // set to track mode |
evanso | 80:870bc6b4bf08 | 331 | Vector2D people_pos = people_vector[i].get_pos(); |
evanso | 82:3211b31e9421 | 332 | for (int x = 0; x < alien_vector.size(); x++) { |
evanso | 85:87bc28b151d8 | 333 | Vector2D alien_pos = alien_vector[x].get_pos(); |
evanso | 85:87bc28b151d8 | 334 | if (people_pos.y < 30 && alien_pos.y < 9 && |
evanso | 82:3211b31e9421 | 335 | alien_vector[x].get_collision_people_element() == i) { |
evanso | 80:870bc6b4bf08 | 336 | |
evanso | 82:3211b31e9421 | 337 | // Set alien who abducted person to track mode |
evanso | 81:78c461e6770b | 338 | alien_vector[x].set_track_flag(true); |
evanso | 81:78c461e6770b | 339 | people_vector.erase(people_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 340 | } |
evanso | 80:870bc6b4bf08 | 341 | |
evanso | 80:870bc6b4bf08 | 342 | } |
evanso | 80:870bc6b4bf08 | 343 | } |
evanso | 80:870bc6b4bf08 | 344 | } |
evanso | 80:870bc6b4bf08 | 345 | |
evanso | 80:870bc6b4bf08 | 346 | |
evanso | 80:870bc6b4bf08 | 347 | |
evanso | 80:870bc6b4bf08 | 348 | |
evanso | 82:3211b31e9421 | 349 | // Map Control------------------------------------------------------------------ |
evanso | 80:870bc6b4bf08 | 350 | |
evanso | 82:3211b31e9421 | 351 | void PlayEngine::reset_map_timer() { |
evanso | 85:87bc28b151d8 | 352 | if (spaceship_destroyed_) { |
evanso | 81:78c461e6770b | 353 | reset_map_counter_++; |
evanso | 81:78c461e6770b | 354 | |
evanso | 82:3211b31e9421 | 355 | // Stops map movement |
evanso | 81:78c461e6770b | 356 | d_ = CENTRE; |
evanso | 81:78c461e6770b | 357 | |
evanso | 82:3211b31e9421 | 358 | // Reset map after set time |
evanso | 85:87bc28b151d8 | 359 | if (reset_map_counter_ == 50) { |
evanso | 81:78c461e6770b | 360 | reset_map(); |
evanso | 81:78c461e6770b | 361 | } |
evanso | 81:78c461e6770b | 362 | } |
evanso | 80:870bc6b4bf08 | 363 | } |
evanso | 80:870bc6b4bf08 | 364 | |
evanso | 82:3211b31e9421 | 365 | void PlayEngine::reset_map() { |
evanso | 80:870bc6b4bf08 | 366 | // Reassign values to variables |
evanso | 80:870bc6b4bf08 | 367 | spaceship.init(); |
evanso | 80:870bc6b4bf08 | 368 | spaceship_lives_--; |
evanso | 80:870bc6b4bf08 | 369 | spaceship_destroyed_ = false; |
evanso | 80:870bc6b4bf08 | 370 | reset_map_counter_ = 0; |
evanso | 80:870bc6b4bf08 | 371 | |
evanso | 82:3211b31e9421 | 372 | // Erase aliens so redrawn in random positions when respawning spaceship |
evanso | 82:3211b31e9421 | 373 | for (int i = (alien_vector.size() - 1); i >= 0 ; i--) { |
evanso | 80:870bc6b4bf08 | 374 | alien_vector.erase(alien_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 375 | } |
evanso | 80:870bc6b4bf08 | 376 | |
evanso | 82:3211b31e9421 | 377 | // Erase all people so redrawn at bottom of map |
evanso | 82:3211b31e9421 | 378 | for (int i = (people_vector.size() - 1); i >= 0 ; i--) { |
evanso | 80:870bc6b4bf08 | 379 | people_vector.erase(people_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 380 | } |
evanso | 80:870bc6b4bf08 | 381 | |
evanso | 82:3211b31e9421 | 382 | // Erase all alien bullets |
evanso | 82:3211b31e9421 | 383 | for (int i = (alien_bullet_vector.size() - 1); i >= 0 ; i--) { |
evanso | 80:870bc6b4bf08 | 384 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
evanso | 80:870bc6b4bf08 | 385 | } |
evanso | 85:87bc28b151d8 | 386 | } |