-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyWin.hpp
181 lines (161 loc) · 6.38 KB
/
MyWin.hpp
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
#pragma once
#include <chrono>
#include <AGL3Window.hpp>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <memory>
#include "Obstacle.hpp"
#include "Player.hpp"
class MyWin : public AGLWindow {
public:
MyWin() {};
MyWin(int _wd, int _ht, const char *name, int vers, int fullscr=0)
: AGLWindow(_wd, _ht, name, vers, fullscr) {}
void MainLoop(int seed, const int N = 10)
{
srand(seed);
ViewportOne(0, 0, wd, ht);
glfwSetWindowSizeCallback(win(), MyWin::windowSizeCallback);
glfwSetInputMode(win(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
float scale = 1.0f / (2 * (float)N);
bool gameFinishFlag{false};
uint32_t itemNumber{0u};
Player player(win(), aspect, gameFinishFlag, itemNumber);
glfwSetCursorPosCallback(win(), Player::mouseCallback);
std::vector<std::shared_ptr<Obstacle>> obstacles;
obstacles.reserve(N*N*N);
float diff{2.0f / N};
float curr_x{-1.0f + diff / 2};
for (int x_index{0}; x_index < N; x_index++)
{
float curr_y{-1.0f + diff / 2};
for (int y_index{0}; y_index < N; y_index++)
{
float curr_z{-1.0f + diff / 2};
for (int z_index{0}; z_index < N; z_index++)
{
if (not (x_index == 0 and y_index == 0 and z_index == 0))
{
ObstacleType obstacleType;
if (x_index == N-1 and y_index == N-1 and z_index == N-1)
obstacleType = ObstacleType::finish;
else
{
auto randomNum = rand() % 100;
if (randomNum < 10)
obstacleType = ObstacleType::trap;
else if (randomNum < 12)
{
obstacleType = ObstacleType::item;
itemNumber++;
}
else
obstacleType = ObstacleType::normal;
}
auto o = std::make_shared<Obstacle>(
glm::vec3(curr_x, curr_y, curr_z),
glm::vec3(rand() % 360, rand() % 360, rand() % 360),
scale,
aspect,
player.cameraPos,
obstacleType);
obstacles.push_back(o);
}
curr_z += diff;
}
curr_y += diff;
}
curr_x += diff;
}
startTime = std::chrono::system_clock::now();
glm::vec3 minimapCameraPos{-3.0, -3.0, -3.0};
auto getClosestObstacles = [&obstacles](){
const int numOfObstacles{obstacles.size() < 4 ? obstacles.size() : 4};
std::sort(
obstacles.begin(),
obstacles.end(),
[](std::shared_ptr<Obstacle> o1, std::shared_ptr<Obstacle> o2){
return o1->getDistanceFromPlayer() < o2->getDistanceFromPlayer();
});
std::vector<std::shared_ptr<Obstacle>> res;
for (auto i{0u}; i < numOfObstacles; i++)
res.push_back(obstacles[i]);
return res;
};
printf("To win you have to collect %d items\n", itemNumber);
do {
glEnable(GL_DEPTH_TEST);
glLineWidth(2.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, wd, ht);
player.catchCamKey();
player.catchMoveKey(getClosestObstacles());
// Draw main view
auto view{player.getViewMatrix()};
for (auto& o : obstacles)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
o->draw(view);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
o->draw(view, true);
}
// Draw minimap
auto p = player.getCenter();
minimapCameraPos = {
p.x > 0.0f ? 3.0f : -3.0f,
3.0f,
p.z > 0.0f ? 3.0f : -3.0f};
auto minimapView = glm::lookAt(
minimapCameraPos,
glm::vec3{0.0f, 0.0f, 0.0f},
glm::vec3{0.0f, 1.0f, 0.0f});
glViewport((wd/2), 0, wd/2, ht/2);
glClear(GL_DEPTH_BUFFER_BIT);
glLineWidth(0.7f);
for (auto& o : obstacles)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
o->draw(minimapView);
}
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
player.draw(minimapView);
if (gameFinishFlag)
{
gameFinish();
break;
}
glfwSwapBuffers(win());
glfwPollEvents();
} while (glfwGetKey(win(), GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(win()) == 0 );
}
void gameFinish()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auto endTime{std::chrono::system_clock::now()};
auto timeMinutes{std::chrono::duration_cast<std::chrono::minutes>(endTime - startTime)};
auto timeSeconds{
std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime) -
std::chrono::seconds(timeMinutes.count()*60)};
auto timeMilliseconds{
std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime) -
std::chrono::milliseconds(timeMinutes.count()*60000) -
std::chrono::milliseconds(timeSeconds.count()*1000)};
printf("You win!\n");
printf("Your time (m:s:ms): %ld:%ld:%ld\n", timeMinutes.count(), timeSeconds.count(), timeMilliseconds.count());
}
static void windowSizeCallback(GLFWwindow* win, int width, int height)
{
void* ptr = glfwGetWindowUserPointer(win);
if (AGLWindow* window = static_cast<AGLWindow*>(ptr))
{
window->wd = width;
window->ht = height;
window->aspect = (float)width / (float)height;
}
glViewport(0, 0, width, height);
}
private:
std::chrono::time_point<std::chrono::system_clock> startTime;
};