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
Enemy/Enemy.h@5:8bd09c675f28, 2019-05-08 (annotated)
- Committer:
- adat80
- Date:
- Wed May 08 19:41:10 2019 +0000
- Revision:
- 5:8bd09c675f28
- Parent:
- 3:97cd7b3d89d0
- Child:
- 6:f06ce4cf068a
Full Game prior to unused method clean-up;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| adat80 | 1:3916f272663e | 1 | #ifndef ENEMY_H |
| adat80 | 1:3916f272663e | 2 | #define ENEMY_H |
| adat80 | 1:3916f272663e | 3 | |
| adat80 | 1:3916f272663e | 4 | #include "mbed.h" |
| adat80 | 1:3916f272663e | 5 | #include "N5110.h" |
| adat80 | 1:3916f272663e | 6 | #include "Gamepad.h" |
| adat80 | 1:3916f272663e | 7 | #include <time.h> |
| adat80 | 1:3916f272663e | 8 | |
| adat80 | 1:3916f272663e | 9 | |
| adat80 | 1:3916f272663e | 10 | /** CrossHairs Class |
| adat80 | 1:3916f272663e | 11 | @author Adam Jones, University of Leeds |
| adat80 | 1:3916f272663e | 12 | @brief Controls the Enemy Sprites in the Wall Defence game |
| adat80 | 1:3916f272663e | 13 | @date April 2017 |
| adat80 | 5:8bd09c675f28 | 14 | @brief Revision 1.0 |
| adat80 | 5:8bd09c675f28 | 15 | |
| adat80 | 5:8bd09c675f28 | 16 | @code |
| adat80 | 5:8bd09c675f28 | 17 | #include "Enemy.h" |
| adat80 | 5:8bd09c675f28 | 18 | #include "N5110.h" |
| adat80 | 5:8bd09c675f28 | 19 | |
| adat80 | 5:8bd09c675f28 | 20 | |
| adat80 | 5:8bd09c675f28 | 21 | int main() |
| adat80 | 5:8bd09c675f28 | 22 | { |
| adat80 | 5:8bd09c675f28 | 23 | //initialise |
| adat80 | 5:8bd09c675f28 | 24 | float timeToAttack = 0.0; //time at which the enemy will begin to move forwards |
| adat80 | 5:8bd09c675f28 | 25 | float speed = 0.5; //speed in pixels per frame at which enemy will move forwards |
| adat80 | 5:8bd09c675f28 | 26 | Enemy _enemy; |
| adat80 | 5:8bd09c675f28 | 27 | _enemy.init(timeToAttack, speed); |
| adat80 | 5:8bd09c675f28 | 28 | |
| adat80 | 5:8bd09c675f28 | 29 | //set action |
| adat80 | 5:8bd09c675f28 | 30 | Action myAc = moving; //Action enumerator |
| adat80 | 5:8bd09c675f28 | 31 | _enemy.set_current_action(myAc); //set enemies current action |
| adat80 | 5:8bd09c675f28 | 32 | |
| adat80 | 5:8bd09c675f28 | 33 | //draw enemy |
| adat80 | 5:8bd09c675f28 | 34 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| adat80 | 5:8bd09c675f28 | 35 | lcd.init(); |
| adat80 | 5:8bd09c675f28 | 36 | _enemy.draw(lcd); //draw enemy sprite to display |
| adat80 | 5:8bd09c675f28 | 37 | |
| adat80 | 5:8bd09c675f28 | 38 | |
| adat80 | 5:8bd09c675f28 | 39 | |
| adat80 | 5:8bd09c675f28 | 40 | |
| adat80 | 5:8bd09c675f28 | 41 | } |
| adat80 | 5:8bd09c675f28 | 42 | |
| adat80 | 5:8bd09c675f28 | 43 | @endcode |
| adat80 | 5:8bd09c675f28 | 44 | |
| adat80 | 5:8bd09c675f28 | 45 | |
| adat80 | 1:3916f272663e | 46 | */ |
| adat80 | 2:88019d96e1da | 47 | |
| adat80 | 5:8bd09c675f28 | 48 | //action enumerator keeps track of enemies current state |
| adat80 | 5:8bd09c675f28 | 49 | //and allows for quick routing to correct process via switch state |
| adat80 | 5:8bd09c675f28 | 50 | enum Action { |
| adat80 | 5:8bd09c675f28 | 51 | waiting, //enemy not yet attacking (off screen) |
| adat80 | 5:8bd09c675f28 | 52 | moving, //enemy moving towards players wall |
| adat80 | 5:8bd09c675f28 | 53 | attacking, //enemy damaging players wall |
| adat80 | 5:8bd09c675f28 | 54 | dying //enemy has been killed by player |
| adat80 | 5:8bd09c675f28 | 55 | }; |
| adat80 | 2:88019d96e1da | 56 | |
| adat80 | 1:3916f272663e | 57 | class Enemy |
| adat80 | 1:3916f272663e | 58 | { |
| adat80 | 1:3916f272663e | 59 | public: |
| adat80 | 1:3916f272663e | 60 | Enemy(); |
| adat80 | 1:3916f272663e | 61 | ~Enemy(); |
| adat80 | 2:88019d96e1da | 62 | |
| adat80 | 2:88019d96e1da | 63 | |
| adat80 | 5:8bd09c675f28 | 64 | /** |
| adat80 | 5:8bd09c675f28 | 65 | * @brief Initialises an enemy with a time to attack and a set speed |
| adat80 | 5:8bd09c675f28 | 66 | * @param timeToAttack @details The time into the new level that the enemy will start moving forwards |
| adat80 | 5:8bd09c675f28 | 67 | * @param speed @details The speed of the enemy in pixels per frame |
| adat80 | 5:8bd09c675f28 | 68 | */ |
| adat80 | 3:97cd7b3d89d0 | 69 | void init(float timeToAttack, float speed); |
| adat80 | 5:8bd09c675f28 | 70 | /** |
| adat80 | 5:8bd09c675f28 | 71 | * @brief Draws the enemy on the lcd |
| adat80 | 5:8bd09c675f28 | 72 | * @param lcd @details the N5110 object |
| adat80 | 5:8bd09c675f28 | 73 | */ |
| adat80 | 1:3916f272663e | 74 | void draw(N5110 &lcd); |
| adat80 | 5:8bd09c675f28 | 75 | /** |
| adat80 | 5:8bd09c675f28 | 76 | * @brief Updates the position of the enemy based on its state |
| adat80 | 5:8bd09c675f28 | 77 | * @param fps @details the games frames per second as an integer |
| adat80 | 5:8bd09c675f28 | 78 | */ |
| adat80 | 2:88019d96e1da | 79 | void update(int fps); |
| adat80 | 5:8bd09c675f28 | 80 | |
| adat80 | 1:3916f272663e | 81 | /// accessors and mutators |
| adat80 | 5:8bd09c675f28 | 82 | /** |
| adat80 | 5:8bd09c675f28 | 83 | * @brief Gets the enemies position |
| adat80 | 5:8bd09c675f28 | 84 | * @return position @details a Vector2D object detailing enemies position |
| adat80 | 5:8bd09c675f28 | 85 | |
| adat80 | 5:8bd09c675f28 | 86 | */ |
| adat80 | 1:3916f272663e | 87 | Vector2D get_pos(); |
| adat80 | 5:8bd09c675f28 | 88 | /** |
| adat80 | 5:8bd09c675f28 | 89 | * @brief Sets the enemies position |
| adat80 | 5:8bd09c675f28 | 90 | * @param position @details a Vector2D position |
| adat80 | 5:8bd09c675f28 | 91 | */ |
| adat80 | 1:3916f272663e | 92 | void set_pos(Vector2D p); |
| adat80 | 2:88019d96e1da | 93 | |
| adat80 | 5:8bd09c675f28 | 94 | /** |
| adat80 | 5:8bd09c675f28 | 95 | * @brief sets a flag on the enemy showing it is attacking |
| adat80 | 5:8bd09c675f28 | 96 | * @param attack @details a boolean value showing if the enemy is attacking |
| adat80 | 5:8bd09c675f28 | 97 | */ |
| adat80 | 5:8bd09c675f28 | 98 | //void set_attack(bool attack); |
| adat80 | 5:8bd09c675f28 | 99 | /** |
| adat80 | 5:8bd09c675f28 | 100 | * @brief Gets the enemies delay time to it starting to attack |
| adat80 | 5:8bd09c675f28 | 101 | * @return time to attack @details returns a float value for the time in to the level the enemy begins to attack |
| adat80 | 5:8bd09c675f28 | 102 | */ |
| adat80 | 1:3916f272663e | 103 | float get_timeToAttack(); |
| adat80 | 1:3916f272663e | 104 | |
| adat80 | 5:8bd09c675f28 | 105 | /** |
| adat80 | 5:8bd09c675f28 | 106 | * @brief Sets enemies current action |
| adat80 | 5:8bd09c675f28 | 107 | * @param Action @details an enumerator with 1 of the following values { waiting, moving, attacking, dying }; |
| adat80 | 5:8bd09c675f28 | 108 | */ |
| adat80 | 2:88019d96e1da | 109 | void set_current_action(Action act); |
| adat80 | 5:8bd09c675f28 | 110 | /** |
| adat80 | 5:8bd09c675f28 | 111 | * @brief Returns enemies current action |
| adat80 | 5:8bd09c675f28 | 112 | * @return Action @details an enumerator with 1 of the following values { waiting, moving, attacking, dying }; |
| adat80 | 5:8bd09c675f28 | 113 | */ |
| adat80 | 3:97cd7b3d89d0 | 114 | Action get_current_action(); |
| adat80 | 5:8bd09c675f28 | 115 | /** |
| adat80 | 5:8bd09c675f28 | 116 | * @brief Set Flag if enemy is alive |
| adat80 | 5:8bd09c675f28 | 117 | * @param alive @details a boolean value detailing if the enemy is alive |
| adat80 | 5:8bd09c675f28 | 118 | */ |
| adat80 | 1:3916f272663e | 119 | void set_alive(bool alive); |
| adat80 | 5:8bd09c675f28 | 120 | /** |
| adat80 | 5:8bd09c675f28 | 121 | * @brief Get Flag if enemy is alive |
| adat80 | 5:8bd09c675f28 | 122 | * @return alive @details a boolean value detailing if the enemy is alive |
| adat80 | 5:8bd09c675f28 | 123 | */ |
| adat80 | 1:3916f272663e | 124 | bool get_alive(); |
| adat80 | 2:88019d96e1da | 125 | |
| adat80 | 2:88019d96e1da | 126 | |
| adat80 | 1:3916f272663e | 127 | private: |
| adat80 | 1:3916f272663e | 128 | |
| adat80 | 3:97cd7b3d89d0 | 129 | float _x; |
| adat80 | 3:97cd7b3d89d0 | 130 | float _y; |
| adat80 | 2:88019d96e1da | 131 | |
| adat80 | 1:3916f272663e | 132 | bool _attack; |
| adat80 | 1:3916f272663e | 133 | bool _alive; |
| adat80 | 1:3916f272663e | 134 | float _timeToAttack; |
| adat80 | 3:97cd7b3d89d0 | 135 | float _speed; |
| adat80 | 2:88019d96e1da | 136 | |
| adat80 | 2:88019d96e1da | 137 | float _animationTick; |
| adat80 | 2:88019d96e1da | 138 | Action currentAction; |
| adat80 | 2:88019d96e1da | 139 | |
| adat80 | 2:88019d96e1da | 140 | |
| adat80 | 2:88019d96e1da | 141 | |
| adat80 | 2:88019d96e1da | 142 | |
| adat80 | 1:3916f272663e | 143 | }; |
| adat80 | 1:3916f272663e | 144 | #endif |