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
|
/**
* @file world.cpp
*
* Copyright (C) 2019 Belle-Isle, Andrew <drumsetmonkey@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "world.hpp"
#include "events/world.hpp"
/*****************
* WORLD CLASS *
*****************/
/* CONSTRUCTORS */
World::World(sol::object param)
{
if (param.get_type() == sol::type::table) {
sol::table tab = param;
if (tab["Seed"] == sol::type::number) {
seed = tab["Seed"];
}
if (tab["Layers"] == sol::type::number) {
layers = tab["Layers"];
}
if (tab["Register"] == sol::type::function) {
registerMat = tab["Register"];
}
if (tab["Generate"] == sol::type::function) {
generate = tab["Generate"];
}
} else {
// TODO better logging
std::cerr << "World paramaters must be stored in a table" << std::endl;
}
// If there is a register function, we should call it here
if (registerMat != sol::nil)
registerMat(this);
// If a generate function is defined, call it
if (generate != sol::nil)
generate(this);
}
// TODO
std::tuple<unsigned int, unsigned int, unsigned int>
World::getSize()
{
//return {width, height, layers};
return {0, 0, 0};
}
/* RENDERING */
void World::generateMesh()
{
for (auto &l : drawLayers) {
// Preallocate size of vertexes
float Z = l->drawLayer;
auto to = l->texture.offset;
auto ts = l->texture.size;
float tr = 1.0f;
float w = l->texture.width/unitSize;
float h = l->texture.height/unitSize;
GLfloat mesh[36] = {0 , 0 , Z, to.x , to.y+ts.y, tr,
0+w, 0 , Z, to.x+ts.x, to.y+ts.y, tr,
0 , 0+h, Z, to.x , to.y , tr,
0+w, 0 , Z, to.x+ts.x, to.y+ts.y, tr,
0+w, 0+h, Z, to.x+ts.x, to.y , tr,
0 , 0+h, Z, to.x , to.y , tr};
glBindBuffer(GL_ARRAY_BUFFER, l->layerVBO);
glBufferData(GL_ARRAY_BUFFER,
36 * sizeof(GLfloat),
mesh,
GL_STATIC_DRAW);
meshAdd.push_back(WorldMeshUpdateEvent(l->layerVBO,
l->texture.tex,
l->normal.tex,
36));
}
}
/* SEED */
unsigned int World::getSeed()
{
return seed;
}
unsigned int World::setSeed(unsigned int s)
{
seed = s;
return seed;
}
/* PHYSICS */
double World::getHeight(double x, double y, double z)
{
(void)y;
double Y = 0.0f;
for (auto &l : solidLayers) {
if (z == l->drawLayer) {
int wx = x*unitSize;
int h = 0.0;
for (auto b : l->hitbox[wx]) {
if (b == true)
Y = h;
h++;
}
return ((Y+1)/unitSize);
}
}
return 0;
}
bool World::isSolid(glm::vec3 pos)
{
for (auto &l : solidLayers) {
if (pos.z == l->drawLayer) {
int wx = pos.x * unitSize;
int wy = pos.y * unitSize;
if (wx < 0 || wy < 0) return true;
return l->hitbox[wx][wy];
}
}
return false;
}
std::vector<glm::vec3>
World::getIntersectingPlanes(glm::vec3 origin, Physics &phys)
{
std::vector<glm::vec3> planes;
glm::vec3 goal = origin;
origin.x += phys.corners[0].x;
origin.y += phys.corners[0].y;
goal.x += phys.corners[3].x;
goal.y += phys.corners[3].y;
float step = 1.0f/unitSize;
for (;origin.y <= goal.y; origin.y += step){
for (;origin.x <= goal.x; origin.x += step) {
if (isSolid(origin)) {
planes.push_back(origin);
}
}
}
return planes;
}
glm::vec3 World::collide(glm::vec3 &start, glm::vec3 &end, Physics &phys)
{
// How far to push the entity to unintersect with the world
glm::vec3 push(0);
for (auto &l : solidLayers) {
if (start.z == l->drawLayer) {
glm::vec3 len = end-start;
glm::vec3 dir = glm::normalize(len);
float step = 1.0f/unitSize;
glm::vec3 pos = start;
for (float i = 0.0f; i < glm::length(len); i+=step, pos+=dir) {
// Get all colliding world spaces
std::vector<glm::vec3> inter = getIntersectingPlanes(pos, phys);
if (i == 0.0f) {
std::cout << inter.size() << std::endl;
if (inter.size()) {
p.standing = true;
}
}
// If there are no colliding world spaces, don't bother
if (inter.size()) {
if (dir.x > 0.0f) {
// Moving to the right
int closest = inter.at(0).x;
for (auto &p : inter) {
if (p.x < closest)
closest = p.x;
}
push.x -= abs(closest - (pos.x + phys.corners[1].x));
} else if (dir.x < 0.0f) {
// Moving to the left
int closest = inter.at(0).x;
for (auto &p : inter) {
if (p.x > closest)
closest = p.x;
}
push.x += abs(closest - (pos.x + phys.corners[0].x));
}
if (dir.y > 0.0f) {
// Moving upwards
int closest = inter.at(0).y;
for (auto &p : inter) {
if (p.y < closest)
closest = p.y;
}
push.y -= abs(closest - (pos.y + phys.corners[2].y));
} else if (dir.y < 0.0f) {
// Moving downwards
int closest = inter.at(0).y;
for (auto &p : inter) {
if (p.y > closest)
closest = p.y;
}
push.y += abs(closest - (pos.y + phys.corners[0].y));
}
if (push != glm::vec3(0.0f))
return pos + push;
}
}
}
}
return end;
}
/*********
* NEW *
*********/
void World::registerLayer(float z, sol::object obj)
{
if (obj.get_type() == sol::type::table) {
sol::table tab = obj;
SolidLayer s(z, tab);
solidLayers.push_back(std::make_shared<SolidLayer>(s));
drawLayers.push_back(std::make_shared<Layer>(s));
} else {
throw std::string("Layer must receive a table");
}
generateMesh();
}
void World::registerDecoLayer(float z, sol::object obj)
{
if (obj.get_type() == sol::type::table) {
sol::table tab = obj;
drawLayers.push_back(std::make_shared<Layer>(Layer(z, tab)));
} else {
throw std::string("Layer must receive a table");
}
generateMesh();
}
/******************
* WORLD SYSTEM *
******************/
World* WorldSystem::addWorld(sol::object t)
{
worlds.push_back(World(t));
return &(worlds.back());
}
void WorldSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
[[maybe_unused]]entityx::EventManager& events)
{
}
void WorldSystem::update([[maybe_unused]]entityx::EntityManager& entities,
[[maybe_unused]]entityx::EventManager& events,
[[maybe_unused]]entityx::TimeDelta dt)
{
if (currentWorld == nullptr) {
currentWorld = &(worlds.front());
events.emit<WorldChangeEvent>(currentWorld);
}
for (auto &ma : currentWorld->meshAdd) {
events.emit<WorldMeshUpdateEvent>(ma);
}
currentWorld->meshAdd.clear();
}
|