-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameManger.h
More file actions
391 lines (326 loc) · 10.5 KB
/
GameManger.h
File metadata and controls
391 lines (326 loc) · 10.5 KB
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
388
389
390
391
#ifndef GAMEMANAGER_H
#define GAMEMANAGER_H
#include <ctime>
#include <string>
#include <vector>
#include "SentenceManager.h"
#include "WordBlock.h"
#include "ItemBox.h"
class GameManager
{
private:
// 점수 관련
int totalScore;
int snowflakeScore; // 눈송이 획득 점수
int targetScore; // 목표물 점수
int timeBonus; // 시간 보너스
int levelBonus; // 레벨 보너스
int timePenaltySeconds;
int timeAdjustment; // 아이템 효과로 조정된 시간 (초)
int scoreMultiplier;
std::string lastItemEffectMessage;
time_t lastItemEffectTime;
int collectedSnowmen;
// 시간 관련
time_t startTime; // 게임 시작 시간
int timeLimit; // 제한시간 (초)
int remainingTime; // 남은 시간
bool timeUp; // 시간 초과 여부
// 게임 상태
int currentLevel;
bool gameRunning;
// 단어 이동 제어
time_t lastWordRenderTime;
time_t lastWordCreateTime; // 추가: 단어 생성 시간 추적
int wordRenderInterval;
double wordCreateInterval; // 추가: 단어 생성 간격 (0.5초)
// 단어 생성 제어 추가
int currentWordIndex; // 현재 생성 중인 단어 인덱스 (0-7) // 8개 단어 모두 생성 완료 여부
bool waitingForCompletion; // 완성 대기 중인지
std::vector<int> wordOrder; // 랜덤 순서로 생성할 단어 인덱스 배열
// 점수 계산 상수
static const int SNOWFLAKE_POINTS = 100;
static const int TARGET_POINTS = 500;
static const int TIME_BONUS_MULTIPLIER = 10;
static const int LEVEL_BONUS_BASE = 1000;
public:
// 생성자
GameManager(int level) : currentLevel(level), totalScore(0), snowflakeScore(0),
targetScore(0), timeBonus(0), levelBonus(0),
gameRunning(false), timeUp(false),
wordRenderInterval(1),
lastWordRenderTime(0),
lastWordCreateTime(0),
wordCreateInterval(3.0), // 0.5초에서 3초로 변경
currentWordIndex(0),
timePenaltySeconds(0),
timeAdjustment(0),
scoreMultiplier(1),
lastItemEffectTime(0),
waitingForCompletion(false),
collectedSnowmen(0)
{
// 레벨에 따른 제한시간 설정
switch (level)
{
case 1:
timeLimit = 180;
break; // 3분
case 2:
timeLimit = 150;
break; // 2분 30초
case 3:
timeLimit = 120;
break; // 2분
default:
timeLimit = 180;
break;
}
// 랜덤 시드 초기화
srand(static_cast<unsigned>(time(nullptr)));
// 단어 순서 초기화 (0-7)
initializeWordOrder();
}
// 랜덤 단어 순서 생성
void initializeWordOrder()
{
wordOrder.clear();
// 0-7 인덱스를 순서대로 추가
for (int i = 0; i < 8; i++)
{
wordOrder.push_back(i);
}
// Fisher-Yates 셔플 알고리즘으로 랜덤하게 섞기
for (int i = 7; i > 0; i--)
{
int j = rand() % (i + 1);
std::swap(wordOrder[i], wordOrder[j]);
}
}
// 게임 시작
void startGame(SentenceManager *sentencemanager)
{
startTime = time(nullptr);
gameRunning = true;
timeUp = false;
totalScore = 0;
snowflakeScore = 0;
targetScore = 0;
timeBonus = 0;
levelBonus = currentLevel * LEVEL_BONUS_BASE;
lastWordRenderTime = startTime;
lastWordCreateTime = startTime;
timePenaltySeconds = 0;
timeAdjustment = 0;
scoreMultiplier = 1;
collectedSnowmen = 0;
// 초기화
currentWordIndex = 0;
waitingForCompletion = false;
// 새로운 랜덤 순서 생성
initializeWordOrder();
// 첫 번째 단어 블록 즉시 생성 (랜덤 순서의 첫 번째)
sentencemanager->createWordBlock(60, wordOrder[currentWordIndex]);
currentWordIndex++;
}
// 시간 업데이트 및 카운트다운
void updateTime()
{
if (!gameRunning)
return;
time_t currentTime = time(nullptr);
int elapsedTime = (int)(currentTime - startTime);
remainingTime = timeLimit - elapsedTime - timePenaltySeconds + timeAdjustment;
if (remainingTime <= 0)
{
remainingTime = 0;
timeUp = true;
gameRunning = false;
}
}
// 점수 관련 메서드
void addSnowflakeScore()
{
snowflakeScore += SNOWFLAKE_POINTS;
updateTotalScore();
}
void addTargetScore()
{
targetScore += TARGET_POINTS;
updateTotalScore();
}
// 시간 감소 (객체가 바닥에 떨어졌을 때 페널티)
void applyTimePenalty(int seconds = 10)
{
if (!gameRunning)
return;
timePenaltySeconds += seconds;
updateTime();
// 시간이 0 이하가 되면 게임 종료
if (remainingTime <= 0)
{
remainingTime = 0;
timeUp = true;
gameRunning = false;
}
}
void calculateTimeBonus()
{
if (remainingTime > 0)
{
timeBonus = remainingTime * TIME_BONUS_MULTIPLIER;
updateTotalScore();
}
}
void updateTotalScore()
{
totalScore = targetScore * scoreMultiplier;
}
// Getter 메서드들
int getTotalScore() const { return totalScore; }
int getSnowflakeScore() const { return snowflakeScore; }
int getTargetScore() const { return targetScore; }
int getTimeBonus() const { return timeBonus; }
int getLevelBonus() const { return levelBonus; }
int getRemainingTime() const { return remainingTime; }
int getTimeLimit() const { return timeLimit; }
int getTimeAdjustment() const { return timeAdjustment; }
bool isTimeUp() const { return timeUp; }
bool isGameRunning() const { return gameRunning; }
// 시간 포맷팅 (MM:SS 형식)
std::string getFormattedTime() const
{
int minutes = remainingTime / 60;
int seconds = remainingTime % 60;
char buffer[16];
snprintf(buffer, sizeof(buffer), "%02d:%02d", minutes, seconds);
return std::string(buffer);
}
// 게임 종료
void endGame()
{
gameRunning = false;
calculateTimeBonus();
}
// 게임 상태 확인
bool checkGameEnd()
{
updateTime();
return timeUp || !gameRunning;
}
bool shouldUpdateWordBlocks()
{
time_t now = time(nullptr);
if (difftime(now, lastWordRenderTime) >= wordRenderInterval)
{
lastWordRenderTime = now;
return true;
}
return false;
}
bool shouldCreateWordBlock()
{
// 이미 8개 모두 생성했거나 완성 대기 중이면 생성하지 않음
if (waitingForCompletion)
{
return false;
}
time_t now = time(nullptr);
if (difftime(now, lastWordCreateTime) >= wordCreateInterval)
{
lastWordCreateTime = now;
return true;
}
return false;
}
// 단어 생성 처리 (interface.h에서 호출)
bool handleWordGeneration(SentenceManager *sentenceManager)
{
if (shouldCreateWordBlock())
{
// 미리 섞어둔 순서대로 단어 생성
currentWordIndex = currentWordIndex % 8;
int wordIndexToCreate = wordOrder[currentWordIndex];
sentenceManager->createWordBlock(58, wordIndexToCreate);
currentWordIndex++;
return true;
}
// correctMatches가 8이 되면 새로운 문장으로 넘어가기
if (!waitingForCompletion && sentenceManager->getCorrectMatches() == 8)
{
waitingForCompletion = true;
return true;
}
return false;
}
void notifySnowmanComplete()
{
// 이미 waitingForCompletion이면 무시
if (waitingForCompletion)
return;
waitingForCompletion = true;
collectedSnowmen++;
}
void prepareNextRound(SentenceManager *sentenceManager)
{
// 새로운 문장 로드
sentenceManager->loadRandomSentence(currentLevel);
// 상태 완전 초기화
currentWordIndex = 0;
waitingForCompletion = false;
// 입력칸 초기화
sentenceManager->getInputHandler()->resetInputs();
// 단어 블록 초기화
auto &blocks = sentenceManager->getWordBlocks();
blocks.clear();
// 새로운 랜덤 순서 생성
initializeWordOrder();
// 점수 추가
addTargetScore();
// 첫 단어 블록 생성
lastWordCreateTime = time(nullptr);
}
void applyItemEffect(ItemBox::ItemType type)
{
switch (type)
{
case ItemBox::ItemType::TIME_BONUS:
timeAdjustment += 10;
lastItemEffectMessage = "TIME +10 SECONDS!";
break;
case ItemBox::ItemType::TIME_MINUS:
timeAdjustment -= 10;
lastItemEffectMessage = "TIME -10 SECONDS!";
break;
case ItemBox::ItemType::SCORE_BOOST:
scoreMultiplier = 2;
lastItemEffectMessage = "SCORE MULTIPLIED!";
break;
default:
break;
}
lastItemEffectTime = time(nullptr);
updateTime();
updateTotalScore();
if (remainingTime <= 0)
{
remainingTime = 0;
timeUp = true;
gameRunning = false;
}
}
// Getter 추가
int getCurrentWordIndex() const { return currentWordIndex; }
bool shouldDisplayItemEffect(double durationSeconds = 3.0) const
{
if (lastItemEffectTime == 0)
{
return false;
}
return difftime(time(nullptr), lastItemEffectTime) < durationSeconds;
}
const std::string &getLastItemEffectMessage() const { return lastItemEffectMessage; }
bool isWaitingForCompletion() const { return waitingForCompletion; }
int getCollectedSnowmen() const { return collectedSnowmen; }
};
#endif // GAMEMANAGER_H