Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Committer:
evanso
Date:
Sat May 23 21:35:16 2020 +0000
Revision:
72:e7492591307e
Parent:
71:bcbac2cfe005
Child:
73:d1aea9b8da92
Added sound class to hold songs. Also added tones for when sprites explode.

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