-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
287 lines (242 loc) · 7.47 KB
/
Game.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "Game.h"
#include <windows.h>
Game::Game(void)
{
}
Game::Game(Board *pBoard, Pieces *pPieces, IO *pIO, int pScreenHeight)
{
mScreenHeight = pScreenHeight;
// Get the pointer to the Board and Pieces classes
mBoard = pBoard;
mPieces = pPieces;
mIO = pIO;
// Game initialization
InitGame ();
}
Game::~Game(void)
{
}
/*
======================================
Get a random int between to integers
Parameters:
>> pA: First number
>> pB: Second number
======================================
*/
int Game::GetRand (int pA, int pB)
{
return pA + (rand() % (int)(pB - pA + 1));
}
/*
======================================
Initial parameters of the game
======================================
*/
void Game::InitGame()
{
// Init random numbers
srand ((unsigned int) time(NULL));
// First piece
mPiece = GetRand (0, 6);
mRotation = GetRand (0, 3);
mPosX = (BOARD_WIDTH / 2) + mPieces->GetXInitialPosition (mPiece, mRotation);
mPosY = mPieces->GetYInitialPosition (mPiece, mRotation);
// Next piece
mNextPiece = GetRand (0, 6);
mNextRotation = GetRand (0, 3);
mNextPosX = BOARD_WIDTH + 4;
mNextPosY = 3;
// Stored Piece
mStoredPiece = GetRand (0,6);
mStoredRotation = GetRand (0,3);
mStoredPosX = BOARD_WIDTH + 4;
mStoredPosY = 10;
mBoard->InitBoard();
WAIT_TIME = WAIT_TIME_DEFAULT;
WAIT_TIME_AMOUNT = WAIT_TIME_DEFAULT;
}
/*
======================================
Create a random piece
======================================
*/
void Game::CreateNewPiece()
{
// The new piece
mPiece = mNextPiece;
mRotation = mNextRotation;
mPosX = (BOARD_WIDTH / 2) + mPieces->GetXInitialPosition (mPiece, mRotation);
mPosY = mPieces->GetYInitialPosition (mPiece, mRotation);
// Random next piece
mNextPiece = GetRand (0, 6);
mNextRotation = GetRand (0, 3);
}
void Game::StorePiece()
{
int tmpPiece = mPiece;
int tmpRotation = mRotation;
int tmpPosX = mPosX;
int tmpPosY = mPosY;
mPiece = mStoredPiece;
mRotation = mStoredRotation;
//will reset its pos
mPosX = (BOARD_WIDTH / 2) + mPieces->GetXInitialPosition (mPiece, mRotation);
//mPosY = mPieces->GetYInitialPosition (mPiece, mRotation);
mStoredPiece = tmpPiece;
mStoredRotation = tmpRotation;
}
/*
======================================
Draw piece
Parameters:
>> pX: Horizontal position in blocks
>> pY: Vertical position in blocks
>> pPiece: Piece to draw
>> pRotation: 1 of the 4 possible rotations
======================================
*/
////////////////////////////////////////////////////////change this shit
void Game::DrawPiece (int pX, int pY, int pPiece, int pRotation, bool isNext)
{
sf::Color mColor; // Color of the block
switch (pPiece)
{
case(0):
mColor = sf::Color::Green;
break;
case(1):
mColor = sf::Color::Red;
break;
case(2):
mColor = sf::Color::Yellow;
break;
case(3):
mColor = sf::Color::Blue;
break;
case(4):
mColor = sf::Color::Cyan;
break;
case(5):
mColor = sf::Color(160,32,240);
break;
case(6):
mColor = sf::Color(255,140,0);
break;
}
// Obtain the position in pixel in the screen of the block we want to draw
int mPixelsX = mBoard->GetXPosInPixels (pX);
int mPixelsY = mBoard->GetYPosInPixels (pY);
int fakey = pY;
while (mBoard->IsPossibleMovement(pX,fakey+1,pPiece,pRotation))
{
fakey++;
}
int fakePixelsY = mBoard->GetYPosInPixels(fakey);
// Travel the matrix of blocks of the piece and draw the blocks that are filled
for (int i = 0; i < PIECE_BLOCKS; i++)
{
for (int j = 0; j < PIECE_BLOCKS; j++)
{
if (mPieces->GetBlockType (pPiece, pRotation, j, i) != 0)
mIO->DrawRectangle (mPixelsX + (i * BLOCK_SIZE),
mPixelsY + (j * BLOCK_SIZE),
BLOCK_SIZE -1,
BLOCK_SIZE -1,
mColor);
if (isNext == false)
{
if (mPieces->GetBlockType (pPiece, pRotation, j, i) != 0)
mIO->DrawRectangle (mPixelsX + (i * BLOCK_SIZE),
fakePixelsY + (j * BLOCK_SIZE),
BLOCK_SIZE -1,
BLOCK_SIZE -1,
sf::Color::Black, true);
}
}
}
}
/*
======================================
Draw board
Draw the two lines that delimit the board
======================================
*/
/////////////////////////////////////////////////change this shit
void Game::DrawBoard ()
{
sf::Color mColor;
// Calculate the limits of the board in pixels
int mX1 = BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) - 1;
int mX2 = BOARD_POSITION + (BLOCK_SIZE * (BOARD_WIDTH / 2));
int mY = mScreenHeight - (BLOCK_SIZE * BOARD_HEIGHT);
// Check that the vertical margin is not to small
//assert (mY > MIN_VERTICAL_MARGIN);
// Rectangles that delimits the board
mIO->DrawRectangle (mX1 - BOARD_LINE_WIDTH, mY, BOARD_LINE_WIDTH, mScreenHeight - 1, sf::Color::White);
mIO->DrawRectangle (mX2, mY, BOARD_LINE_WIDTH, mScreenHeight - 1, sf::Color::White);
mIO->DrawRectangle (15, 100, 140, 230, sf::Color::Transparent, true);
mIO->DrawRectangle (480, 100, 140, 290, sf::Color::Transparent, true);
// Check that the horizontal margin is not to small
//assert (mX1 > MIN_HORIZONTAL_MARGIN);
// Drawing the blocks that are already stored in the board
mX1 += 1;
for (int i = 0; i < BOARD_WIDTH; i++)
{
for (int j = 0; j < BOARD_HEIGHT; j++)
{
// Check if the block is filled, if so, draw it
if (!mBoard->IsFreeBlock(i, j))
{
switch (mBoard->mBoard[i][j])
{
case(1):
mColor = sf::Color::Green;
break;
case(2):
mColor = sf::Color::Red;
break;
case(3):
mColor = sf::Color::Yellow;
break;
case(4):
mColor = sf::Color::Blue;
break;
case(5):
mColor = sf::Color::Cyan;
break;
case(6):
mColor = sf::Color(160,32,240);
break;
case(7):
mColor = sf::Color(255,140,0);
break;
}
mIO->DrawRectangle ( mX1 + (i * BLOCK_SIZE),
mY + (j * BLOCK_SIZE),
BLOCK_SIZE - 1,
BLOCK_SIZE - 1,
mColor);
}
}
}
}
/*
======================================
Draw scene
Draw all the objects of the scene
======================================
*/
void Game::DrawScene ()
{
DrawBoard (); // Draw the delimitation lines and blocks stored in the board
//DrawPiece (0, 0, mPiece, mRotation);
DrawPiece (mPosX, mPosY, mPiece, mRotation); // Draw the playing piece
DrawPiece (mStoredPosX, mStoredPosY, mStoredPiece, mStoredRotation, true);
DrawPiece (mNextPosX, mNextPosY, mNextPiece, mNextRotation, true); // Draw the next piece
for(int i = 0; i < mBoard->particleEngines.size(); i++)
{
mBoard->particleEngines[i].Draw(&(mIO->window));
}
//mIO->DrawScore(mBoard->mScore);
}