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.
Map/Map.h@8:9b77eea95088, 2019-05-08 (annotated)
- Committer:
- kocemax
- Date:
- Wed May 08 13:49:01 2019 +0000
- Revision:
- 8:9b77eea95088
- Parent:
- 7:cd3cafda3dd4
- Child:
- 9:f720f5d87420
Finally, implemented power ups and fixed the angles after pad collision. Also started commenting, not much time left, so will finish the commenting and if I have more time will add a couple more 'negative' power ups.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kocemax | 5:12c179da4788 | 1 | #ifndef MAP_H |
| kocemax | 5:12c179da4788 | 2 | #define MAP_H |
| kocemax | 5:12c179da4788 | 3 | |
| kocemax | 5:12c179da4788 | 4 | #include "mbed.h" |
| kocemax | 5:12c179da4788 | 5 | #include "N5110.h" |
| kocemax | 5:12c179da4788 | 6 | #include "Gamepad.h" |
| kocemax | 8:9b77eea95088 | 7 | #include "Paddle.h" |
| kocemax | 5:12c179da4788 | 8 | #include "Ball.h" |
| kocemax | 6:39bda45efeed | 9 | #include <vector> |
| kocemax | 5:12c179da4788 | 10 | |
| kocemax | 7:cd3cafda3dd4 | 11 | |
| kocemax | 5:12c179da4788 | 12 | /** Map Class |
| kocemax | 5:12c179da4788 | 13 | @author Kostadin Chakarov, University of Leeds |
| kocemax | 8:9b77eea95088 | 14 | @brief Creates the map and controls collision between objects in the Breakout game |
| kocemax | 7:cd3cafda3dd4 | 15 | @date April 2019 |
| kocemax | 7:cd3cafda3dd4 | 16 | */ |
| kocemax | 8:9b77eea95088 | 17 | |
| kocemax | 8:9b77eea95088 | 18 | /** Struct containing all parameters for the bricks */ |
| kocemax | 6:39bda45efeed | 19 | struct Brick { |
| kocemax | 8:9b77eea95088 | 20 | int x, y, w, h; /**x and y coords, width, height*/ |
| kocemax | 5:12c179da4788 | 21 | }; |
| kocemax | 8:9b77eea95088 | 22 | |
| kocemax | 8:9b77eea95088 | 23 | |
| kocemax | 8:9b77eea95088 | 24 | /** Forward declaration */ |
| kocemax | 7:cd3cafda3dd4 | 25 | class Map; |
| kocemax | 7:cd3cafda3dd4 | 26 | |
| kocemax | 8:9b77eea95088 | 27 | |
| kocemax | 8:9b77eea95088 | 28 | /** Level Class |
| kocemax | 8:9b77eea95088 | 29 | @author Kostadin Chakarov, University of Leeds |
| kocemax | 8:9b77eea95088 | 30 | @brief Creates the levels in the Breakout game |
| kocemax | 8:9b77eea95088 | 31 | @date April 2019 |
| kocemax | 8:9b77eea95088 | 32 | */ |
| kocemax | 8:9b77eea95088 | 33 | |
| kocemax | 8:9b77eea95088 | 34 | |
| kocemax | 7:cd3cafda3dd4 | 35 | class Level |
| kocemax | 7:cd3cafda3dd4 | 36 | { |
| kocemax | 7:cd3cafda3dd4 | 37 | public: |
| kocemax | 8:9b77eea95088 | 38 | /** Draws the bricks and stores them in the levels vector |
| kocemax | 8:9b77eea95088 | 39 | * Virtual as it changes for each level |
| kocemax | 8:9b77eea95088 | 40 | */ |
| kocemax | 8:9b77eea95088 | 41 | virtual void initBricks(Map &map) {} |
| kocemax | 7:cd3cafda3dd4 | 42 | }; |
| kocemax | 7:cd3cafda3dd4 | 43 | |
| kocemax | 8:9b77eea95088 | 44 | /** Inherits from level and doesn't have any additional functions */ |
| kocemax | 7:cd3cafda3dd4 | 45 | class Level1 : public Level |
| kocemax | 7:cd3cafda3dd4 | 46 | { |
| kocemax | 7:cd3cafda3dd4 | 47 | public: |
| kocemax | 8:9b77eea95088 | 48 | /** Draws the bricks and stores them in the levels vector */ |
| kocemax | 8:9b77eea95088 | 49 | virtual void initBricks(Map &map); |
| kocemax | 8:9b77eea95088 | 50 | }; |
| kocemax | 8:9b77eea95088 | 51 | |
| kocemax | 8:9b77eea95088 | 52 | /** Inherits from level and doesn't have any additional functions */ |
| kocemax | 8:9b77eea95088 | 53 | class Level2 : public Level |
| kocemax | 8:9b77eea95088 | 54 | { |
| kocemax | 8:9b77eea95088 | 55 | public: |
| kocemax | 8:9b77eea95088 | 56 | /** Draws the bricks and stores them in the levels vector */ |
| kocemax | 7:cd3cafda3dd4 | 57 | virtual void initBricks(Map &map); |
| kocemax | 7:cd3cafda3dd4 | 58 | }; |
| kocemax | 7:cd3cafda3dd4 | 59 | |
| kocemax | 8:9b77eea95088 | 60 | |
| kocemax | 8:9b77eea95088 | 61 | /* |
| kocemax | 8:9b77eea95088 | 62 | Power ups Coding strategy: |
| kocemax | 8:9b77eea95088 | 63 | 1. Define the PowerUp and it's draw method, set up all it's parameters in ctor |
| kocemax | 8:9b77eea95088 | 64 | 2. Lifetime cycle management: |
| kocemax | 8:9b77eea95088 | 65 | 2.a. Identify the spawn event (e.g. when brick is killed), then go to that code |
| kocemax | 8:9b77eea95088 | 66 | 2.b. Create the power up and put it in the map's powerUps vector |
| kocemax | 8:9b77eea95088 | 67 | 2.c. During update, iterate over all powerUps and move them |
| kocemax | 8:9b77eea95088 | 68 | 2.d. In checkCollisions, iterate over all powerUps and check if any hit the pad |
| kocemax | 8:9b77eea95088 | 69 | 2.d.i. If hit the paddle, remove and apply power-up effect |
| kocemax | 8:9b77eea95088 | 70 | 2.d.ii. else if fell below paddle height, just remove and don't apply effect |
| kocemax | 8:9b77eea95088 | 71 | */ |
| kocemax | 8:9b77eea95088 | 72 | |
| kocemax | 8:9b77eea95088 | 73 | |
| kocemax | 8:9b77eea95088 | 74 | /** Forward declaration since needed in PowerUpType struct */ |
| kocemax | 8:9b77eea95088 | 75 | class PowerUp; |
| kocemax | 8:9b77eea95088 | 76 | |
| kocemax | 8:9b77eea95088 | 77 | |
| kocemax | 8:9b77eea95088 | 78 | struct PowerUpType { |
| kocemax | 8:9b77eea95088 | 79 | int type; |
| kocemax | 8:9b77eea95088 | 80 | int* sprite; |
| kocemax | 8:9b77eea95088 | 81 | |
| kocemax | 8:9b77eea95088 | 82 | PowerUpType(int type, int* sprite) : type(type), sprite(sprite) {} |
| kocemax | 8:9b77eea95088 | 83 | |
| kocemax | 8:9b77eea95088 | 84 | virtual void giveBonus(Paddle &paddle, Ball &ball) {} |
| kocemax | 8:9b77eea95088 | 85 | virtual void draw(N5110 &lcd, PowerUp &pUp); |
| kocemax | 8:9b77eea95088 | 86 | }; |
| kocemax | 8:9b77eea95088 | 87 | |
| kocemax | 8:9b77eea95088 | 88 | struct PaddleSizePUpType : public PowerUpType { |
| kocemax | 8:9b77eea95088 | 89 | PaddleSizePUpType(int type, int* sprite) : PowerUpType(type, sprite) {} |
| kocemax | 8:9b77eea95088 | 90 | |
| kocemax | 8:9b77eea95088 | 91 | virtual void giveBonus(Paddle &paddle, Ball &ball); |
| kocemax | 8:9b77eea95088 | 92 | }; |
| kocemax | 8:9b77eea95088 | 93 | |
| kocemax | 8:9b77eea95088 | 94 | struct PaddleSpeedPUpType : public PowerUpType { |
| kocemax | 8:9b77eea95088 | 95 | PaddleSpeedPUpType(int type, int* sprite) : PowerUpType(type, sprite) {} |
| kocemax | 8:9b77eea95088 | 96 | |
| kocemax | 8:9b77eea95088 | 97 | virtual void giveBonus(Paddle &paddle, Ball &ball); |
| kocemax | 8:9b77eea95088 | 98 | }; |
| kocemax | 8:9b77eea95088 | 99 | |
| kocemax | 8:9b77eea95088 | 100 | struct BallSpeedPUpType : public PowerUpType { |
| kocemax | 8:9b77eea95088 | 101 | BallSpeedPUpType(int type, int* sprite) : PowerUpType(type, sprite) {} |
| kocemax | 8:9b77eea95088 | 102 | |
| kocemax | 8:9b77eea95088 | 103 | virtual void giveBonus(Paddle &paddle, Ball &ball); |
| kocemax | 8:9b77eea95088 | 104 | }; |
| kocemax | 8:9b77eea95088 | 105 | |
| kocemax | 8:9b77eea95088 | 106 | #define PowerUpW 3 |
| kocemax | 8:9b77eea95088 | 107 | #define PowerUpH 4 |
| kocemax | 8:9b77eea95088 | 108 | #define PowerUpDropChancePct 100 |
| kocemax | 8:9b77eea95088 | 109 | |
| kocemax | 8:9b77eea95088 | 110 | |
| kocemax | 8:9b77eea95088 | 111 | /** PowerUp Class |
| kocemax | 8:9b77eea95088 | 112 | @author Kostadin Chakarov, University of Leeds |
| kocemax | 8:9b77eea95088 | 113 | @brief Creates the power ups in the Breakout game |
| kocemax | 8:9b77eea95088 | 114 | @date April 2019 |
| kocemax | 8:9b77eea95088 | 115 | */ |
| kocemax | 8:9b77eea95088 | 116 | |
| kocemax | 8:9b77eea95088 | 117 | class PowerUp : public GameObject { |
| kocemax | 8:9b77eea95088 | 118 | //PowerUpType& pUpType; |
| kocemax | 8:9b77eea95088 | 119 | |
| kocemax | 8:9b77eea95088 | 120 | // we cannot store a reference because (for some reason) it is not copy-constructable |
| kocemax | 8:9b77eea95088 | 121 | // and for some reason, PowerUp needs to be that. |
| kocemax | 8:9b77eea95088 | 122 | PowerUpType* _pUpType; |
| kocemax | 8:9b77eea95088 | 123 | |
| kocemax | 8:9b77eea95088 | 124 | |
| kocemax | 8:9b77eea95088 | 125 | |
| kocemax | 7:cd3cafda3dd4 | 126 | public: |
| kocemax | 8:9b77eea95088 | 127 | PowerUp(float x, float y, PowerUpType& pUpType); |
| kocemax | 8:9b77eea95088 | 128 | ~PowerUp() {}; |
| kocemax | 8:9b77eea95088 | 129 | |
| kocemax | 8:9b77eea95088 | 130 | void giveBonus(Paddle &paddle, Ball &ball) { |
| kocemax | 8:9b77eea95088 | 131 | _pUpType->giveBonus(paddle, ball); |
| kocemax | 8:9b77eea95088 | 132 | } |
| kocemax | 8:9b77eea95088 | 133 | virtual void draw(N5110 &lcd); |
| kocemax | 7:cd3cafda3dd4 | 134 | }; |
| kocemax | 7:cd3cafda3dd4 | 135 | |
| kocemax | 7:cd3cafda3dd4 | 136 | |
| kocemax | 5:12c179da4788 | 137 | |
| kocemax | 8:9b77eea95088 | 138 | |
| kocemax | 5:12c179da4788 | 139 | class Map |
| kocemax | 5:12c179da4788 | 140 | { |
| kocemax | 6:39bda45efeed | 141 | private: |
| kocemax | 8:9b77eea95088 | 142 | int currentLevel; /** Keeps track of the current level */ |
| kocemax | 8:9b77eea95088 | 143 | std::vector<Level*> levels; /** Stores the levels in a vector */ |
| kocemax | 8:9b77eea95088 | 144 | std::vector<Brick> bricks; /** Stores the bricks in a vector */ |
| kocemax | 8:9b77eea95088 | 145 | std::vector<PowerUp> powerUps; /** Stores the powerups in a vector */ |
| kocemax | 5:12c179da4788 | 146 | |
| kocemax | 5:12c179da4788 | 147 | public: |
| kocemax | 8:9b77eea95088 | 148 | /** Constructor */ |
| kocemax | 7:cd3cafda3dd4 | 149 | Map(); |
| kocemax | 8:9b77eea95088 | 150 | /** Destructor */ |
| kocemax | 5:12c179da4788 | 151 | ~Map(); |
| kocemax | 8:9b77eea95088 | 152 | /** Gets the current level |
| kocemax | 8:9b77eea95088 | 153 | * @return the number of the current level |
| kocemax | 8:9b77eea95088 | 154 | */ |
| kocemax | 8:9b77eea95088 | 155 | int getCurrentLevel() { return currentLevel; } |
| kocemax | 8:9b77eea95088 | 156 | /** Checks if all levels are defeated |
| kocemax | 8:9b77eea95088 | 157 | * @return true if current level = number of levels |
| kocemax | 8:9b77eea95088 | 158 | */ |
| kocemax | 8:9b77eea95088 | 159 | bool hasWon() { return currentLevel >= levels.size(); } |
| kocemax | 8:9b77eea95088 | 160 | /** ???? */ |
| kocemax | 6:39bda45efeed | 161 | void initBricks(); |
| kocemax | 8:9b77eea95088 | 162 | /** Updates drawing of moving objects on map */ |
| kocemax | 8:9b77eea95088 | 163 | void update(); |
| kocemax | 8:9b77eea95088 | 164 | /** Creates powerUps depending on the percentage chance */ |
| kocemax | 8:9b77eea95088 | 165 | void onBrickHit(const Brick&); |
| kocemax | 8:9b77eea95088 | 166 | /** Initializes and draws all the bricks on the level, as well as powerUps */ |
| kocemax | 5:12c179da4788 | 167 | void drawMap(N5110 &lcd); |
| kocemax | 8:9b77eea95088 | 168 | /** Checks if brick <-> ball collided */ |
| kocemax | 8:9b77eea95088 | 169 | void checkCollision(Ball &ball, Paddle &paddle); |
| kocemax | 8:9b77eea95088 | 170 | /** Resolves the collision */ |
| kocemax | 8:9b77eea95088 | 171 | void resolveCollision(const Brick &brick, GameObject &obj); |
| kocemax | 8:9b77eea95088 | 172 | /** Checks if we cleared the level and if we won */ |
| kocemax | 8:9b77eea95088 | 173 | bool checkLevel(); |
| kocemax | 8:9b77eea95088 | 174 | /** Resets the whole map when game is restarted */ |
| kocemax | 8:9b77eea95088 | 175 | void reset(); |
| kocemax | 8:9b77eea95088 | 176 | /** Adds the bricks to the vector */ |
| kocemax | 8:9b77eea95088 | 177 | void addBrick(Brick& brick) { bricks.push_back(brick); } |
| kocemax | 5:12c179da4788 | 178 | |
| kocemax | 5:12c179da4788 | 179 | }; |
| kocemax | 5:12c179da4788 | 180 | #endif |