-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piece.cpp
147 lines (104 loc) · 2.9 KB
/
Piece.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include "nlohmann/json.hpp"
#include "Log.h"
#include "Piece.h"
static std::vector<Piece> all_pieces;
static std::unordered_map<std::string, Piece> all_pieces_map;
void Piece::Create(Piece& aPiece, int blockType) {
for (const Piece& tmpPiece : all_pieces) {
if (tmpPiece.id == blockType) {
aPiece = tmpPiece;
break;
}
}
}
void Piece::Create(Piece& aPiece, std::string blockName) {
aPiece = all_pieces_map[blockName];
}
void Piece::setPosition(int x, int y) {
this->x = x;
this->y = y;
}
void Piece::Rotate() {
int i, j, temp[4][4];
//copy and rotate the piece to the temporary array
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
temp[3 - j][i] = tile[i][j];
}
}
//Copy rotated piece back into original
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
tile[i][j] = temp[i][j];
}
}
}
void Piece::Move(int deltaX, int deltaY) {
x += deltaX;
y += deltaY;
}
void Piece::Delete() {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tile[x][y] = 0;
}
}
x = 0;
y = 0;
}
Piece::~Piece() {
}
bool Piece::Load(const std::string& fileName) { //Load pieces information from JSON file.
std::stringstream logString;
{ // Parse the JSON and create the pieces.
using namespace nlohmann;
ordered_json j;
try {
std::ifstream i(fileName);
j = ordered_json::parse(i);
}
catch (ordered_json::parse_error& ex) {
logString.str("");
logString << "parse error at byte " << ex.byte << std::endl;
LOG_ERROR(logString.str());
}
auto pieces = j.at("pieces");
int n = 0;
for (auto pieceIterator = pieces.begin(); pieceIterator < pieces.end(); pieceIterator++) {
Piece tmpPiece;
tmpPiece.id = pieceIterator->at("id");
tmpPiece.name = pieceIterator->at("name");
//initialize the piece to all blank
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tmpPiece.tile[x][y] = Tile::NODRAW;
}
}
auto shape = pieceIterator->at("shape");
for (auto shapeIterator = shape.begin(); shapeIterator < shape.end(); shapeIterator++) {
tmpPiece.tile[shapeIterator->at(0)][shapeIterator->at(1)] = pieceIterator->at("colorIndex");
}
all_pieces.push_back(tmpPiece);
all_pieces_map[tmpPiece.name] = tmpPiece;
}
}
return true;
}
int Piece::NumPieces() {
//return (int)all_pieces_map.size();
return (int)all_pieces.size();
}
void Piece::Draw(DisplayManager* dm) {
for (int xmy = 0; xmy < 4; xmy++) {
for (int ymx = 0; ymx < 4; ymx++) {
if (tile[xmy][ymx] != Tile::NODRAW) {
dm->DrawTile(x + xmy, y + ymx, tile[xmy][ymx]);
}
}
}
}