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@81:78c461e6770b, 2020-05-25 (annotated)
- Committer:
- evanso
- Date:
- Mon May 25 15:59:14 2020 +0000
- Revision:
- 81:78c461e6770b
- Parent:
- 80:870bc6b4bf08
- Child:
- 82:3211b31e9421
Removed arrowing(deeply nested if statements) and added game lunch screen.
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 | 81:78c461e6770b | 70 | // controlls speed that bullet can be fired |
| evanso | 81:78c461e6770b | 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 | 81:78c461e6770b | 85 | void PlayEngine::create_weapons_smart_bomb(){ |
| evanso | 81:78c461e6770b | 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 | 81:78c461e6770b | 90 | //Deletes alien object if on screen and person object if abducted |
| evanso | 80:870bc6b4bf08 | 91 | for (int i = (alien_vector.size() -1) ; i >= 0 ; i--){ |
| evanso | 80:870bc6b4bf08 | 92 | Vector2D alien_pos = alien_vector[i].get_pos(); |
| evanso | 81:78c461e6770b | 93 | |
| evanso | 80:870bc6b4bf08 | 94 | // Creats explosion if alien was on the screen |
| evanso | 80:870bc6b4bf08 | 95 | if(alien_pos.x <= 84 && alien_pos.x >= -6){ |
| evanso | 81:78c461e6770b | 96 | create_explosion(alien_pos); |
| evanso | 80:870bc6b4bf08 | 97 | alien_vector.erase(alien_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 98 | points_ ++; |
| evanso | 80:870bc6b4bf08 | 99 | } |
| evanso | 81:78c461e6770b | 100 | |
| evanso | 81:78c461e6770b | 101 | //delete person object if was being abducted by destroyed alien |
| evanso | 81:78c461e6770b | 102 | if(alien_vector[i].get_collision_people_element() > -1){ |
| evanso | 81:78c461e6770b | 103 | people_vector.erase(people_vector.begin() + |
| evanso | 81:78c461e6770b | 104 | alien_vector[i].get_collision_people_element()); |
| evanso | 81:78c461e6770b | 105 | } |
| evanso | 80:870bc6b4bf08 | 106 | } |
| evanso | 81:78c461e6770b | 107 | |
| evanso | 81:78c461e6770b | 108 | // timer to stop smart bomb button be accidently pressed twice or spammed |
| evanso | 80:870bc6b4bf08 | 109 | smart_bomb_timer_ = 30; |
| evanso | 80:870bc6b4bf08 | 110 | } |
| evanso | 81:78c461e6770b | 111 | smart_bomb_timer_--; |
| evanso | 80:870bc6b4bf08 | 112 | } |
| evanso | 80:870bc6b4bf08 | 113 | |
| evanso | 80:870bc6b4bf08 | 114 | void PlayEngine::draw_bullets(){ |
| evanso | 80:870bc6b4bf08 | 115 | // interates over bullet vectors, moves and draws bullet objects |
| evanso | 80:870bc6b4bf08 | 116 | |
| evanso | 80:870bc6b4bf08 | 117 | // Alien bullets |
| evanso | 80:870bc6b4bf08 | 118 | for (int i = 0; i < alien_bullet_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 119 | alien_bullet_vector[i].draw_alien_bullet(lcd, d_); |
| evanso | 80:870bc6b4bf08 | 120 | |
| evanso | 80:870bc6b4bf08 | 121 | // deletes alien bullet object after bullet has moved set distance |
| evanso | 80:870bc6b4bf08 | 122 | if(alien_bullet_vector[i].get_bullet_delete_counter() >> 5){ |
| evanso | 80:870bc6b4bf08 | 123 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 124 | } |
| evanso | 80:870bc6b4bf08 | 125 | |
| evanso | 80:870bc6b4bf08 | 126 | // Deletes bullet and shpaceship if collision detected |
| evanso | 80:870bc6b4bf08 | 127 | if (spaceship.check_collision(alien_bullet_vector[i]) && |
| evanso | 80:870bc6b4bf08 | 128 | !spaceship_destroyed_ ){ |
| evanso | 80:870bc6b4bf08 | 129 | create_explosion(spaceship.get_pos()); |
| evanso | 80:870bc6b4bf08 | 130 | spaceship_destroyed_ = true; |
| evanso | 80:870bc6b4bf08 | 131 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 132 | } |
| evanso | 80:870bc6b4bf08 | 133 | } |
| evanso | 80:870bc6b4bf08 | 134 | |
| evanso | 80:870bc6b4bf08 | 135 | // Spaceship bullets |
| evanso | 80:870bc6b4bf08 | 136 | for (int i = 0; i < bullet_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 137 | bullet_vector[i].draw_bullet(lcd); |
| evanso | 80:870bc6b4bf08 | 138 | |
| evanso | 80:870bc6b4bf08 | 139 | // deletes bullet object after bullet has moved set distance |
| evanso | 80:870bc6b4bf08 | 140 | if(bullet_vector[i].get_bullet_delete_counter() >> 5){ |
| evanso | 80:870bc6b4bf08 | 141 | bullet_vector.erase(bullet_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 142 | } |
| 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 | //Alien Control ---------------------------------------------------------------- |
| evanso | 80:870bc6b4bf08 | 150 | |
| evanso | 80:870bc6b4bf08 | 151 | void PlayEngine::spawn_aliens(){ |
| evanso | 80:870bc6b4bf08 | 152 | if(alien_vector.size() <= alien_number_){ |
| evanso | 80:870bc6b4bf08 | 153 | create_alien(); |
| evanso | 80:870bc6b4bf08 | 154 | } |
| evanso | 80:870bc6b4bf08 | 155 | |
| evanso | 80:870bc6b4bf08 | 156 | //printf( " alien_number_counter_ = %d\n",alien_number_counter_); |
| evanso | 80:870bc6b4bf08 | 157 | |
| evanso | 80:870bc6b4bf08 | 158 | //slowley incresing the alien counter as game goes on to make harder |
| evanso | 80:870bc6b4bf08 | 159 | if(spawn_alien_counter_%(500*spawn_time_multipler_) == 0){ |
| evanso | 80:870bc6b4bf08 | 160 | alien_number_++; |
| evanso | 80:870bc6b4bf08 | 161 | spawn_time_multipler_++; |
| evanso | 80:870bc6b4bf08 | 162 | } |
| evanso | 80:870bc6b4bf08 | 163 | |
| evanso | 80:870bc6b4bf08 | 164 | spawn_alien_counter_++; |
| evanso | 80:870bc6b4bf08 | 165 | } |
| evanso | 80:870bc6b4bf08 | 166 | |
| evanso | 80:870bc6b4bf08 | 167 | void PlayEngine::create_alien(){ |
| evanso | 80:870bc6b4bf08 | 168 | int position_x_start = 0; |
| evanso | 80:870bc6b4bf08 | 169 | |
| evanso | 80:870bc6b4bf08 | 170 | // Alien object |
| evanso | 80:870bc6b4bf08 | 171 | Alien new_alien; |
| evanso | 80:870bc6b4bf08 | 172 | |
| evanso | 80:870bc6b4bf08 | 173 | //spawns aliens between x > 84 and x <0 |
| evanso | 80:870bc6b4bf08 | 174 | if(rand() % 2){ |
| evanso | 80:870bc6b4bf08 | 175 | position_x_start = rand() % 84 + 84; |
| evanso | 80:870bc6b4bf08 | 176 | }else{ |
| evanso | 80:870bc6b4bf08 | 177 | position_x_start =rand() % 83 - 84; |
| evanso | 80:870bc6b4bf08 | 178 | } |
| evanso | 80:870bc6b4bf08 | 179 | new_alien.init(pad, position_x_start, (rand() % 33 + 9)); |
| evanso | 80:870bc6b4bf08 | 180 | |
| evanso | 80:870bc6b4bf08 | 181 | // Stores alien object in vector |
| evanso | 80:870bc6b4bf08 | 182 | alien_vector.push_back(new_alien); |
| evanso | 80:870bc6b4bf08 | 183 | } |
| evanso | 80:870bc6b4bf08 | 184 | |
| evanso | 80:870bc6b4bf08 | 185 | void PlayEngine::check_alien_people_collision(int i){ |
| evanso | 80:870bc6b4bf08 | 186 | // only a collision if all three of theses conditions are met: |
| evanso | 80:870bc6b4bf08 | 187 | // check all these codition for every alien to every person |
| evanso | 80:870bc6b4bf08 | 188 | // 1)person hasn't already had a collision |
| evanso | 80:870bc6b4bf08 | 189 | // 2)There is an acutaual collision of people and alien |
| evanso | 80:870bc6b4bf08 | 190 | for (int x = 0; x < people_vector.size(); x++){ |
| evanso | 80:870bc6b4bf08 | 191 | if (!people_vector[x].get_alien_collision_flag() && |
| evanso | 80:870bc6b4bf08 | 192 | people_vector[x].check_alien_collision(alien_vector[i])){ |
| evanso | 80:870bc6b4bf08 | 193 | alien_vector[i].set_collision_people_element(x); |
| evanso | 80:870bc6b4bf08 | 194 | } |
| evanso | 80:870bc6b4bf08 | 195 | } |
| evanso | 80:870bc6b4bf08 | 196 | |
| evanso | 80:870bc6b4bf08 | 197 | //draws collision if detected |
| evanso | 80:870bc6b4bf08 | 198 | if(alien_vector[i].get_collision_people_element() > -1){ |
| evanso | 80:870bc6b4bf08 | 199 | alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, |
| evanso | 80:870bc6b4bf08 | 200 | map.get_length_map(), map.get_position_x_map(),true); |
| evanso | 80:870bc6b4bf08 | 201 | }else{ |
| 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(),false); |
| evanso | 80:870bc6b4bf08 | 204 | } |
| evanso | 80:870bc6b4bf08 | 205 | } |
| evanso | 80:870bc6b4bf08 | 206 | |
| evanso | 80:870bc6b4bf08 | 207 | void PlayEngine::alliens_fire_bullets(int i){ |
| evanso | 80:870bc6b4bf08 | 208 | if (alien_vector[i].get_alien_fire_counter()%60 == 0){ |
| evanso | 80:870bc6b4bf08 | 209 | Weapons alien_bullet; |
| evanso | 80:870bc6b4bf08 | 210 | |
| evanso | 80:870bc6b4bf08 | 211 | // fires bullet towards direction of spaceship |
| evanso | 80:870bc6b4bf08 | 212 | bool alien_bullet_direction = false; |
| evanso | 80:870bc6b4bf08 | 213 | Vector2D spaceship_pos = spaceship.get_pos(); |
| evanso | 80:870bc6b4bf08 | 214 | Vector2D alien_pos = alien_vector[i].get_pos(); |
| evanso | 80:870bc6b4bf08 | 215 | if(spaceship_pos.x > alien_pos.x){ |
| evanso | 80:870bc6b4bf08 | 216 | alien_bullet_direction = true; |
| evanso | 80:870bc6b4bf08 | 217 | } |
| evanso | 80:870bc6b4bf08 | 218 | |
| evanso | 80:870bc6b4bf08 | 219 | alien_bullet.init(alien_vector[i].get_pos(), |
| evanso | 80:870bc6b4bf08 | 220 | alien_bullet_direction , false); |
| evanso | 80:870bc6b4bf08 | 221 | |
| evanso | 80:870bc6b4bf08 | 222 | // Stores bullet object in vector |
| evanso | 80:870bc6b4bf08 | 223 | alien_bullet_vector.push_back(alien_bullet); |
| evanso | 80:870bc6b4bf08 | 224 | } |
| evanso | 80:870bc6b4bf08 | 225 | } |
| evanso | 80:870bc6b4bf08 | 226 | |
| evanso | 80:870bc6b4bf08 | 227 | void PlayEngine::delete_aliens(int i){ |
| evanso | 80:870bc6b4bf08 | 228 | for (int x = 0; x < bullet_vector.size(); x++){ |
| evanso | 80:870bc6b4bf08 | 229 | if (alien_vector[i].check_collision(bullet_vector[x])){ |
| evanso | 80:870bc6b4bf08 | 230 | create_explosion(alien_vector[i].get_pos()); |
| evanso | 80:870bc6b4bf08 | 231 | bullet_vector.erase(bullet_vector.begin()+ x); |
| evanso | 80:870bc6b4bf08 | 232 | alien_vector.erase(alien_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 233 | points_ ++; |
| evanso | 80:870bc6b4bf08 | 234 | } |
| evanso | 81:78c461e6770b | 235 | |
| evanso | 81:78c461e6770b | 236 | //delete person object if was carried by destroyed alien |
| evanso | 81:78c461e6770b | 237 | if(alien_vector[i].get_collision_people_element() > -1){ |
| evanso | 81:78c461e6770b | 238 | people_vector.erase(people_vector.begin() + |
| evanso | 81:78c461e6770b | 239 | alien_vector[i].get_collision_people_element()); |
| evanso | 81:78c461e6770b | 240 | } |
| evanso | 80:870bc6b4bf08 | 241 | } |
| evanso | 80:870bc6b4bf08 | 242 | } |
| evanso | 80:870bc6b4bf08 | 243 | |
| evanso | 80:870bc6b4bf08 | 244 | void PlayEngine::draw_aliens(){ |
| evanso | 80:870bc6b4bf08 | 245 | // interates over alien vector and draws each new_alien object |
| evanso | 80:870bc6b4bf08 | 246 | for (int i = 0; i < alien_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 247 | |
| evanso | 80:870bc6b4bf08 | 248 | check_alien_people_collision(i); |
| evanso | 80:870bc6b4bf08 | 249 | |
| evanso | 80:870bc6b4bf08 | 250 | // deleted spaceship if alien ship touches spaceship |
| evanso | 80:870bc6b4bf08 | 251 | if (spaceship.check_alien_collision(alien_vector[i]) && |
| evanso | 80:870bc6b4bf08 | 252 | !spaceship_destroyed_){ |
| evanso | 80:870bc6b4bf08 | 253 | create_explosion(spaceship.get_pos()); |
| evanso | 80:870bc6b4bf08 | 254 | spaceship_destroyed_ = true; |
| evanso | 80:870bc6b4bf08 | 255 | } |
| evanso | 80:870bc6b4bf08 | 256 | |
| evanso | 80:870bc6b4bf08 | 257 | alliens_fire_bullets(i); |
| evanso | 80:870bc6b4bf08 | 258 | |
| evanso | 80:870bc6b4bf08 | 259 | delete_aliens(i); |
| evanso | 80:870bc6b4bf08 | 260 | } |
| evanso | 80:870bc6b4bf08 | 261 | } |
| evanso | 80:870bc6b4bf08 | 262 | |
| evanso | 80:870bc6b4bf08 | 263 | |
| evanso | 80:870bc6b4bf08 | 264 | |
| evanso | 80:870bc6b4bf08 | 265 | |
| evanso | 80:870bc6b4bf08 | 266 | //Explotion Control ------------------------------------------------------------ |
| evanso | 80:870bc6b4bf08 | 267 | |
| evanso | 80:870bc6b4bf08 | 268 | void PlayEngine::create_explosion(Vector2D destroyed_position){ |
| evanso | 80:870bc6b4bf08 | 269 | // explosion object |
| evanso | 80:870bc6b4bf08 | 270 | Explosion new_explosion; |
| evanso | 80:870bc6b4bf08 | 271 | |
| evanso | 80:870bc6b4bf08 | 272 | new_explosion.init(destroyed_position); |
| evanso | 80:870bc6b4bf08 | 273 | |
| evanso | 80:870bc6b4bf08 | 274 | // Stores explosion object in vector |
| evanso | 80:870bc6b4bf08 | 275 | explosion_vector.push_back(new_explosion); |
| evanso | 80:870bc6b4bf08 | 276 | |
| evanso | 80:870bc6b4bf08 | 277 | |
| evanso | 81:78c461e6770b | 278 | //plays explosion sound if sound fx on |
| evanso | 81:78c461e6770b | 279 | if (sound_fx_ == on){ |
| evanso | 80:870bc6b4bf08 | 280 | pad.tone(40,0.1); |
| evanso | 81:78c461e6770b | 281 | } |
| evanso | 80:870bc6b4bf08 | 282 | } |
| evanso | 80:870bc6b4bf08 | 283 | |
| evanso | 80:870bc6b4bf08 | 284 | void PlayEngine::draw_explosions(){ |
| evanso | 80:870bc6b4bf08 | 285 | // interates over expoltion vector and draws each explosion object then |
| evanso | 80:870bc6b4bf08 | 286 | // deleted object after set size |
| evanso | 80:870bc6b4bf08 | 287 | for (int i = 0; i < explosion_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 288 | explosion_vector[i].draw_explosion(lcd); |
| evanso | 80:870bc6b4bf08 | 289 | |
| evanso | 80:870bc6b4bf08 | 290 | // delete explosion after reaches set size |
| evanso | 80:870bc6b4bf08 | 291 | if(explosion_vector[i].get_explosion_radius() == 8){ |
| evanso | 80:870bc6b4bf08 | 292 | explosion_vector.erase(explosion_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 293 | } |
| evanso | 80:870bc6b4bf08 | 294 | } |
| evanso | 80:870bc6b4bf08 | 295 | } |
| evanso | 80:870bc6b4bf08 | 296 | |
| evanso | 80:870bc6b4bf08 | 297 | |
| evanso | 80:870bc6b4bf08 | 298 | |
| evanso | 80:870bc6b4bf08 | 299 | |
| evanso | 80:870bc6b4bf08 | 300 | //People Control --------------------------------------------------------------- |
| evanso | 80:870bc6b4bf08 | 301 | |
| evanso | 80:870bc6b4bf08 | 302 | void PlayEngine::spawn_people(){ |
| evanso | 80:870bc6b4bf08 | 303 | // Keeps number of people objects constant as people objecs are erased |
| evanso | 80:870bc6b4bf08 | 304 | if(people_vector.size() <= 5){ |
| evanso | 80:870bc6b4bf08 | 305 | create_people(); |
| evanso | 80:870bc6b4bf08 | 306 | } |
| evanso | 80:870bc6b4bf08 | 307 | } |
| evanso | 80:870bc6b4bf08 | 308 | |
| evanso | 80:870bc6b4bf08 | 309 | void PlayEngine::create_people(){ |
| evanso | 80:870bc6b4bf08 | 310 | // People object |
| evanso | 80:870bc6b4bf08 | 311 | People people; |
| evanso | 80:870bc6b4bf08 | 312 | |
| evanso | 80:870bc6b4bf08 | 313 | people.init(pad, (rand() % 168 - 84)); |
| evanso | 80:870bc6b4bf08 | 314 | |
| evanso | 80:870bc6b4bf08 | 315 | // Stores alien object in vector |
| evanso | 80:870bc6b4bf08 | 316 | people_vector.push_back(people); |
| evanso | 80:870bc6b4bf08 | 317 | } |
| evanso | 80:870bc6b4bf08 | 318 | |
| evanso | 80:870bc6b4bf08 | 319 | void PlayEngine::draw_people(){ |
| evanso | 80:870bc6b4bf08 | 320 | for (int i = 0; i < people_vector.size(); i++){ |
| evanso | 80:870bc6b4bf08 | 321 | people_vector[i].draw_people(lcd, d_, |
| evanso | 80:870bc6b4bf08 | 322 | map.get_length_map(), map.get_position_x_map()); |
| evanso | 80:870bc6b4bf08 | 323 | |
| evanso | 80:870bc6b4bf08 | 324 | //errase person if at top of screen as captured by alien and alien is |
| evanso | 80:870bc6b4bf08 | 325 | //set to track mode |
| evanso | 80:870bc6b4bf08 | 326 | Vector2D people_pos = people_vector[i].get_pos(); |
| evanso | 80:870bc6b4bf08 | 327 | |
| evanso | 80:870bc6b4bf08 | 328 | for (int x = 0; x < alien_vector.size(); x++){ |
| evanso | 80:870bc6b4bf08 | 329 | Vector2D alien_pos = alien_vector[x].get_pos(); |
| evanso | 81:78c461e6770b | 330 | if(people_pos.y < 30 && alien_pos.y < 9 && |
| evanso | 81:78c461e6770b | 331 | alien_vector[x].get_collision_people_element() == i){ |
| evanso | 80:870bc6b4bf08 | 332 | |
| evanso | 80:870bc6b4bf08 | 333 | //set alien who abducted person to track mode |
| evanso | 81:78c461e6770b | 334 | alien_vector[x].set_track_flag(true); |
| evanso | 81:78c461e6770b | 335 | people_vector.erase(people_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 336 | } |
| evanso | 80:870bc6b4bf08 | 337 | |
| evanso | 80:870bc6b4bf08 | 338 | } |
| evanso | 80:870bc6b4bf08 | 339 | } |
| evanso | 80:870bc6b4bf08 | 340 | } |
| evanso | 80:870bc6b4bf08 | 341 | |
| evanso | 80:870bc6b4bf08 | 342 | |
| evanso | 80:870bc6b4bf08 | 343 | |
| evanso | 80:870bc6b4bf08 | 344 | |
| evanso | 80:870bc6b4bf08 | 345 | //Map Control------------------------------------------------------------------- |
| evanso | 80:870bc6b4bf08 | 346 | |
| evanso | 80:870bc6b4bf08 | 347 | void PlayEngine::reset_map_timer(){ |
| evanso | 81:78c461e6770b | 348 | if(spaceship_destroyed_){ |
| evanso | 81:78c461e6770b | 349 | reset_map_counter_++; |
| evanso | 81:78c461e6770b | 350 | |
| evanso | 81:78c461e6770b | 351 | // stops map movement |
| evanso | 81:78c461e6770b | 352 | d_ = CENTRE; |
| evanso | 81:78c461e6770b | 353 | |
| evanso | 81:78c461e6770b | 354 | // reset map after set time |
| evanso | 81:78c461e6770b | 355 | if(reset_map_counter_ == 50){ |
| evanso | 81:78c461e6770b | 356 | reset_map(); |
| evanso | 81:78c461e6770b | 357 | } |
| evanso | 81:78c461e6770b | 358 | } |
| evanso | 80:870bc6b4bf08 | 359 | } |
| evanso | 80:870bc6b4bf08 | 360 | |
| evanso | 80:870bc6b4bf08 | 361 | void PlayEngine::reset_map(){ |
| evanso | 80:870bc6b4bf08 | 362 | // Reassign values to variables |
| evanso | 80:870bc6b4bf08 | 363 | spaceship.init(); |
| evanso | 80:870bc6b4bf08 | 364 | spaceship_lives_--; |
| evanso | 80:870bc6b4bf08 | 365 | spaceship_destroyed_ = false; |
| evanso | 80:870bc6b4bf08 | 366 | reset_map_counter_ = 0; |
| evanso | 80:870bc6b4bf08 | 367 | |
| evanso | 80:870bc6b4bf08 | 368 | // erase aliens so redrawn in random positions when respawning spaceship |
| evanso | 80:870bc6b4bf08 | 369 | for (int i = (alien_vector.size() - 1); i >= 0 ; i--){ |
| evanso | 80:870bc6b4bf08 | 370 | alien_vector.erase(alien_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 371 | } |
| evanso | 80:870bc6b4bf08 | 372 | |
| evanso | 80:870bc6b4bf08 | 373 | // erase all people so redrawn at bottom of map |
| evanso | 80:870bc6b4bf08 | 374 | for (int i = (people_vector.size() - 1); i >= 0 ; i--){ |
| evanso | 80:870bc6b4bf08 | 375 | people_vector.erase(people_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 376 | } |
| evanso | 80:870bc6b4bf08 | 377 | |
| evanso | 80:870bc6b4bf08 | 378 | // erase all alien bullets |
| evanso | 80:870bc6b4bf08 | 379 | for (int i = (alien_bullet_vector.size() - 1); i >= 0 ; i--){ |
| evanso | 80:870bc6b4bf08 | 380 | alien_bullet_vector.erase(alien_bullet_vector.begin()+ i); |
| evanso | 80:870bc6b4bf08 | 381 | } |
| evanso | 80:870bc6b4bf08 | 382 | } |