-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
387 lines (361 loc) · 8.97 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include "Game.h"
#include "ScoreScreen.h"
#include "WorldConstants.h"
#include "InputHelper.h"
using namespace std;
using namespace world_constants;
Game::Game(ScreenBuffer *screenBuffer):
Screen(screenBuffer),
m_paused(false),
m_timeStart(CLOCK::now()),
m_standardStepTime(1100),
m_currentStepTime(m_standardStepTime),
m_stepTimeFast(50),
m_currentStone(),
m_nextStone(),
m_removedLinesLevel(0),
m_removedLinesTotal(0),
m_level(0),
m_score(0)
{
m_currentStone.toFieldStartPos();
m_standardStepTime = (1000 - (m_level * 100)) + 100;
// Handle window closing itself instead of parent class
m_handleScreenClosing = false;
}
void Game::update()
{
if (!m_paused)
{
removeFullRows();
if (isStepTimeLeft())
{
updateTimeAffected();
m_draw = true;
m_timeStart = CLOCK::now();
}
// Set the current step time back to its standard so its only
// faster as long the player press the specific key
m_currentStepTime = m_standardStepTime;
}
}
bool Game::isCurrentStoneColliding() const
{
if (m_currentStone.getLeft() < 0)
return true;
if (m_currentStone.getRight() >= (world_constants::FIELD_COLUMN))
return true;
if (m_currentStone.getBottom() == world_constants::FIELD_ROW)
return true;
bool collidingWithFallen = false;
for (FallenStone fallenStone : m_fallenStones)
{
if(m_currentStone.isCollidingWithPoint(fallenStone.getPosition()))
{
collidingWithFallen = true;
}
}
return collidingWithFallen;
}
bool Game::isStepTimeLeft() const
{
CLOCK::time_point now = CLOCK::now();
chrono::duration<float> timeSpan = (now - m_timeStart);
auto milli = chrono::duration_cast<chrono::milliseconds>(timeSpan);
auto time = milli.count();
if (time >= m_currentStepTime)
{
return true;
}
return false;
}
void Game::cleanFullRow(const int row)
{
auto remove_st = remove_if(m_fallenStones.begin(), m_fallenStones.end(),
[row](FallenStone &fallenStone)
{
return fallenStone.getPosition().getIntY() == row;
});
m_fallenStones.erase(remove_st, m_fallenStones.end());
// Now move the Stone down which are over the deleted line
for (FallenStone &fallenStone : m_fallenStones)
{
if (fallenStone.getPosition().getIntY() < row)
{
fallenStone.moveDown();
}
}
}
void Game::removeFullRows()
{
// Count the remove lines so we can later calculate the score
unsigned int removedLines = 0;
bool rowDeletion = true;
while (rowDeletion)
{
rowDeletion = false;
// The Amount of fallen stones in the rows
unsigned int fallenStonesRow[world_constants::FIELD_ROW] = { 0 };
for (FallenStone fallenStone : m_fallenStones)
{
// Increase the number of the row where the fallen stone is
(fallenStonesRow[fallenStone.getPosition().getIntY()])++;
}
for (int i = 0; i != world_constants::FIELD_ROW; i++)
{
int actualRow = i;
// Remove the stones which are in a full row
if (fallenStonesRow[actualRow] == world_constants::FIELD_COLUMN)
{
cleanFullRow(actualRow);
// Set the row Deleted to true and break the loop so we can restart
// checking if a row is full, because by moving down
// now there can be new full lines
rowDeletion = true;
removedLines++;
break;
}
}
}
if (removedLines > 0)
{
m_removedLinesLevel += removedLines;
m_removedLinesTotal += removedLines;
if (m_removedLinesLevel >= 10 && m_level <= 10)
{
m_level++;
m_standardStepTime = (1000 - (m_level * 100)) + 100;
m_currentStepTime = m_standardStepTime;
// Add the rest lines to the actual level lines
m_removedLinesLevel = m_removedLinesLevel % 10;
}
m_score += (40 * (m_level + 1) * pow(2 , removedLines));
}
}
void Game::leaveGame()
{
// Only leave game and show screen when game is not allready leaved
if (m_running)
{
// leave the game
m_running = false;
// Show the players score
ScoreScreen scoreScreen(m_screenBuffer, m_score);
scoreScreen.run();
}
}
bool Game::isGameOver()
{
return m_currentStone.getBottom() <= 0;
}
void Game::spawnStone()
{
m_currentStone = m_nextStone;
m_currentStone.toFieldStartPos();
m_nextStone = Stone();
}
void Game::updateTimeAffected()
{
/*
* Move down the last added stone, because its
* the actual stone which the user can control
*/
m_currentStone.moveDown();
/*
* If the Stone collides with the ground "freeze" the Stone
* (Add the Stone to the m_stones container so it is not longer
* under the users control.
*/
if (isCurrentStoneColliding())
{
m_currentStone.restoreOldPosition();
if (isGameOver())
{
leaveGame();
}
PointF subStones[4];
m_currentStone.fillWithGlobalPoints(subStones);
for (PointF subStone : subStones)
{
FallenStone fallenStone(subStone, m_currentStone.getShape());
m_fallenStones.push_back(fallenStone);
}
spawnStone();
removeFullRows();
}
}
void Game::handleInput()
{
// First check if there was an input
if (m_command != '\0')
{
if (m_command == '0')
{
m_paused = !m_paused;
}
else if (!m_paused)
{
if (m_command == 'a')
{
m_currentStone.moveLeft();
}
else if (m_command == 'd')
{
m_currentStone.moveRight();
}
else if (m_command == 's')
{
// The stone should fall faster as long the button is pressed
m_currentStepTime = m_stepTimeFast;
}
else if (m_command == 'o')
{
m_currentStone.rotateLeft();
}
else if (m_command == 'p')
{
m_currentStone.rotateRight();
}
// Debuging commands
else if (m_command == '1')
{
spawnStone();
}
if (isCurrentStoneColliding())
{
// Restore the Stones old Position if it is colliding with something
m_currentStone.restoreOldPosition();
/*
if (isGameOver())
{
leaveGame();
}*/
}
}
// Game is paused
else
{
// Leave Game if player press c and game is paused
if (m_command == 'c')
{
leaveGame();
}
}
m_command = '\0';
// Something has changed so the Screen should redraw
m_draw = true;
}
}
void Game::drawStats()
{
// First store all string in a array which should drawm later
string stats[3] = {
"Score: " + to_string(m_score),
"Lines: " + to_string(m_removedLinesTotal),
"Level: " + to_string(m_level)
};
// Now we have to convert then to chars so we can store then in the ScreenBuffer
for (int i = 0; i != 3; i++)
{
string stat = stats[i];
for (size_t j = 0; j != stat.size(); j++)
{
char c = stat[j];
m_screenBuffer->add(STAT_START_X + j, STAT_START_Y + i, c);
}
}
}
void Game::drawNextStone()
{
// Store the Next Stone Box in buffer
for (int y = NEXTSTONE_BOX_START_Y; y != NEXTSTONE_BOX_START_Y + 6; y++)
{
for (int x = NEXTSTONE_BOX_START_X; x != NEXTSTONE_BOX_START_X + 9; x++)
{
if (x == NEXTSTONE_BOX_START_X || x == NEXTSTONE_BOX_START_X + 8 ||
y == NEXTSTONE_BOX_START_Y || y == NEXTSTONE_BOX_START_Y + 5)
{
m_screenBuffer->add(x, y, '#');
}
}
}
//Store the Next Stone in buffer
m_nextStone.fillScreenBuffer
(NEXTSTONE_BOX_START_X + 4, NEXTSTONE_BOX_START_Y + 3, m_screenBuffer);
}
void Game::drawGameField()
{
// Store the Game Screen with borders and the ground in buffer
for (int y = FIELD_START_Y; y != FIELD_START_Y + FIELD_WHOLE_HEIGHT; y++)
{
for (int x = FIELD_START_X; x != FIELD_START_X + FIELD_WHOLE_WIDTH; x++)
{
// Store the ground
if (y == FIELD_START_Y + FIELD_WHOLE_HEIGHT - 1)
{
m_screenBuffer->add(x, y, '#');
}
// Store the borders left and right in the Screen
else if (x == FIELD_START_X || x == FIELD_START_X + FIELD_WHOLE_WIDTH - 1)
{
m_screenBuffer->add(x, y, '#');
}
// Store the empty Screen
else
{
m_screenBuffer->add(x, y, ' ');
}
}
}
}
void Game::drawFallenStones()
{
// Store the fallen Stones in buffer
for (FallenStone fallenStone : m_fallenStones)
{
fallenStone.fillScreenBuffer
(FIELD_START_X + 1, FIELD_START_Y, m_screenBuffer);
}
}
void Game::drawControlInfo()
{
const string TextPause = "[0] Pause/Unpause";
// Draw the text right to the field
const int TextX = FIELD_START_X + FIELD_WHOLE_WIDTH + 2;
const int TextY = 3;
m_screenBuffer->add(TextX, TextY, TextPause);
}
void Game::drawPauseInfo()
{
if (m_paused)
{
const string Text = "--PAUSED--";
const int TextY = static_cast<int> (SCREEN_HEIGHT / 2);
const int TextX = static_cast<int>
(FIELD_START_X + (FIELD_WHOLE_WIDTH / 2) - (Text.length() / 2) );
m_screenBuffer->add(TextX, TextY, Text);
const string TextClose = "--Press c to close--";
const int TextCloseY = static_cast<int> ( (SCREEN_HEIGHT / 2) + 1);
const int TextCloseX = static_cast<int>
(FIELD_START_X + (FIELD_WHOLE_WIDTH / 2) - (TextClose.length() / 2) );
m_screenBuffer->add(TextCloseX, TextCloseY, TextClose);
}
}
void Game::fillScreenBuffer()
{
// Store the statistics in buffer
drawStats();
drawNextStone();
drawGameField();
drawFallenStones();
drawControlInfo();
// FIELD_START_X + 1 because we have a border with a with of one,
// so start after the left border
m_currentStone.fillScreenBuffer(FIELD_START_X + 1, FIELD_START_Y, m_screenBuffer);
drawPauseInfo();
}