-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetrominoes.cpp
55 lines (51 loc) · 1.17 KB
/
Tetrominoes.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
#include "Tetrominoes.h"
Tetromino::Tetromino(const bool* shape, int dimension, Color color, const Board& board)
:
shape(shape),
dimension(dimension),
color(color),
boardPos(board.GetWidth() / 2 - (dimension / 2), 0),
board(board),
currentRotation(Rotation::UP)
{
}
void Tetromino::RotateClockwise()
{
currentRotation = Rotation((int(currentRotation) + 1) % 4);
}
void Tetromino::RotateCounterClockwise()
{
// Number 3 is enum size - 1
currentRotation = Rotation((int(currentRotation) + 3) % 4);
}
void Tetromino::Draw() const
{
for (int y = 0; y < dimension; ++y)
{
for (int x = 0; x < dimension; ++x)
{
bool cell = false;
switch (currentRotation)
{
case Tetromino::Rotation::UP:
cell = shape[y * dimension + x];
break;
case Tetromino::Rotation::RIGHT:
cell = shape[dimension * (dimension - 1) - dimension * x + y];
break;
case Tetromino::Rotation::DOWN:
cell = shape[(dimension * dimension - 1) - dimension * y - x];
break;
case Tetromino::Rotation::LEFT:
cell = shape[dimension - 1 + dimension * x - y];
break;
default:
break;
}
if (cell)
{
board.DrawCell(boardPos + Vec2<int>{x,y}, color);
}
}
}
}