-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_state.cpp
196 lines (168 loc) · 5.25 KB
/
game_state.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
#include "game_state.h"
#include "object.h"
#include "context.h"
#include "skydome.h"
#include "helper.h"
#include "plane.h"
#include "terrain_map.h"
#include "loops.h"
#include "MicroGlut.h"
#include "GL_utilities.h"
#include "VectorUtils4.h"
#include "LittleOBJLoader.h"
#include "LoadTGA.h"
#include <iostream>
Game_State::Game_State(Context *c) : State(c->settings["game_state"], c)
{
map = new TerrainMap(context->settings["terrain"], cameraPosition, frustum_obj);
skydome = new Skydome(context->settings["skydome"], cameraPosition);
plane = new Plane(context->settings["planes"][0], vec3(0,150,0));
loop = new Loops(context->settings["loops"], vec3(0, 150, -1000));
glutHideCursor();
return;
}
void Game_State::keyboard(unsigned char key, int x, int y)
{
State::keyboard(key, x, y);
switch (key)
{
case '1':
{
if (current_plane == 0)
break;
vec3 tmp_pos = plane->get_lookAtPoint();
delete plane;
plane = new Plane(context->settings["planes"][0], tmp_pos);
current_plane = 0;
break;
}
case '2':
{
if (current_plane == 1)
break;
vec3 tmp_pos = plane->get_lookAtPoint();
delete plane;
plane = new Plane(context->settings["planes"][1], tmp_pos);
current_plane = 1;
break;
}
case '3':
{
if (current_plane == 2)
break;
vec3 tmp_pos = plane->get_lookAtPoint();
delete plane;
plane = new Plane(context->settings["planes"][2], tmp_pos);
current_plane = 2;
break;
}
case 'k':
{
light_intensity -= 0.1;
if (light_intensity < 0.1)
light_intensity = 0.1;
break;
}
case 'l':
{
light_intensity += 0.1;
if (light_intensity > 1.0)
light_intensity = 1.0;
break;
}
case 'r':
{
loop_on = !loop_on;
break;
}
}
}
void Game_State::mouse(int x, int y)
{
if (!keys_toggle['m'])
{
return;
}
// Warp the cursor back to the center of the window
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
if (deltaX == -1 && deltaY == -1)
{
deltaX = 0;
deltaY = 0;
return;
}
// Calculate the delta from the center of the window
int deltaX = x - glutGet(GLUT_WINDOW_WIDTH) / 2;
int deltaY = y - glutGet(GLUT_WINDOW_HEIGHT) / 2;
// Update theta and phi based on the mouse movement
theta += deltaX * 0.001f;
phi -= deltaY * 0.001f;
// Clamp phi to be between -pi/2 and pi/2 to prevent the camera from flipping upside down
float epsilon = 0.01f; // Small value to prevent phi from reaching exactly 0 or pi/2
phi = std::max(std::min(phi, M_PI / 2.0f - epsilon), epsilon - M_PI / 2.0f);
// Calculate the direction vector
mouse_direction = vec3(cos(phi) * cos(theta), sin(phi), cos(phi) * sin(theta));
}
void Game_State::move_camera(int time_elapsed)
{
if (!keys_toggle['m'])
{
world2view = plane->get_lookAtMatrix();
}
else
{
cameraPosition = plane->get_pos();
lookAtPoint = mouse_direction + cameraPosition;
upVector = vec3(0, 1, 0);
world2view = lookAtv(cameraPosition, lookAtPoint, upVector);
}
}
void Game_State::update(int time_elapsed)
{
// Update camera etc. here, then update objects.
plane->update(time_elapsed, plane->get_pos(), lookAtPoint, keys_pressed);
map->update(plane->get_pos(), world2view);
skydome->update(time_elapsed, plane->get_pos(), lookAtPoint, keys_pressed);
if (loop_on) {
loop->update(time_elapsed, plane->get_pos(), plane -> get_lookAtPoint(), keys_pressed);
}
for (Object *object : objects)
{
object->update(time_elapsed, plane->get_pos(), plane -> get_lookAtPoint(), keys_pressed);
}
move_camera(time_elapsed);
// Check for collisions
std::map<std::pair<int, int>, int> points = plane->get_points_on_radius();
if (map->collision(points))
{
std::cout << "Collision detected" << std::endl;
delete plane;
plane = new Plane(context->settings["planes"][current_plane], vec3(0,150,0));
return;
}
}
void Game_State::display()
{
// Upload matrices to shader, then display objects, map etc.
// We make sure to use the program before uploading matrices.
glUseProgram(program);
upload2shader();
cameraPosition = plane->get_pos();
skydome->display(program, world2view, projection, light_intensity);
glUniform1i(glGetUniformLocation(program, "map"), 1);
map->display(program, world2view, projection, plane->get_pos());
glUniform1i(glGetUniformLocation(program, "map"), 0);
plane->display(program, world2view, projection);
if (loop_on) {
glUniform1i(glGetUniformLocation(program, "map"), 2);
loop->display(program, world2view, projection);
}
}
Game_State::~Game_State()
{
// Objects, ground and skydome are deleted in State
// Program is deleted in State
delete plane;
delete loop;
return;
}