-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
105 lines (89 loc) · 2.27 KB
/
Board.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "Board.h"
#include <assert.h>
/* Cell Realizations */
Board::Cell::Cell()
:
bExists(false),
color(WHITE)
{
}
void Board::Cell::SetColor(Color color)
{
this->color = color;
bExists = true;
}
// Method to remove the Cell // We do not care about color because removed tile is not even shown up
void Board::Cell::Remove()
{
bExists = false;
}
Color Board::Cell::GetColor() const
{
return color;
}
bool Board::Cell::Exists() const
{
return bExists;
}
/* Board Realizations */
Board::Board(Vec2<int> screenPos, Vec2<int> widthHeight, int cellSize, int padding)
:
screenPos(screenPos),
width(widthHeight.GetX()),
height(widthHeight.GetY()),
cellSize(cellSize),
padding(padding)
{
assert(this->width > 0 && this->height > 0); // If assertion triggers : The width or height is smaller than 0
assert(this->cellSize > 0); // If assertion triggers : The cellSize is smaller than 0
cells.resize(this->width * this->height);
}
void Board::SetCell(Vec2<int> pos, Color color)
{
assert(pos.GetX() >= 0 && pos.GetY() >= 0 && pos.GetX() < width && pos.GetY() < height); // If assertion triggers : x or y is out of bounds
cells[pos.GetY() * width + pos.GetX()].SetColor(color); // translate position on 2d board to 1d vector
}
void Board::DrawCell(Vec2<int> pos) const
{
Color c = cells[pos.GetY() * width + pos.GetX()].GetColor();
DrawCell(pos, c);
}
void Board::DrawCell(Vec2<int> pos, Color color) const
{
assert(pos.GetX() >= 0 && pos.GetY() >= 0 && pos.GetX() < width && pos.GetY() < height); // If assertion triggers : x or y is out of bounds
Vec2<int> topLeft = screenPos + padding + (pos * cellSize);
raycpp::DrawRectangle(topLeft, Vec2{ cellSize ,cellSize } - padding, color);
}
void Board::DrawBorder() const
{
raycpp::DrawRectangleLinesEx(screenPos-(cellSize/2),
Vec2{width*cellSize, height*cellSize} + cellSize /*+ padding/2*/,
(float)cellSize/2,
WHITE);
}
void Board::Draw() const
{
for (int iY = 0; iY < height; iY++)
{
for (int iX = 0; iX < width; iX++)
{
if(CellExists({iX,iY}))
{
DrawCell({ iX, iY });
}
}
}
DrawBorder();
}
bool Board::CellExists(Vec2<int> pos) const
{
return cells[pos.GetY()*width + pos.GetX()].Exists();
}
int Board::GetWidth() const
{
return width;
}
int Board::GetHeight() const
{
return height;
}