-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.h
102 lines (80 loc) · 2.6 KB
/
Shape.h
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
//
// Created by kali on 2/28/22.
//
#ifndef CSCCLUB_TETRIS_SHAPE_H
#define CSCCLUB_TETRIS_SHAPE_H
#include "Color.h"
// /(•ㅅ•)\ Will contain:
// - 2D array of (x,y) coordinates for each shape
// - Designation for tile patterns
// - 2D array of boolean for each square that needs to be checked for each type of collision
// - int for rotation state
struct Shape
{
struct Square
{
int x=0, y=0;
constexpr Square() = default;
constexpr Square(int x, int y): x(x), y(y) {};
};
// x and y correspond to the pivot point
int x=0, y=0;
Square* shape;
// shortcut way of accessing shape
Square operator[](int index) const { return shape[index]; }
Color color;
enum Piece { NONE=-1,
O, I, J, L, Z, S, T,
// allows iteration
SIZE
} piece = NONE;
static constexpr int N_SQUARES = 4;
Shape(Piece);
Shape();
bool rotateR() { return rotate(-1, +1); }
bool rotateL() { return rotate(+1, -1); }
bool moveDown() { return move(0,1); }
bool moveR() { return move(1,0); }
bool moveL() { return move(-1,0); }
// sets position where x,y is the position of the top left tile.
void setPos(int x, int y=0) {
auto start = getStartingPos();
this->x = start.x + x;
this->y = start.y + y;
}
void setPos(Square s) { setPos(s.x,s.y); }
Shape(Square *shape)
: shape(shape) {};
// ensures that the values and not the array itself is copied.
Shape(const Shape& shape)
: Shape(new Square[4]{shape[0], shape[1], shape[2], shape[3]})
{};
~Shape() {
delete shape;
}
// moves horizontally according to a direction. 0 does nothing and returns false
constexpr bool move(int dx)
{ return dx != 0 && move(dx,0); }
// rotates according to a direction. 0 does nothing and returns false.
constexpr bool rotate(int dir)
{ return dir != 0 && rotate(dir, -dir); }
bool isInvalidState()
{
for(int i=0; i < N_SQUARES; i++)
if(isInvalidPosition(shape[i])) return true;
return false;
}
// necessary offset for the shape to get rendered in the grid
Square getStartingPos() const;
private:
static bool isInvalidPosition(int x, int y);
bool isInvalidPosition(Square s) const
{ return isInvalidPosition(s.x+x,s.y+y); }
bool move(int x,int y);
bool rotate(int x, int y, bool=false);
int nRotation = 0;
int nRotations = 0;
// maximum size for a rotation
Square rotations[4][4];
};
#endif //CSCCLUB_TETRIS_SHAPE_H