-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cpp
More file actions
287 lines (256 loc) · 8.86 KB
/
GameManager.cpp
File metadata and controls
287 lines (256 loc) · 8.86 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
#include "C:\Users\jacob\source\repos\3DSnake\3DSnake\GameManager.h"
#include <glm/glm.hpp>
#include "C:\Users\jacob\source\repos\3DSnake\3DSnake\TextureManager.h"
#include "C:\Users\jacob\source\repos\3DSnake\3DSnake\Shader.h"
#include "C:\Users\jacob\source\repos\3DSnake\3DSnake\Object.h"
#include "C:\Users\jacob\source\repos\3DSnake\3DSnake\Player.h"
#include "C:\Users\jacob\source\repos\3DSnake\3DSnake\AppContext.h"
GameManager::GameManager(float sizeInUnits, unsigned int sizeInTiles) {
this->sizeInTiles = sizeInTiles;
this->sizeInUnits = sizeInUnits;
this->startIndex = (this->sizeInTiles * this->sizeInTiles) / 2;
this->unitsPerTile = this->sizeInUnits / this->sizeInTiles;
this->boardCenter = glm::vec3(0.0f, 0.0f, -4.0f);
this->boardTopLeft = this->boardCenter - glm::vec3((this->sizeInUnits / 2.0f) - this->unitsPerTile/2,
0.0f, (this->sizeInUnits / 2.0f) - this->unitsPerTile / 2);
this->leftPositionOffset = glm::vec3(this->unitsPerTile / 2, 0.0f, 0.0f);
this->rightPositionOffset = glm::vec3(-(float)(this->unitsPerTile / 2), 0.0f, 0.0f);
this->frontPositionOffset = glm::vec3(0.0f, 0.0f, this->unitsPerTile / 2);
this->backPositionOffset = glm::vec3(0.0f, 0.0f, -(float)((this->unitsPerTile / 2)));
this->defaultDirection = FORWARD;
this->gameRunning = false;
this->player = Player();
this->board = PrismObject();
/*this->snakeTexture = appContext.get_texture_manager().generate_texture_2D(
"C:\\Users\\jacob\\source\\repos\\3DSnake\\3DSnake\\Textures\\snake_scale.png",
GL_RGBA, GL_REPEAT, GL_LINEAR);*/
}
void GameManager::start_game(AppContext appContext) {
Texture tileTexture = this->get_generated_texture(FLOOR);
this->gameRunning = true;
//Object creation
this->board = PrismObject(36, appContext.get_shader(), appContext);
generate_prism(this->board, appContext, -(sizeInUnits / 2), (sizeInUnits / 2),
-0.5f, 0.5f, -(sizeInUnits / 2), (sizeInUnits / 2), this->sizeInTiles);
this->board.set_position(this->boardCenter - glm::vec3(0.0f, 1.01f, 0.0f));
this->board.set_texture(tileTexture);
//Player creation
this->player = Player(appContext);
}
Player& GameManager::get_player() {
assert(this->gameRunning);
return this->player;
}
PrismObject& GameManager::get_board() {
assert(this->gameRunning);
return this->board;
}
Texture GameManager::get_generated_texture(GeneratedTextures texture) {
switch (texture) {
case FLOOR:
return this->floorTexture;
case SNAKE:
return this->snakeTexture;
default:
printf("ERROR: GeneratedTextures enum bigger than switch statement 1\n");
Texture texture = Texture();
return texture;
}
}
void GameManager::set_generated_texture(GeneratedTextures gen, Texture texture) {
switch (gen) {
case FLOOR:
this->floorTexture = texture;
break;
case SNAKE:
this->snakeTexture = texture;
break;
default:
printf("ERROR: GeneratedTextures enum bigger than switch statement 2\n");
break;
}
}
unsigned int GameManager::index_to_row(unsigned int index) {
assert(index < this->sizeInTiles * this->sizeInTiles);
return index / this->sizeInTiles;
}
unsigned int GameManager::index_to_column(unsigned int index) {
assert(index < sizeInTiles * sizeInTiles);
return index % this->sizeInTiles;
}
unsigned int GameManager::row_col_to_index(unsigned int row, unsigned int column) {
//assert(row < this->sizeInTiles && column < this->sizeInTiles);
return row * this->sizeInTiles + column;
}
glm::vec3 GameManager::board_to_vec3(glm::vec2 boardLoc) {
glm::vec3 boardTopLeft = this->boardTopLeft;
float unitsPerTile = this->unitsPerTile;
int row = static_cast<int>(boardLoc.x);
int column = static_cast<int>(boardLoc.y);
glm::vec3 result = glm::vec3(boardTopLeft.x + (float)column * unitsPerTile, 0.0f, boardTopLeft.z + row * unitsPerTile);
return result;
}
glm::vec3 GameManager::get_orientation_offset(Direction direction) {
switch (direction) {
case FORWARD:
return this->frontPositionOffset;
break;
case BACKWARD:
return this->backPositionOffset;
break;
case LEFT:
return this->leftPositionOffset;
break;
case RIGHT:
return this->rightPositionOffset;
break;
}
}
glm::vec2 GameManager::vec3_to_grid_position(glm::vec3 position) {
glm::vec3 normalizedPosition = (glm::vec3(position.x + this->unitsPerTile - this->boardCenter.x,
0.0f, position.z + 3*this->unitsPerTile/2 - this->boardCenter.z));
int row = static_cast<int>(normalizedPosition.z / this->unitsPerTile + this->sizeInTiles / 2) - 1;
int column = static_cast<int>(normalizedPosition.x / this->unitsPerTile + this->sizeInTiles / 2) - 1;
glm::vec2 result = glm::vec2(row, column);
return result;
}
glm::vec2 GameManager::vec3_to_length_adjusted_tile(SnakeScaleObject head, glm::vec3 position, AppContext appContext) {
if (player.get_body_cubes().size() == 1) {
glm::vec3 lengthAdjustedPosition = centered_vec3_to_edge(head, position, appContext);
return vec3_to_grid_position(lengthAdjustedPosition);
//return vec3_to_grid_position(lengthAdjustedPosition) + get_tile_offset(player.get_head_direction()).operator*=(player.get_length() - 1);
}
else return vec3_to_grid_position(head.get_position());
}
bool GameManager::singleton_in_new_tile(Player& player, glm::vec3 currentAdjustedPosition) {
assert(player.get_body_cubes().size() == 1);
float xOffset = 0.0f;
float zOffset = 0.0f;
switch (player.get_head_direction()) {
case FORWARD:
zOffset = -unitsPerTile / 2;
break;
case BACKWARD:
zOffset = unitsPerTile / 2;
break;
case LEFT:
xOffset = -unitsPerTile / 2;
break;
case RIGHT:
zOffset = unitsPerTile / 2;
break;
}
glm::vec2 tileOffset = this->get_tile_offset(player.get_head_direction());
glm::vec2 currentTile = player.get_head_grid_position();
glm::vec2 targetTile = currentTile + tileOffset;
glm::vec3 directionalOffset = glm::vec3(xOffset, 0.0f, zOffset);
glm::vec3 targetPosition = this->board_to_vec3(targetPosition) + (directionalOffset);
glm::vec3 positionChange = glm::abs(targetPosition - (currentAdjustedPosition));
if (positionChange.x <= 0.1 && positionChange.z <= 0.1) return true;
else return false;
}
//void GameManager::singleton_tile_check(Player& player, glm::vec3 currentAdjustedPosition) {
// if (player.get_body_cubes().size() > 1) return;
//
// if (this->is_in_new_tile(player, currentAdjustedPosition)) {
// glm::vec2 tileOffset = this->get_tile_offset(player.get_head_direction());
// player.set_head_grid_position(currentAdjustedPosition.operator+=(tileOffset));
// }
//
// //glm::vec3 normalizedPosition = (glm::vec3(position.x + this->unitsPerTile - this->boardCenter.x,
// // 0.0f, position.z + 3 * this->unitsPerTile / 2 - this->boardCenter.z);
//
// //int row = static_cast<int>(normalizedPosition.z / this->unitsPerTile + this->sizeInTiles / 2) - 1;
// //int column = static_cast<int>(normalizedPosition.x / this->unitsPerTile + this->sizeInTiles / 2) - 1;
//
// //glm::vec2 result = glm::vec2(row, column);
//
// //if (player.get_body_cubes().size() == 1)
// // return vec3_to_grid_position(position) + get_tile_offset(direction/*player.get_head_direction()*/).operator*=(player.get_length() - 1);
// //else return vec3_to_grid_position(position);
//}
// REMEMBER, IT'S STORED AS ROW,COL
glm::vec2 GameManager::get_tile_offset(Direction direction) {
switch (direction) {
case FORWARD:
return glm::vec2(-1.0f, 0.0f);
case BACKWARD:
return glm::vec2(1.0f, 0.0f);
case LEFT:
return glm::vec2(0.0f, -1.0f);
case RIGHT:
return glm::vec2(0.0f, 1.0f);
}
}
Direction GameManager::get_opposite_direction(Direction direction) {
switch (direction) {
case FORWARD:
return BACKWARD;
case BACKWARD:
return FORWARD;
case LEFT:
return RIGHT;
case RIGHT:
return LEFT;
}
}
const char* direction_to_string(Direction direction) {
const char* result = "UNKNOWN DIRECTION";
switch (direction) {
case FORWARD:
result = "FORWARD";
break;
case BACKWARD:
result = "BACKWARD";
break;
case LEFT:
result = "LEFT";
break;
case RIGHT:
result = "RIGHT";
break;
default:
break;
}
return result;
}
glm::vec3 get_scaled_grid_vector(Direction direction, glm::vec3 scale, float tileSize) {
assert(scale.x > 0.0f && scale.y > 0.0f && scale.z > 0.0f);
glm::vec3 vect;
switch (direction) {
case LEFT:
vect = glm::vec3(-tileSize * (scale.x - 1), 0.0f, 0.0f);
break;
case RIGHT:
vect = glm::vec3(tileSize * (scale.x - 1), 0.0f, 0.0f);
break;
case FORWARD:
vect = glm::vec3(0.0f, 0.0f, -tileSize * (scale.z - 1));
break;
case BACKWARD:
vect = glm::vec3(0.0f, 0.0f, tileSize * (scale.z - 1));
break;
}
return vect;
}
glm::vec3 get_vec3_directional_grid_offset(Direction direction, AppContext appContext) {
GameManager gameManager = appContext.get_game_manager();
float unitsPerTile = gameManager.unitsPerTile;
float xOffset = 0.0f;
float zOffset = 0.0f;
switch (direction) {
case FORWARD:
zOffset = -unitsPerTile;
break;
case BACKWARD:
zOffset = unitsPerTile;
break;
case LEFT:
xOffset = -unitsPerTile;
break;
case RIGHT:
xOffset = unitsPerTile;
break;
}
return glm::vec3(xOffset, 0.0f, zOffset);
}