MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
piece.cpp
Go to the documentation of this file.
1#include "piece.hpp"
2
3#include <algorithm>
4#include <array>
5#include <cstdlib>
6
7#include "game_grid.hpp"
8
9namespace mutatris {
10
12 : grid(owner) {
13 }
14
15 void Piece::reset() {
16 x = grid->width() / 2;
17 y = 0;
18 do {
19 for (Block &block : blocks) {
20 block.color = 1 + (std::rand() % 7);
21 }
22 } while (blocks[0].color == blocks[1].color && blocks[0].color == blocks[2].color);
23 direction = 0;
24 }
25
27 const std::array<Block, 3> previous = blocks;
28 blocks[0] = previous[2];
29 blocks[1] = previous[0];
30 blocks[2] = previous[1];
31 }
32
33 bool Piece::checkLocation(int next_x, int next_y) const {
34 std::array<const Block *, 3> target{};
35 switch (direction) {
36 case 0:
37 target = {grid->at(next_x, next_y), grid->at(next_x, next_y + 1), grid->at(next_x, next_y + 2)};
38 break;
39 case 1:
40 target = {grid->at(next_x, next_y), grid->at(next_x + 1, next_y), grid->at(next_x + 2, next_y)};
41 break;
42 case 2:
43 target = {grid->at(next_x, next_y), grid->at(next_x - 1, next_y), grid->at(next_x - 2, next_y)};
44 break;
45 case 3:
46 target = {grid->at(next_x, next_y), grid->at(next_x, next_y - 1), grid->at(next_x, next_y - 2)};
47 break;
48 default:
49 break;
50 }
51
52 return std::all_of(target.begin(), target.end(), [](const Block *block) {
53 return block != nullptr && block->color == 0;
54 });
55 }
56
58 if (checkLocation(x - 1, y)) {
59 --x;
60 }
61 }
62
64 if (checkLocation(x + 1, y)) {
65 ++x;
66 }
67 }
68
70 if (checkLocation(x, y + 1)) {
71 ++y;
72 return false;
73 } else {
74 return setBlock();
75 }
76 }
77
78 bool Piece::drop() {
79 while (checkLocation(x, y + 1)) {
80 ++y;
81 }
82 return setBlock();
83 }
84
86 const int oldDirection = direction;
87 direction = (direction + 1) % 4;
88 if (!checkLocation(x, y)) {
89 direction = oldDirection;
90 }
91 }
92
94 std::array<Block *, 3> target{};
95 switch (direction) {
96 case 0:
97 target = {grid->at(x, y), grid->at(x, y + 1), grid->at(x, y + 2)};
98 break;
99 case 1:
100 target = {grid->at(x, y), grid->at(x + 1, y), grid->at(x + 2, y)};
101 break;
102 case 2:
103 target = {grid->at(x, y), grid->at(x - 1, y), grid->at(x - 2, y)};
104 break;
105 case 3:
106 target = {grid->at(x, y), grid->at(x, y - 1), grid->at(x, y - 2)};
107 break;
108 default:
109 break;
110 }
111
112 for (std::size_t i = 0; i < target.size(); ++i) {
113 if (target[i] == nullptr || target[i]->color != 0) {
114 return false;
115 }
116 target[i]->color = blocks[i].color;
117 target[i]->clearElapsedMs = 0;
118 }
119 reset();
120 return true;
121 }
122
123} // namespace mutatris
Piece(GameGrid *owner)
Definition piece.cpp:11
bool drop()
Definition piece.cpp:78
bool checkLocation(int next_x, int next_y) const
Definition piece.cpp:33
bool setBlock()
Definition piece.cpp:93
void shiftDirection()
Definition piece.cpp:85
bool moveDown()
Definition piece.cpp:69
void shiftColors()
Definition piece.cpp:26
void moveLeft()
Definition piece.cpp:57
void moveRight()
Definition piece.cpp:63
void reset()
Definition piece.cpp:15