Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
GameEngine/PlayEngine.cpp@80:870bc6b4bf08, 2020-05-25 (annotated)
- Committer:
- evanso
- Date:
- Mon May 25 15:07:24 2020 +0000
- Revision:
- 80:870bc6b4bf08
- Child:
- 81:78c461e6770b
Split GameEngine class into two different classes to reduce its size The parent class PlayEngine was created to hold the function that control the interaction between classes in the playable part of the game and so they can be unit tested.
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 | 80:870bc6b4bf08 | 3 | //Spaceship control ------------------------------------------------------------ |
| evanso | 80:870bc6b4bf08 | 4 | |
| evanso | 80:870bc6b4bf08 | 5 | void PlayEngine::read_joystick_direction(){ |
| evanso | 80:870bc6b4bf08 | 6 | d_ = pad.get_direction(); |
| evanso | 80:870bc6b4bf08 | 7 | } |
| evanso | 80:870bc6b4bf08 | 8 | |
| evanso | 80:870bc6b4bf08 | 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 | 80:870bc6b4bf08 | 12 | //north |
| evanso | 80:870bc6b4bf08 | 13 | if(pitch >= -35){ |
| evanso | 80:870bc6b4bf08 | 14 | if(roll >= 10){ |
| evanso | 80:870bc6b4bf08 | 15 | d_ = NE; |
| evanso | 80:870bc6b4bf08 | 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 | 80:870bc6b4bf08 | 21 | //south |
| evanso | 80:870bc6b4bf08 | 22 | }else if(pitch <= -55){ |
| evanso | 80:870bc6b4bf08 | 23 | if (roll >= 10){ |
| evanso | 80:870bc6b4bf08 | 24 | d_ = SE; |
| evanso | 80:870bc6b4bf08 | 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 | 80:870bc6b4bf08 | 32 | if (roll >= 10){ |
| evanso | 80:870bc6b4bf08 | 33 | d_ = E; |
| evanso | 80:870bc6b4bf08 | 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 | 80:870bc6b4bf08 | 42 | void PlayEngine::spaceship_lives_leds(){ |
| evanso | 80:870bc6b4bf08 | 43 | pad.leds_off(); |
| evanso | 80:870bc6b4bf08 | 44 | |
| evanso | 80:870bc6b4bf08 | 45 | //top two red leds on if 1 lives left |
| evanso | 80:870bc6b4bf08 | 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 | 80:870bc6b4bf08 | 51 | //middle two orange leds also on if 2 lives left |
| evanso | 80:870bc6b4bf08 | 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 | 80:870bc6b4bf08 | 57 | //bottom two red leds also on if 3 lives left |
| evanso | 80:870bc6b4bf08 | 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 | 80:870bc6b4bf08 | 67 | //Weapon control --------------------------------------------------------------- |
| evanso | 80:870bc6b4bf08 | 68 | |
| evanso | 80:870bc6b4bf08 | 69 | void PlayEngine::create_weapons_bullets(){ |
| evanso | 80:870bc6b4bf08 | 70 | // controlls speed that bullet can be fired |
| evanso | 80:870bc6b4bf08 | 71 | if(bullet_timer_ <=0){ |
| evanso | 80:870bc6b4bf08 | 72 | if (pad.A_pressed()){ |
| evanso | 80:870bc6b4bf08 | 73 | // Bullet object |
| evanso | 80:870bc6b4bf08 | 74 | Weapons new_bullet; |
| evanso | 80:870bc6b4bf08 | 75 | |
| evanso | 80:870bc6b4bf08 | 76 | new_bullet.init(spaceship.get_pos(), |
| evanso | 80:870bc6b4bf08 | 77 | spaceship.get_spaceship_sprite_direction(), true); |
| evanso | 80:870bc6b4bf08 | 78 | |
| evanso | 80:870bc6b4bf08 | 79 | // Stores bullet object in vector |
| evanso | 80:870bc6b4bf08 | 80 | bullet_vector.push_back(new_bullet); |
| evanso | 80:870bc6b4bf08 | 81 | } |
| evanso | 80:870bc6b4bf08 | 82 | bullet_timer_ = 10; |
| evanso | 80:870bc6b4bf08 | 83 | } |
| evanso | 80:870bc6b4bf08 | 84 | bullet_timer_--; |
| evanso | 80:870bc6b4bf08 | 85 | } |
| evanso | 80:870bc6b4bf08 | 86 | |
| evanso | 80:870bc6b4bf08 | 87 | void PlayEngine::create_weapons_smart_bomb(){ |
| evanso | 80:870bc6b4bf08 | 88 | // timer to stop smart bomb button be accidently pressed twice |
| evanso | 80:870bc6b4bf08 | 89 | if(smart_bomb_timer_ <=0){ |
| evanso | 80:870bc6b4bf08 | 90 | if (pad.B_pressed() && smart_bomb_counter_ > 0){ |
| evanso | 80:870bc6b4bf08 | 91 | weapons.smart_bomb(lcd); |
| evanso | 80:870bc6b4bf08 | 92 | smart_bomb_counter_--; |
| evanso | 80:870bc6b4bf08 | 93 | |
| evanso | 80:870bc6b4bf08 | 94 | //Deletes alien object if on screen |
| evanso | 80:870bc6b4bf08 | 95 | for (int i = (alien_vector.size() -1) ; i >= 0 ; i--){ |
| evanso | 80:870bc6b4bf08 | 96 | Vector2D alien_pos = alien_vector[i].get_pos(); |
| evanso | 80:870bc6b4bf08 | 97 | |
| evanso | 80:870bc6b4bf08 | 98 | |
| evanso | 80:870bc6b4bf08 | 99 | // Creats explosion if alien was on the screen |
| evanso | 80:870bc6b4bf08 | 100 | if(alien_pos.x <= 84 && alien_pos.x >= -6){ |
| evanso | 80:870bc6b4bf08 | 101 | create_explosion(alien_pos); |
| evanso | 80:870bc6b4bf08 | 102 | |
| evanso | 80:870bc6b4bf08 | 103 | //delete person object if was being abducted by destroyed alien |
| evanso | 80:870bc6b4bf08 | 104 | if(alien_vector[i].get_collision_people_element() > -1){ |
| evanso | 80:870bc6b4bf08 | 105 | people_vector.erase(people_vector.begin() + |
| evanso | 80:870bc6b4bf08 | 106 | alien_vector[i].get_collision_people_element()); |
| evanso | 80:870bc6b4bf08 | 107 | } |
| evanso | 80:870bc6b4bf08 | 108 | |
| evanso | 80:870bc6b4bf08 | 109 | alien_vector.erase(alien_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 110 | points_ ++; |
| evanso | 80:870bc6b4bf08 | 111 | } |
| evanso | 80:870bc6b4bf08 | 112 | } |
| evanso | 80:870bc6b4bf08 | 113 | smart_bomb_timer_ = 30; |
| evanso | 80:870bc6b4bf08 | 114 | } |
| evanso | 80:870bc6b4bf08 | 115 | } |
| evanso | 80:870bc6b4bf08 | 116 | smart_bomb_timer_--; |
| evanso | 80:870bc6b4bf08 | 117 | } |
| evanso | 80:870bc6b4bf08 | 118 | |
| evanso | 80:870bc6b4bf08 | 119 | void PlayEngine::draw_bullets(){ |
| evanso | 80:870bc6b4bf08 | 120 | // interates over bullet vectors, moves and draws bullet objects |
| evanso | 80:870bc6b4bf08 | 121 | |
| evanso | 80:870bc6b4bf08 | 122 | // Alien bullets |
| evanso | 80:870bc6b4bf08 | 123 | for (int i = 0; i < alien_bullet_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 124 | alien_bullet_vector[i].draw_alien_bullet(lcd, d_); |
| evanso | 80:870bc6b4bf08 | 125 | |
| evanso | 80:870bc6b4bf08 | 126 | // deletes alien bullet object after bullet has moved set distance |
| evanso | 80:870bc6b4bf08 | 127 | if(alien_bullet_vector[i].get_bullet_delete_counter() >> 5){ |
| evanso | 80:870bc6b4bf08 | 128 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 129 | } |
| evanso | 80:870bc6b4bf08 | 130 | |
| evanso | 80:870bc6b4bf08 | 131 | // Deletes bullet and shpaceship if collision detected |
| evanso | 80:870bc6b4bf08 | 132 | if (spaceship.check_collision(alien_bullet_vector[i]) && |
| evanso | 80:870bc6b4bf08 | 133 | !spaceship_destroyed_ ){ |
| evanso | 80:870bc6b4bf08 | 134 | create_explosion(spaceship.get_pos()); |
| evanso | 80:870bc6b4bf08 | 135 | |
| evanso | 80:870bc6b4bf08 | 136 | spaceship_destroyed_ = true; |
| evanso | 80:870bc6b4bf08 | 137 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 138 | } |
| evanso | 80:870bc6b4bf08 | 139 | } |
| evanso | 80:870bc6b4bf08 | 140 | |
| evanso | 80:870bc6b4bf08 | 141 | // Spaceship bullets |
| evanso | 80:870bc6b4bf08 | 142 | for (int i = 0; i < bullet_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 143 | bullet_vector[i].draw_bullet(lcd); |
| evanso | 80:870bc6b4bf08 | 144 | |
| evanso | 80:870bc6b4bf08 | 145 | // deletes bullet object after bullet has moved set distance |
| evanso | 80:870bc6b4bf08 | 146 | if(bullet_vector[i].get_bullet_delete_counter() >> 5){ |
| evanso | 80:870bc6b4bf08 | 147 | bullet_vector.erase(bullet_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 148 | } |
| evanso | 80:870bc6b4bf08 | 149 | } |
| evanso | 80:870bc6b4bf08 | 150 | } |
| evanso | 80:870bc6b4bf08 | 151 | |
| evanso | 80:870bc6b4bf08 | 152 | |
| evanso | 80:870bc6b4bf08 | 153 | |
| evanso | 80:870bc6b4bf08 | 154 | |
| evanso | 80:870bc6b4bf08 | 155 | //Alien Control ---------------------------------------------------------------- |
| evanso | 80:870bc6b4bf08 | 156 | |
| evanso | 80:870bc6b4bf08 | 157 | void PlayEngine::spawn_aliens(){ |
| evanso | 80:870bc6b4bf08 | 158 | if(alien_vector.size() <= alien_number_){ |
| evanso | 80:870bc6b4bf08 | 159 | create_alien(); |
| evanso | 80:870bc6b4bf08 | 160 | } |
| evanso | 80:870bc6b4bf08 | 161 | |
| evanso | 80:870bc6b4bf08 | 162 | //printf( " alien_number_counter_ = %d\n",alien_number_counter_); |
| evanso | 80:870bc6b4bf08 | 163 | |
| evanso | 80:870bc6b4bf08 | 164 | //slowley incresing the alien counter as game goes on to make harder |
| evanso | 80:870bc6b4bf08 | 165 | if(spawn_alien_counter_%(500*spawn_time_multipler_) == 0){ |
| evanso | 80:870bc6b4bf08 | 166 | alien_number_++; |
| evanso | 80:870bc6b4bf08 | 167 | spawn_time_multipler_++; |
| evanso | 80:870bc6b4bf08 | 168 | } |
| evanso | 80:870bc6b4bf08 | 169 | |
| evanso | 80:870bc6b4bf08 | 170 | spawn_alien_counter_++; |
| evanso | 80:870bc6b4bf08 | 171 | } |
| evanso | 80:870bc6b4bf08 | 172 | |
| evanso | 80:870bc6b4bf08 | 173 | void PlayEngine::create_alien(){ |
| evanso | 80:870bc6b4bf08 | 174 | int position_x_start = 0; |
| evanso | 80:870bc6b4bf08 | 175 | |
| evanso | 80:870bc6b4bf08 | 176 | // Alien object |
| evanso | 80:870bc6b4bf08 | 177 | Alien new_alien; |
| evanso | 80:870bc6b4bf08 | 178 | |
| evanso | 80:870bc6b4bf08 | 179 | //spawns aliens between x > 84 and x <0 |
| evanso | 80:870bc6b4bf08 | 180 | if(rand() % 2){ |
| evanso | 80:870bc6b4bf08 | 181 | position_x_start = rand() % 84 + 84; |
| evanso | 80:870bc6b4bf08 | 182 | }else{ |
| evanso | 80:870bc6b4bf08 | 183 | position_x_start =rand() % 83 - 84; |
| evanso | 80:870bc6b4bf08 | 184 | } |
| evanso | 80:870bc6b4bf08 | 185 | new_alien.init(pad, position_x_start, (rand() % 33 + 9)); |
| evanso | 80:870bc6b4bf08 | 186 | |
| evanso | 80:870bc6b4bf08 | 187 | // Stores alien object in vector |
| evanso | 80:870bc6b4bf08 | 188 | alien_vector.push_back(new_alien); |
| evanso | 80:870bc6b4bf08 | 189 | } |
| evanso | 80:870bc6b4bf08 | 190 | |
| evanso | 80:870bc6b4bf08 | 191 | void PlayEngine::check_alien_people_collision(int i){ |
| evanso | 80:870bc6b4bf08 | 192 | // only a collision if all three of theses conditions are met: |
| evanso | 80:870bc6b4bf08 | 193 | // check all these codition for every alien to every person |
| evanso | 80:870bc6b4bf08 | 194 | // 1)person hasn't already had a collision |
| evanso | 80:870bc6b4bf08 | 195 | // 2)There is an acutaual collision of people and alien |
| evanso | 80:870bc6b4bf08 | 196 | for (int x = 0; x < people_vector.size(); x++){ |
| evanso | 80:870bc6b4bf08 | 197 | if (!people_vector[x].get_alien_collision_flag() && |
| evanso | 80:870bc6b4bf08 | 198 | people_vector[x].check_alien_collision(alien_vector[i])){ |
| evanso | 80:870bc6b4bf08 | 199 | alien_vector[i].set_collision_people_element(x); |
| evanso | 80:870bc6b4bf08 | 200 | } |
| evanso | 80:870bc6b4bf08 | 201 | } |
| evanso | 80:870bc6b4bf08 | 202 | |
| evanso | 80:870bc6b4bf08 | 203 | //draws collision if detected |
| evanso | 80:870bc6b4bf08 | 204 | if(alien_vector[i].get_collision_people_element() > -1){ |
| 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(),true); |
| evanso | 80:870bc6b4bf08 | 207 | }else{ |
| evanso | 80:870bc6b4bf08 | 208 | alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, |
| evanso | 80:870bc6b4bf08 | 209 | map.get_length_map(), map.get_position_x_map(),false); |
| evanso | 80:870bc6b4bf08 | 210 | } |
| evanso | 80:870bc6b4bf08 | 211 | } |
| evanso | 80:870bc6b4bf08 | 212 | |
| evanso | 80:870bc6b4bf08 | 213 | void PlayEngine::alliens_fire_bullets(int i){ |
| evanso | 80:870bc6b4bf08 | 214 | if (alien_vector[i].get_alien_fire_counter()%60 == 0){ |
| evanso | 80:870bc6b4bf08 | 215 | Weapons alien_bullet; |
| evanso | 80:870bc6b4bf08 | 216 | |
| evanso | 80:870bc6b4bf08 | 217 | // fires bullet towards direction of spaceship |
| evanso | 80:870bc6b4bf08 | 218 | bool alien_bullet_direction = false; |
| evanso | 80:870bc6b4bf08 | 219 | Vector2D spaceship_pos = spaceship.get_pos(); |
| evanso | 80:870bc6b4bf08 | 220 | Vector2D alien_pos = alien_vector[i].get_pos(); |
| evanso | 80:870bc6b4bf08 | 221 | if(spaceship_pos.x > alien_pos.x){ |
| evanso | 80:870bc6b4bf08 | 222 | alien_bullet_direction = true; |
| evanso | 80:870bc6b4bf08 | 223 | } |
| evanso | 80:870bc6b4bf08 | 224 | |
| evanso | 80:870bc6b4bf08 | 225 | alien_bullet.init(alien_vector[i].get_pos(), |
| evanso | 80:870bc6b4bf08 | 226 | alien_bullet_direction , false); |
| evanso | 80:870bc6b4bf08 | 227 | |
| evanso | 80:870bc6b4bf08 | 228 | // Stores bullet object in vector |
| evanso | 80:870bc6b4bf08 | 229 | alien_bullet_vector.push_back(alien_bullet); |
| evanso | 80:870bc6b4bf08 | 230 | } |
| evanso | 80:870bc6b4bf08 | 231 | } |
| evanso | 80:870bc6b4bf08 | 232 | |
| evanso | 80:870bc6b4bf08 | 233 | void PlayEngine::delete_aliens(int i){ |
| evanso | 80:870bc6b4bf08 | 234 | for (int x = 0; x < bullet_vector.size(); x++){ |
| evanso | 80:870bc6b4bf08 | 235 | if (alien_vector[i].check_collision(bullet_vector[x])){ |
| evanso | 80:870bc6b4bf08 | 236 | create_explosion(alien_vector[i].get_pos()); |
| evanso | 80:870bc6b4bf08 | 237 | |
| evanso | 80:870bc6b4bf08 | 238 | //delete person object if was carried by destroyed alien |
| evanso | 80:870bc6b4bf08 | 239 | if(alien_vector[i].get_collision_people_element() > -1){ |
| evanso | 80:870bc6b4bf08 | 240 | people_vector.erase(people_vector.begin() + |
| evanso | 80:870bc6b4bf08 | 241 | alien_vector[i].get_collision_people_element()); |
| evanso | 80:870bc6b4bf08 | 242 | } |
| evanso | 80:870bc6b4bf08 | 243 | |
| evanso | 80:870bc6b4bf08 | 244 | bullet_vector.erase(bullet_vector.begin()+ x); |
| evanso | 80:870bc6b4bf08 | 245 | alien_vector.erase(alien_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 246 | points_ ++; |
| evanso | 80:870bc6b4bf08 | 247 | } |
| evanso | 80:870bc6b4bf08 | 248 | } |
| evanso | 80:870bc6b4bf08 | 249 | } |
| evanso | 80:870bc6b4bf08 | 250 | |
| evanso | 80:870bc6b4bf08 | 251 | void PlayEngine::draw_aliens(){ |
| evanso | 80:870bc6b4bf08 | 252 | // interates over alien vector and draws each new_alien object |
| evanso | 80:870bc6b4bf08 | 253 | for (int i = 0; i < alien_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 254 | |
| evanso | 80:870bc6b4bf08 | 255 | check_alien_people_collision(i); |
| evanso | 80:870bc6b4bf08 | 256 | |
| evanso | 80:870bc6b4bf08 | 257 | // deleted spaceship if alien ship touches spaceship |
| evanso | 80:870bc6b4bf08 | 258 | if (spaceship.check_alien_collision(alien_vector[i]) && |
| evanso | 80:870bc6b4bf08 | 259 | !spaceship_destroyed_){ |
| evanso | 80:870bc6b4bf08 | 260 | create_explosion(spaceship.get_pos()); |
| evanso | 80:870bc6b4bf08 | 261 | spaceship_destroyed_ = true; |
| evanso | 80:870bc6b4bf08 | 262 | } |
| evanso | 80:870bc6b4bf08 | 263 | |
| evanso | 80:870bc6b4bf08 | 264 | alliens_fire_bullets(i); |
| evanso | 80:870bc6b4bf08 | 265 | |
| evanso | 80:870bc6b4bf08 | 266 | delete_aliens(i); |
| evanso | 80:870bc6b4bf08 | 267 | } |
| evanso | 80:870bc6b4bf08 | 268 | } |
| evanso | 80:870bc6b4bf08 | 269 | |
| evanso | 80:870bc6b4bf08 | 270 | |
| evanso | 80:870bc6b4bf08 | 271 | |
| evanso | 80:870bc6b4bf08 | 272 | |
| evanso | 80:870bc6b4bf08 | 273 | //Explotion Control ------------------------------------------------------------ |
| evanso | 80:870bc6b4bf08 | 274 | |
| evanso | 80:870bc6b4bf08 | 275 | void PlayEngine::create_explosion(Vector2D destroyed_position){ |
| evanso | 80:870bc6b4bf08 | 276 | // explosion object |
| evanso | 80:870bc6b4bf08 | 277 | Explosion new_explosion; |
| evanso | 80:870bc6b4bf08 | 278 | |
| evanso | 80:870bc6b4bf08 | 279 | new_explosion.init(destroyed_position); |
| evanso | 80:870bc6b4bf08 | 280 | |
| evanso | 80:870bc6b4bf08 | 281 | // Stores explosion object in vector |
| evanso | 80:870bc6b4bf08 | 282 | explosion_vector.push_back(new_explosion); |
| evanso | 80:870bc6b4bf08 | 283 | |
| evanso | 80:870bc6b4bf08 | 284 | |
| evanso | 80:870bc6b4bf08 | 285 | //plays explosion sound if sound fx on |
| evanso | 80:870bc6b4bf08 | 286 | if (sound_fx_ == on){ |
| evanso | 80:870bc6b4bf08 | 287 | pad.tone(40,0.1); |
| evanso | 80:870bc6b4bf08 | 288 | } |
| evanso | 80:870bc6b4bf08 | 289 | } |
| evanso | 80:870bc6b4bf08 | 290 | |
| evanso | 80:870bc6b4bf08 | 291 | void PlayEngine::draw_explosions(){ |
| evanso | 80:870bc6b4bf08 | 292 | // interates over expoltion vector and draws each explosion object then |
| evanso | 80:870bc6b4bf08 | 293 | // deleted object after set size |
| evanso | 80:870bc6b4bf08 | 294 | for (int i = 0; i < explosion_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 295 | explosion_vector[i].draw_explosion(lcd); |
| evanso | 80:870bc6b4bf08 | 296 | |
| evanso | 80:870bc6b4bf08 | 297 | // delete explosion after reaches set size |
| evanso | 80:870bc6b4bf08 | 298 | if(explosion_vector[i].get_explosion_radius() == 8){ |
| evanso | 80:870bc6b4bf08 | 299 | explosion_vector.erase(explosion_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 300 | } |
| evanso | 80:870bc6b4bf08 | 301 | } |
| evanso | 80:870bc6b4bf08 | 302 | } |
| evanso | 80:870bc6b4bf08 | 303 | |
| evanso | 80:870bc6b4bf08 | 304 | |
| evanso | 80:870bc6b4bf08 | 305 | |
| evanso | 80:870bc6b4bf08 | 306 | |
| evanso | 80:870bc6b4bf08 | 307 | //People Control --------------------------------------------------------------- |
| evanso | 80:870bc6b4bf08 | 308 | |
| evanso | 80:870bc6b4bf08 | 309 | void PlayEngine::spawn_people(){ |
| evanso | 80:870bc6b4bf08 | 310 | // Keeps number of people objects constant as people objecs are erased |
| evanso | 80:870bc6b4bf08 | 311 | if(people_vector.size() <= 5){ |
| evanso | 80:870bc6b4bf08 | 312 | create_people(); |
| evanso | 80:870bc6b4bf08 | 313 | } |
| evanso | 80:870bc6b4bf08 | 314 | } |
| evanso | 80:870bc6b4bf08 | 315 | |
| evanso | 80:870bc6b4bf08 | 316 | void PlayEngine::create_people(){ |
| evanso | 80:870bc6b4bf08 | 317 | // People object |
| evanso | 80:870bc6b4bf08 | 318 | People people; |
| evanso | 80:870bc6b4bf08 | 319 | |
| evanso | 80:870bc6b4bf08 | 320 | people.init(pad, (rand() % 168 - 84)); |
| evanso | 80:870bc6b4bf08 | 321 | |
| evanso | 80:870bc6b4bf08 | 322 | // Stores alien object in vector |
| evanso | 80:870bc6b4bf08 | 323 | people_vector.push_back(people); |
| evanso | 80:870bc6b4bf08 | 324 | } |
| evanso | 80:870bc6b4bf08 | 325 | |
| evanso | 80:870bc6b4bf08 | 326 | void PlayEngine::draw_people(){ |
| evanso | 80:870bc6b4bf08 | 327 | for (int i = 0; i < people_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 328 | people_vector[i].draw_people(lcd, d_, |
| evanso | 80:870bc6b4bf08 | 329 | map.get_length_map(), map.get_position_x_map()); |
| evanso | 80:870bc6b4bf08 | 330 | |
| evanso | 80:870bc6b4bf08 | 331 | //errase person if at top of screen as captured by alien and alien is |
| evanso | 80:870bc6b4bf08 | 332 | //set to track mode |
| evanso | 80:870bc6b4bf08 | 333 | Vector2D people_pos = people_vector[i].get_pos(); |
| evanso | 80:870bc6b4bf08 | 334 | |
| evanso | 80:870bc6b4bf08 | 335 | for (int x = 0; x < alien_vector.size(); x++){ |
| evanso | 80:870bc6b4bf08 | 336 | Vector2D alien_pos = alien_vector[x].get_pos(); |
| evanso | 80:870bc6b4bf08 | 337 | if(people_pos.y < 30 && alien_pos.y < 9){ |
| evanso | 80:870bc6b4bf08 | 338 | |
| evanso | 80:870bc6b4bf08 | 339 | //set alien who abducted person to track mode |
| evanso | 80:870bc6b4bf08 | 340 | if(alien_vector[x].get_collision_people_element() == i){ |
| evanso | 80:870bc6b4bf08 | 341 | alien_vector[x].set_track_flag(true); |
| evanso | 80:870bc6b4bf08 | 342 | people_vector.erase(people_vector.begin()+ i); |
| 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 | 80:870bc6b4bf08 | 349 | } |
| evanso | 80:870bc6b4bf08 | 350 | |
| evanso | 80:870bc6b4bf08 | 351 | |
| evanso | 80:870bc6b4bf08 | 352 | |
| evanso | 80:870bc6b4bf08 | 353 | |
| evanso | 80:870bc6b4bf08 | 354 | //Map Control------------------------------------------------------------------- |
| evanso | 80:870bc6b4bf08 | 355 | |
| evanso | 80:870bc6b4bf08 | 356 | void PlayEngine::reset_map_timer(){ |
| evanso | 80:870bc6b4bf08 | 357 | if(spaceship_destroyed_){ |
| evanso | 80:870bc6b4bf08 | 358 | reset_map_counter_++; |
| evanso | 80:870bc6b4bf08 | 359 | |
| evanso | 80:870bc6b4bf08 | 360 | // stops map movement |
| evanso | 80:870bc6b4bf08 | 361 | d_ = CENTRE; |
| evanso | 80:870bc6b4bf08 | 362 | |
| evanso | 80:870bc6b4bf08 | 363 | // reset map after set time |
| evanso | 80:870bc6b4bf08 | 364 | if(reset_map_counter_ == 50){ |
| evanso | 80:870bc6b4bf08 | 365 | reset_map(); |
| evanso | 80:870bc6b4bf08 | 366 | } |
| evanso | 80:870bc6b4bf08 | 367 | } |
| evanso | 80:870bc6b4bf08 | 368 | } |
| evanso | 80:870bc6b4bf08 | 369 | |
| evanso | 80:870bc6b4bf08 | 370 | void PlayEngine::reset_map(){ |
| evanso | 80:870bc6b4bf08 | 371 | // Reassign values to variables |
| evanso | 80:870bc6b4bf08 | 372 | spaceship.init(); |
| evanso | 80:870bc6b4bf08 | 373 | spaceship_lives_--; |
| evanso | 80:870bc6b4bf08 | 374 | spaceship_destroyed_ = false; |
| evanso | 80:870bc6b4bf08 | 375 | reset_map_counter_ = 0; |
| evanso | 80:870bc6b4bf08 | 376 | |
| evanso | 80:870bc6b4bf08 | 377 | // erase aliens so redrawn in random positions when respawning spaceship |
| evanso | 80:870bc6b4bf08 | 378 | for (int i = (alien_vector.size() - 1); i >= 0 ; i--){ |
| evanso | 80:870bc6b4bf08 | 379 | alien_vector.erase(alien_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 380 | } |
| evanso | 80:870bc6b4bf08 | 381 | |
| evanso | 80:870bc6b4bf08 | 382 | // erase all people so redrawn at bottom of map |
| evanso | 80:870bc6b4bf08 | 383 | for (int i = (people_vector.size() - 1); i >= 0 ; i--){ |
| evanso | 80:870bc6b4bf08 | 384 | people_vector.erase(people_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 385 | } |
| evanso | 80:870bc6b4bf08 | 386 | |
| evanso | 80:870bc6b4bf08 | 387 | // erase all alien bullets |
| evanso | 80:870bc6b4bf08 | 388 | for (int i = (alien_bullet_vector.size() - 1); i >= 0 ; i--){ |
| evanso | 80:870bc6b4bf08 | 389 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 390 | } |
| evanso | 80:870bc6b4bf08 | 391 | } |