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.
Diff: Map/Map.h
- Revision:
- 8:9b77eea95088
- Parent:
- 7:cd3cafda3dd4
- Child:
- 9:f720f5d87420
--- a/Map/Map.h Wed Apr 10 09:18:25 2019 +0000 +++ b/Map/Map.h Wed May 08 13:49:01 2019 +0000 @@ -4,67 +4,177 @@ #include "mbed.h" #include "N5110.h" #include "Gamepad.h" -#include "PlayerControl.h" +#include "Paddle.h" #include "Ball.h" -//#include "PowerUp.h" #include <vector> /** Map Class @author Kostadin Chakarov, University of Leeds -@brief Creates the map in the Breakout game +@brief Creates the map and controls collision between objects in the Breakout game @date April 2019 */ -//x and y coords, width, height + +/** Struct containing all parameters for the bricks */ struct Brick { - int x, y, w, h; + int x, y, w, h; /**x and y coords, width, height*/ }; -//need to write above to able to use in level + + +/** Forward declaration */ class Map; -//this only contains the initBricks function as it stores the bricks in the level + +/** Level Class +@author Kostadin Chakarov, University of Leeds +@brief Creates the levels in the Breakout game +@date April 2019 +*/ + + class Level { public: - virtual void initBricks(Map &map) {} //virtual because will be different for each level + /** Draws the bricks and stores them in the levels vector + * Virtual as it changes for each level + */ + virtual void initBricks(Map &map) {} }; +/** Inherits from level and doesn't have any additional functions */ class Level1 : public Level { public: + /** Draws the bricks and stores them in the levels vector */ + virtual void initBricks(Map &map); +}; + +/** Inherits from level and doesn't have any additional functions */ +class Level2 : public Level +{ +public: + /** Draws the bricks and stores them in the levels vector */ virtual void initBricks(Map &map); }; -class Level2 : public Level -{ + +/* +Power ups Coding strategy: +1. Define the PowerUp and it's draw method, set up all it's parameters in ctor +2. Lifetime cycle management: +2.a. Identify the spawn event (e.g. when brick is killed), then go to that code +2.b. Create the power up and put it in the map's powerUps vector +2.c. During update, iterate over all powerUps and move them +2.d. In checkCollisions, iterate over all powerUps and check if any hit the pad +2.d.i. If hit the paddle, remove and apply power-up effect +2.d.ii. else if fell below paddle height, just remove and don't apply effect +*/ + + +/** Forward declaration since needed in PowerUpType struct */ +class PowerUp; + + +struct PowerUpType { + int type; + int* sprite; + + PowerUpType(int type, int* sprite) : type(type), sprite(sprite) {} + + virtual void giveBonus(Paddle &paddle, Ball &ball) {} + virtual void draw(N5110 &lcd, PowerUp &pUp); +}; + +struct PaddleSizePUpType : public PowerUpType { + PaddleSizePUpType(int type, int* sprite) : PowerUpType(type, sprite) {} + + virtual void giveBonus(Paddle &paddle, Ball &ball); +}; + +struct PaddleSpeedPUpType : public PowerUpType { + PaddleSpeedPUpType(int type, int* sprite) : PowerUpType(type, sprite) {} + + virtual void giveBonus(Paddle &paddle, Ball &ball); +}; + +struct BallSpeedPUpType : public PowerUpType { + BallSpeedPUpType(int type, int* sprite) : PowerUpType(type, sprite) {} + + virtual void giveBonus(Paddle &paddle, Ball &ball); +}; + +#define PowerUpW 3 +#define PowerUpH 4 +#define PowerUpDropChancePct 100 + + +/** PowerUp Class +@author Kostadin Chakarov, University of Leeds +@brief Creates the power ups in the Breakout game +@date April 2019 +*/ + +class PowerUp : public GameObject { + //PowerUpType& pUpType; + + // we cannot store a reference because (for some reason) it is not copy-constructable + // and for some reason, PowerUp needs to be that. + PowerUpType* _pUpType; + + + public: - virtual void initBricks(Map &map); + PowerUp(float x, float y, PowerUpType& pUpType); + ~PowerUp() {}; + + void giveBonus(Paddle &paddle, Ball &ball) { + _pUpType->giveBonus(paddle, ball); + } + virtual void draw(N5110 &lcd); }; + class Map { private: - int currentLevel; //keeps track of the current level - std::vector<Level*> levels; //stores the levels in a vector - std::vector<Brick> bricks; //stores the bricks in a vector - //std::vector<PowerUp> powerUps; + int currentLevel; /** Keeps track of the current level */ + std::vector<Level*> levels; /** Stores the levels in a vector */ + std::vector<Brick> bricks; /** Stores the bricks in a vector */ + std::vector<PowerUp> powerUps; /** Stores the powerups in a vector */ public: + /** Constructor */ Map(); + /** Destructor */ ~Map(); - - int getCurrentLevel() { return currentLevel; } //returns the current level - bool hasWon() { return currentLevel >= levels.size(); } //true if current level = number of levels - + /** Gets the current level + * @return the number of the current level + */ + int getCurrentLevel() { return currentLevel; } + /** Checks if all levels are defeated + * @return true if current level = number of levels + */ + bool hasWon() { return currentLevel >= levels.size(); } + /** ???? */ void initBricks(); + /** Updates drawing of moving objects on map */ + void update(); + /** Creates powerUps depending on the percentage chance */ + void onBrickHit(const Brick&); + /** Initializes and draws all the bricks on the level, as well as powerUps */ void drawMap(N5110 &lcd); - void checkCollision(GameObject &obj); // checks if brick<->ball collided - void resolveCollision(const Brick &brick, GameObject &obj); //resolves the collision - bool checkLevel(); //checks if we cleared the level and if we won - void reset(); //resets game - void addBrick(Brick& brick) { bricks.push_back(brick); } //adds the bricks to the vector + /** Checks if brick <-> ball collided */ + void checkCollision(Ball &ball, Paddle &paddle); + /** Resolves the collision */ + void resolveCollision(const Brick &brick, GameObject &obj); + /** Checks if we cleared the level and if we won */ + bool checkLevel(); + /** Resets the whole map when game is restarted */ + void reset(); + /** Adds the bricks to the vector */ + void addBrick(Brick& brick) { bricks.push_back(brick); } }; #endif \ No newline at end of file