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