Ben Evans University Second Year Project. Game Called Defender.

Dependencies:   mbed

https://os.mbed.com/media/uploads/evanso/84bc1a30759fd6a1e3f1fd1fae3e97c2.png

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.

Committer:
evanso
Date:
Tue May 19 16:46:40 2020 +0000
Revision:
51:35cb8e604b72
Parent:
49:ed569eceeaa4
Child:
55:c04568b25617
Added check if the file can open function to saved games class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 8:dd1037c5435b 1 #include "GameEngine.h"
evanso 47:49fa1adc10b4 2
evanso 47:49fa1adc10b4 3 /** Define acelleromter object */
evanso 47:49fa1adc10b4 4 FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
evanso 8:dd1037c5435b 5
evanso 49:ed569eceeaa4 6 /** Define SD card object */
evanso 49:ed569eceeaa4 7 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
evanso 49:ed569eceeaa4 8
evanso 8:dd1037c5435b 9 GameEngine::GameEngine() {
evanso 44:2d957de3bce3 10 //attach ticker to isr
evanso 42:3aed75338272 11 ticker.attach(callback(this, &GameEngine::lcd_frame_time_isr), 0.04 );
evanso 8:dd1037c5435b 12 }
evanso 8:dd1037c5435b 13
evanso 8:dd1037c5435b 14 GameEngine::~GameEngine() {
evanso 8:dd1037c5435b 15
evanso 8:dd1037c5435b 16 }
evanso 8:dd1037c5435b 17
evanso 13:12276eed13ac 18 void GameEngine::init() {
evanso 36:27aa597db3d2 19 // Initalise objects
evanso 8:dd1037c5435b 20 pad.init();
evanso 8:dd1037c5435b 21 lcd.init();
evanso 8:dd1037c5435b 22 spaceship.init();
evanso 13:12276eed13ac 23 map.init(pad);
evanso 40:71f947254fda 24 menu.init();
evanso 47:49fa1adc10b4 25 accelerometer.init();
evanso 36:27aa597db3d2 26
evanso 36:27aa597db3d2 27 // Set seed value
evanso 36:27aa597db3d2 28 srand(pad.read_adc()*64000);
evanso 36:27aa597db3d2 29
evanso 36:27aa597db3d2 30 // Assign values to variables
evanso 45:fc3238cd28c6 31 spawn_time_multipler_ = 1;
evanso 51:35cb8e604b72 32 alien_number_ = 3;
evanso 45:fc3238cd28c6 33 spawn_alien_counter_ = 0;
evanso 45:fc3238cd28c6 34 spaceship_lives_ = 3;
evanso 45:fc3238cd28c6 35 spaceship_destroyed_ = false;
evanso 45:fc3238cd28c6 36 reset_map_counter_ = 0;
evanso 45:fc3238cd28c6 37 smart_bomb_counter_ = 4;
evanso 33:7fedd8029473 38 create_people();
evanso 45:fc3238cd28c6 39 points_ = 0;
evanso 45:fc3238cd28c6 40 smart_bomb_timer_ = 0;
evanso 45:fc3238cd28c6 41 bullet_timer_ = 0;
evanso 41:5959256f4aab 42 current_menu_part_= menu.get_current_menu_part();
evanso 45:fc3238cd28c6 43 lcd_frame_time_flag_ = 0;
evanso 8:dd1037c5435b 44 }
evanso 8:dd1037c5435b 45
evanso 44:2d957de3bce3 46 // Menu Control-----------------------------------------------------------------
evanso 44:2d957de3bce3 47
evanso 44:2d957de3bce3 48 void GameEngine::game_select_part(){
evanso 44:2d957de3bce3 49 switch (current_menu_part_) {
evanso 44:2d957de3bce3 50 case main_menu: run_menu(); break;
evanso 44:2d957de3bce3 51 case play: run_play(); break;
evanso 44:2d957de3bce3 52 case settings: run_settings(); break;
evanso 44:2d957de3bce3 53 case saved_games: run_saved_games(); break;
evanso 44:2d957de3bce3 54 }
evanso 44:2d957de3bce3 55 }
evanso 44:2d957de3bce3 56
evanso 44:2d957de3bce3 57 void GameEngine::run_menu(){
evanso 44:2d957de3bce3 58 lcd.setContrast(pad.read_pot1());
evanso 44:2d957de3bce3 59 lcd.clear();
evanso 44:2d957de3bce3 60
evanso 44:2d957de3bce3 61 // Scrolls, draws and selcts menu parts
evanso 44:2d957de3bce3 62 read_joystick_direction();
evanso 44:2d957de3bce3 63 menu.menu_scroll(pad, d_);
evanso 44:2d957de3bce3 64 menu.draw_part(lcd);
evanso 44:2d957de3bce3 65 menu.select_part(lcd, pad);
evanso 44:2d957de3bce3 66
evanso 44:2d957de3bce3 67 current_menu_part_ = menu.get_current_menu_part();
evanso 44:2d957de3bce3 68
evanso 44:2d957de3bce3 69 lcd.refresh();
evanso 44:2d957de3bce3 70 }
evanso 44:2d957de3bce3 71
evanso 44:2d957de3bce3 72 void GameEngine::run_play(){
evanso 44:2d957de3bce3 73 while (1) {
evanso 44:2d957de3bce3 74 // Checks flag and prints spaceship on lcd then resets flag
evanso 45:fc3238cd28c6 75 if (lcd_frame_time_flag_) {
evanso 44:2d957de3bce3 76 // resets ISR flag
evanso 45:fc3238cd28c6 77 lcd_frame_time_flag_ = 0;
evanso 44:2d957de3bce3 78
evanso 44:2d957de3bce3 79 gameplay_loop();
evanso 44:2d957de3bce3 80
evanso 44:2d957de3bce3 81 // break out of game loop when lives = 0
evanso 45:fc3238cd28c6 82 if(!spaceship_lives_){
evanso 44:2d957de3bce3 83 break;
evanso 44:2d957de3bce3 84
evanso 44:2d957de3bce3 85 }
evanso 44:2d957de3bce3 86 }
evanso 44:2d957de3bce3 87
evanso 44:2d957de3bce3 88 // MCU put to sleep between each frame to save power
evanso 44:2d957de3bce3 89 sleep();
evanso 44:2d957de3bce3 90 }
evanso 51:35cb8e604b72 91
evanso 51:35cb8e604b72 92 //game over screen then back to menu
evanso 44:2d957de3bce3 93 lcd.clear();
evanso 44:2d957de3bce3 94 lcd.printString("Game Over",15,3);
evanso 44:2d957de3bce3 95 lcd.refresh();
evanso 44:2d957de3bce3 96 wait(3);
evanso 44:2d957de3bce3 97 current_menu_part_ = main_menu;
evanso 44:2d957de3bce3 98 }
evanso 44:2d957de3bce3 99
evanso 13:12276eed13ac 100 void GameEngine::gameplay_loop() {
evanso 41:5959256f4aab 101
evanso 30:814674b189f0 102 // clear screen and set contrast
evanso 13:12276eed13ac 103 lcd.setContrast(pad.read_pot1());
evanso 8:dd1037c5435b 104 lcd.clear();
evanso 11:ab578a151f67 105
evanso 41:5959256f4aab 106 // creat objects
evanso 41:5959256f4aab 107 spawn_aliens();
evanso 41:5959256f4aab 108 spawn_people();
evanso 41:5959256f4aab 109
evanso 41:5959256f4aab 110 //If spaceship is destroyed stop joystick input, dont draw spaceship sprite
evanso 45:fc3238cd28c6 111 if (!spaceship_destroyed_){
evanso 47:49fa1adc10b4 112 //read_joystick_direction();
evanso 47:49fa1adc10b4 113 read_accelerometer_direction();
evanso 41:5959256f4aab 114 spaceship.movement(d_);
evanso 41:5959256f4aab 115 spaceship.draw(lcd);
evanso 47:49fa1adc10b4 116 create_weapons_bullets();
evanso 47:49fa1adc10b4 117 create_weapons_smart_bomb();
evanso 41:5959256f4aab 118 }
evanso 41:5959256f4aab 119
evanso 41:5959256f4aab 120 // Draws objects
evanso 41:5959256f4aab 121 map.draw_map(lcd, d_);
evanso 41:5959256f4aab 122 draw_aliens();
evanso 41:5959256f4aab 123 draw_bullets();
evanso 41:5959256f4aab 124 draw_explosions();
evanso 41:5959256f4aab 125 draw_people();
evanso 45:fc3238cd28c6 126 hud.draw_HUD(lcd, spaceship_lives_, points_, smart_bomb_counter_);
evanso 41:5959256f4aab 127 spaceship_lives_leds();
evanso 41:5959256f4aab 128
evanso 41:5959256f4aab 129 reset_map_timer();
evanso 30:814674b189f0 130
evanso 18:11068b98e261 131 // refresh's screen
evanso 18:11068b98e261 132 lcd.refresh();
evanso 13:12276eed13ac 133 }
evanso 13:12276eed13ac 134
evanso 41:5959256f4aab 135 void GameEngine::run_settings(){
evanso 41:5959256f4aab 136
evanso 41:5959256f4aab 137 }
evanso 41:5959256f4aab 138
evanso 41:5959256f4aab 139 void GameEngine::run_saved_games(){
evanso 41:5959256f4aab 140
evanso 41:5959256f4aab 141 }
evanso 41:5959256f4aab 142
evanso 42:3aed75338272 143 void GameEngine::lcd_frame_time_isr(){
evanso 42:3aed75338272 144 // set ISR flag
evanso 45:fc3238cd28c6 145 lcd_frame_time_flag_ = 1;
evanso 42:3aed75338272 146 }
evanso 42:3aed75338272 147
evanso 44:2d957de3bce3 148
evanso 44:2d957de3bce3 149
evanso 39:fc5586b930e3 150 //Spaceship control ------------------------------------------------------------
evanso 39:fc5586b930e3 151
evanso 15:90b6821bcf64 152 void GameEngine::read_joystick_direction(){
evanso 13:12276eed13ac 153 d_ = pad.get_direction();
evanso 18:11068b98e261 154 }
evanso 18:11068b98e261 155
evanso 47:49fa1adc10b4 156 void GameEngine::read_accelerometer_direction(){
evanso 47:49fa1adc10b4 157 float roll = accelerometer.get_roll_angle();
evanso 47:49fa1adc10b4 158 float pitch = accelerometer.get_pitch_angle();
evanso 47:49fa1adc10b4 159 // printf("Pitch = %f Roll = %f\n", pitch, roll);
evanso 47:49fa1adc10b4 160
evanso 47:49fa1adc10b4 161 //north
evanso 47:49fa1adc10b4 162 if(pitch >= -35){
evanso 47:49fa1adc10b4 163 if(roll >= 10){
evanso 47:49fa1adc10b4 164 d_ = NE;
evanso 47:49fa1adc10b4 165 }else if (roll <= -10){
evanso 47:49fa1adc10b4 166 d_ = NW;
evanso 47:49fa1adc10b4 167 }else {
evanso 47:49fa1adc10b4 168 d_ = N;
evanso 47:49fa1adc10b4 169 }
evanso 47:49fa1adc10b4 170 //south
evanso 47:49fa1adc10b4 171 }else if(pitch <= -55){
evanso 47:49fa1adc10b4 172 if (roll >= 10){
evanso 47:49fa1adc10b4 173 d_ = SE;
evanso 47:49fa1adc10b4 174 }else if(roll <= -10){
evanso 47:49fa1adc10b4 175 d_ = SW;
evanso 47:49fa1adc10b4 176 }else {
evanso 47:49fa1adc10b4 177 d_ = S;
evanso 47:49fa1adc10b4 178 }
evanso 47:49fa1adc10b4 179 // East/west
evanso 47:49fa1adc10b4 180 }else {
evanso 47:49fa1adc10b4 181 if (roll >= 10){
evanso 47:49fa1adc10b4 182 d_ = E;
evanso 47:49fa1adc10b4 183 }else if(roll <= -10){
evanso 47:49fa1adc10b4 184 d_ = W;
evanso 47:49fa1adc10b4 185 }else{
evanso 47:49fa1adc10b4 186 d_ = CENTRE;
evanso 47:49fa1adc10b4 187 }
evanso 47:49fa1adc10b4 188 }
evanso 47:49fa1adc10b4 189 }
evanso 47:49fa1adc10b4 190
evanso 39:fc5586b930e3 191 void GameEngine::spaceship_lives_leds(){
evanso 39:fc5586b930e3 192 pad.leds_off();
evanso 39:fc5586b930e3 193
evanso 39:fc5586b930e3 194 //top two red leds on if 1 lives left
evanso 45:fc3238cd28c6 195 if(spaceship_lives_ >= 1){
evanso 39:fc5586b930e3 196 pad.led(1,1);
evanso 39:fc5586b930e3 197 pad.led(4,1);
evanso 39:fc5586b930e3 198 }
evanso 39:fc5586b930e3 199
evanso 39:fc5586b930e3 200 //middle two orange leds also on if 2 lives left
evanso 45:fc3238cd28c6 201 if(spaceship_lives_ >= 2){
evanso 39:fc5586b930e3 202 pad.led(2,1);
evanso 39:fc5586b930e3 203 pad.led(5,1);
evanso 39:fc5586b930e3 204 }
evanso 39:fc5586b930e3 205
evanso 39:fc5586b930e3 206 //bottom two red leds also on if 3 lives left
evanso 45:fc3238cd28c6 207 if(spaceship_lives_ == 3){
evanso 39:fc5586b930e3 208 pad.led(3,1);
evanso 39:fc5586b930e3 209 pad.led(6,1);
evanso 39:fc5586b930e3 210 }
evanso 39:fc5586b930e3 211 }
evanso 39:fc5586b930e3 212
evanso 44:2d957de3bce3 213
evanso 44:2d957de3bce3 214
evanso 36:27aa597db3d2 215 //Weapon control ---------------------------------------------------------------
evanso 36:27aa597db3d2 216
evanso 36:27aa597db3d2 217 void GameEngine::create_weapons_bullets(){
evanso 39:fc5586b930e3 218 // controlls speed that bullet can be fired
evanso 45:fc3238cd28c6 219 if(bullet_timer_ <=0){
evanso 39:fc5586b930e3 220 if (pad.A_pressed()){
evanso 36:27aa597db3d2 221 // Bullet object
evanso 36:27aa597db3d2 222 Weapons new_bullet;
evanso 36:27aa597db3d2 223
evanso 36:27aa597db3d2 224 new_bullet.init(spaceship.get_pos(),
evanso 36:27aa597db3d2 225 spaceship.get_spaceship_sprite_direction(), true);
evanso 36:27aa597db3d2 226
evanso 36:27aa597db3d2 227 // Stores bullet object in vector
evanso 36:27aa597db3d2 228 bullet_vector.push_back(new_bullet);
evanso 39:fc5586b930e3 229 }
evanso 45:fc3238cd28c6 230 bullet_timer_ = 5;
evanso 39:fc5586b930e3 231 }
evanso 45:fc3238cd28c6 232 bullet_timer_--;
evanso 36:27aa597db3d2 233 }
evanso 36:27aa597db3d2 234
evanso 36:27aa597db3d2 235 void GameEngine::create_weapons_smart_bomb(){
evanso 39:fc5586b930e3 236 // timer to stop smart bomb button be accidently pressed twice
evanso 45:fc3238cd28c6 237 if(smart_bomb_timer_ <=0){
evanso 45:fc3238cd28c6 238 if (pad.B_pressed() && smart_bomb_counter_ > 0){
evanso 39:fc5586b930e3 239 weapons.smart_bomb(lcd);
evanso 45:fc3238cd28c6 240 smart_bomb_counter_--;
evanso 39:fc5586b930e3 241
evanso 39:fc5586b930e3 242 //Deletes alien object if on screen
evanso 39:fc5586b930e3 243 for (int i = (alien_vector.size() -1) ; i >= 0 ; i--){
evanso 39:fc5586b930e3 244 Vector2D alien_pos = alien_vector[i].get_pos();
evanso 36:27aa597db3d2 245
evanso 39:fc5586b930e3 246 // Creats explosion if alien was on the screen
evanso 39:fc5586b930e3 247 if(alien_pos.x <= 84 && alien_pos.x >= -6){
evanso 39:fc5586b930e3 248 create_explosion(alien_pos);
evanso 39:fc5586b930e3 249
evanso 39:fc5586b930e3 250 //delete person object if was being abducted by destroyed alien
evanso 39:fc5586b930e3 251 if(alien_vector[i].get_collision_people_element() > -1){
evanso 39:fc5586b930e3 252 people_vector.erase(people_vector.begin() +
evanso 39:fc5586b930e3 253 alien_vector[i].get_collision_people_element());
evanso 39:fc5586b930e3 254 }
evanso 39:fc5586b930e3 255
evanso 39:fc5586b930e3 256 alien_vector.erase(alien_vector.begin()+ i);
evanso 45:fc3238cd28c6 257 points_ ++;
evanso 39:fc5586b930e3 258 }
evanso 36:27aa597db3d2 259 }
evanso 45:fc3238cd28c6 260 smart_bomb_timer_ = 30;
evanso 39:fc5586b930e3 261 }
evanso 39:fc5586b930e3 262 }
evanso 45:fc3238cd28c6 263 smart_bomb_timer_--;
evanso 36:27aa597db3d2 264 }
evanso 36:27aa597db3d2 265
evanso 36:27aa597db3d2 266 void GameEngine::draw_bullets(){
evanso 36:27aa597db3d2 267 // interates over bullet vectors, moves and draws bullet objects
evanso 36:27aa597db3d2 268
evanso 36:27aa597db3d2 269 // Alien bullets
evanso 36:27aa597db3d2 270 for (int i = 0; i < alien_bullet_vector.size(); i++){
evanso 36:27aa597db3d2 271 alien_bullet_vector[i].draw_alien_bullet(lcd, d_);
evanso 36:27aa597db3d2 272
evanso 36:27aa597db3d2 273 // deletes alien bullet object after bullet has moved set distance
evanso 36:27aa597db3d2 274 if(alien_bullet_vector[i].get_bullet_delete_counter() >> 5){
evanso 36:27aa597db3d2 275 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 36:27aa597db3d2 276 }
evanso 36:27aa597db3d2 277
evanso 36:27aa597db3d2 278 // Deletes bullet and shpaceship if collision detected
evanso 36:27aa597db3d2 279 if (spaceship.check_collision(alien_bullet_vector[i]) &&
evanso 45:fc3238cd28c6 280 !spaceship_destroyed_ ){
evanso 36:27aa597db3d2 281 create_explosion(spaceship.get_pos());
evanso 36:27aa597db3d2 282
evanso 45:fc3238cd28c6 283 spaceship_destroyed_ = true;
evanso 36:27aa597db3d2 284 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 36:27aa597db3d2 285 }
evanso 36:27aa597db3d2 286 }
evanso 36:27aa597db3d2 287
evanso 36:27aa597db3d2 288 // Spaceship bullets
evanso 36:27aa597db3d2 289 for (int i = 0; i < bullet_vector.size(); i++){
evanso 36:27aa597db3d2 290 bullet_vector[i].draw_bullet(lcd);
evanso 36:27aa597db3d2 291
evanso 36:27aa597db3d2 292 // deletes bullet object after bullet has moved set distance
evanso 36:27aa597db3d2 293 if(bullet_vector[i].get_bullet_delete_counter() >> 5){
evanso 36:27aa597db3d2 294 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 36:27aa597db3d2 295 }
evanso 36:27aa597db3d2 296 }
evanso 36:27aa597db3d2 297 }
evanso 36:27aa597db3d2 298
evanso 36:27aa597db3d2 299
evanso 36:27aa597db3d2 300
evanso 36:27aa597db3d2 301 //Alien Control ----------------------------------------------------------------
evanso 36:27aa597db3d2 302
evanso 32:c006a9882778 303 void GameEngine::spawn_aliens(){
evanso 51:35cb8e604b72 304 if(alien_vector.size() <= alien_number_){
evanso 34:85ccc16f24d2 305 create_alien();
evanso 32:c006a9882778 306 }
evanso 32:c006a9882778 307
evanso 45:fc3238cd28c6 308 //printf( " alien_number_counter_ = %d\n",alien_number_counter_);
evanso 45:fc3238cd28c6 309
evanso 34:85ccc16f24d2 310 //slowley incresing the alien counter as game goes on to make harder
evanso 45:fc3238cd28c6 311 if(spawn_alien_counter_%(500*spawn_time_multipler_) == 0){
evanso 51:35cb8e604b72 312 alien_number_++;
evanso 45:fc3238cd28c6 313 spawn_time_multipler_++;
evanso 32:c006a9882778 314 }
evanso 34:85ccc16f24d2 315
evanso 45:fc3238cd28c6 316 spawn_alien_counter_++;
evanso 32:c006a9882778 317 }
evanso 32:c006a9882778 318
evanso 36:27aa597db3d2 319 void GameEngine::create_alien(){
evanso 36:27aa597db3d2 320 int position_x_start = 0;
evanso 36:27aa597db3d2 321
evanso 36:27aa597db3d2 322 // Alien object
evanso 36:27aa597db3d2 323 Alien new_alien;
evanso 36:27aa597db3d2 324
evanso 36:27aa597db3d2 325 //spawns aliens between x > 84 and x <0
evanso 36:27aa597db3d2 326 if(rand() % 2){
evanso 36:27aa597db3d2 327 position_x_start = rand() % 84 + 84;
evanso 36:27aa597db3d2 328 }else{
evanso 36:27aa597db3d2 329 position_x_start =rand() % 83 - 84;
evanso 36:27aa597db3d2 330 }
evanso 39:fc5586b930e3 331 new_alien.init(pad, position_x_start, (rand() % 33 + 9));
evanso 36:27aa597db3d2 332
evanso 36:27aa597db3d2 333 // Stores alien object in vector
evanso 36:27aa597db3d2 334 alien_vector.push_back(new_alien);
evanso 36:27aa597db3d2 335 }
evanso 36:27aa597db3d2 336
evanso 36:27aa597db3d2 337 void GameEngine::check_alien_people_collision(int i){
evanso 40:71f947254fda 338 // only a collision if all three of theses conditions are met:
evanso 40:71f947254fda 339 // check all these codition for every alien to every person
evanso 40:71f947254fda 340 // 1)person hasn't already had a collision
evanso 40:71f947254fda 341 // 2)There is an acutaual collision of people and alien
evanso 36:27aa597db3d2 342 for (int x = 0; x < people_vector.size(); x++){
evanso 36:27aa597db3d2 343 if (!people_vector[x].get_alien_collision_flag() &&
evanso 36:27aa597db3d2 344 people_vector[x].check_alien_collision(alien_vector[i])){
evanso 36:27aa597db3d2 345 alien_vector[i].set_collision_people_element(x);
evanso 36:27aa597db3d2 346 }
evanso 36:27aa597db3d2 347 }
evanso 36:27aa597db3d2 348
evanso 36:27aa597db3d2 349 //draws collision if detected
evanso 36:27aa597db3d2 350 if(alien_vector[i].get_collision_people_element() > -1){
evanso 36:27aa597db3d2 351 alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_,
evanso 36:27aa597db3d2 352 map.get_length_map(), map.get_position_x_map(),true);
evanso 36:27aa597db3d2 353 }else{
evanso 36:27aa597db3d2 354 alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_,
evanso 36:27aa597db3d2 355 map.get_length_map(), map.get_position_x_map(),false);
evanso 36:27aa597db3d2 356 }
evanso 36:27aa597db3d2 357 }
evanso 36:27aa597db3d2 358
evanso 36:27aa597db3d2 359 void GameEngine::alliens_fire_bullets(int i){
evanso 36:27aa597db3d2 360 if (alien_vector[i].get_alien_fire_counter()%60 == 0){
evanso 36:27aa597db3d2 361 Weapons alien_bullet;
evanso 36:27aa597db3d2 362
evanso 36:27aa597db3d2 363 // fires bullet towards direction of spaceship
evanso 36:27aa597db3d2 364 bool alien_bullet_direction = false;
evanso 36:27aa597db3d2 365 Vector2D spaceship_pos = spaceship.get_pos();
evanso 36:27aa597db3d2 366 Vector2D alien_pos = alien_vector[i].get_pos();
evanso 36:27aa597db3d2 367 if(spaceship_pos.x > alien_pos.x){
evanso 36:27aa597db3d2 368 alien_bullet_direction = true;
evanso 36:27aa597db3d2 369 }
evanso 36:27aa597db3d2 370
evanso 36:27aa597db3d2 371 alien_bullet.init(alien_vector[i].get_pos(),
evanso 36:27aa597db3d2 372 alien_bullet_direction , false);
evanso 36:27aa597db3d2 373
evanso 36:27aa597db3d2 374 // Stores bullet object in vector
evanso 36:27aa597db3d2 375 alien_bullet_vector.push_back(alien_bullet);
evanso 36:27aa597db3d2 376 }
evanso 36:27aa597db3d2 377 }
evanso 36:27aa597db3d2 378
evanso 36:27aa597db3d2 379 void GameEngine::delete_aliens(int i){
evanso 36:27aa597db3d2 380 for (int x = 0; x < bullet_vector.size(); x++){
evanso 36:27aa597db3d2 381 if (alien_vector[i].check_collision(bullet_vector[x])){
evanso 36:27aa597db3d2 382 create_explosion(alien_vector[i].get_pos());
evanso 36:27aa597db3d2 383
evanso 36:27aa597db3d2 384 //delete person object if was carried by destroyed alien
evanso 36:27aa597db3d2 385 if(alien_vector[i].get_collision_people_element() > -1){
evanso 36:27aa597db3d2 386 people_vector.erase(people_vector.begin() +
evanso 36:27aa597db3d2 387 alien_vector[i].get_collision_people_element());
evanso 36:27aa597db3d2 388 }
evanso 36:27aa597db3d2 389
evanso 39:fc5586b930e3 390 bullet_vector.erase(bullet_vector.begin()+ x);
evanso 39:fc5586b930e3 391 alien_vector.erase(alien_vector.begin()+ i);
evanso 45:fc3238cd28c6 392 points_ ++;
evanso 36:27aa597db3d2 393 }
evanso 36:27aa597db3d2 394 }
evanso 36:27aa597db3d2 395 }
evanso 36:27aa597db3d2 396
evanso 36:27aa597db3d2 397 void GameEngine::draw_aliens(){
evanso 36:27aa597db3d2 398 // interates over alien vector and draws each new_alien object
evanso 36:27aa597db3d2 399 for (int i = 0; i < alien_vector.size(); i++){
evanso 36:27aa597db3d2 400
evanso 36:27aa597db3d2 401 check_alien_people_collision(i);
evanso 36:27aa597db3d2 402
evanso 36:27aa597db3d2 403 // deleted spaceship if alien ship touches spaceship
evanso 36:27aa597db3d2 404 if (spaceship.check_alien_collision(alien_vector[i]) &&
evanso 45:fc3238cd28c6 405 !spaceship_destroyed_){
evanso 36:27aa597db3d2 406 create_explosion(spaceship.get_pos());
evanso 45:fc3238cd28c6 407 spaceship_destroyed_ = true;
evanso 36:27aa597db3d2 408 }
evanso 36:27aa597db3d2 409
evanso 36:27aa597db3d2 410 alliens_fire_bullets(i);
evanso 36:27aa597db3d2 411
evanso 36:27aa597db3d2 412 delete_aliens(i);
evanso 34:85ccc16f24d2 413 }
evanso 34:85ccc16f24d2 414 }
evanso 34:85ccc16f24d2 415
evanso 36:27aa597db3d2 416
evanso 36:27aa597db3d2 417
evanso 36:27aa597db3d2 418 //Explotion Control ------------------------------------------------------------
evanso 36:27aa597db3d2 419
evanso 36:27aa597db3d2 420 void GameEngine::create_explosion(Vector2D destroyed_position){
evanso 36:27aa597db3d2 421 // explosion object
evanso 36:27aa597db3d2 422 Explosion new_explosion;
evanso 36:27aa597db3d2 423
evanso 36:27aa597db3d2 424 new_explosion.init(destroyed_position);
evanso 33:7fedd8029473 425
evanso 36:27aa597db3d2 426 // Stores explosion object in vector
evanso 36:27aa597db3d2 427 explosion_vector.push_back(new_explosion);
evanso 36:27aa597db3d2 428 }
evanso 36:27aa597db3d2 429
evanso 36:27aa597db3d2 430 void GameEngine::draw_explosions(){
evanso 36:27aa597db3d2 431 // interates over expoltion vector and draws each explosion object then
evanso 36:27aa597db3d2 432 // deleted object after set size
evanso 36:27aa597db3d2 433 for (int i = 0; i < explosion_vector.size(); i++){
evanso 36:27aa597db3d2 434 explosion_vector[i].draw_explosion(lcd);
evanso 31:6015e8ed859c 435
evanso 36:27aa597db3d2 436 // delete explosion after reaches set size
evanso 36:27aa597db3d2 437 if(explosion_vector[i].get_explosion_radius() == 8){
evanso 36:27aa597db3d2 438 explosion_vector.erase(explosion_vector.begin()+ i);
evanso 31:6015e8ed859c 439 }
evanso 36:27aa597db3d2 440 }
evanso 36:27aa597db3d2 441 }
evanso 36:27aa597db3d2 442
evanso 36:27aa597db3d2 443
evanso 36:27aa597db3d2 444
evanso 36:27aa597db3d2 445 //People Control ---------------------------------------------------------------
evanso 36:27aa597db3d2 446
evanso 36:27aa597db3d2 447 void GameEngine::spawn_people(){
evanso 36:27aa597db3d2 448 // Keeps number of people objects constant as people objecs are erased
evanso 36:27aa597db3d2 449 if(people_vector.size() <= 5){
evanso 36:27aa597db3d2 450 create_people();
evanso 31:6015e8ed859c 451 }
evanso 20:febd920ec29e 452 }
evanso 20:febd920ec29e 453
evanso 33:7fedd8029473 454 void GameEngine::create_people(){
evanso 33:7fedd8029473 455 // People object
evanso 33:7fedd8029473 456 People people;
evanso 33:7fedd8029473 457
evanso 33:7fedd8029473 458 people.init(pad, (rand() % 168 - 84));
evanso 33:7fedd8029473 459
evanso 33:7fedd8029473 460 // Stores alien object in vector
evanso 33:7fedd8029473 461 people_vector.push_back(people);
evanso 33:7fedd8029473 462 }
evanso 33:7fedd8029473 463
evanso 33:7fedd8029473 464 void GameEngine::draw_people(){
evanso 33:7fedd8029473 465 for (int i = 0; i < people_vector.size(); i++){
evanso 34:85ccc16f24d2 466 people_vector[i].draw_people(lcd, d_,
evanso 34:85ccc16f24d2 467 map.get_length_map(), map.get_position_x_map());
evanso 34:85ccc16f24d2 468
evanso 34:85ccc16f24d2 469 //errase person if at top of screen as captured by alien and alien is
evanso 34:85ccc16f24d2 470 //set to track mode
evanso 34:85ccc16f24d2 471 Vector2D people_pos = people_vector[i].get_pos();
evanso 40:71f947254fda 472
evanso 40:71f947254fda 473 for (int x = 0; x < alien_vector.size(); x++){
evanso 40:71f947254fda 474 Vector2D alien_pos = alien_vector[x].get_pos();
evanso 40:71f947254fda 475 if(people_pos.y < 30 && alien_pos.y < 9){
evanso 40:71f947254fda 476
evanso 40:71f947254fda 477 //set alien who abducted person to track mode
evanso 34:85ccc16f24d2 478 if(alien_vector[x].get_collision_people_element() == i){
evanso 34:85ccc16f24d2 479 alien_vector[x].set_track_flag(true);
evanso 40:71f947254fda 480 people_vector.erase(people_vector.begin()+ i);
evanso 34:85ccc16f24d2 481 }
evanso 40:71f947254fda 482
evanso 40:71f947254fda 483 }
evanso 34:85ccc16f24d2 484
evanso 34:85ccc16f24d2 485 }
evanso 33:7fedd8029473 486 }
evanso 33:7fedd8029473 487 }
evanso 33:7fedd8029473 488
evanso 19:1bc0a2d22054 489
evanso 25:70b55f5bfc87 490
evanso 36:27aa597db3d2 491 //Map Control-------------------------------------------------------------------
evanso 19:1bc0a2d22054 492
evanso 36:27aa597db3d2 493 void GameEngine::reset_map_timer(){
evanso 45:fc3238cd28c6 494 if(spaceship_destroyed_){
evanso 45:fc3238cd28c6 495 reset_map_counter_++;
evanso 36:27aa597db3d2 496
evanso 36:27aa597db3d2 497 // stops map movement
evanso 36:27aa597db3d2 498 d_ = CENTRE;
evanso 36:27aa597db3d2 499
evanso 36:27aa597db3d2 500 // reset map after set time
evanso 45:fc3238cd28c6 501 if(reset_map_counter_ == 50){
evanso 36:27aa597db3d2 502 reset_map();
evanso 25:70b55f5bfc87 503 }
evanso 36:27aa597db3d2 504 }
evanso 25:70b55f5bfc87 505 }
evanso 36:27aa597db3d2 506
evanso 30:814674b189f0 507 void GameEngine::reset_map(){
evanso 36:27aa597db3d2 508 // Reassign values to variables
evanso 30:814674b189f0 509 spaceship.init();
evanso 45:fc3238cd28c6 510 spaceship_lives_--;
evanso 45:fc3238cd28c6 511 spaceship_destroyed_ = false;
evanso 45:fc3238cd28c6 512 reset_map_counter_ = 0;
evanso 33:7fedd8029473 513
evanso 34:85ccc16f24d2 514 // erase aliens so redrawn in random positions when respawning spaceship
evanso 34:85ccc16f24d2 515 for (int i = (alien_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 516 alien_vector.erase(alien_vector.begin()+ i);
evanso 34:85ccc16f24d2 517 }
evanso 36:27aa597db3d2 518
evanso 34:85ccc16f24d2 519 // erase all people so redrawn at bottom of map
evanso 34:85ccc16f24d2 520 for (int i = (people_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 521 people_vector.erase(people_vector.begin()+ i);
evanso 34:85ccc16f24d2 522 }
evanso 36:27aa597db3d2 523
evanso 36:27aa597db3d2 524 // erase all alien bullets
evanso 34:85ccc16f24d2 525 for (int i = (alien_bullet_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 526 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 33:7fedd8029473 527 }
evanso 20:febd920ec29e 528 }