@sed -e 's/.*://' -e 's/\\$$//' < $(OUTDIR)/$*.$(DEPEXT).tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $(OUTDIR)/$*.$(DEPEXT)
@rm -f $(OUTDIR)/$*.$(DEPEXT).tmp
--.PHONY: all remake clean cleaner resources
++mem: $(EXEC)
++ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./$(EXEC)
+
++.PHONY: all remake clean cleaner resources mem
x = 0.0,
y = 0.0
},
- Physics = 0,
+ Physics = {
- gravity = true,
+ hitbox = {
- {x = -0.5, y = -0.8},
- {x = 0.5, y = -0.8},
- {x = -0.5, y = 0.8},
- {x = 0.5, y = 0.8},
++ ll = {x = -0.5, y = -0.8},
++ lr = {x = 0.5, y = -0.8},
++ ul = {x = -0.5, y = 0.8},
++ ur = {x = 0.5, y = 0.8},
+ }
+ },
Name = "bord",
- hellotrue = true,
- hellofalse = false,
Render = {
texture = "Assets/player.png",
- visible = true
+ visible = true,
+ offset = {
+ ll = {x = -0.5, y = -0.8},
+ lr = {x = 0.5, y = -0.8},
+ ul = {x = -0.5, y = 0.8},
+ ur = {x = 0.5, y = 0.8},
+ }
},
Light = {
r = 1.0,
-world = {
- Seed = 5345345,
- Layers = 2,
-
- -- This is run when the world is registered and not after,
- -- although it is possible to register materials later
+newWorld = {
Register = function(self)
-
- -- TODO make world have global textures to speed up rendering
-
- self:registerMaterial("grass", {
- -- TODO combine both of these into 1
- texture = {
- file = "Assets/world.png",
- offset = { x = 0, y = 0 },
- size = { x = 64, y = 64 }
- },
- normal = {
- file = "Assets/world_normal.png",
- offset = { x = 0, y = 0 },
- size = { x = 64, y = 64 }
- }
+ self.unitSize = 8;
+ self:createLayer(0, {
+ texture = { file = "Assets/world/world1/layers/0/texture.png" },
+ normal = { file = "Assets/world/world1/layers/0/normal.png" },
+ hitbox = "Assets/world/world1/layers/0/hitbox.png"
});
- self:registerMaterial("dirt", {
- texture = {
- file = "Assets/world.png",
- offset = { x = 64, y = 0 },
- size = { x = 64, y = 64 }
- },
- normal = {
- file = "Assets/world_normal.png",
- offset = { x = 64, y = 0 },
- size = { x = 64, y = 64 }
- }
+ self:createLayer(1, {
+ texture = { file = "Assets/world/world1/layers/1/texture.png" },
+ normal = { file = "Assets/world/world1/layers/1/normal.png" },
+ hitbox = "Assets/world/world1/layers/1/hitbox.png"
});
- self:createDecoLayer(10, {
- texture = { file = "Assets/world/world1/layers/deco/texture.png" },
- self:registerMaterial("stone", {
- texture = {
- file = "Assets/world.png",
- offset = { x = 128, y = 0 },
- size = { x = 64, y = 64 }
- },
- normal = {
- file = "Assets/world_normal.png",
- offset = { x = 128, y = 0 },
- size = { x = 64, y = 64 }
- }
++ self:createDecoLayer(7, {
++ texture = { file = "Assets/world/world1/layers/deco/forestTileBack.png" },
++ normal = { file = "Assets/world/world1/layers/deco/normal.png" },
+ });
- self:registerMaterial("flower", {
- texture = {
- file = "Assets/world.png",
- offset = { x = 192, y = 0 },
- size = { x = 64, y = 64 }
- },
- normal = {
- file = "Assets/world_normal.png",
- offset = { x = 192, y = 0 },
- size = { x = 64, y = 64 }
- },
- passable = true
++ self:createDecoLayer(5.5, {
++ texture = { file = "Assets/world/world1/layers/deco/forestTileMid.png" },
++ normal = { file = "Assets/world/world1/layers/deco/normal.png" },
+ });
- self:registerMaterial("trunk", {
- texture = {
- file = "Assets/world.png",
- offset = { x = 256, y = 0 },
- size = { x = 64, y = 64 }
- },
- normal = {
- file = "Assets/world_normal.png",
- offset = { x = 256, y = 0 },
- size = { x = 64, y = 64 }
- }
++ self:createDecoLayer(4, {
++ texture = { file = "Assets/world/world1/layers/deco/forestTileFront.png" },
+ normal = { file = "Assets/world/world1/layers/deco/normal.png" },
});
end,
-
Generate = function(self)
- print("Generating");
- math.randomseed(self.Seed)
- xsize, ysize, zsize = self:setSize(250, 128, 3)
- for Z = 0,zsize-1 do
- for X = 0,xsize-1 do
- if Z == 0 then
- YGen = math.floor(6*math.sin(X/20)) + 64
- elseif Z == 1 then
- YGen = math.floor(9*math.sin(X/20)) + 64
- else
- YGen = math.floor(15*math.sin(X/20)) + 64
- end
- YDepth = math.random(3,5)
- for Y = 0,ysize-1 do
- if Y == YGen then
- self:setData(X, Y, Z, "grass");
- elseif Y < YGen and Y > (YGen - YDepth) then
- self:setData(X, Y, Z, "dirt");
- elseif Y < YGen then
- self:setData(X, Y, Z, "stone");
- elseif Y == YGen + 1 then
- if math.random(0, 100) == 53 then
- self:setData(X, Y, Z, "flower");
- elseif math.random(0, 100) == 45 then
- self:setData(X, Y, Z, "trunk");
- end
- end
- --print(X..","..Y..","..Z);
- end
- end
- end
- --self:setData(1000, 1345, 5, "grass"); -- Test error checking
- print("Done with world gen");
end
}
---world:Generate()
-game.worldRegister(world)
+game.worldRegister(newWorld);
-
- --world = {
- -- Seed = 5345345,
- -- Layers = 2,
- --
- -- -- This is run when the world is registered and not after,
- -- -- although it is possible to register materials later
- -- Register = function(self)
- --
- -- -- TODO make world have global textures to speed up rendering
- -- self:registerMaterial("grass", {
- -- -- TODO combine both of these into 1
- -- texture = {
- -- file = "Assets/world.png",
- -- offset = { x = 0, y = 0 },
- -- size = { x = 64, y = 64 }
- -- },
- -- normal = {
- -- file = "Assets/world_normal.png",
- -- offset = { x = 0, y = 0 },
- -- size = { x = 64, y = 64 }
- -- }
- -- });
- -- self:registerMaterial("dirt", {
- -- texture = {
- -- file = "Assets/world.png",
- -- offset = { x = 64, y = 0 },
- -- size = { x = 64, y = 64 }
- -- },
- -- normal = {
- -- file = "Assets/world_normal.png",
- -- offset = { x = 64, y = 0 },
- -- size = { x = 64, y = 64 }
- -- }
- -- });
- -- self:registerMaterial("stone", {
- -- texture = {
- -- file = "Assets/world.png",
- -- offset = { x = 128, y = 0 },
- -- size = { x = 64, y = 64 }
- -- },
- -- normal = {
- -- file = "Assets/world_normal.png",
- -- offset = { x = 128, y = 0 },
- -- size = { x = 64, y = 64 }
- -- }
- -- });
- -- self:registerMaterial("flower", {
- -- texture = {
- -- file = "Assets/world.png",
- -- offset = { x = 192, y = 0 },
- -- size = { x = 64, y = 64 }
- -- },
- -- normal = {
- -- file = "Assets/world_normal.png",
- -- offset = { x = 192, y = 0 },
- -- size = { x = 64, y = 64 }
- -- },
- -- passable = true
- -- });
- -- self:registerMaterial("trunk", {
- -- texture = {
- -- file = "Assets/world.png",
- -- offset = { x = 256, y = 0 },
- -- size = { x = 64, y = 64 }
- -- },
- -- normal = {
- -- file = "Assets/world_normal.png",
- -- offset = { x = 256, y = 0 },
- -- size = { x = 64, y = 64 }
- -- }
- -- });
- -- end,
- --
- -- Generate = function(self)
- -- math.randomseed(self.Seed)
- -- xsize, ysize, zsize = self:setSize(250, 128, 3)
- -- for Z = 0,zsize-1 do
- -- for X = 0,xsize-1 do
- -- if Z == 0 then
- -- YGen = math.floor(6*math.sin(X/20)) + 64
- -- elseif Z == 1 then
- -- YGen = math.floor(9*math.sin(X/20)) + 64
- -- else
- -- YGen = math.floor(15*math.sin(X/20)) + 64
- -- end
- -- YDepth = math.random(3,5)
- -- for Y = 0,ysize-1 do
- -- if Y == YGen then
- -- self:setData(X, Y, Z, "grass");
- -- elseif Y < YGen and Y > (YGen - YDepth) then
- -- self:setData(X, Y, Z, "dirt");
- -- elseif Y < YGen then
- -- self:setData(X, Y, Z, "stone");
- -- elseif Y == YGen + 1 then
- -- if math.random(0, 100) == 53 then
- -- self:setData(X, Y, Z, "flower");
- -- elseif math.random(0, 100) == 45 then
- -- self:setData(X, Y, Z, "trunk");
- -- end
- -- end
- -- --print(X..","..Y..","..Z);
- -- end
- -- end
- -- end
- -- self:setData(1000, 1345, 5, "grass"); -- Test error checking
- -- print("Done with world gen");
- -- end
- --}
-
- --world:Generate()
- --game.worldRegister(world)
{
public:
bool standing = true;
++ bool gravity = true;
++ glm::vec2 corners[4] = {
++ glm::vec2(-0.5, -0.5), // lower left
++ glm::vec2( 0.5, -0.5), // lower right
++ glm::vec2(-0.5, 0.5), // upper left
++ glm::vec2( 0.5, 0.5) // upper right
++ };
++
Physics FromLua([[maybe_unused]] sol::object ref)
{
++ if (ref.get_type() == sol::type::table) {
++ sol::table tab = ref;
++
++ if (tab["gravity"].get_type() == sol::type::boolean)
++ this->gravity = tab["gravity"];
++
++ if (tab["hitbox"].get_type() == sol::type::table) {
++ sol::table hitbox = tab["hitbox"];
++ if (hitbox["ll"] == sol::type::table)
++ corners[0] = Script::to<glm::vec2>(hitbox["ll"]);
++ if (hitbox["lr"] == sol::type::table)
++ corners[1] = Script::to<glm::vec2>(hitbox["lr"]);
++ if (hitbox["ul"] == sol::type::table)
++ corners[2] = Script::to<glm::vec2>(hitbox["ul"]);
++ if (hitbox["ur"] == sol::type::table)
++ corners[3] = Script::to<glm::vec2>(hitbox["ur"]);
++ }
++ } else {
++ throw std::string(
++ "Physics component table formatted incorrectly"
++ );
++ }
return *this;
}
struct Position : Component<Position>
{
public:
- float x, y;
- double x, y;
++ float x, y, z;
- Position(float _x = 0, float _y = 0) :
- Position(double _x = 0, double _y = 0) :
-- x(_x), y(_y) {}
++ Position(float _x = 0, float _y = 0, float _z = 0) :
++ x(_x), y(_y), z(_z) {}
Position FromLua(sol::object ref)
{
- glm::vec2 vec = Script::to<glm::vec2>(ref);
- if (ref.get_type() == sol::type::table) {
- sol::table tab = ref;
- if (tab["x"] != nullptr)
- this->x = tab["x"];
- if (tab["y"] != nullptr)
- this->y = tab["y"];
- } else {
- throw std::string("Position table not formatted properly");
- }
++ glm::vec3 vec = Script::to<glm::vec3>(ref);
+ this->x = vec.x;
+ this->y = vec.y;
++ this->z = vec.z;
+
return *this;
}
- glm::vec2 vec()
++ glm::vec3 vec()
+ {
- return glm::vec2(x, y);
++ return glm::vec3(x, y, z);
+ }
+
void serialize(cereal::JSONOutputArchive& ar) final {
ar(CEREAL_NVP(x), CEREAL_NVP(y));
}
void ScriptSystem::scriptExport(void)
{
- std::function<sol::table(sol::table)> entitySpawn =
- [this](sol::table t){ return spawn(t);};
-
- std::function<World* (sol::object)> worldRegister =
- [this](sol::object t){ return worldSystem.addWorld(t); };
-
lua.new_usertype<Position>("Position",
- sol::constructors<Position(float x, float y), Position()>(),
- sol::constructors<Position(double x, double y), Position()>(),
++ sol::constructors<Position(float x, float y, float z), Position()>(),
"x", &Position::x,
-- "y", &Position::y);
++ "y", &Position::y,
++ "z", &Position::z);
lua.new_usertype<Name>("Name",
sol::constructors<Name(std::string), Name()>(),
lua.new_usertype<Physics>("Physics",
sol::constructors<Physics(void), Physics()>(),
-- "standing", &Physics::standing);
++ "standing", &Physics::standing,
++ "gravity", &Physics::gravity);
lua.new_usertype<World>("World",
sol::constructors<World(sol::object), World(void)>(),
"Generate", &World::generate,
"Seed", sol::property(&World::setSeed, &World::getSeed),
- "setData", &World::setData,
- "registerMaterial", &World::registerMaterial,
- "setSize", &World::setSize,
- "getSize", &World::getSize);
+ "getSize", &World::getSize,
+
+ // New stuff
+ "unitSize", sol::property(&World::setUnitSize, &World::getUnitSize),
+ "createLayer", &World::registerLayer,
+ "createDecoLayer", &World::registerDecoLayer);
game = lua["game"].get_or_create<sol::table>();
- game.set_function("spawn", entitySpawn);
- game.set_function("worldRegister", worldRegister);
+ game.set_function("spawn", bindInstance(&ScriptSystem::spawn, this));
+ game.set_function("worldRegister", bindInstance(&WorldSystem::addWorld,
+ &worldSystem));
}
sol::table ScriptSystem::spawn(sol::object param)
/* RENDERING */
void World::generateMesh()
{
- for (auto &l : solidLayers) {
- //const unsigned int voxelLength = 6; // 2 triangles @ 3 points each
-
- // Preallocate size of vertexes
- mesh = std::basic_string<WorldMeshData>();
- for (float Z = data.size() - 1; Z >= 0; Z--) {
- for (float X = 0; X < data.at(Z).size(); X++) {
- for (float Y = 0; Y < data.at(Z).at(X).size(); Y++) {
- int d = data.at(Z).at(X).at(Y);
-
- if (d == -1) // Don't make a mesh for air of course
- continue;
-
- Texture &t = registry.at(d).texture;
- glm::vec2& to = t.offset;
- glm::vec2& ts = t.size;
-
- float tr = 1.0f;
-
- // TODO play with this a bit so it only goes trans
- // if player is behind the front layer
- try {
- if (Z < data.size() - 1 && Z >= 0) {
- if (data.at(Z+1).at(X).at(Y) == -1)
- tr = 1.0f;
- }
- } catch (...) {
- tr = 1.0f;
- }
-
- mesh += {X , Y , Z, to.x , to.y+ts.y, tr};
- mesh += {X+1, Y , Z, to.x+ts.x, to.y+ts.y, tr};
- mesh += {X , Y+1, Z, to.x , to.y , tr};
-
- mesh += {X+1, Y , Z, to.x+ts.x, to.y+ts.y, tr};
- mesh += {X+1, Y+1, Z, to.x+ts.x, to.y , tr};
- mesh += {X , Y+1, Z, to.x , to.y , tr};
- }
- }
++ for (auto &l : drawLayers) {
+
+ // Preallocate size of vertexes
+
- float Z = l.drawLayer;
- auto to = l.texture.offset;
- auto ts = l.texture.size;
++ 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;
++ 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);
++ 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,
++ meshAdd.push_back(WorldMeshUpdateEvent(l->layerVBO,
++ l->texture.tex,
++ l->normal.tex,
+ 36));
}
--
- for (auto &l : drawLayers) {
- (void)l;
- }
- glBindBuffer(GL_ARRAY_BUFFER, worldVBO);
- glBufferData(GL_ARRAY_BUFFER,
- mesh.size() * sizeof(WorldMeshData),
- mesh.data(),
- GL_STATIC_DRAW);
-
- meshUpdated = true;
}
/* SEED */
/* PHYSICS */
double World::getHeight(double x, double y, double z)
{
- unsigned int X = static_cast<unsigned int>(x);
- unsigned int Z = static_cast<unsigned int>(z);
-
- double Y = 0.0;
- try {
- auto &d = data.at(Z).at(X);
- for (int yi = d.size()-1; yi >= 0; yi--) {
- if (d.at(yi) >= 0) {
- if (!registry.at(d.at(yi)).passable) {
- Y = static_cast<double>(yi);
- Y += 1;
- break;
+ (void)y;
+ double Y = 0.0f;
+ for (auto &l : solidLayers) {
- if (z == l.drawLayer) {
++ if (z == l->drawLayer) {
+ int wx = x*unitSize;
+
+ int h = 0.0;
- for (auto b : l.hitbox[wx]) {
++ for (auto b : l->hitbox[wx]) {
+ if (b == true)
+ Y = h;
+ h++;
+ }
- return (Y/unitSize);
++ 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
++
+ }
+ }
+ }
- } catch (...) { // If we get any errors, just let the character
- //return y;
- (void)y;
- return 0.0;
+ }
++ return glm::vec3(0);
++}
++
++
+/*********
+* NEW *
+*********/
+void World::registerLayer(float z, sol::object obj)
+{
+ if (obj.get_type() == sol::type::table) {
+ sol::table tab = obj;
- solidLayers.push_back(SolidLayer(z, tab));
++ 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();
+}
- return Y;
+void World::registerDecoLayer(float z, sol::object obj)
+{
+ if (obj.get_type() == sol::type::table) {
+ sol::table tab = obj;
- drawLayers.push_back(Layer(z, tab));
++ drawLayers.push_back(std::make_shared<Layer>(Layer(z, tab)));
+ } else {
+ throw std::string("Layer must receive a table");
+ }
+ generateMesh();
}
#include <entityx/entityx.h>
#include <sol/sol.hpp>
+#include <soil/SOIL.h>
+
#include "texture.hpp"
-struct WorldMeshData
-{
- float posX, posY, posZ;
- float texX, texY;
- float transparency;
-}__attribute__((packed));
+#include "events/render.hpp"
+
++#include <components/Position.hpp> // For entity position
++#include <components/Velocity.hpp> // For entity velocity
++#include <components/Physics.hpp> // For entity hitbox(es)
+
+
struct WorldMaterial
{
bool passable = false;
unsigned int seed;
unsigned int layers;
- unsigned int height;
- unsigned int width;
-
- std::vector<std::vector<std::vector<int>>> data;
+ unsigned int unitSize;
- std::vector<SolidLayer> solidLayers;
- std::vector<Layer> drawLayers;
++ std::vector<std::shared_ptr<SolidLayer>> solidLayers;
++ std::vector<std::shared_ptr<Layer>> drawLayers;
+
- std::unordered_map<std::string, int> string_registry;
- std::vector<WorldMaterial> registry;
++ std::vector<std::pair<glm::vec2, glm::vec2>>
++ getIntersectingPlanes(glm::vec2 origin, glm::vec2 dir);
protected:
// RENDER
/* PHYSICS */
double getHeight(double x, double y, double z);
++ glm::vec3 collide(glm::vec3 &start, glm::vec3 &end, Physics &phys);
+
+ // NEW
+ unsigned int getUnitSize() {return unitSize;}
+ void setUnitSize(unsigned int u) {unitSize = u;}
+
+ void registerLayer(float, sol::object);
+ void registerDecoLayer(float, sol::object);
};
/**