-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.h
88 lines (73 loc) · 1.72 KB
/
piece.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
#ifndef PIECE_H
#define PIECE_H
#include <QPoint>
#include <array>
// shape type
enum class ShapeType : int {
SquareShape,
LineShape,
MirroredLShape,
SShape,
ZShape,
LShape,
TShape
};
#ifdef QT_DEBUG
const char * to_string(ShapeType type);
#endif
// rotation
enum class Rotation : int {
rot_0,
rot_90,
rot_180,
rot_270,
};
#ifdef QT_DEBUG
const char * to_string(Rotation rot);
#endif
class QGraphicsRectItem;
class QGraphicsScene;
class QGraphicsPixmapItem;
class QPixmap;
// a tetris piece
class Piece
{
public:
enum Action {
no_action,
move_up,
move_down,
move_left,
move_right,
rotate_left,
rotate_rigth
};
Piece() = delete;
Piece(const Piece&) = delete;
Piece(QGraphicsScene& scene, ShapeType type, Rotation rotation, const QPoint &pos);
~Piece();
// shape type
ShapeType type() const { return _type; }
// rotation
Rotation rotation() const { return _rotation; }
// set position
void set_pos(const QPoint &pos);
// move/rotate piece
bool action(Action action, bool check_collines = true);
// collide with something
bool isColliding();
// move elements pointers
std::array<QGraphicsPixmapItem *,4> move_elements();
private:
QGraphicsScene& _scene;
ShapeType _type;
Rotation _rotation;
std::array<QGraphicsPixmapItem *,4> _elements = {};
QPoint _position;
Action _last_action = no_action;
static QPixmap * _tiles;
// redraw blocks regarding to rotation, type and point
void redraw();
Rotation rotate(int versus) const;
};
#endif // PIECE_H