-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameRule.cpp
397 lines (346 loc) · 11.8 KB
/
GameRule.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
388
389
390
391
392
393
394
395
396
397
#include "GameRule.h"
#include <ctime>
#include "Resource.h"
int GameRule::maxScramble = 20; // 스크램블 최대 횟수
const GLfloat Particle::gravity = 0.3f;
const Vector4f Particle::colors[10] = {
{1.f, 1.f, 1.f, 1.f},
{0.34f, 0.34f, 0.34f, 1.f},
{1.f, 0.f, 0.f, 1.f},
{0.f, 0.f, 1.f, 1.f},
{0.f, 1.f, 0.f, 1.f},
{1.f, 0.f, 1.f, 1.f},
{0.f, 1.f, 1.f, 1.f},
{1.f, 1.f, 0.f, 1.f},
{1.f, 0.57f, 0.2f, 1.f},
{0.5f, 0.29f, 0.1f, 1.f}
};
GameRule::GameRule(std::weak_ptr<RubiksCube> rubiksCube, std::weak_ptr<AnimationManager> animationManager, std::weak_ptr<Camera> camera)
: rubiksCube(rubiksCube),
animationManager(animationManager),
camera(camera),
debugMode(false),
onFinishedTwistListener(this, &GameRule::onFinishedTwist),
onCursorMovedListener(this, &GameRule::onCursorMoved),
onCursorRotatedListener(this, &GameRule::onCursorRotated)
{
this->rubiksCube.lock()->onFinishedTwist.addListener(this->onFinishedTwistListener);
this->rubiksCube.lock()->getCursor()->onCursorMoved.addListener(this->onCursorMovedListener);
this->rubiksCube.lock()->getCursor()->onCursorRotated.addListener(this->onCursorRotatedListener);
this->gameStarted = false;
print("Press 'Y' to start game");
}
void GameRule::resetGame() {
rubiksCube.lock()->resetBlocksAndCursor();
print("Resetted : Press 'Y' to start");
this->gameStarted = false;
this->blockedInput = false;
}
void GameRule::scramble() {
rubiksCube.lock()->resetBlocksAndCursor();
srand((unsigned)time(NULL));
const int indices[3] = { 0, 1, 2 };
const Vector3f axis_vectors[3] = { Vector3f::xVector(), Vector3f::yVector(), Vector3f::zVector() };
Rotation rot = Rotation();
const GLfloat rot_degrees[3] = { 90.f, 180.f, 270.f };
int previous_axis_vector = 0;
int previous_index = 0;
int previous_rotation_degree = 0;
for(int i = 0; i < maxScramble; i++)
{
int current_axis_vector = (int)((double)rand() / RAND_MAX * 3);
int current_index = (int)((double)rand() / RAND_MAX * 3);
int current_rotation_degree = (int)((double)rand() / RAND_MAX * 3);
// 만약 이전에 수행한 twist와 비슷한 조건이면 다른 조건을 뽑게 다시 실행
if(previous_axis_vector == current_axis_vector
|| previous_index == current_index
|| previous_rotation_degree == current_rotation_degree) {
i--;
continue;
}
rubiksCube.lock()->twist(indices[current_index], rot.rotateByEuler(axis_vectors[current_axis_vector] * rot_degrees[current_rotation_degree]));
previous_axis_vector = current_axis_vector;
previous_index = current_index;
previous_rotation_degree = current_rotation_degree;
}
// game start
print("Game Start!");
this->gameStarted = true;
}
bool GameRule::isAllBlockAligned(Vector3f std_vector) const {
size_t size = rubiksCube.lock()->getSize();
// 첫번째 블럭이 transform 한 대로, 기준 벡터를 transform 해서 다른 블럭들의 비교 대상으로 삼을 벡터를 만든다.
Vector3f comparison_vector_f = rubiksCube.lock()->blocks[0][0][0].lock()->getTransform().transformDirection(std_vector);
Vector<GLint, 3> comparison_vector_i = {
(int)(comparison_vector_f[0] + 0.5f),
(int)(comparison_vector_f[1] + 0.5f),
(int)(comparison_vector_f[2] + 0.5f)
};
// 각각의 블럭에 대해 위와 같이 수행해서, 위에서 만든 비교 대상 벡터와 같은지 확인한다.
// 모든 블럭이 같다면, 기준 벡터에 대해 모든 블럭이 같은 방향으로 transform 한 것.
for (size_t x = 0; x < size; x++)
{
for (size_t y = 0; y < size; y++)
{
for (size_t z = 0; z < size; z++)
{
Vector3f vector_f = rubiksCube.lock()->blocks[x][y][z].lock()->getTransform().transformDirection(std_vector);
Vector<GLint, 3> vector_i = {
(int)(vector_f[0] + 0.5f),
(int)(vector_f[1] + 0.5f),
(int)(vector_f[2] + 0.5f)
};
if (vector_i != comparison_vector_i) return false;
}
}
}
return true;
}
void GameRule::print(const std::string & message) {
if (this->messageAnimation) {
this->messageAnimation->interrupt();
}
this->messageAnimation = std::make_shared<PrintStringAnimation>(camera, message);
this->animationManager.lock()->push(this->messageAnimation);
}
void GameRule::onFinishedTwist() {
if (this->gameStarted) {
if (this->judge()) {
win();
}
}
}
bool GameRule::judge() {
if (this->gameStarted) {
// z vector와 y vector를 기준으로 모든 블럭이 같은 방향을 보고 있는지 확인한다.
if(isAllBlockAligned(Vector3f::zVector()) && isAllBlockAligned(Vector3f::yVector())) {
return true;
}
}
return false;
}
void GameRule::win() {
print("YOU WIN!");
if (this->particleAnimation) {
this->particleAnimation->interrupt();
}
this->particleAnimation = std::make_shared<ParticleAnimation>(camera);
this->animationManager.lock()->push(this->particleAnimation);
if (this->scatterAnimation)
{
this->scatterAnimation->interrupt();
}
this->scatterAnimation = std::make_shared<ScatterAnimation>(*this, this->rubiksCube);
this->animationManager.lock()->push(this->scatterAnimation);
this->gameStarted = false;
this->blockedInput = true;
}
bool GameRule::isStarted() const {
return this->gameStarted;
}
bool GameRule::toggleDebugMode()
{
this->debugMode = !(this->debugMode);
if(this->debugMode)
{
maxScramble = 1;
this->print("Cheat Mode ON");
} else {
this->print("Cheat Mode OFF");
maxScramble = 20;
}
return this->debugMode;
}
void GameRule::onCursorMoved(const RubiksCube::Cursor & cursor, const Vector2f & movement) {
auto ani = std::make_shared<CursorMovementFollowAnimation>(camera, cursor, movement);
bool empty = this->cursorMoveQueue.isEmpty();
this->cursorMoveQueue.push([=](bool interrupted) {
if (!ani->isStarted()) {
this->animationManager.lock()->push(ani);
}
if (interrupted) {
ani->interrupt();
}
return ani->isFinished();
});
if (empty) {
// first entry
this->cursorMoveQueue.execute();
}
}
void GameRule::onCursorRotated(const RubiksCube::Cursor & cursor, const bool clockwise) {
auto ani = std::make_shared<CursorRotationFollowAnimation>(camera, cursor, clockwise);
bool empty = this->cursorMoveQueue.isEmpty();
this->cursorMoveQueue.push([=](bool interrupted) {
if (!ani->isStarted()) {
this->animationManager.lock()->push(ani);
}
if (interrupted) {
ani->interrupt();
}
return ani->isFinished();
});
if (empty) {
// first entry
this->cursorMoveQueue.execute();
}
}
void GameRule::step() {
this->cursorMoveQueue.execute();
}
void GameRule::move(const Vector2f & vector){
if (blockedInput) return;
this->rubiksCube.lock()->getCursor()->move(vector);
}
void GameRule::twist(bool clockwise) {
if (blockedInput) return;
this->rubiksCube.lock()->getCursor()->twist(clockwise);
}
void GameRule::rotateAxis(bool clockwise) {
if (blockedInput) return;
this->rubiksCube.lock()->getCursor()->rotateAxis(clockwise);
}
void GameRule::setBlockedInput(bool blocked) {
this->blockedInput = blocked;
}
PrintStringAnimation::PrintStringAnimation(std::weak_ptr<Camera> camera, const std::string & message)
: camera(camera), message(message) {
}
bool PrintStringAnimation::stepFrame(const double timeElapsed, const double timeDelta) {
glColor3f(1.f, 1.f, 1.f);
const Vector3f & vrp = camera.lock()->getViewReferencePoint();
const Vector3f & vpn = camera.lock()->getViewPlaneNormal();
glRasterPos3f(vrp[0] - (vpn[0] * 2.f), vrp[1] - (vpn[1] * 2.f), vrp[2] - (vpn[2] * 2.f));
for (size_t i = 0; i < this->message.size(); i++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, this->message[i]);
}
return timeElapsed > 3;
}
Particle::Particle() {
this->createComponent<Model>()->bindMesh(Resource::meshes[Resource::Meshes::Particle])
.bindShaderProgram(Resource::shaderPrograms[Resource::ShaderPrograms::Phong])
.setColor(colors[(int)((float)rand() / RAND_MAX * 10)]);
xSpeed = ((float)rand() / RAND_MAX * 0.1f - 0.2f);
zSpeed = ((float)rand() / RAND_MAX * 0.1f - 0.2f);
this->setTransform(Transform(this->getTransform()).scalePre(2.0f));
}
ParticleAnimation::ParticleAnimation(std::weak_ptr<Camera> camera)
: camera(camera) {
}
void ParticleAnimation::onStart() {
// particle들의 초기 위치 설정
const Vector3f & vrp = camera.lock()->getViewReferencePoint();
const Vector3f & vpn = camera.lock()->getViewPlaneNormal();
for(int i = 0; i < MAX_PARTICLES; i++)
{
particles[i].setTransform(
Transform(particles[i].getTransform())
.translatePost({
vrp[0] - (vpn[0] * 2.f) - ((float)rand() / RAND_MAX * 5.f - 3.0f),
vrp[1] - (vpn[1] * 2.f) - ((float)rand() / RAND_MAX * 5.f - 2.5f),
vrp[2] - (vpn[2] * 2.f) - ((float)rand() / RAND_MAX * 5.f)
})
);
}
}
bool ParticleAnimation::stepFrame(const double timeElapsed, const double timeDelta) {
// particle들이 각자 x, z는 랜덤으로 y는 정해진 떨어지는 속도에 따라 움직임
for (int i = 0; i < MAX_PARTICLES; i++)
{
particles[i].xSpeed += ((float)rand() / RAND_MAX - 0.5f) * (float)timeDelta;
particles[i].zSpeed += ((float)rand() / RAND_MAX - 0.5f) * (float)timeDelta;
particles[i].setTransform(
Transform(particles[i].getTransform())
.translatePost({
(float)timeDelta * particles[i].xSpeed,
-((float)timeDelta * Particle::gravity),
(float)timeDelta * particles[i].zSpeed
})
);
camera.lock()->render(particles[i], true);
}
return timeElapsed > 5;
}
ScatterAnimation::ScatterAnimation(GameRule & gameRule, std::weak_ptr<RubiksCube> rubiksCube) : gameRule(gameRule), rubiksCube(rubiksCube)
{
}
bool ScatterAnimation::stepFrame(const double timeElapsed, const double timeDelta) {
float scale = 1.0f;
if (timeElapsed < 3) {
scale = ((float)sin(-timeElapsed * 5) * 0.15f + 1.0f);
speed += (float)timeDelta * 3.f;
}
else {
speed -= speed * (float)timeDelta;
for (std::shared_ptr<Actor> & block : *this->rubiksCube.lock()) {
block->setTransform(
Transform(block->getTransform())
.translatePost(block->getTransform().transformPoint({ 0, 0, 0 }).normalized())
);
}
}
rubiksCube.lock()->setTransform(
Transform(rubiksCube.lock()->getTransform())
.scalePost(scale)
.rotatePost(
Rotation().rotateByEuler(Vector3f(speed))
)
);
return timeElapsed > 5;
}
void ScatterAnimation::onFinished() {
rubiksCube.lock()->setTransform(Transform());
size_t i = 0;
for (std::shared_ptr<Actor> & block : *this->rubiksCube.lock()) {
block->setTransform(
Transform(this->initialBlockTransforms[i++])
);
}
gameRule.setBlockedInput(false);
}
void ScatterAnimation::onStart() {
for (std::shared_ptr<Actor> & block : *this->rubiksCube.lock()) {
this->initialBlockTransforms.push_back(block->getTransform());
}
speed = 1.f;
}
GameRule::CursorMovementFollowAnimation::CursorMovementFollowAnimation(std::weak_ptr<Camera> & camera, const RubiksCube::Cursor & cursor, const Vector2f & movement) {
this->camera = camera;
this->startingTransform = cursor.getWorldTransform();
this->movement = movement;
}
bool GameRule::CursorMovementFollowAnimation::stepFrame(const double timeElapsed, const double timeDelta) {
static const float duration = 0.2f;
float phase = (duration - (float)timeElapsed) / duration;
if (phase < 0) {
phase = 0;
}
this->camera.lock()->setTransform(
Transform()
.translatePost({ 0, 0, 9 })
.rotatePost(Rotation().rotateByEuler({ -15, 15, 0 }))
.translatePost({ -this->movement * phase * 2, 0 })
.pushPost(this->startingTransform)
);
return phase <= 0;
}
GameRule::CursorRotationFollowAnimation::CursorRotationFollowAnimation(std::weak_ptr<Camera>& camera, const RubiksCube::Cursor & cursor, const bool clockwise) {
this->camera = camera;
this->startingTransform = cursor.getWorldTransform();
this->clockwise = clockwise;
}
bool GameRule::CursorRotationFollowAnimation::stepFrame(const double timeElapsed, const double timeDelta) {
static const float duration = 0.2f;
float phase = (duration - (float)timeElapsed) / duration;
if (phase < 0) {
phase = 0;
}
this->camera.lock()->setTransform(
Transform()
.translatePost({ 0, 0, 9 })
.rotatePost(Rotation().rotateByEuler({ -15, 15, 0 }))
.rotatePost(Rotation().rotateByEuler(Vector3f::zVector() * phase * (this->clockwise ? 90.f : -90.f)))
.pushPost(this->startingTransform)
);
return phase <= 0;
}