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