MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mutatris::Piece Class Reference

#include <examples/mutatris/piece.hpp>

Public Member Functions

const Blockat (int index) const
bool checkLocation (int next_x, int next_y) const
bool drop ()
int getDirection () const
int getX () const
int getY () const
bool moveDown ()
void moveLeft ()
void moveRight ()
 Piece (GameGrid *owner)
void reset ()
bool setBlock ()
void shiftColors ()
void shiftDirection ()

Detailed Description

Definition at line 13 of file piece.hpp.

Constructor & Destructor Documentation

◆ Piece()

mutatris::Piece::Piece ( GameGrid * owner)
explicit

Definition at line 11 of file piece.cpp.

12 : grid(owner) {
13 }

Member Function Documentation

◆ at()

const Block * mutatris::Piece::at ( int index) const
inlinenodiscard

Definition at line 30 of file piece.hpp.

30 {
31 return index >= 0 && index < static_cast<int>(blocks.size()) ? &blocks[static_cast<std::size_t>(index)] : nullptr;
32 }

◆ checkLocation()

bool mutatris::Piece::checkLocation ( int next_x,
int next_y ) const
nodiscard

Definition at line 33 of file piece.cpp.

33 {
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 }

◆ drop()

bool mutatris::Piece::drop ( )
nodiscard

Definition at line 78 of file piece.cpp.

78 {
79 while (checkLocation(x, y + 1)) {
80 ++y;
81 }
82 return setBlock();
83 }
bool checkLocation(int next_x, int next_y) const
Definition piece.cpp:33
bool setBlock()
Definition piece.cpp:93

◆ getDirection()

int mutatris::Piece::getDirection ( ) const
inlinenodiscard

Definition at line 29 of file piece.hpp.

29{ return direction; }

◆ getX()

int mutatris::Piece::getX ( ) const
inlinenodiscard

Definition at line 27 of file piece.hpp.

27{ return x; }

◆ getY()

int mutatris::Piece::getY ( ) const
inlinenodiscard

Definition at line 28 of file piece.hpp.

28{ return y; }

◆ moveDown()

bool mutatris::Piece::moveDown ( )
nodiscard

Definition at line 69 of file piece.cpp.

69 {
70 if (checkLocation(x, y + 1)) {
71 ++y;
72 return false;
73 } else {
74 return setBlock();
75 }
76 }

◆ moveLeft()

void mutatris::Piece::moveLeft ( )

Definition at line 57 of file piece.cpp.

57 {
58 if (checkLocation(x - 1, y)) {
59 --x;
60 }
61 }

◆ moveRight()

void mutatris::Piece::moveRight ( )

Definition at line 63 of file piece.cpp.

63 {
64 if (checkLocation(x + 1, y)) {
65 ++x;
66 }
67 }

◆ reset()

void mutatris::Piece::reset ( )

Definition at line 15 of file piece.cpp.

15 {
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 }

◆ setBlock()

bool mutatris::Piece::setBlock ( )
nodiscard

Definition at line 93 of file piece.cpp.

93 {
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 }
void reset()
Definition piece.cpp:15

◆ shiftColors()

void mutatris::Piece::shiftColors ( )

Definition at line 26 of file piece.cpp.

26 {
27 const std::array<Block, 3> previous = blocks;
28 blocks[0] = previous[2];
29 blocks[1] = previous[0];
30 blocks[2] = previous[1];
31 }

◆ shiftDirection()

void mutatris::Piece::shiftDirection ( )

Definition at line 85 of file piece.cpp.

85 {
86 const int oldDirection = direction;
87 direction = (direction + 1) % 4;
88 if (!checkLocation(x, y)) {
89 direction = oldDirection;
90 }
91 }

The documentation for this class was generated from the following files: