Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Committer:
evanso
Date:
Sun May 24 18:38:50 2020 +0000
Revision:
76:6daba3002424
Parent:
74:6827b43c689d
Child:
80:870bc6b4bf08
Added functions to high score that save and read the high score. Displays "NEW HIGH SOCRE" on the game over screen and plays a different song

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 11:ab578a151f67 1 #ifndef GAMEENGINE_H
evanso 11:ab578a151f67 2 #define GAMEENGINE_H
evanso 7:0af4ced868f5 3
evanso 27:8bb2bd97c319 4 // Included libraries ----------------------------------------------------------
evanso 7:0af4ced868f5 5 #include "mbed.h"
evanso 7:0af4ced868f5 6 #include "N5110.h"
evanso 7:0af4ced868f5 7 #include "Gamepad.h"
evanso 8:dd1037c5435b 8 #include "Spaceship.h"
evanso 8:dd1037c5435b 9 #include "Map.h"
evanso 17:25d79cca203a 10 #include "Weapons.h"
evanso 19:1bc0a2d22054 11 #include "Alien.h"
evanso 25:70b55f5bfc87 12 #include "Explosion.h"
evanso 33:7fedd8029473 13 #include "People.h"
evanso 39:fc5586b930e3 14 #include "HUD.h"
evanso 40:71f947254fda 15 #include "Menu.h"
evanso 47:49fa1adc10b4 16 #include "FXOS8700CQ.h"
evanso 49:ed569eceeaa4 17 #include "SDFileSystem.h"
evanso 56:663d0546c235 18 #include "SavedGames.h"
evanso 69:753ba27325ce 19 #include "Settings.h"
evanso 72:e7492591307e 20 #include "Sounds.h"
evanso 76:6daba3002424 21 #include "HighScore.h"
evanso 67:a2984682d641 22
evanso 67:a2984682d641 23 #include <vector>
evanso 47:49fa1adc10b4 24
evanso 11:ab578a151f67 25 /** GameEngine class
evanso 27:8bb2bd97c319 26 * @brief Runs the different parts of the game
evanso 27:8bb2bd97c319 27 * @author Benjamin Evans, University of Leeds
evanso 27:8bb2bd97c319 28 * @date April 2020
evanso 27:8bb2bd97c319 29 */
evanso 7:0af4ced868f5 30 class GameEngine {
evanso 7:0af4ced868f5 31 public:
evanso 7:0af4ced868f5 32 /** Constructor */
evanso 7:0af4ced868f5 33 GameEngine();
evanso 7:0af4ced868f5 34
evanso 7:0af4ced868f5 35 /** Destructor */
evanso 7:0af4ced868f5 36 ~GameEngine();
evanso 7:0af4ced868f5 37
evanso 14:7419c680656f 38 /** Initalises GameEngine */
evanso 13:12276eed13ac 39 void init();
evanso 7:0af4ced868f5 40
evanso 47:49fa1adc10b4 41 /** Switch statement to run run different menu options */
evanso 42:3aed75338272 42 void game_select_part();
evanso 15:90b6821bcf64 43
evanso 27:8bb2bd97c319 44 // Accessors and mutators --------------------------------------------------
evanso 11:ab578a151f67 45
evanso 11:ab578a151f67 46 private:
evanso 27:8bb2bd97c319 47 // Function prototypes -----------------------------------------------------
evanso 64:e9dfc35a1738 48
evanso 41:5959256f4aab 49 // Menu Control
evanso 64:e9dfc35a1738 50 /** Runs the menu */
evanso 64:e9dfc35a1738 51 void run_menu();
evanso 64:e9dfc35a1738 52
evanso 64:e9dfc35a1738 53 // Menu Play
evanso 64:e9dfc35a1738 54 /** Runs the play game */
evanso 64:e9dfc35a1738 55 void run_play();
evanso 42:3aed75338272 56
evanso 64:e9dfc35a1738 57 /** Initialises the play part of the game*/
evanso 64:e9dfc35a1738 58 void play_init();
evanso 71:bcbac2cfe005 59
evanso 71:bcbac2cfe005 60 /** Only runs movement when the spaceship is not destroyed */
evanso 71:bcbac2cfe005 61 void spaceship_not_detroyed();
evanso 64:e9dfc35a1738 62
evanso 42:3aed75338272 63 /** Main gameplay loop that runs playable part of game */
evanso 42:3aed75338272 64 void gameplay_loop();
evanso 41:5959256f4aab 65
evanso 64:e9dfc35a1738 66 /** Draws the game over screen*/
evanso 64:e9dfc35a1738 67 void draw_game_over_screen();
evanso 41:5959256f4aab 68
evanso 56:663d0546c235 69 /** Runs the paused screen */
evanso 56:663d0546c235 70 void run_paused_game();
evanso 56:663d0546c235 71
evanso 55:c04568b25617 72 /** Draws the pause screen*/
evanso 55:c04568b25617 73 void draw_pause_screen();
evanso 55:c04568b25617 74
evanso 64:e9dfc35a1738 75 /** Runs saved games screen */
evanso 64:e9dfc35a1738 76 void run_saved_games();
evanso 64:e9dfc35a1738 77
evanso 64:e9dfc35a1738 78 /** Time-triggered interrupt to wake MCU from sleep */
evanso 64:e9dfc35a1738 79 void lcd_frame_time_isr();
evanso 57:d4ce42c24561 80
evanso 74:6827b43c689d 81 /** Plays the music if its turned on */
evanso 74:6827b43c689d 82 void play_music();
evanso 74:6827b43c689d 83
evanso 74:6827b43c689d 84 /** Stops the music if its turned off */
evanso 74:6827b43c689d 85 void stop_music();
evanso 74:6827b43c689d 86
evanso 64:e9dfc35a1738 87 // Menu Setting
evanso 64:e9dfc35a1738 88 /** Runs settings screen*/
evanso 64:e9dfc35a1738 89 void run_settings();
evanso 58:a9a39424df52 90
evanso 64:e9dfc35a1738 91 // Menu Saved Games
evanso 59:0b2e43312d6b 92 /** Initialises the play part of the game and sets the variables to
evanso 59:0b2e43312d6b 93 * saved values
evanso 59:0b2e43312d6b 94 */
evanso 64:e9dfc35a1738 95 void saved_games_overide_init();
evanso 59:0b2e43312d6b 96
evanso 62:f0c86a854a9e 97 /** Runs save a game screen */
evanso 64:e9dfc35a1738 98 void run_save_a_game();
evanso 62:f0c86a854a9e 99
evanso 76:6daba3002424 100 // Menu High Score
evanso 76:6daba3002424 101 /** Runs high score screen */
evanso 76:6daba3002424 102 void run_highscore();
evanso 76:6daba3002424 103
evanso 76:6daba3002424 104 /** Calculates if the current end game score is a new high score and
evanso 76:6daba3002424 105 * saves it if it is
evanso 76:6daba3002424 106 * @return new_high_score @detials bool true if there is new high score
evanso 76:6daba3002424 107 */
evanso 76:6daba3002424 108 bool calculate_new_score();
evanso 76:6daba3002424 109
evanso 39:fc5586b930e3 110 //Spaceship Control
evanso 27:8bb2bd97c319 111 /** Gets joystick direction from gamepad and stores it in d_ */
evanso 18:11068b98e261 112 void read_joystick_direction();
evanso 11:ab578a151f67 113
evanso 47:49fa1adc10b4 114 /** Gets the pitch and roll of the gamepad and calculates d_ */
evanso 65:daa792a09e1f 115 void read_accelerometer_direction(float roll, float pitch);
evanso 47:49fa1adc10b4 116
evanso 39:fc5586b930e3 117 /** Turns on specific leds depending on how many lives left */
evanso 39:fc5586b930e3 118 void spaceship_lives_leds();
evanso 39:fc5586b930e3 119
evanso 36:27aa597db3d2 120 //Weapon Control
evanso 39:fc5586b930e3 121 /** Creates weapons object if button A is pressed and stores in vector*/
evanso 36:27aa597db3d2 122 void create_weapons_bullets();
evanso 36:27aa597db3d2 123
evanso 36:27aa597db3d2 124 /** Creates smart bomb if button B is pressed */
evanso 36:27aa597db3d2 125 void create_weapons_smart_bomb();
evanso 19:1bc0a2d22054 126
evanso 27:8bb2bd97c319 127 /** Draws each bullet object and deleted object after set movement
evanso 27:8bb2bd97c319 128 * distance
evanso 27:8bb2bd97c319 129 */
evanso 19:1bc0a2d22054 130 void draw_bullets();
evanso 13:12276eed13ac 131
evanso 36:27aa597db3d2 132 //Alien Control
evanso 36:27aa597db3d2 133 /** Spawns aliens in random position of the screen*/
evanso 36:27aa597db3d2 134 void spawn_aliens();
evanso 36:27aa597db3d2 135
evanso 22:053c11a202e1 136 /** Creats alien object and stores in vector*/
evanso 20:febd920ec29e 137 void create_alien();
evanso 20:febd920ec29e 138
evanso 36:27aa597db3d2 139 /** Checks for alien people collision and sets abduction movements
evanso 36:27aa597db3d2 140 * @param i @details iterator from draw_alien for loop
evanso 36:27aa597db3d2 141 */
evanso 36:27aa597db3d2 142 void check_alien_people_collision(int i);
evanso 36:27aa597db3d2 143
evanso 36:27aa597db3d2 144 /** Gets alien to fire bullets randomley towards spaceship
evanso 36:27aa597db3d2 145 * @param i @details iterator from draw_alien for loop
evanso 36:27aa597db3d2 146 */
evanso 36:27aa597db3d2 147 void alliens_fire_bullets(int i);
evanso 36:27aa597db3d2 148
evanso 36:27aa597db3d2 149 /**Deletes bullet and alien if collision detected and draws explosion
evanso 36:27aa597db3d2 150 * @param i @details iterator from draw_alien for loop
evanso 36:27aa597db3d2 151 */
evanso 36:27aa597db3d2 152 void delete_aliens(int i);
evanso 36:27aa597db3d2 153
evanso 27:8bb2bd97c319 154 /** Draws each alien object and deletes both objects if collision
evanso 27:8bb2bd97c319 155 * detected
evanso 27:8bb2bd97c319 156 */
evanso 20:febd920ec29e 157 void draw_aliens();
evanso 20:febd920ec29e 158
evanso 36:27aa597db3d2 159 //Explotion Control
evanso 36:27aa597db3d2 160 /** Creates bullet object if button A is pressed and stores in vector */
evanso 29:e96d91f1d39c 161 void create_explosion(Vector2D destroyed_position);
evanso 25:70b55f5bfc87 162
evanso 27:8bb2bd97c319 163 /** Draws each explosion object if collision detected */
evanso 25:70b55f5bfc87 164 void draw_explosions();
evanso 25:70b55f5bfc87 165
evanso 36:27aa597db3d2 166 //People Control
evanso 36:27aa597db3d2 167 /** Spawns people in random places at bottom of screen */
evanso 36:27aa597db3d2 168 void spawn_people();
evanso 32:c006a9882778 169
evanso 33:7fedd8029473 170 /** Spawns people in random position at bottom of the screen*/
evanso 33:7fedd8029473 171 void create_people();
evanso 33:7fedd8029473 172
evanso 33:7fedd8029473 173 /** Draws each people object */
evanso 33:7fedd8029473 174 void draw_people();
evanso 36:27aa597db3d2 175
evanso 36:27aa597db3d2 176 //Map Control
evanso 36:27aa597db3d2 177 /** Resets map after set time so spaceship explosion animation showes */
evanso 36:27aa597db3d2 178 void reset_map_timer();
evanso 33:7fedd8029473 179
evanso 36:27aa597db3d2 180 /** Resets the map after spaceship death and timer has ended */
evanso 36:27aa597db3d2 181 void reset_map();
evanso 36:27aa597db3d2 182
evanso 27:8bb2bd97c319 183 // Variables ---------------------------------------------------------------
evanso 41:5959256f4aab 184
evanso 41:5959256f4aab 185 // Menu Control
evanso 43:d43759dbddb9 186 /** The part of the menu that is currently selected and in*/
evanso 41:5959256f4aab 187 MenuParts current_menu_part_;
evanso 42:3aed75338272 188
evanso 42:3aed75338272 189 /** Volatile flag for ISR */
evanso 45:fc3238cd28c6 190 volatile int lcd_frame_time_flag_;
evanso 56:663d0546c235 191
evanso 56:663d0546c235 192 /** Paused flag */
evanso 56:663d0546c235 193 bool paused_flag_;
evanso 56:663d0546c235 194
evanso 62:f0c86a854a9e 195 /** Flag to exit the play part of the game */
evanso 57:d4ce42c24561 196 bool exit_flag_;
evanso 62:f0c86a854a9e 197
evanso 62:f0c86a854a9e 198 /** Flag to run the save game screen */
evanso 64:e9dfc35a1738 199 bool run_save_a_game_flag_;
evanso 68:bb1650c657ef 200
evanso 68:bb1650c657ef 201 /** Counter for pause screen so pause button doesnt double press */
evanso 68:bb1650c657ef 202 int paused_counter_;
evanso 73:d1aea9b8da92 203
evanso 73:d1aea9b8da92 204 /** Hold on or off depending if sound fx are set on or off */
evanso 73:d1aea9b8da92 205 SoundParts sound_fx_;
evanso 74:6827b43c689d 206
evanso 74:6827b43c689d 207 /** Hold on or off depending if music is set on or off */
evanso 74:6827b43c689d 208 MusicParts music_fx_;
evanso 74:6827b43c689d 209
evanso 39:fc5586b930e3 210 //Spacehip Control
evanso 39:fc5586b930e3 211 /** Define points*/
evanso 45:fc3238cd28c6 212 int points_;
evanso 18:11068b98e261 213
evanso 27:8bb2bd97c319 214 /** Define direction d of joystick*/
evanso 15:90b6821bcf64 215 Direction d_;
evanso 18:11068b98e261 216
evanso 36:27aa597db3d2 217 /** Flag for if spaceship is destroyed*/
evanso 45:fc3238cd28c6 218 bool spaceship_destroyed_;
evanso 36:27aa597db3d2 219
evanso 30:814674b189f0 220 /** Number of spaceship lives remaining*/
evanso 45:fc3238cd28c6 221 int spaceship_lives_;
evanso 30:814674b189f0 222
evanso 64:e9dfc35a1738 223 // Weapon Control
evanso 64:e9dfc35a1738 224 /** Counter for smart bomb timer*/
evanso 64:e9dfc35a1738 225 int smart_bomb_timer_;
evanso 64:e9dfc35a1738 226
evanso 64:e9dfc35a1738 227 /** Counter for bullet timer*/
evanso 64:e9dfc35a1738 228 int bullet_timer_;
evanso 64:e9dfc35a1738 229
evanso 64:e9dfc35a1738 230 /** Counter for how smart bombs left*/
evanso 64:e9dfc35a1738 231 int smart_bomb_counter_;
evanso 64:e9dfc35a1738 232
evanso 39:fc5586b930e3 233 // Alien Control
evanso 32:c006a9882778 234 /** Counter for spawning aliens*/
evanso 45:fc3238cd28c6 235 int spawn_alien_counter_;
evanso 32:c006a9882778 236
evanso 51:35cb8e604b72 237 /** Numeber of aliens on the screen at a time*/
evanso 51:35cb8e604b72 238 int alien_number_;
evanso 34:85ccc16f24d2 239
evanso 45:fc3238cd28c6 240 /** Miltiplier to increase number of alien as time goes on */
evanso 45:fc3238cd28c6 241 int spawn_time_multipler_;
evanso 64:e9dfc35a1738 242
evanso 64:e9dfc35a1738 243 // Map Control
evanso 64:e9dfc35a1738 244 /** Counter to reset map after set amount of frames*/
evanso 64:e9dfc35a1738 245 int reset_map_counter_;
evanso 64:e9dfc35a1738 246
evanso 27:8bb2bd97c319 247 // Vectors -----------------------------------------------------------------
evanso 27:8bb2bd97c319 248
evanso 27:8bb2bd97c319 249 /** Vector to store each new bullet object*/
evanso 19:1bc0a2d22054 250 std::vector<Weapons> bullet_vector;
evanso 19:1bc0a2d22054 251
evanso 28:a5958497d5ce 252 /** Vector to store each alien bullet object*/
evanso 28:a5958497d5ce 253 std::vector<Weapons> alien_bullet_vector;
evanso 28:a5958497d5ce 254
evanso 27:8bb2bd97c319 255 /** Vector to store each new alien object*/
evanso 20:febd920ec29e 256 std::vector<Alien> alien_vector;
evanso 20:febd920ec29e 257
evanso 27:8bb2bd97c319 258 /** Vector to store each new eplosion object*/
evanso 25:70b55f5bfc87 259 std::vector<Explosion> explosion_vector;
evanso 25:70b55f5bfc87 260
evanso 33:7fedd8029473 261 /** Vector to store each new people object*/
evanso 33:7fedd8029473 262 std::vector<People> people_vector;
evanso 33:7fedd8029473 263
evanso 27:8bb2bd97c319 264 // Objects -----------------------------------------------------------------
evanso 27:8bb2bd97c319 265
evanso 43:d43759dbddb9 266 /** Define Gamepad object */
evanso 13:12276eed13ac 267 Gamepad pad;
evanso 13:12276eed13ac 268
evanso 43:d43759dbddb9 269 /** Define LCD object */
evanso 13:12276eed13ac 270 N5110 lcd;
evanso 13:12276eed13ac 271
evanso 27:8bb2bd97c319 272 /** Define Spaceship object */
evanso 13:12276eed13ac 273 Spaceship spaceship;
evanso 13:12276eed13ac 274
evanso 43:d43759dbddb9 275 /** Define Map object */
evanso 19:1bc0a2d22054 276 Map map;
evanso 33:7fedd8029473 277
evanso 43:d43759dbddb9 278 /** Define Weapons object */
evanso 33:7fedd8029473 279 Weapons weapons;
evanso 39:fc5586b930e3 280
evanso 43:d43759dbddb9 281 /** Define HUD object */
evanso 39:fc5586b930e3 282 HUD hud;
evanso 40:71f947254fda 283
evanso 43:d43759dbddb9 284 /** Define Menu object */
evanso 40:71f947254fda 285 Menu menu;
evanso 42:3aed75338272 286
evanso 69:753ba27325ce 287 /** Define Setting object */
evanso 69:753ba27325ce 288 Settings setting;
evanso 69:753ba27325ce 289
evanso 64:e9dfc35a1738 290 /** Define Ticker object for lcd framerate */
evanso 47:49fa1adc10b4 291 Ticker ticker;
evanso 56:663d0546c235 292
evanso 64:e9dfc35a1738 293 /** Define SavedGames object */
evanso 56:663d0546c235 294 SavedGames saved;
evanso 76:6daba3002424 295
evanso 76:6daba3002424 296 /** Define HighScore object */
evanso 76:6daba3002424 297 HighScore h_score;
evanso 7:0af4ced868f5 298 };
evanso 7:0af4ced868f5 299
evanso 7:0af4ced868f5 300 #endif