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
- Committer:
- kocemax
- Date:
- 2019-05-08
- Revision:
- 8:9b77eea95088
- Parent:
- 7:cd3cafda3dd4
- Child:
- 9:f720f5d87420
File content as of revision 8:9b77eea95088:
#ifndef MAP_H
#define MAP_H
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
#include "Paddle.h"
#include "Ball.h"
#include <vector>
/** Map Class
@author Kostadin Chakarov, University of Leeds
@brief Creates the map and controls collision between objects in the Breakout game
@date April 2019
*/
/** Struct containing all parameters for the bricks */
struct Brick {
int x, y, w, h; /**x and y coords, width, height*/
};
/** Forward declaration */
class Map;
/** Level Class
@author Kostadin Chakarov, University of Leeds
@brief Creates the levels in the Breakout game
@date April 2019
*/
class Level
{
public:
/** 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);
};
/*
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:
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; /** Stores the powerups in a vector */
public:
/** Constructor */
Map();
/** Destructor */
~Map();
/** 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);
/** 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