Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Revision:
9:f720f5d87420
Parent:
8:9b77eea95088
Child:
10:da5743dfb137
--- a/Map/Map.cpp	Wed May 08 13:49:01 2019 +0000
+++ b/Map/Map.cpp	Wed May 08 23:09:43 2019 +0000
@@ -3,14 +3,16 @@
 
 // Levels
 
+/** Stores the bricks in the levels vector */
 void Level1::initBricks(Map & map)
 {
-    int w = 41;
-    int h = 2;
-    int gap = 1;
-    int y = 0;
+    int w = 41;     //width 
+    int h = 2;      //height 
+    int gap = 1;    //gap between bricks
+    int y = 0;      //first y coord of columns
+    
     for (int j = 0; j < 9; ++j) {
-        int x = 0;
+        int x = 0;  //first x coord of rows
         for (int i = 0; i < 2; ++i) {
             Brick b;
             b.x = x;
@@ -25,15 +27,16 @@
     }
 }
 
+/** Stores the bricks in the levels vector */
 void Level2::initBricks(Map & map)
 {
-    int w = 20;
-    int h = 2;
-    int gap = 1;
-    int y = 0;
+    int w = 20;     //width 
+    int h = 2;      //height 
+    int gap = 1;    //gap between bricks
+    int y = 0;      //first y coord of columns
 
     for (int j = 0; j < 9; ++j) {
-        int x = 0;
+        int x = 0;  //first x coord of rows
         for (int i = 0; i < 4; ++i) {
             Brick b;
             b.x = x;
@@ -48,11 +51,35 @@
     }
 }
 
+/** Stores the bricks in the levels vector */
+void Level3::initBricks(Map & map)
+{
+    int w = 13;     //width 
+    int h = 2;      //height 
+    int gap = 1;    //gap between bricks
+    int y = 0;      //first y coord of columns
+
+    for (int j = 0; j < 9; ++j) {
+        int x = 0;  //first x coord of rows
+        for (int i = 0; i < 6; ++i) {
+            Brick b;
+            b.x = x;
+            b.y = y;
+            b.w = w;
+            b.h = h;
+            map.addBrick(b);
+
+            x += w + gap;
+        }
+        y += h + gap;
+    }
+}
 
 // Power-Ups
 
-  
-//                      rows,cols
+//Sprite for each powerup
+
+//                              rows,cols
 int powerUpPaddleSizeSprite[PowerUpH][PowerUpW] =   {
     { 0,1,0 },
     { 1,1,1 },
@@ -60,7 +87,7 @@
     { 1,1,1 }
 };
 
-//                      rows,cols
+//                              rows,cols
 int powerUpPaddleSpeedSprite[PowerUpH][PowerUpW] =   {
     { 0,1,0 },
     { 0,1,0 },
@@ -68,7 +95,7 @@
     { 1,1,1 }
 };
 
-//                      rows,cols
+//                              rows,cols
 int powerUpBallSpeedSprite[PowerUpH][PowerUpW] =   {
     { 0,1,0 },
     { 1,1,1 },
@@ -76,19 +103,22 @@
     { 0,1,0 }
 };
 
+/** Draws each powerup on the LCD depending on its type */
 void PowerUpType::draw(N5110 &lcd, PowerUp &pUp) {
     //printf("%f, %f; %d,%d - %d\n", pUp.getPos().x,pUp.getPos().y,pUp.getH(),pUp.getW(), (int)((int*)sprite));
     lcd.drawSprite(pUp.getPos().x,pUp.getPos().y,pUp.getH(),pUp.getW(), (int*)sprite);
 }
 
+/** Array containing all the powerup types */
 PowerUpType* PowerUpTypes[3] = {
     new PaddleSizePUpType(0, *powerUpPaddleSizeSprite),
     new PaddleSpeedPUpType(1, *powerUpPaddleSpeedSprite),
     new BallSpeedPUpType(2, *powerUpBallSpeedSprite)
 };
 
+/** Constructor */
 PowerUp::PowerUp(float x, float y, PowerUpType& pUpType) : _pUpType(&pUpType)
- {
+{
     pos.x = x;
     pos.y = y;
     velocity.x = 0;
@@ -97,22 +127,25 @@
     h = PowerUpH;
 }
 
-
+/** Draws each powerup on the LCD */
 void PowerUp::draw(N5110 &lcd) {
     //printf("powerup %.02f , %.02f, %d\n", pos.x, pos.y, powerUpSprite[0][2]);
     _pUpType->draw(lcd, *this);
 }
 
+/** Gives the paddle size increase powerup */
 void PaddleSizePUpType::giveBonus(Paddle &paddle, Ball &ball) {
     int add = 2;
     paddle.setW(paddle.getW()+add);
 }
 
+/** Gives the paddle speed increase powerup */
 void PaddleSpeedPUpType::giveBonus(Paddle &paddle, Ball &ball) {
     float add = 0.2f;
     paddle.setSpeed(paddle.getSpeed() + add);
 }
 
+/** Gives the ball speed decrease powerup */
 void BallSpeedPUpType::giveBonus(Paddle &paddle, Ball &ball) {
     float multiply = 0.9f;
     Vector2D& v = ball.getVelocity();
@@ -133,9 +166,11 @@
 /** Constructor */
 Map::Map()
 {
-    levels.push_back(new Level1());
-    levels.push_back(new Level2());
-    reset();
+    score = 0;                      /** Initial score is zero */
+    levels.push_back(new Level1()); /** Stores level 1 in the vector */
+    levels.push_back(new Level2()); /** Stores level 2 in the vector */
+    levels.push_back(new Level3()); /** Stores level 3 in the vector */
+    reset();                        /** Initializes map */
 }
 
 
@@ -145,6 +180,7 @@
 
 }
 
+/** Get the level object based on the current level number and call initBricks() on it */
 void Map::initBricks()
 {
     bricks.clear();
@@ -153,6 +189,7 @@
     level->initBricks(*this);
 }
 
+/** Draws all the bricks on the level, as well as powerUps */
 void Map::drawMap(N5110 &lcd)
 {   
     vector<Brick>::size_type end = bricks.size();
@@ -195,129 +232,143 @@
     d.y = d.y - s * n.y;
 }
 
+/** Resolves the collision */
 void Map::resolveCollision(const Brick &b, GameObject &obj)
 {
-    // take velocity of the ball to determine direction and previous coords
+    /** take velocity of the ball to determine direction and previous coords */
     float vx = obj.getVelocity().x;
     float vy = obj.getVelocity().y;
-    // take the previous x and y coords
+    /** take the previous x and y coords */
     float x = obj.getPos().x - vx;
     float y = obj.getPos().y - vy;
-    // sides of the bricks
+    /** sides of the bricks */
     float leftX = b.x;
     float rightX = b.x + b.w;
-    //float topY = b.y;
-    //float bottomY = b.y + b.h;
 
-    // mid of x and y coords
-    //float cx = b.x + b.w/2.0f;
+    /** mid of x and y coords */
+    //float cx = b.x + b.w/2.0f; not used
     float cy = b.y + b.h/2.0f;
 
+    
     Vector2D &vel = obj.getVelocity();
     float thresh = 0.1;
+    // hit right 
     if (x > rightX - thresh) {
-        // hit right
         reflect(vel, Right);
-    } else if (x < leftX + thresh) {
-        // hit left
+    } 
+    // hit left
+    else if (x < leftX + thresh) {
         reflect(vel, Left);
-    } else if (y < cy) {
-        // hit top
+    } 
+    // hit top
+    else if (y < cy) {
         reflect(vel, Up);
-    } else if (y > cy) {
-        // hit bottom
+    } 
+    // hit bottom
+    else if (y > cy) {
         reflect(vel, Down);
-    } else {
+    } 
+    else {
+        // hit top
         if (vy > 0) {
-            // hit top
             reflect(vel, Up);
-        } else {
-            // hit bottom
+        } 
+        // hit bottom
+        else {
             reflect(vel, Down);
         }
     }
 }
 
+/** Checks if brick <-> ball collided */
 void Map::checkCollision(Ball &ball, Paddle &paddle)
 {
-    // check bricks
+    /** Check all bricks */
     vector<Brick>::size_type end = bricks.size();
     for (int i = 0; i < end; i++) {
         const Brick& b = bricks[i];
         if (rectIntersect(b.x, b.y, b.w, b.h,
                           ball.getPos().x, ball.getPos().y, ball.getW(), ball.getH())) {
-            // brick collision event!
+            /** Brick collision event! */
             
-            // bounce the ball
+            /** Bounce the ball */
             resolveCollision(b, ball);
             
-            // (maybe) spawn power-up
+            /** (maybe) spawn power-up */
             onBrickHit(b);
             
-            // remove brick
+            /** Remove brick */
             bricks.erase(bricks.begin() + i);
             
+            /** Increment score */
+            score = score + 10;
             break;
         
         }
     }
     
-    /** check power ups */
+    /** Check all power ups */
     for (int i = 0; i < powerUps.size(); i++) 
     {
         PowerUp& p = powerUps[i];
         /** use rectIntersect to check for collisions between power-up and pad */
         if (rectIntersect(p.getPos().x, p.getPos().y, p.getW(), p.getH(),
                           paddle.getPos().x, paddle.getPos().y, paddle.getW(), paddle.getH())) {
+            /** If collided give bonus and remove powerup */
+            p.giveBonus(paddle, ball);                
+            powerUps.erase(&p);
             
-            p.giveBonus(paddle, ball);
-                            
-            powerUps.erase(&p);
             break;             
         }
                           
     }
 }
 
+/** Creates powerUps depending on the PowerUpDropChancePct */
 void Map::onBrickHit(const Brick& brick) {
+    /** spawn power up */
     if ((rand() % 100) < PowerUpDropChancePct) {
-        // spawn power up
-        int type = rand() % 3;  // each power up has the same rarity
+        
+        int type = rand() % 3;  /** each power up has the same rarity */
         
         //printf("Power up - t=%d", type);
-        PowerUpType& pUpType = *PowerUpTypes[type];
+        PowerUpType& pUpType = *PowerUpTypes[type]; /** assign the pUpType to the type we got */
         
         //printf(", pUpType=%d\n", pUpType.type);
         
+        /** store the powerup in the powerups vector */
         powerUps.push_back( 
             PowerUp(brick.x+(brick.w/2)-PowerUpW/2, brick.y, pUpType)
         );
        }
 }
 
+/** Updates the moving powerups on map */
 void Map::update() {
     for (int i = 0; i < powerUps.size(); i++) {
         powerUps[i].move();
     }
 }
 
+/** Checks if we cleared the level and if we won */
 bool Map::checkLevel()
-{
+{   
+    /** cleared level */
     if (bricks.size() == 0) {
-        // cleared level
-        ++currentLevel;
+        ++currentLevel;     /** increment to next level */
         // printf("cleared level! %d %d\n", currentLevel, hasWon());
         if (!hasWon()) {
-            // initialize next level
-            initBricks();
+            initBricks();   /** initialize next level */
         }
         return true;
     }
     return false;
 }
 
+/** Resets the whole map when game is (re-)started */
 void Map::reset()
 {
+    score = 0;
     currentLevel = 0;
     initBricks();
     powerUps.clear();