Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Map/Map.cpp

Committer:
kocemax
Date:
2019-04-08
Revision:
6:39bda45efeed
Parent:
5:12c179da4788
Child:
7:cd3cafda3dd4

File content as of revision 6:39bda45efeed:

#include "Map.h"
#include "Ball.h"


// Constructor
Map::Map()
{
    
}

// Destructor
Map::~Map()
{

}

void Map::initBricks()
{
    int w = 5;
    int h = 1;
    int gap = 1;
    
    int y = 0;
    
    for (int j = 0; j < 15; ++j) {
        int x = 0;
        for (int i = 0; i < 14; ++i) {
            Brick b;
            b.x = x;
            b.y = y;
            b.w = w;
            b.h = h;
            bricks.push_back(b);
            
            x += w + gap;
        } 
        y += h + gap;
    }  
}

void Map::drawMap(N5110 &lcd) 
{   
    vector<Brick>::size_type end = bricks.size();
    for (int i = 0; i < end; i++) 
    {
        const Brick& b = bricks[i];
        lcd.drawRect(b.x,b.y,b.w,b.h,FILL_BLACK);
    }
}

//Taken from a forum, took me a bit to understand/main collision detection
bool isIntersect(
  int Ax, int Ay, int Aw, int Ah,
  float Bx, float By, float Bw, float Bh)
{
  return
    Bx + Bw > Ax &&
    By + Bh > Ay &&
    Ax + Aw > Bx &&
    Ay + Ah > By;
}

void Map::checkCollision(GameObject &obj)
{
    vector<Brick>::size_type end = bricks.size();
    for (int i = 0; i < end; i++) 
    {
        const Brick& b = bricks[i];
        if (isIntersect(b.x, b.y, b.w, b.h,
            obj.getballPos().x, obj.getballPos().y, obj.getW(), obj.getH())) {
            bricks.erase(bricks.begin() + i);
            break;
        }
    }
}

void Map::destroyMap(N5110 &lcd, Gamepad &pad, Ball &ball) 
{   
/*
    Vector2D posBall = ball.get_ballPos(pad);
    vector<Centerpoints>::size_type end = point.size();
    for (int i = 0; i < end; i++) 
    {
        for (int j = 0; j < 5; j++)
        {
            if (posBall.x == point[i].x+j)
            {
               point.at(i).x = 70;
               point.at(i).y = 40;
               printf("collided\n");
            }
        }
    }
    */
}