Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Committer:
kocemax
Date:
Thu May 09 11:41:58 2019 +0000
Revision:
12:b3ec47d606a5
Parent:
11:bb36db678a6d
Child:
13:3585d2ea4ff4
Had to fix more of the comments, to improve the documentation.

Who changed what in which revision?

UserRevisionLine numberNew 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 8:9b77eea95088 12 /** Struct containing all parameters for the bricks */
kocemax 6:39bda45efeed 13 struct Brick {
kocemax 12:b3ec47d606a5 14 int x, y, w, h; // x and y coords, width, height
kocemax 5:12c179da4788 15 };
kocemax 8:9b77eea95088 16
kocemax 8:9b77eea95088 17
kocemax 12:b3ec47d606a5 18 // Forward declaration
kocemax 7:cd3cafda3dd4 19 class Map;
kocemax 7:cd3cafda3dd4 20
kocemax 8:9b77eea95088 21
kocemax 8:9b77eea95088 22 /** Level Class
kocemax 11:bb36db678a6d 23 Creates the levels in the Breakout game
kocemax 8:9b77eea95088 24 @author Kostadin Chakarov, University of Leeds
kocemax 8:9b77eea95088 25 @date April 2019
kocemax 8:9b77eea95088 26 */
kocemax 8:9b77eea95088 27
kocemax 8:9b77eea95088 28
kocemax 7:cd3cafda3dd4 29 class Level
kocemax 7:cd3cafda3dd4 30 {
kocemax 7:cd3cafda3dd4 31 public:
kocemax 12:b3ec47d606a5 32 /** Draws the bricks and stores them in the levels vector,
kocemax 12:b3ec47d606a5 33 * virtual as it changes for each level
kocemax 8:9b77eea95088 34 */
kocemax 8:9b77eea95088 35 virtual void initBricks(Map &map) {}
kocemax 7:cd3cafda3dd4 36 };
kocemax 7:cd3cafda3dd4 37
kocemax 8:9b77eea95088 38 /** Inherits from level and doesn't have any additional functions */
kocemax 7:cd3cafda3dd4 39 class Level1 : public Level
kocemax 7:cd3cafda3dd4 40 {
kocemax 7:cd3cafda3dd4 41 public:
kocemax 11:bb36db678a6d 42 /** Draws the bricks for level 1 and stores them in the levels vector */
kocemax 8:9b77eea95088 43 virtual void initBricks(Map &map);
kocemax 8:9b77eea95088 44 };
kocemax 8:9b77eea95088 45
kocemax 8:9b77eea95088 46 /** Inherits from level and doesn't have any additional functions */
kocemax 8:9b77eea95088 47 class Level2 : public Level
kocemax 8:9b77eea95088 48 {
kocemax 8:9b77eea95088 49 public:
kocemax 11:bb36db678a6d 50 /** Draws the bricks for level 2 and stores them in the levels vector */
kocemax 7:cd3cafda3dd4 51 virtual void initBricks(Map &map);
kocemax 7:cd3cafda3dd4 52 };
kocemax 7:cd3cafda3dd4 53
kocemax 9:f720f5d87420 54 /** Inherits from level and doesn't have any additional functions */
kocemax 9:f720f5d87420 55 class Level3 : public Level
kocemax 9:f720f5d87420 56 {
kocemax 9:f720f5d87420 57 public:
kocemax 11:bb36db678a6d 58 /** Draws the bricks for level 3 and stores them in the levels vector */
kocemax 9:f720f5d87420 59 virtual void initBricks(Map &map);
kocemax 9:f720f5d87420 60 };
kocemax 9:f720f5d87420 61
kocemax 8:9b77eea95088 62
kocemax 8:9b77eea95088 63 /*
kocemax 8:9b77eea95088 64 Power ups Coding strategy:
kocemax 8:9b77eea95088 65 1. Define the PowerUp and it's draw method, set up all it's parameters in ctor
kocemax 8:9b77eea95088 66 2. Lifetime cycle management:
kocemax 8:9b77eea95088 67 2.a. Identify the spawn event (e.g. when brick is killed), then go to that code
kocemax 8:9b77eea95088 68 2.b. Create the power up and put it in the map's powerUps vector
kocemax 8:9b77eea95088 69 2.c. During update, iterate over all powerUps and move them
kocemax 8:9b77eea95088 70 2.d. In checkCollisions, iterate over all powerUps and check if any hit the pad
kocemax 8:9b77eea95088 71 2.d.i. If hit the paddle, remove and apply power-up effect
kocemax 8:9b77eea95088 72 2.d.ii. else if fell below paddle height, just remove and don't apply effect
kocemax 8:9b77eea95088 73 */
kocemax 8:9b77eea95088 74
kocemax 8:9b77eea95088 75
kocemax 12:b3ec47d606a5 76 // Forward declaration since needed in PowerUpType struct
kocemax 8:9b77eea95088 77 class PowerUp;
kocemax 8:9b77eea95088 78
kocemax 11:bb36db678a6d 79 /** Struct which contains all the powerup parameters and functions */
kocemax 8:9b77eea95088 80 struct PowerUpType {
kocemax 9:f720f5d87420 81 int type; /** Stores the type of the powerup */
kocemax 9:f720f5d87420 82 int* sprite; /** Stores the sprite of the powerup */
kocemax 8:9b77eea95088 83
kocemax 9:f720f5d87420 84 /** Constructor */
kocemax 8:9b77eea95088 85 PowerUpType(int type, int* sprite) : type(type), sprite(sprite) {}
kocemax 8:9b77eea95088 86
kocemax 9:f720f5d87420 87 /** Gives the powerup effect to the player */
kocemax 8:9b77eea95088 88 virtual void giveBonus(Paddle &paddle, Ball &ball) {}
kocemax 9:f720f5d87420 89 /** Draws the powerup on the LCD */
kocemax 8:9b77eea95088 90 virtual void draw(N5110 &lcd, PowerUp &pUp);
kocemax 8:9b77eea95088 91 };
kocemax 8:9b77eea95088 92
kocemax 9:f720f5d87420 93 /** Inherits from the powerUpType struct and only changes the giveBonus function */
kocemax 8:9b77eea95088 94 struct PaddleSizePUpType : public PowerUpType {
kocemax 9:f720f5d87420 95 /** Constructor */
kocemax 8:9b77eea95088 96 PaddleSizePUpType(int type, int* sprite) : PowerUpType(type, sprite) {}
kocemax 11:bb36db678a6d 97 /** Gives the paddle size increase powerup effect to the player */
kocemax 8:9b77eea95088 98 virtual void giveBonus(Paddle &paddle, Ball &ball);
kocemax 8:9b77eea95088 99 };
kocemax 8:9b77eea95088 100
kocemax 9:f720f5d87420 101 /** Inherits from the powerUpType struct and only changes the giveBonus function */
kocemax 8:9b77eea95088 102 struct PaddleSpeedPUpType : public PowerUpType {
kocemax 9:f720f5d87420 103 /** Constructor */
kocemax 8:9b77eea95088 104 PaddleSpeedPUpType(int type, int* sprite) : PowerUpType(type, sprite) {}
kocemax 11:bb36db678a6d 105 /** Gives the paddle speed incrase powerup effect to the player */
kocemax 8:9b77eea95088 106 virtual void giveBonus(Paddle &paddle, Ball &ball);
kocemax 8:9b77eea95088 107 };
kocemax 8:9b77eea95088 108
kocemax 9:f720f5d87420 109 /** Inherits from the powerUpType struct and only changes the giveBonus function */
kocemax 8:9b77eea95088 110 struct BallSpeedPUpType : public PowerUpType {
kocemax 9:f720f5d87420 111 /** Constructor */
kocemax 8:9b77eea95088 112 BallSpeedPUpType(int type, int* sprite) : PowerUpType(type, sprite) {}
kocemax 11:bb36db678a6d 113 /** Gives the ball speed decrease powerup effect to the player */
kocemax 8:9b77eea95088 114 virtual void giveBonus(Paddle &paddle, Ball &ball);
kocemax 8:9b77eea95088 115 };
kocemax 8:9b77eea95088 116
kocemax 8:9b77eea95088 117 #define PowerUpW 3
kocemax 8:9b77eea95088 118 #define PowerUpH 4
kocemax 10:da5743dfb137 119 #define PowerUpDropChancePct 15
kocemax 8:9b77eea95088 120
kocemax 8:9b77eea95088 121
kocemax 8:9b77eea95088 122 /** PowerUp Class
kocemax 11:bb36db678a6d 123 Creates the power ups in the Breakout game
kocemax 8:9b77eea95088 124 @author Kostadin Chakarov, University of Leeds
kocemax 8:9b77eea95088 125 @date April 2019
kocemax 8:9b77eea95088 126 */
kocemax 8:9b77eea95088 127
kocemax 8:9b77eea95088 128 class PowerUp : public GameObject {
kocemax 8:9b77eea95088 129 //PowerUpType& pUpType;
kocemax 8:9b77eea95088 130
kocemax 8:9b77eea95088 131 // we cannot store a reference because (for some reason) it is not copy-constructable
kocemax 8:9b77eea95088 132 // and for some reason, PowerUp needs to be that.
kocemax 8:9b77eea95088 133 PowerUpType* _pUpType;
kocemax 8:9b77eea95088 134
kocemax 8:9b77eea95088 135
kocemax 8:9b77eea95088 136
kocemax 7:cd3cafda3dd4 137 public:
kocemax 9:f720f5d87420 138 /** Constructor
kocemax 9:f720f5d87420 139 * @param x the initial x-position of the PowerUp
kocemax 9:f720f5d87420 140 * @param y the initial y-position of the PowerUp
kocemax 9:f720f5d87420 141 * @param pUpType the type of the PowerUp
kocemax 9:f720f5d87420 142 */
kocemax 8:9b77eea95088 143 PowerUp(float x, float y, PowerUpType& pUpType);
kocemax 9:f720f5d87420 144 /** Destructor - empty */
kocemax 8:9b77eea95088 145 ~PowerUp() {};
kocemax 8:9b77eea95088 146
kocemax 9:f720f5d87420 147 /** Determines the bonus depending on the type of the powerup */
kocemax 8:9b77eea95088 148 void giveBonus(Paddle &paddle, Ball &ball) {
kocemax 8:9b77eea95088 149 _pUpType->giveBonus(paddle, ball);
kocemax 8:9b77eea95088 150 }
kocemax 9:f720f5d87420 151 /** Draws the powerup */
kocemax 8:9b77eea95088 152 virtual void draw(N5110 &lcd);
kocemax 7:cd3cafda3dd4 153 };
kocemax 7:cd3cafda3dd4 154
kocemax 7:cd3cafda3dd4 155
kocemax 9:f720f5d87420 156 /** Map Class
kocemax 11:bb36db678a6d 157 Creates the map and controls collision between objects in the Breakout game
kocemax 9:f720f5d87420 158 @author Kostadin Chakarov, University of Leeds
kocemax 9:f720f5d87420 159 @date April 2019
kocemax 9:f720f5d87420 160 */
kocemax 8:9b77eea95088 161
kocemax 5:12c179da4788 162 class Map
kocemax 5:12c179da4788 163 {
kocemax 6:39bda45efeed 164 private:
kocemax 8:9b77eea95088 165 int currentLevel; /** Keeps track of the current level */
kocemax 8:9b77eea95088 166 std::vector<Level*> levels; /** Stores the levels in a vector */
kocemax 8:9b77eea95088 167 std::vector<Brick> bricks; /** Stores the bricks in a vector */
kocemax 8:9b77eea95088 168 std::vector<PowerUp> powerUps; /** Stores the powerups in a vector */
kocemax 5:12c179da4788 169
kocemax 5:12c179da4788 170 public:
kocemax 8:9b77eea95088 171 /** Constructor */
kocemax 7:cd3cafda3dd4 172 Map();
kocemax 8:9b77eea95088 173 /** Destructor */
kocemax 5:12c179da4788 174 ~Map();
kocemax 8:9b77eea95088 175 /** Gets the current level
kocemax 8:9b77eea95088 176 * @return the number of the current level
kocemax 8:9b77eea95088 177 */
kocemax 8:9b77eea95088 178 int getCurrentLevel() { return currentLevel; }
kocemax 8:9b77eea95088 179 /** Checks if all levels are defeated
kocemax 9:f720f5d87420 180 * @return true if current level == number of levels
kocemax 8:9b77eea95088 181 */
kocemax 8:9b77eea95088 182 bool hasWon() { return currentLevel >= levels.size(); }
kocemax 9:f720f5d87420 183 /** Get the level object based on the current level number and call initBricks() on it */
kocemax 6:39bda45efeed 184 void initBricks();
kocemax 9:f720f5d87420 185 /** Updates the moving powerups on map */
kocemax 8:9b77eea95088 186 void update();
kocemax 9:f720f5d87420 187 /** Creates powerUps depending on the PowerUpDropChancePct */
kocemax 8:9b77eea95088 188 void onBrickHit(const Brick&);
kocemax 9:f720f5d87420 189 /** Draws all the bricks on the level, as well as powerUps */
kocemax 5:12c179da4788 190 void drawMap(N5110 &lcd);
kocemax 8:9b77eea95088 191 /** Checks if brick <-> ball collided */
kocemax 8:9b77eea95088 192 void checkCollision(Ball &ball, Paddle &paddle);
kocemax 8:9b77eea95088 193 /** Resolves the collision */
kocemax 8:9b77eea95088 194 void resolveCollision(const Brick &brick, GameObject &obj);
kocemax 9:f720f5d87420 195 /** Checks if we cleared the level and if we won
kocemax 9:f720f5d87420 196 * @return true if level is cleared
kocemax 9:f720f5d87420 197 */
kocemax 8:9b77eea95088 198 bool checkLevel();
kocemax 9:f720f5d87420 199 /** Resets the whole map when game is (re-)started */
kocemax 8:9b77eea95088 200 void reset();
kocemax 9:f720f5d87420 201 /** Adds the brick to the vector */
kocemax 8:9b77eea95088 202 void addBrick(Brick& brick) { bricks.push_back(brick); }
kocemax 9:f720f5d87420 203 /** Stores the score of the game */
kocemax 9:f720f5d87420 204 int score;
kocemax 5:12c179da4788 205 };
kocemax 5:12c179da4788 206 #endif