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
Projectile/Projectile.h@21:44e87d88afe2, 2019-05-09 (annotated)
- Committer:
- el17mcd
- Date:
- Thu May 09 13:10:16 2019 +0000
- Revision:
- 21:44e87d88afe2
- Parent:
- 17:cb39d9fa08dc
Doxygen comments added.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17mcd | 7:a3ccabdebe2e | 1 | #ifndef PROJECTILE_H |
| el17mcd | 7:a3ccabdebe2e | 2 | #define PROJECTILE_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 | |
| el17mcd | 21:44e87d88afe2 | 8 | /** Projectile Class |
| el17mcd | 21:44e87d88afe2 | 9 | * @brief Generates a projectile object that has ballistic motion and generates a hitbox. |
| el17mcd | 21:44e87d88afe2 | 10 | * @author Maxim C. Delacoe |
| el17mcd | 21:44e87d88afe2 | 11 | * @date April 2019 |
| el17mcd | 21:44e87d88afe2 | 12 | |
| el17mcd | 21:44e87d88afe2 | 13 | @code |
| el17mcd | 21:44e87d88afe2 | 14 | |
| el17mcd | 21:44e87d88afe2 | 15 | #include "mbed.h" |
| el17mcd | 21:44e87d88afe2 | 16 | #include "N5110.h" |
| el17mcd | 21:44e87d88afe2 | 17 | #include "Projectile.h" |
| el17mcd | 21:44e87d88afe2 | 18 | #include "Graphics.h" |
| el17mcd | 21:44e87d88afe2 | 19 | |
| el17mcd | 21:44e87d88afe2 | 20 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| el17mcd | 21:44e87d88afe2 | 21 | Projectile _proj; |
| el17mcd | 21:44e87d88afe2 | 22 | Graphics _graphics; |
| el17mcd | 21:44e87d88afe2 | 23 | |
| el17mcd | 21:44e87d88afe2 | 24 | |
| el17mcd | 21:44e87d88afe2 | 25 | int main() { |
| el17mcd | 21:44e87d88afe2 | 26 | |
| el17mcd | 21:44e87d88afe2 | 27 | lcd.init(); |
| el17mcd | 21:44e87d88afe2 | 28 | |
| el17mcd | 21:44e87d88afe2 | 29 | while (1) { |
| el17mcd | 21:44e87d88afe2 | 30 | |
| el17mcd | 21:44e87d88afe2 | 31 | srand(time(0)); // seed rand fucntion |
| el17mcd | 21:44e87d88afe2 | 32 | int angle = rand() % 90; // randomise launch angle from 0 to 90 degrees |
| el17mcd | 21:44e87d88afe2 | 33 | _proj.set_launch_parameters(0, 0, angle, 1.3, 0.02, 0); // set launch parameters including randomised launch angle |
| el17mcd | 21:44e87d88afe2 | 34 | |
| el17mcd | 21:44e87d88afe2 | 35 | for (int i = 0; i < 300; i++) { |
| el17mcd | 21:44e87d88afe2 | 36 | |
| el17mcd | 21:44e87d88afe2 | 37 | _proj.update_flight(); |
| el17mcd | 21:44e87d88afe2 | 38 | _proj.generate_hitbox(); |
| el17mcd | 21:44e87d88afe2 | 39 | |
| el17mcd | 21:44e87d88afe2 | 40 | lcd.clear(); |
| el17mcd | 21:44e87d88afe2 | 41 | int x = _proj.get_position_x(); |
| el17mcd | 21:44e87d88afe2 | 42 | int y = _proj.get_position_y(); // get the projectile's position and update sprite |
| el17mcd | 21:44e87d88afe2 | 43 | _graphics.draw_projectile(x, y, lcd); |
| el17mcd | 21:44e87d88afe2 | 44 | lcd.refresh(); |
| el17mcd | 21:44e87d88afe2 | 45 | |
| el17mcd | 21:44e87d88afe2 | 46 | wait_ms(1000/60); |
| el17mcd | 21:44e87d88afe2 | 47 | } |
| el17mcd | 21:44e87d88afe2 | 48 | } |
| el17mcd | 21:44e87d88afe2 | 49 | } |
| el17mcd | 21:44e87d88afe2 | 50 | @endcode |
| el17mcd | 21:44e87d88afe2 | 51 | */ |
| el17mcd | 7:a3ccabdebe2e | 52 | |
| el17mcd | 7:a3ccabdebe2e | 53 | class Projectile |
| el17mcd | 7:a3ccabdebe2e | 54 | { |
| el17mcd | 16:a2c945279b79 | 55 | |
| el17mcd | 7:a3ccabdebe2e | 56 | public: |
| el17mcd | 21:44e87d88afe2 | 57 | // Constructor and destructor. |
| el17mcd | 21:44e87d88afe2 | 58 | /** |
| el17mcd | 21:44e87d88afe2 | 59 | * @brief Constructor |
| el17mcd | 21:44e87d88afe2 | 60 | * @details Non user specified. |
| el17mcd | 21:44e87d88afe2 | 61 | */ |
| el17mcd | 17:cb39d9fa08dc | 62 | Projectile(); |
| el17mcd | 21:44e87d88afe2 | 63 | /** |
| el17mcd | 21:44e87d88afe2 | 64 | * @brief Destructor |
| el17mcd | 21:44e87d88afe2 | 65 | * @details Non user specified. |
| el17mcd | 21:44e87d88afe2 | 66 | */ |
| el17mcd | 17:cb39d9fa08dc | 67 | ~Projectile(); |
| el17mcd | 16:a2c945279b79 | 68 | |
| el17mcd | 17:cb39d9fa08dc | 69 | // Accessors |
| el17mcd | 21:44e87d88afe2 | 70 | /** |
| el17mcd | 21:44e87d88afe2 | 71 | * @brief Gets the projectile's position in the x direction. |
| el17mcd | 21:44e87d88afe2 | 72 | * @returns the projectile's position in the x direction |
| el17mcd | 21:44e87d88afe2 | 73 | */ |
| el17mcd | 17:cb39d9fa08dc | 74 | int get_position_x(); |
| el17mcd | 21:44e87d88afe2 | 75 | /** |
| el17mcd | 21:44e87d88afe2 | 76 | * @brief Gets the projectile's position in the y direction. |
| el17mcd | 21:44e87d88afe2 | 77 | * @returns the projectile's position in the y direction |
| el17mcd | 21:44e87d88afe2 | 78 | */ |
| el17mcd | 17:cb39d9fa08dc | 79 | int get_position_y(); |
| el17mcd | 21:44e87d88afe2 | 80 | /** |
| el17mcd | 21:44e87d88afe2 | 81 | * @brief Gets the projectile's ith element of its hitbox array. |
| el17mcd | 21:44e87d88afe2 | 82 | * @returns a single hitbox integer value |
| el17mcd | 21:44e87d88afe2 | 83 | */ |
| el17mcd | 17:cb39d9fa08dc | 84 | int get_hitbox(int i); |
| el17mcd | 17:cb39d9fa08dc | 85 | // Mutators |
| el17mcd | 21:44e87d88afe2 | 86 | /** |
| el17mcd | 21:44e87d88afe2 | 87 | * @brief Sets the launch parameters for the projectile's ballistic motion. |
| el17mcd | 21:44e87d88afe2 | 88 | * @param x @details The projectile's position in the x direction |
| el17mcd | 21:44e87d88afe2 | 89 | * @param y @details The projectile's position in the y direction |
| el17mcd | 21:44e87d88afe2 | 90 | * @param ang @details The projectile's angle of launch from the ground |
| el17mcd | 21:44e87d88afe2 | 91 | * @param vel @details The projectile's initial velocity |
| el17mcd | 21:44e87d88afe2 | 92 | * @param grav @details The projectile's acceleration in the y direction due to gravity |
| el17mcd | 21:44e87d88afe2 | 93 | * @param wind @details The projectile's acceleration in the x direction due to wind |
| el17mcd | 21:44e87d88afe2 | 94 | */ |
| el17mcd | 17:cb39d9fa08dc | 95 | void set_launch_parameters(int x, int y, float ang, float vel, |
| el17mcd | 17:cb39d9fa08dc | 96 | float grav, float wind); |
| el17mcd | 17:cb39d9fa08dc | 97 | // Other Methods |
| el17mcd | 21:44e87d88afe2 | 98 | /** |
| el17mcd | 21:44e87d88afe2 | 99 | * @brief Fills a 4 integer array with all the values associated with the projectile's area. |
| el17mcd | 21:44e87d88afe2 | 100 | */ |
| el17mcd | 17:cb39d9fa08dc | 101 | void generate_hitbox(); |
| el17mcd | 21:44e87d88afe2 | 102 | /** |
| el17mcd | 21:44e87d88afe2 | 103 | * @brief Increments the time component of the equations for ballistic mothion and updates x and y positions. |
| el17mcd | 21:44e87d88afe2 | 104 | */ |
| el17mcd | 17:cb39d9fa08dc | 105 | void update_flight(); |
| el17mcd | 21:44e87d88afe2 | 106 | /** |
| el17mcd | 21:44e87d88afe2 | 107 | * @brief Checks whether the projectile has fallen outside the left, bottom or right of the screen. |
| el17mcd | 21:44e87d88afe2 | 108 | * @returns a boolean true if it is out of bounds |
| el17mcd | 21:44e87d88afe2 | 109 | */ |
| el17mcd | 17:cb39d9fa08dc | 110 | bool check_boundaries(); |
| el17mcd | 7:a3ccabdebe2e | 111 | |
| el17mcd | 7:a3ccabdebe2e | 112 | private: |
| el17mcd | 7:a3ccabdebe2e | 113 | |
| el17mcd | 17:cb39d9fa08dc | 114 | int _position_x; |
| el17mcd | 17:cb39d9fa08dc | 115 | int _position_y; |
| el17mcd | 17:cb39d9fa08dc | 116 | int _hitbox[5]; |
| el17mcd | 17:cb39d9fa08dc | 117 | int _init_x; |
| el17mcd | 17:cb39d9fa08dc | 118 | int _init_y; |
| el17mcd | 17:cb39d9fa08dc | 119 | float _time; |
| el17mcd | 17:cb39d9fa08dc | 120 | float _lnch_ang; |
| el17mcd | 17:cb39d9fa08dc | 121 | float _init_vel; |
| el17mcd | 17:cb39d9fa08dc | 122 | float _grav_acc; |
| el17mcd | 17:cb39d9fa08dc | 123 | float _wind_acc; |
| el17mcd | 7:a3ccabdebe2e | 124 | }; |
| el17mcd | 7:a3ccabdebe2e | 125 | |
| el17mcd | 7:a3ccabdebe2e | 126 | #endif // PROJECTILE_H |