ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Thu May 09 14:07:09 2019 +0000
Revision:
34:a9b14a4ccd46
Parent:
31:5c4acae51026
Finish adding comments to the engine file and started adding comments to the floors explaining each function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17m2h 2:360a6c301a4e 1 #ifndef ENGINE_H
el17m2h 2:360a6c301a4e 2 #define ENGINE_H
el17m2h 2:360a6c301a4e 3
el17m2h 22:0d2ac98a8b48 4 #include <iostream>
el17m2h 2:360a6c301a4e 5 #include "mbed.h"
el17m2h 2:360a6c301a4e 6 #include "N5110.h"
el17m2h 2:360a6c301a4e 7 #include "Gamepad.h"
el17m2h 2:360a6c301a4e 8 #include "Floors.h"
el17m2h 4:8ec314f806ae 9 #include "Doodler.h"
el17m2h 17:74de8c17ddac 10 #include "Bullet.h"
el17m2h 2:360a6c301a4e 11
el17m2h 24:67dc71a8f009 12 /** Engine class
el17m2h 24:67dc71a8f009 13 @brief Class for the engine of the game
el17m2h 24:67dc71a8f009 14 @author Melissa Hartmann
el17m2h 24:67dc71a8f009 15 @date May 2019
el17m2h 24:67dc71a8f009 16 */
el17m2h 29:15e9640646b7 17 class Engine
el17m2h 29:15e9640646b7 18 {
el17m2h 2:360a6c301a4e 19 public:
el17m2h 2:360a6c301a4e 20 Engine();
el17m2h 2:360a6c301a4e 21 ~Engine();
el17m2h 29:15e9640646b7 22
el17m2h 29:15e9640646b7 23 /**
el17m2h 29:15e9640646b7 24 @brief Initializing game
el17m2h 29:15e9640646b7 25 @details The function defines the screen's size, and the initial position of the doodler,
el17m2h 34:a9b14a4ccd46 26 the bullet and the 6 floors. This is done by inputing the functions of initialization to the
el17m2h 26:d16a5b1e0ace 27 corresponding class. A for loop is used to repeat the process to create 6 floors, by defining
el17m2h 26:d16a5b1e0ace 28 them as an array of 6 objects.
el17m2h 24:67dc71a8f009 29 */
el17m2h 26:d16a5b1e0ace 30 void init();
el17m2h 29:15e9640646b7 31
el17m2h 29:15e9640646b7 32 /**
el17m2h 26:d16a5b1e0ace 33 @brief Initializes the screen's size and the initial score
el17m2h 29:15e9640646b7 34 @details The function defines the screen's position, height and width (which remains the same
el17m2h 29:15e9640646b7 35 throughout the game). It also sets an initial score of 0.
el17m2h 24:67dc71a8f009 36 */
el17m2h 23:9be87557b89a 37 void screen_init();
el17m2h 29:15e9640646b7 38
el17m2h 29:15e9640646b7 39 /**
el17m2h 26:d16a5b1e0ace 40 @brief Sets the inputed gamepad parameters
el17m2h 29:15e9640646b7 41 @param Gamepad &pad
el17m2h 29:15e9640646b7 42 @details The function sets the inputed gamepad parameters for the direction of the joystick,
el17m2h 34:a9b14a4ccd46 43 its magnitude of inclination and checks if the button Y is pressed.
el17m2h 24:67dc71a8f009 44 */
el17m2h 5:8814d6de77d0 45 void read_input(Gamepad &pad);
el17m2h 29:15e9640646b7 46
el17m2h 29:15e9640646b7 47 /**
el17m2h 26:d16a5b1e0ace 48 @brief Prints the doodler, floors and bullet
el17m2h 26:d16a5b1e0ace 49 @param N5110 &lcd
el17m2h 26:d16a5b1e0ace 50 @details The function calls the draw functions for the doodler, floors and bullet. For the floors, a
el17m2h 26:d16a5b1e0ace 51 for loop is used to repeat the process to draw the 6 floors. It also draws the screen rectangle and the score.
el17m2h 26:d16a5b1e0ace 52 */
el17m2h 26:d16a5b1e0ace 53 void draw(N5110 &lcd);
el17m2h 29:15e9640646b7 54
el17m2h 29:15e9640646b7 55 /**
el17m2h 26:d16a5b1e0ace 56 @brief Updates the classes in the game by calling it's update functions
el17m2h 26:d16a5b1e0ace 57 @param Gamepad &pad
el17m2h 29:15e9640646b7 58 @details Firstly, the function checks the collision of the doodler with the floors by getting their current positions
el17m2h 27:af14fcd5a520 59 and inputing them to the "check_floors_collision" function, which will update the velocity of the doodler. Secondly, it
el17m2h 29:15e9640646b7 60 calls the "update" functions for the floors and doodler which will update their positions. It then calls the
el17m2h 27:af14fcd5a520 61 "add_score" function to update the score value. Next, it checks if the doodler fire's by inputing the doodler's
el17m2h 27:af14fcd5a520 62 previous position and updated position, as well as the pad's input. Finally, it calls a function to check if the doodler
el17m2h 27:af14fcd5a520 63 has reached the bottom of the screen to end the game.
el17m2h 24:67dc71a8f009 64 */
el17m2h 5:8814d6de77d0 65 void update(Gamepad &pad);
el17m2h 29:15e9640646b7 66
el17m2h 29:15e9640646b7 67 /**
el17m2h 26:d16a5b1e0ace 68 @brief Checks a collision and sets the doodler's velocity (if it jumps) direction
el17m2h 29:15e9640646b7 69 @param Gamepad &pad to make a sound each time the doodler jumps
el17m2h 26:d16a5b1e0ace 70 @param float doodler_pos_x
el17m2h 26:d16a5b1e0ace 71 @param float doodler_pos_y
el17m2h 26:d16a5b1e0ace 72 @param double doodler_vel_y
el17m2h 29:15e9640646b7 73 @details Checks a collision by comparing the doodler's position with the floors' ones with 3 conditions:
el17m2h 29:15e9640646b7 74 if the doodler is moving downwards (its velocity is positive), if the doodler's x-coordinate is within the
el17m2h 34:a9b14a4ccd46 75 floor's x-coordinate and if the floors' y-coordinate is equal or +2/-1 bits of the doodler's feet.
el17m2h 29:15e9640646b7 76 If the conditions are met, it means there is a collision, which means the doodler's velocity will change direction
el17m2h 29:15e9640646b7 77 (will be set to a negative value). The function also checks the doodler's velocity function to update the velocity
el17m2h 26:d16a5b1e0ace 78 accordingly (make it increase or decrease by multiplying it with constant values) so that the doodler accelerates
el17m2h 29:15e9640646b7 79 or decelerates according to its current velocity.
el17m2h 24:67dc71a8f009 80 */
el17m2h 29:15e9640646b7 81 void check_floors_collision(Gamepad &pad, float doodler_pos_x, float doodler_pos_y, double doodler_vel_y);
el17m2h 29:15e9640646b7 82
el17m2h 29:15e9640646b7 83 /**
el17m2h 26:d16a5b1e0ace 84 @brief Checks if the doodler fires
el17m2h 26:d16a5b1e0ace 85 @param Gamepad &pad
el17m2h 26:d16a5b1e0ace 86 @param float updated_doodler_pos_x
el17m2h 26:d16a5b1e0ace 87 @param float updated_doodler_pos_y
el17m2h 26:d16a5b1e0ace 88 @param float doodler_pos_y
el17m2h 29:15e9640646b7 89 @details The function checks the bullet's position and the input of the user (if the
el17m2h 29:15e9640646b7 90 Y button has been pressed). If the bullet is not currently fired (its position
el17m2h 29:15e9640646b7 91 equals the current doodler's trunk position) and the button Y is pressed, then the bullet is
el17m2h 26:d16a5b1e0ace 92 updated to a new position (upwards) and the pad makes a sound indicating it is shooting. If
el17m2h 29:15e9640646b7 93 it is currently above the doodler's position, it means it has been fired, so continues being
el17m2h 29:15e9640646b7 94 updated. The update function will eventually check if the bullet has reached the end top of the
el17m2h 26:d16a5b1e0ace 95 screen, which will make the bullet return to the doodler's trunk's position. If this is the case
el17m2h 29:15e9640646b7 96 and the button Y has not been pressed, the bullet remains in the doodler's position (by inputing
el17m2h 26:d16a5b1e0ace 97 the doodler's position to the initialize function instead of the update one).
el17m2h 24:67dc71a8f009 98 */
el17m2h 23:9be87557b89a 99 void check_fire(Gamepad &pad, float updated_doodler_pos_x, float updated_doodler_pos_y, float doodler_pos_y);
el17m2h 34:a9b14a4ccd46 100
el17m2h 29:15e9640646b7 101 /**
el17m2h 26:d16a5b1e0ace 102 @brief Increases score by checking the position of the floor
el17m2h 29:15e9640646b7 103 @details The function uses a foor loop to check the current position of every floor and
el17m2h 29:15e9640646b7 104 if they have reached the top of the screen (meaning they have re-appeared at the top, the
el17m2h 26:d16a5b1e0ace 105 score will have one point added. This means that the score depends on how many floors re-appear,
el17m2h 29:15e9640646b7 106 which represents how high the doodler has moved upwards.
el17m2h 24:67dc71a8f009 107 */
el17m2h 21:6b16ca9834e6 108 void add_score();
el17m2h 24:67dc71a8f009 109
el17m2h 29:15e9640646b7 110 /**
el17m2h 29:15e9640646b7 111 @brief Sets the decision statement to end the game depending on the doodler's y-coordinate position
el17m2h 29:15e9640646b7 112 @param float doodler_pos_y
el17m2h 29:15e9640646b7 113 @details The function sets the decision statement to end the game by using an if statement dependent on
el17m2h 29:15e9640646b7 114 the read doodler's position. If the position has reached the bottom of the screen, the game should end, so
el17m2h 29:15e9640646b7 115 the decision will be true.
el17m2h 29:15e9640646b7 116 */
el17m2h 27:af14fcd5a520 117 void set_game_over(float doodler_pos_y);
el17m2h 34:a9b14a4ccd46 118
el17m2h 34:a9b14a4ccd46 119 /**
el17m2h 34:a9b14a4ccd46 120 @brief Function called on main.cpp to fix the limitation of the score addition and avoid an infinite addition
el17m2h 34:a9b14a4ccd46 121 @details The function will subtract one score value within the engine class when called on the main function. It is
el17m2h 34:a9b14a4ccd46 122 called if the a floor remains at the same position (that makes it add points to the score) for more than one update.
el17m2h 34:a9b14a4ccd46 123 */
el17m2h 34:a9b14a4ccd46 124 void subtract_score();
el17m2h 34:a9b14a4ccd46 125
el17m2h 34:a9b14a4ccd46 126 /**
el17m2h 34:a9b14a4ccd46 127 @brief Returns the current score
el17m2h 34:a9b14a4ccd46 128 @details The function returns the current score of the game. It is called in the main.cpp to display
el17m2h 34:a9b14a4ccd46 129 the final score on the game over screen.
el17m2h 34:a9b14a4ccd46 130 */
el17m2h 34:a9b14a4ccd46 131 int get_score();
el17m2h 29:15e9640646b7 132
el17m2h 29:15e9640646b7 133 /**
el17m2h 34:a9b14a4ccd46 134 @brief Returns the decision to fix the score addition of the score limitation.
el17m2h 34:a9b14a4ccd46 135 @details The function is called in the main.cpp to check if the score needs to be substracted or not.
el17m2h 34:a9b14a4ccd46 136 */
el17m2h 34:a9b14a4ccd46 137 bool get_score_check();
el17m2h 34:a9b14a4ccd46 138
el17m2h 34:a9b14a4ccd46 139 /**
el17m2h 34:a9b14a4ccd46 140 @brief Returns the decision to end the game
el17m2h 34:a9b14a4ccd46 141 @details The function is called in the main.cpp to check if the user loose the game and depending on the true/false
el17m2h 34:a9b14a4ccd46 142 statement the main.cpp will display the game over screen.
el17m2h 29:15e9640646b7 143 */
el17m2h 27:af14fcd5a520 144 bool get_game_over();
el17m2h 29:15e9640646b7 145
el17m2h 19:5a7b0cdf013b 146 private:
el17m2h 34:a9b14a4ccd46 147 //////////////////// OBJECTS //////////////////////////
el17m2h 34:a9b14a4ccd46 148 Bullet _b;
el17m2h 34:a9b14a4ccd46 149 Floors _f[6]; // array of 6 floors
el17m2h 34:a9b14a4ccd46 150 Doodler _dood;
el17m2h 34:a9b14a4ccd46 151
el17m2h 34:a9b14a4ccd46 152 ////////////////// VARIABLES //////////////////////////
el17m2h 21:6b16ca9834e6 153 int _score;
el17m2h 29:15e9640646b7 154 bool _score_check;
el17m2h 31:5c4acae51026 155 bool enemy_collision[6];
el17m2h 27:af14fcd5a520 156 bool game_ends;
el17m2h 34:a9b14a4ccd46 157 int _screen_pos_x;
el17m2h 34:a9b14a4ccd46 158 int _screen_pos_y;
el17m2h 34:a9b14a4ccd46 159 int _screen_width;
el17m2h 34:a9b14a4ccd46 160 int _screen_height;
el17m2h 34:a9b14a4ccd46 161 Direction _d;
el17m2h 34:a9b14a4ccd46 162 float _mag;
el17m2h 34:a9b14a4ccd46 163 bool _button_Y;
el17m2h 2:360a6c301a4e 164 int _floors_height;
el17m2h 2:360a6c301a4e 165 int _floors_width;
el17m2h 34:a9b14a4ccd46 166 Vector2D _f_pos[6]; // x and y positions of the floors
el17m2h 24:67dc71a8f009 167 float doodler_pos_x;
el17m2h 24:67dc71a8f009 168 float doodler_pos_y;
el17m2h 24:67dc71a8f009 169 float updated_doodler_pos_x;
el17m2h 24:67dc71a8f009 170 float updated_doodler_pos_y;
el17m2h 24:67dc71a8f009 171 float doodler_vel_x;
el17m2h 29:15e9640646b7 172 double doodler_vel_y;
el17m2h 34:a9b14a4ccd46 173 float _b_pos_x;
el17m2h 34:a9b14a4ccd46 174 float _b_pos_y;
el17m2h 2:360a6c301a4e 175 };
el17m2h 2:360a6c301a4e 176 #endif