aboutsummaryrefslogtreecommitdiffstats
path: root/src/world.cpp
blob: 8987eacfc0a4de15ff84f88d3bee501b16f5a63f (plain)
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
/**
 * @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;
}

std::vector<std::pair<glm::vec2, glm::vec2>> 
World::getIntersectingPlanes(glm::vec2 origin, glm::vec2 dest)
{
    (void)origin;
    (void)dest;
    //glm::ivec2 worldOrigin = origin*unitSize;
    //glm::ivec2 worldDest = dest*unitSize;

    return std::vector<std::pair<glm::vec2, glm::vec2>>();
}

glm::vec3 World::collide(glm::vec3 &start, glm::vec3 &end, Physics &phys)
{
    (void)start;
    (void)end;
    (void)phys;
    for (auto &l : solidLayers) {
        if (end.z == l->drawLayer) {
            glm::vec2 len = end-start;
            glm::vec2 dir = glm::normalize(len);
            float step = 1.0f/unitSize;

            // TODO move this
            glm::vec2 pos = start;

            for (float i = 0; i < len.length(); i+=step) {
                pos += dir;

                if (dir.x > 0.0f) {
                // Moving to the right
                    //glm::vec2 origin = pos + phys.corners[1]; // bottom right
                    //glm::vec2 orDir = glm::vec2(0, 1);

                } else if (dir.x < 0.0f) {
                // Moving to the left

                }

                if (dir.y > 0.0f) {
                // Moving upwards

                } else if (dir.y < 0.0f) {
                // Moving downwards

                }
            }
        }
    }
    return glm::vec3(0);
}


/*********
*  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();
}