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