ELEC2645 (2018/19) / Mbed 2 deprecated EL17MCD

Dependencies:   mbed

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?

UserRevisionLine numberNew contents of line
el17mcd 12:9e6d5d0a0c82 1 #ifndef TANK_H
el17mcd 12:9e6d5d0a0c82 2 #define TANK_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 /** Tank Class
el17mcd 21:44e87d88afe2 9 * @brief Generates a tank object that can move position and generate 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 "Tank.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 Tank _tank;
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 int i = 0;
el17mcd 21:44e87d88afe2 29 _tank.set_position(15, 0); // Set initial position
el17mcd 21:44e87d88afe2 30 _tank.set_movement_limits(0, 73); // Set movement limits within lcd
el17mcd 21:44e87d88afe2 31 _tank.set_speed(-25); // slow speed down
el17mcd 21:44e87d88afe2 32
el17mcd 21:44e87d88afe2 33 while (1) {
el17mcd 21:44e87d88afe2 34
el17mcd 21:44e87d88afe2 35 if (i < 1499) {
el17mcd 21:44e87d88afe2 36 _tank.move_position(1); // tank moves right for the first 1500 iterations
el17mcd 21:44e87d88afe2 37 } else {
el17mcd 21:44e87d88afe2 38 _tank.move_position(-1); // otherwise the tank moves left
el17mcd 21:44e87d88afe2 39 }
el17mcd 21:44e87d88afe2 40 _tank.generate_hitbox(); // update the objects hitbox
el17mcd 21:44e87d88afe2 41
el17mcd 21:44e87d88afe2 42 lcd.clear();
el17mcd 21:44e87d88afe2 43 int x = _tank.get_position_x();
el17mcd 21:44e87d88afe2 44 int y = _tank.get_position_y(); // get the tank's position and update sprite
el17mcd 21:44e87d88afe2 45 _graphics.draw_tank_l(x, y, lcd);
el17mcd 21:44e87d88afe2 46 _graphics.draw_turret_l(x, y, 90, lcd) ;
el17mcd 21:44e87d88afe2 47 lcd.refresh();
el17mcd 21:44e87d88afe2 48
el17mcd 21:44e87d88afe2 49 i++;
el17mcd 21:44e87d88afe2 50 if (i > 3000) { // reset incrementer
el17mcd 21:44e87d88afe2 51 i = 0;
el17mcd 21:44e87d88afe2 52 wait_ms(1000/60);
el17mcd 21:44e87d88afe2 53 }
el17mcd 21:44e87d88afe2 54 }
el17mcd 21:44e87d88afe2 55 }
el17mcd 21:44e87d88afe2 56
el17mcd 21:44e87d88afe2 57 @endcode
el17mcd 21:44e87d88afe2 58 */
el17mcd 21:44e87d88afe2 59
el17mcd 12:9e6d5d0a0c82 60 class Tank
el17mcd 7:a3ccabdebe2e 61 {
el17mcd 16:a2c945279b79 62
el17mcd 7:a3ccabdebe2e 63 public:
el17mcd 21:44e87d88afe2 64 // Constructor and destructor.
el17mcd 21:44e87d88afe2 65 /**
el17mcd 21:44e87d88afe2 66 * @brief Constructor
el17mcd 21:44e87d88afe2 67 * @details Sets the default movement speed.
el17mcd 21:44e87d88afe2 68 */
el17mcd 17:cb39d9fa08dc 69 Tank();
el17mcd 21:44e87d88afe2 70 /**
el17mcd 21:44e87d88afe2 71 * @brief Destructor
el17mcd 21:44e87d88afe2 72 * @details Non user specified.
el17mcd 21:44e87d88afe2 73 */
el17mcd 17:cb39d9fa08dc 74 ~Tank();
el17mcd 17:cb39d9fa08dc 75
el17mcd 17:cb39d9fa08dc 76 //Accessors
el17mcd 21:44e87d88afe2 77 /**
el17mcd 21:44e87d88afe2 78 * @brief Gets the tank's position in the x direction.
el17mcd 21:44e87d88afe2 79 * @returns the tank's position in the x direction
el17mcd 21:44e87d88afe2 80 */
el17mcd 17:cb39d9fa08dc 81 int get_position_x();
el17mcd 21:44e87d88afe2 82 /**
el17mcd 21:44e87d88afe2 83 * @brief Gets the tank's position in the y direction.
el17mcd 21:44e87d88afe2 84 * @returns the tank's position in the y direction
el17mcd 21:44e87d88afe2 85 */
el17mcd 17:cb39d9fa08dc 86 int get_position_y();
el17mcd 21:44e87d88afe2 87 /**
el17mcd 21:44e87d88afe2 88 * @brief Gets the tank's ith element of its hitbox array.
el17mcd 21:44e87d88afe2 89 * @returns a single hitbox integer value
el17mcd 21:44e87d88afe2 90 */
el17mcd 17:cb39d9fa08dc 91 int get_hitbox(int i);
el17mcd 21:44e87d88afe2 92 /**
el17mcd 21:44e87d88afe2 93 * @brief Gets the value of the tank's health.
el17mcd 21:44e87d88afe2 94 * @returns The tank's health value.
el17mcd 21:44e87d88afe2 95 */
el17mcd 17:cb39d9fa08dc 96 int get_health();
el17mcd 17:cb39d9fa08dc 97 //Mutators
el17mcd 21:44e87d88afe2 98 /**
el17mcd 21:44e87d88afe2 99 * @brief Sets the x and y positions of the tank.
el17mcd 21:44e87d88afe2 100 * @param x @details The tank's position in the x direction
el17mcd 21:44e87d88afe2 101 * @param y @details The tank's position in the y direction
el17mcd 21:44e87d88afe2 102 */
el17mcd 17:cb39d9fa08dc 103 void set_position(int x, int y);
el17mcd 21:44e87d88afe2 104 /**
el17mcd 21:44e87d88afe2 105 * @brief Sets the minimum and maximum positions the tank can hold in the x direction.
el17mcd 21:44e87d88afe2 106 * @param left @details The tank's minimum position in the x direction
el17mcd 21:44e87d88afe2 107 * @param right @details The tank's maximum position in the y direction
el17mcd 21:44e87d88afe2 108 */
el17mcd 17:cb39d9fa08dc 109 void set_movement_limits(int left, int right);
el17mcd 21:44e87d88afe2 110 /**
el17mcd 21:44e87d88afe2 111 * @brief Sets the tank's movement speed.
el17mcd 21:44e87d88afe2 112 * @param s @details The speed of the tank's movement
el17mcd 21:44e87d88afe2 113 */
el17mcd 21:44e87d88afe2 114 void Tank::set_speed(int s);
el17mcd 21:44e87d88afe2 115 /**
el17mcd 21:44e87d88afe2 116 * @brief Sets the tank's health.
el17mcd 21:44e87d88afe2 117 * @param h @details The value of the tank's health
el17mcd 21:44e87d88afe2 118 */
el17mcd 17:cb39d9fa08dc 119 void set_health(int h);
el17mcd 21:44e87d88afe2 120
el17mcd 21:44e87d88afe2 121 //Member Methods
el17mcd 21:44e87d88afe2 122 /**
el17mcd 21:44e87d88afe2 123 * @brief Moves the tank left or right depending on the whether the input is negative or posititive.
el17mcd 21:44e87d88afe2 124 * @param d @details The direction the tank should move
el17mcd 21:44e87d88afe2 125 */
el17mcd 17:cb39d9fa08dc 126 void move_position(int d);
el17mcd 21:44e87d88afe2 127 /**
el17mcd 21:44e87d88afe2 128 * @brief Decrements the tank's health.
el17mcd 21:44e87d88afe2 129 */
el17mcd 17:cb39d9fa08dc 130 void lose_health();
el17mcd 21:44e87d88afe2 131 /**
el17mcd 21:44e87d88afe2 132 * @brief Fills a 40 integer array with all the values associated with the tank's area.
el17mcd 21:44e87d88afe2 133 */
el17mcd 17:cb39d9fa08dc 134 void generate_hitbox();
el17mcd 17:cb39d9fa08dc 135
el17mcd 17:cb39d9fa08dc 136 private:
el17mcd 16:a2c945279b79 137
el17mcd 17:cb39d9fa08dc 138 int _position_x;
el17mcd 17:cb39d9fa08dc 139 int _position_y;
el17mcd 17:cb39d9fa08dc 140 int _left_lim;
el17mcd 17:cb39d9fa08dc 141 int _right_lim;
el17mcd 17:cb39d9fa08dc 142 int _move_counter;
el17mcd 17:cb39d9fa08dc 143 int _speed;
el17mcd 17:cb39d9fa08dc 144 int _health;
el17mcd 17:cb39d9fa08dc 145 int _hitbox[40];
el17mcd 7:a3ccabdebe2e 146 };
el17mcd 7:a3ccabdebe2e 147
el17mcd 12:9e6d5d0a0c82 148 #endif // TANK_H