2020-06-07 22:14:08 +03:00
|
|
|
#include "SynEngine.hpp"
|
|
|
|
|
2020-06-08 15:51:54 +03:00
|
|
|
class Grid : public Drawable
|
2020-06-07 22:14:08 +03:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
sf::Vector2i offset;
|
|
|
|
sf::Vector2i gridSize;
|
2020-06-08 15:51:54 +03:00
|
|
|
Cell *cellArray;
|
|
|
|
unsigned int cellCount;
|
2020-06-07 22:14:08 +03:00
|
|
|
|
2020-06-08 15:51:54 +03:00
|
|
|
Cell *CreateCellArray(sf::Vector2i);
|
2020-06-07 22:14:08 +03:00
|
|
|
public:
|
|
|
|
Grid();
|
2020-06-08 15:51:54 +03:00
|
|
|
bool SetGrid();
|
|
|
|
void UpdateCell(unsigned int, unsigned int, unsigned int, int = -1);
|
|
|
|
void Draw(sf::RenderWindow *);
|
2020-06-07 22:14:08 +03:00
|
|
|
~Grid();
|
|
|
|
};
|
|
|
|
|
|
|
|
Grid::Grid()
|
|
|
|
{
|
2020-06-08 15:51:54 +03:00
|
|
|
cellArray = NULL;
|
2020-06-07 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 15:51:54 +03:00
|
|
|
Cell *Grid::CreateCellArray(sf::Vector2i)
|
2020-06-07 22:14:08 +03:00
|
|
|
{
|
2020-06-08 15:51:54 +03:00
|
|
|
cellCount = gridSize.x *gridSize.y;
|
|
|
|
Cell *array = new Cell[cellCount];
|
2020-06-07 22:14:08 +03:00
|
|
|
|
2020-06-08 15:51:54 +03:00
|
|
|
if(!array) return NULL;
|
2020-06-07 22:14:08 +03:00
|
|
|
|
2020-06-08 15:51:54 +03:00
|
|
|
GameConfiguration *config = GameManager::GetConfig();
|
|
|
|
|
|
|
|
sf::Vector2i cellSize = config -> GetSpriteCellSize();
|
|
|
|
float sizeMultiplier = config -> GetSpriteSizeMultiplier();
|
|
|
|
int xPos;
|
|
|
|
int yPos;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < cellCount; i++)
|
|
|
|
{
|
|
|
|
xPos = (i % gridSize.x) * cellSize.x * sizeMultiplier + offset.x;
|
|
|
|
yPos = (i / gridSize.x) * cellSize.y * sizeMultiplier + offset.y;
|
|
|
|
|
|
|
|
(array + i) -> Set(xPos, yPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Grid::SetGrid()
|
|
|
|
{
|
|
|
|
GameConfiguration *config = GameManager::GetConfig();
|
|
|
|
|
|
|
|
if(!config) return false;
|
|
|
|
|
|
|
|
sf::Vector2i screenDimensions = config -> GetScreenDimensions();
|
|
|
|
gridSize = config -> GetGridSize();
|
|
|
|
offset = config -> GetGridOffset();
|
|
|
|
|
|
|
|
cellArray = CreateCellArray(gridSize);
|
2020-06-07 22:14:08 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:51:54 +03:00
|
|
|
void Grid::UpdateCell(unsigned int x, unsigned int y, unsigned int spriteIndex, int direction)
|
|
|
|
{
|
|
|
|
int index = x + gridSize.x * y;
|
|
|
|
Cell *cell = cellArray + index;
|
|
|
|
|
|
|
|
cell -> UpdateSprite(spriteIndex);
|
|
|
|
if(direction >= 0)
|
|
|
|
cell -> UpdateRotation(direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Grid::Draw(sf::RenderWindow *window)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < cellCount; i++)
|
|
|
|
(cellArray + i) -> Draw(window);
|
|
|
|
}
|
|
|
|
|
2020-06-07 22:14:08 +03:00
|
|
|
Grid::~Grid()
|
|
|
|
{
|
2020-06-08 15:51:54 +03:00
|
|
|
delete[] cellArray;
|
2020-06-07 22:14:08 +03:00
|
|
|
}
|