Tetris/map.cpp
2014-11-07 20:47:50 +01:00

141 lines
1.9 KiB
C++

#include "map.hpp"
map::map()
{
reset();
}
void map::reset()
{
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
data[y][x] = 0;
}
}
}
bool map::isCollision(part Part, bool strict)
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
if (strict)
{
if ( (Part.getPosY() + y >= 0) &&
( ( data[Part.getPosY() + y][Part.getPosX() + x] && Part.getElement(x, y) ) ||
( Part.getElement(x, y) && Part.getPosX() + x >= WIDTH)) )
{
return true;
}
}
else
{
if ( (Part.getPosY() + y >= 0) &&
( ( data[Part.getPosY() + y + 1][Part.getPosX() + x] && Part.getElement(x, y) ) ||
( Part.getElement(x, y) && Part.getPosY() + y >= HEIGHT - 1)) )
{
return true;
}
}
}
}
return false;
}
void map::addPart(part Part)
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
if (Part.getElement(x, y))
data[Part.getPosY() + y][Part.getPosX() + x] = Part.getColor() + 1;
}
}
}
void map::applyGravity(int h)
{
int tmp = 0;
for (int y = h; y > -1; y--)
{
for (int x = 0; x < WIDTH; x++)
{
if (y < (HEIGHT - 1) && data[y + 1][x] == 0 && data[y][x])
{
tmp = data[y + 1][x];
data[y + 1][x] = data[y][x];
data[y][x] = 0;
}
}
}
}
int map::destroyLines()
{
int pocet = 0;
for (int y = 0; y < HEIGHT; y++)
{
bool good = true;
for (int x = 0; x < WIDTH; x++)
{
if (!data[y][x])
{
good = false;
break;
}
}
if (good)
{
for (int x = 0; x < WIDTH; x++)
{
data[y][x] = 0;
}
applyGravity(y);
pocet++;
y = 0;
}
}
return pocet;
}
bool map::isFull()
{
for (int x = 0; x < WIDTH; x++)
{
if (data[0][x])
{
return true;
}
}
return false;
}
void map::draw(SDL_Surface *screen)
{
for(int i = 0; i < HEIGHT; i++)
{
for(int m = 0; m < WIDTH; m++)
{
if(data[i][m])
{
drawQuad(screen, m * SIZE, i * SIZE, colors[data[i][m] - 1]);
}
}
}
}