-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
319 lines (294 loc) · 6.99 KB
/
Copy pathGame.cpp
File metadata and controls
319 lines (294 loc) · 6.99 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
#include "Game.h"
int olds[2] = { 100 + 1280 / 2, 100 + 800 / 2 };
Game::Game()
{
intKeys();
fps_cap = 1.0/89 * 1000;
player = new Player({ 0, 1, 0}, { -10,0,0 });
timer = Timer();
ColidingObjects.push_back(new StableObject({ 5,0,0 }, { 0,0,0 }, 1));
//ColidingObjects.push_back(new StableObject({ 10,10,10 }, { 10,10,10 }, 0));
//ColidingObjects.push_back(new StableObject({ -5,2,0 }, { 0,0,0 }, 4, { 50,50,50 }));
programValuesInit;
initShaders();
initParkour(5000);
}
Game::~Game()
{
for (unsigned int i = ColidingObjects.size() - 1; i > 0; i--)
{
delete ColidingObjects[i];
ColidingObjects.pop_back();
}
for (unsigned int i = sources.size() - 1; i > 0; i--)
{
delete sources[i];
sources.pop_back();
}
for (unsigned int i = shaders.size() - 1; i > 0; i--)
{
delete shaders[i];
shaders.pop_back();
}
delete player;
}
void Game::initParkour(int amount)
{
for (int i = 0; i < amount; i++)
{
ColidingObjects.push_back(new StableObject({ 0.3*i+24 * sinf(3.14 * 2 * i / 12),i * 1.1-1,0.36 * i + 24 * cosf(3.14 * 2 * i / 12) }, { 0,0,0 }, 5,
{ 2,2,2 }, { sinf(3.14 * 2 * i / 12), cosf(3.14 * 2 * i / 4), sinf(3.14 * 2 * i / 20),1.0f }));
}
}
void Game::setLighting()
{
//glm::vec3 pos, glm::vec4 col, glm::vec3 amb, glm::vec3 diff, glm::vec3 spec
sources.push_back(new LightSource(&shaders, 0, { 10.0f, 0.0f,10.0f }, { 0,0,1,0 }, { 0,0,0.4 }, { 0,0,0.6 }, {0,0,0.5}));
sources.push_back(new LightSource(&shaders, 0, { 10.0f, 0.0f,-10.0f }, { 0,1,0,0 }, { 0,0.4,0 }, { 0,0.6,0 }, { 0,0.5,0 }));
sources.push_back(new LightSource(&shaders, 1, { -10.0f, 10.0f,10.0f }));
sources.push_back(new LightSource(&shaders, 1, { -10.0f, 10.0f,-10.0f }));
//sources.push_back(new LightSource(&shaders, 2, { 0.0f, 10.0f,0.0f }, {1.0f, 1.0f, 1.0f}, { 0.8f,0.8f,0.8f }, { 0.2f,0.2f,0.2f }, { 0.5f,0.5f,0.5f }));
}
void Game::initShaders()
{
shaders.push_back(new Shader("Shaders/lighting.vert", "Shaders/lighting.frag"));
shaders.push_back(new Shader("Shaders/lamp.vert", "Shaders/lamp.frag"));
}
void Game::display()
{
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
CameraDir = player->camera->actualizeCamera();
viewMatrix = player->camera->viewMatrix;
//CameraPosition = player->camera->position;
shaders[0]->run();
programValuesInit.setCameraPos( *shaders[0]);
programValuesInit.setProjetionMatrix(*shaders[0]);
programValuesInit.test(*shaders[0]);
programValuesInit.setViewMarix(*shaders[0]);
GLint lightPosLoc = glGetUniformLocation(shaders[0]->Program, "light.postion");
glUniform3f(lightPosLoc, player->get_position().x, player->get_position().y+1.0, player->get_position().z-0.5);
glPushMatrix();
map.draw(shaders[0]);
glPopMatrix();
for (unsigned int i= 0; i < ColidingObjects.size(); i++)
{
//objects[i]->rotate(1, { 0,1,0 });
ColidingObjects[i]->draw(shaders[0]);
}
player->draw(shaders[0]);
glutSwapBuffers();
}
void Game::update()
{
dtime = timer.getDeltaTime();
if (dtime < fps_cap)
{
Sleep((fps_cap - dtime));
dtime = fps_cap - dtime;
}
Keys_procesor();
if (!map.is_touching(*player->box))
{
player->is_standing = false;
player->gravity();
}
else
{
player->is_standing = true;
player->movePlayer({ 0.0f, -player->get_position().y+0.5, 0.0f });
}
for (unsigned int i = 0; i < ColidingObjects.size(); i++)
{
if (player->box->getColision(*ColidingObjects[i]->box))
{
player->response(*ColidingObjects[i]->box);
}
}
player->updateEntity(dtime);
//reakcja gracza z map¹
//reakcja gracza z innymi obiektami
ColidingObjects[1]->rotate({ 0,1,0 });
}
void Game::reshape(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 200.0f);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
void Game::intKeys()
{
Keys["Forward"] = 'W';
Keys["Backward"] = 'S';
Keys["MoveLeft"] = 'A';
Keys["MoveRight"] = 'D';
Keys["GoDown"] = 'V';
Keys["Spacebar"] = 32;
Keys["Alt"] = 3000;
Keys["Shift"] = 3001;
Keys["Ctrl"] = 3002;
for (auto it = Keys.begin(); it != Keys.end(); ++it)
{
is_key_pressed[it->first] = 0;
}
}
void Game::normal_keys(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit;
break;
}
if (key > 96 && key < 123)
{
key = key - 32;
}
for (auto it = Keys.begin(); it != Keys.end(); ++it)
{
if (key == it->second)
{
is_key_pressed[it->first] = true;
break;
}
}
}
void Game::normal_keys_up(unsigned char key, int x, int y)
{
if (key > 96 && key < 123)
{
key = key - 32;
}
for (auto it = Keys.begin(); it != Keys.end(); ++it)
{
if (key == it->second)
{
is_key_pressed[it->first] = false;
break;
}
}
}
void Game::special_keys(int key, int x, int y)
{
switch (key)
{
case(0x0070): //lewy shift
case(0x0071)://prawy shift
is_key_pressed["Shift"] = true;
break;
case(0x0072): //lewy ctrl
case(0x0073): //prawy ctrl
is_key_pressed["Ctrl"] = true;
break;
case(0x0074)://lewy alt
case(0x0075)://prawy alt
is_key_pressed["Alt"] = true;
break;
}
}
void Game::special_keys_up(int key, int x, int y)
{
switch (key)
{
case(0x70): //lewy shift
case(0x71)://prawy shift
is_key_pressed["Shift"] = false;
break;
case(0x72): //lewy ctrl
case(0x73): //prawy ctrl
is_key_pressed["Ctrl"] = false;
break;
case(0x74)://lewy alt
case(0x75)://prawy alt
is_key_pressed["Alt"] = false;
break;
}
}
void Game::special_special_keys()
{
if (glutGetModifiers() == GLUT_ACTIVE_SHIFT)
{
is_key_pressed["Shift"] = true;
}
if (glutGetModifiers() == GLUT_ACTIVE_CTRL)
{
is_key_pressed["Ctrl"] = true;
}
if (glutGetModifiers() == GLUT_ACTIVE_ALT)
{
is_key_pressed["Alt"] = true;
}
}
void Game::move_mouse(int x, int y)
{
player->rotate( { float(y - olds[1]) * 0.05f,float(x - olds[0]) * 0.05f,0 });
glutWarpPointer(olds[0], olds[1]);
}
void Game::special_special_keys_up()
{
is_key_pressed["Shift"] = false;
is_key_pressed["Ctrl"] = false;
is_key_pressed["Alt"] = false;
}
void Game::Keys_procesor()
{
glm::vec3 add_acc = { 0.0f,0.0f,0.0f };
if (is_key_pressed["Forward"])
{
add_acc.z += 1.0f;
}
if (is_key_pressed["Backward"])
{
add_acc.z += -1.0f;
}
if (is_key_pressed["MoveLeft"])
{
add_acc.x += 1.0f;
}
if (is_key_pressed["MoveRight"])
{
add_acc.x += -1.0f;
}
if (is_key_pressed["Spacebar"])
{
if (player->is_standing )
{
player->is_standing = false;
add_acc.y += 0.0025f;
}
}
if (is_key_pressed["GoDown"])
{
add_acc.y += -5.0f;
}
player->set_acceleration(add_acc);
/*
if (is_key_pressed["Ctrl"])
{
camera->updateCamera({ 0,-1,0 }, { 0, 0, 0 });
}
if (is_key_pressed["Alt"])
{
camera->updateCamera({ 0,-1,0 }, { 0, 0, 0 });
}
if (is_key_pressed["Shift"])
{
camera->updateCamera({ 0,-1,0 }, { 0, 0, 0 });
}
*/
}