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.
Dependencies: mbed
TanksEngine/TanksEngine.h@21:44e87d88afe2, 2019-05-09 (annotated)
- Committer:
- el17mcd
- Date:
- Thu May 09 13:10:16 2019 +0000
- Revision:
- 21:44e87d88afe2
- Parent:
- 17:cb39d9fa08dc
- Child:
- 22:9e9685856ce1
Doxygen comments added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17mcd | 16:a2c945279b79 | 1 | #ifndef TANKENGINE_H |
el17mcd | 16:a2c945279b79 | 2 | #define TANKENGINE_H |
el17mcd | 7:a3ccabdebe2e | 3 | |
el17mcd | 7:a3ccabdebe2e | 4 | #include "mbed.h" |
el17mcd | 7:a3ccabdebe2e | 5 | #include "N5110.h" |
el17mcd | 7:a3ccabdebe2e | 6 | #include "Gamepad.h" |
el17mcd | 7:a3ccabdebe2e | 7 | #include "Projectile.h" |
el17mcd | 12:9e6d5d0a0c82 | 8 | #include "Tank.h" |
el17mcd | 12:9e6d5d0a0c82 | 9 | #include "Graphics.h" |
el17mcd | 15:fa5282fcd134 | 10 | #include "Menus.h" |
el17mcd | 16:a2c945279b79 | 11 | #include "Scores.h" |
el17mcd | 7:a3ccabdebe2e | 12 | |
el17mcd | 21:44e87d88afe2 | 13 | /** TanksEngine class |
el17mcd | 21:44e87d88afe2 | 14 | @brief Class that holds the main game loop and game mechanics. It governs the |
el17mcd | 21:44e87d88afe2 | 15 | the use of game inputs like player actions through controls and game |
el17mcd | 21:44e87d88afe2 | 16 | outputs like updating the lcd, lighting LEDs and playing piezo tones. |
el17mcd | 21:44e87d88afe2 | 17 | @version 1.0 |
el17mcd | 21:44e87d88afe2 | 18 | @author Maxim C. Delacoe |
el17mcd | 21:44e87d88afe2 | 19 | @date April 2019 |
el17mcd | 21:44e87d88afe2 | 20 | @code |
el17mcd | 21:44e87d88afe2 | 21 | |
el17mcd | 21:44e87d88afe2 | 22 | #include "mbed.h" |
el17mcd | 21:44e87d88afe2 | 23 | #include "Gamepad.h" |
el17mcd | 21:44e87d88afe2 | 24 | #include "N5110.h" |
el17mcd | 21:44e87d88afe2 | 25 | #include "Bitmap.h" |
el17mcd | 21:44e87d88afe2 | 26 | #include "TanksEngine.h" |
el17mcd | 21:44e87d88afe2 | 27 | #include "Tank.h" |
el17mcd | 21:44e87d88afe2 | 28 | #include "Projectile.h" |
el17mcd | 21:44e87d88afe2 | 29 | #include "Graphics.h" |
el17mcd | 21:44e87d88afe2 | 30 | #include "Menus.h" |
el17mcd | 21:44e87d88afe2 | 31 | #include "Scores.h" |
el17mcd | 21:44e87d88afe2 | 32 | |
el17mcd | 21:44e87d88afe2 | 33 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17mcd | 21:44e87d88afe2 | 34 | TanksEngine engine; |
el17mcd | 21:44e87d88afe2 | 35 | Gamepad pad; |
el17mcd | 21:44e87d88afe2 | 36 | Graphics graphics; |
el17mcd | 21:44e87d88afe2 | 37 | Menus menus; |
el17mcd | 21:44e87d88afe2 | 38 | Scores scores; |
el17mcd | 21:44e87d88afe2 | 39 | |
el17mcd | 21:44e87d88afe2 | 40 | int main() |
el17mcd | 21:44e87d88afe2 | 41 | { // need to initialise LCD and Gamepad |
el17mcd | 21:44e87d88afe2 | 42 | lcd.init(); |
el17mcd | 21:44e87d88afe2 | 43 | pad.init(); |
el17mcd | 21:44e87d88afe2 | 44 | |
el17mcd | 21:44e87d88afe2 | 45 | while(1) { // infinite loop |
el17mcd | 21:44e87d88afe2 | 46 | // Run start up screen and wait for player input. |
el17mcd | 21:44e87d88afe2 | 47 | menus.start_up_screen(graphics, lcd, pad); |
el17mcd | 21:44e87d88afe2 | 48 | // Run the menus and save any player preferences like mute enabled and |
el17mcd | 21:44e87d88afe2 | 49 | starting health for the tanks. |
el17mcd | 21:44e87d88afe2 | 50 | menus.main_menu(graphics, lcd, pad, scores); |
el17mcd | 21:44e87d88afe2 | 51 | // Initialise the game based on the players preferences. Setting starting |
el17mcd | 21:44e87d88afe2 | 52 | positions, health, etc. |
el17mcd | 21:44e87d88afe2 | 53 | engine.initgame(menus); |
el17mcd | 21:44e87d88afe2 | 54 | // Run the main game loop until a player reaches 0 health. |
el17mcd | 21:44e87d88afe2 | 55 | engine.game_loop(graphics, lcd, pad, menus, scores); |
el17mcd | 21:44e87d88afe2 | 56 | } |
el17mcd | 21:44e87d88afe2 | 57 | } |
el17mcd | 21:44e87d88afe2 | 58 | |
el17mcd | 21:44e87d88afe2 | 59 | @endcode |
el17mcd | 21:44e87d88afe2 | 60 | */ |
el17mcd | 7:a3ccabdebe2e | 61 | class TanksEngine |
el17mcd | 7:a3ccabdebe2e | 62 | { |
el17mcd | 16:a2c945279b79 | 63 | |
el17mcd | 7:a3ccabdebe2e | 64 | public: |
el17mcd | 21:44e87d88afe2 | 65 | // Constructor and destructor. |
el17mcd | 21:44e87d88afe2 | 66 | /** |
el17mcd | 21:44e87d88afe2 | 67 | * @brief Constructor |
el17mcd | 21:44e87d88afe2 | 68 | * @details Sets the movement limits and gravity's acceleration. |
el17mcd | 21:44e87d88afe2 | 69 | */ |
el17mcd | 17:cb39d9fa08dc | 70 | TanksEngine(); |
el17mcd | 21:44e87d88afe2 | 71 | /** |
el17mcd | 21:44e87d88afe2 | 72 | * @brief Destructor |
el17mcd | 21:44e87d88afe2 | 73 | * @details Non user specified. |
el17mcd | 21:44e87d88afe2 | 74 | */ |
el17mcd | 17:cb39d9fa08dc | 75 | ~TanksEngine(); |
el17mcd | 21:44e87d88afe2 | 76 | |
el17mcd | 21:44e87d88afe2 | 77 | /** |
el17mcd | 21:44e87d88afe2 | 78 | * @brief Resets member variables to their default values from previous games and sets the values specified in setting. |
el17mcd | 21:44e87d88afe2 | 79 | * @param &menus * @details The menus object from Menus class. |
el17mcd | 21:44e87d88afe2 | 80 | */ |
el17mcd | 17:cb39d9fa08dc | 81 | void initgame(Menus &menus); |
el17mcd | 21:44e87d88afe2 | 82 | /** |
el17mcd | 21:44e87d88afe2 | 83 | * @brief Runs the main gameloop updating the game as the player inputs controls and updating display based on game events. |
el17mcd | 21:44e87d88afe2 | 84 | * @param &graphics * @details The graphics object from Graphics class. |
el17mcd | 21:44e87d88afe2 | 85 | * @param &lcd * @details The lcd object from N5110 class. |
el17mcd | 21:44e87d88afe2 | 86 | * @param &pad * @details The pad object from Gamepad class. |
el17mcd | 21:44e87d88afe2 | 87 | * @param &menus * @details The menus object from Menus class. |
el17mcd | 21:44e87d88afe2 | 88 | * @param &scores * @details The scores object from Scores class. |
el17mcd | 21:44e87d88afe2 | 89 | */ |
el17mcd | 17:cb39d9fa08dc | 90 | void game_loop(Graphics &graphics, N5110 &lcd, Gamepad &pad, |
el17mcd | 17:cb39d9fa08dc | 91 | Menus &menus, Scores &scores); |
el17mcd | 7:a3ccabdebe2e | 92 | |
el17mcd | 7:a3ccabdebe2e | 93 | private: |
el17mcd | 12:9e6d5d0a0c82 | 94 | |
el17mcd | 16:a2c945279b79 | 95 | // Game Flow |
el17mcd | 17:cb39d9fa08dc | 96 | void _left_tank_turn(Graphics &graphics, Gamepad &pad); |
el17mcd | 17:cb39d9fa08dc | 97 | void _right_tank_turn(Graphics &graphics, Gamepad &pad); |
el17mcd | 17:cb39d9fa08dc | 98 | void _projectile_phase(N5110 &lcd, Gamepad &pad); |
el17mcd | 17:cb39d9fa08dc | 99 | void _change_turn(); |
el17mcd | 17:cb39d9fa08dc | 100 | void _end(Graphics &graphics, N5110 &lcd, Gamepad &pad, Scores &scores); |
el17mcd | 16:a2c945279b79 | 101 | // Game Mechanics |
el17mcd | 17:cb39d9fa08dc | 102 | void _tank_shoots(int x, int y, int turn, Gamepad &pad); |
el17mcd | 17:cb39d9fa08dc | 103 | void _object_hit(int x, int y, N5110 &lcd, Gamepad &pad); |
el17mcd | 17:cb39d9fa08dc | 104 | bool _collision_pl(Tank _tankl, Projectile _proj); |
el17mcd | 17:cb39d9fa08dc | 105 | bool _collision_pr(Tank _tankr, Projectile _proj); |
el17mcd | 17:cb39d9fa08dc | 106 | bool _collision_pm(int x, int y, N5110 &lcd, Projectile _proj); |
el17mcd | 16:a2c945279b79 | 107 | // Game Inputs |
el17mcd | 17:cb39d9fa08dc | 108 | void _read_input(Gamepad &pad); |
el17mcd | 17:cb39d9fa08dc | 109 | void _decrement_cooldowns(); |
el17mcd | 16:a2c945279b79 | 110 | // Game Outputs |
el17mcd | 17:cb39d9fa08dc | 111 | void _draw(Graphics graphics, N5110 &lcd); |
el17mcd | 17:cb39d9fa08dc | 112 | void _sounds(int n, Gamepad &pad); |
el17mcd | 16:a2c945279b79 | 113 | |
el17mcd | 17:cb39d9fa08dc | 114 | Tank _tankl; |
el17mcd | 17:cb39d9fa08dc | 115 | Tank _tankr; |
el17mcd | 7:a3ccabdebe2e | 116 | |
el17mcd | 17:cb39d9fa08dc | 117 | Projectile _proj; |
el17mcd | 16:a2c945279b79 | 118 | |
el17mcd | 17:cb39d9fa08dc | 119 | int _initial_health; |
el17mcd | 17:cb39d9fa08dc | 120 | int _cooldown_l; |
el17mcd | 17:cb39d9fa08dc | 121 | int _cooldown_r; |
el17mcd | 17:cb39d9fa08dc | 122 | int _move; |
el17mcd | 17:cb39d9fa08dc | 123 | int _turn; |
el17mcd | 17:cb39d9fa08dc | 124 | int _turn_counter; |
el17mcd | 17:cb39d9fa08dc | 125 | bool _fire; |
el17mcd | 17:cb39d9fa08dc | 126 | bool _mute; |
el17mcd | 17:cb39d9fa08dc | 127 | float _angle; |
el17mcd | 17:cb39d9fa08dc | 128 | float _mag; |
el17mcd | 17:cb39d9fa08dc | 129 | float _vel; |
el17mcd | 17:cb39d9fa08dc | 130 | float _grav; |
el17mcd | 17:cb39d9fa08dc | 131 | float _wind; |
el17mcd | 7:a3ccabdebe2e | 132 | }; |
el17mcd | 7:a3ccabdebe2e | 133 | |
el17mcd | 16:a2c945279b79 | 134 | #endif // TANKSENGINE_H |