From: Andy Belle-Isle Date: Tue, 8 Oct 2019 07:03:16 +0000 (-0400) Subject: Using polymorphism for world layer storage now, and added demo backdrops X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=a422f32613441b5313e4a3bb0fab61f8cb87914c;p=clyne%2Fgamedev2.git Using polymorphism for world layer storage now, and added demo backdrops --- a422f32613441b5313e4a3bb0fab61f8cb87914c diff --cc Assets/world/world1/layers/0/forestTileBack.png index 0000000,0000000..f1c1a8b new file mode 100644 Binary files differ diff --cc Assets/world/world1/layers/deco/forestTileBack.png index 0000000,0000000..c5e9dac new file mode 100644 Binary files differ diff --cc Assets/world/world1/layers/deco/forestTileFront.png index 0000000,0000000..234520c new file mode 100644 Binary files differ diff --cc Assets/world/world1/layers/deco/forestTileMid.png index 0000000,0000000..b51d0bc new file mode 100644 Binary files differ diff --cc Assets/world/world1/layers/deco/normal.png index c0aab94,0000000..767f7e3 mode 100644,000000..100644 Binary files differ diff --cc Makefile index 9c9fafe,dcbac2b..f79435c --- a/Makefile +++ b/Makefile @@@ -82,4 -82,5 +82,7 @@@ $(OUTDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SR @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 diff --cc Scripts/init.lua index 46fe040,ec9e350..da2517f --- a/Scripts/init.lua +++ b/Scripts/init.lua @@@ -34,25 -35,13 +34,24 @@@ player = 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, diff --cc Scripts/world.lua index f1d2b66,bb6c61e..1fb74d2 --- a/Scripts/world.lua +++ b/Scripts/world.lua @@@ -1,136 -1,112 +1,31 @@@ -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) diff --cc src/components/Physics.hpp index 378e87f,378e87f..cb4d08a --- a/src/components/Physics.hpp +++ b/src/components/Physics.hpp @@@ -25,8 -25,8 +25,38 @@@ struct Physics : Componentgravity = 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(hitbox["ll"]); ++ if (hitbox["lr"] == sol::type::table) ++ corners[1] = Script::to(hitbox["lr"]); ++ if (hitbox["ul"] == sol::type::table) ++ corners[2] = Script::to(hitbox["ul"]); ++ if (hitbox["ur"] == sol::type::table) ++ corners[3] = Script::to(hitbox["ur"]); ++ } ++ } else { ++ throw std::string( ++ "Physics component table formatted incorrectly" ++ ); ++ } return *this; } diff --cc src/components/Position.hpp index fc5cb9a,56e8707..bfa4b41 --- a/src/components/Position.hpp +++ b/src/components/Position.hpp @@@ -24,25 -24,25 +24,26 @@@ struct Position : Component { 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(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(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)); } diff --cc src/script.cpp index 4cfcddd,0f940d3..4fda543 --- a/src/script.cpp +++ b/src/script.cpp @@@ -87,16 -87,10 +87,11 @@@ void ScriptSystem::doFile(void void ScriptSystem::scriptExport(void) { - std::function entitySpawn = - [this](sol::table t){ return spawn(t);}; - - std::function worldRegister = - [this](sol::object t){ return worldSystem.addWorld(t); }; - lua.new_usertype("Position", - sol::constructors(), - sol::constructors(), ++ sol::constructors(), "x", &Position::x, -- "y", &Position::y); ++ "y", &Position::y, ++ "z", &Position::z); lua.new_usertype("Name", sol::constructors(), @@@ -125,22 -119,21 +120,24 @@@ lua.new_usertype("Physics", sol::constructors(), -- "standing", &Physics::standing); ++ "standing", &Physics::standing, ++ "gravity", &Physics::gravity); lua.new_usertype("World", sol::constructors(), "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(); - 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) diff --cc src/world.cpp index fb846eb,cd89a22..8987eac --- a/src/world.cpp +++ b/src/world.cpp @@@ -68,41 -133,53 +68,37 @@@ World::getSize( /* 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(); - 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 */ @@@ -120,47 -197,28 +116,101 @@@ unsigned int World::setSeed(unsigned in /* PHYSICS */ double World::getHeight(double x, double y, double z) { - unsigned int X = static_cast(x); - unsigned int Z = static_cast(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(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> ++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>(); ++} ++ ++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(s)); ++ drawLayers.push_back(std::make_shared(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(z, tab))); + } else { + throw std::string("Layer must receive a table"); + } + generateMesh(); } diff --cc src/world.hpp index 66d9e3f,9314fa6..0c1275e --- a/src/world.hpp +++ b/src/world.hpp @@@ -26,11 -26,15 +26,16 @@@ #include #include +#include + #include "texture.hpp" +#include "events/render.hpp" + ++#include // For entity position ++#include // For entity velocity ++#include // For entity hitbox(es) + -struct WorldMeshData -{ - float posX, posY, posZ; - float texX, texY; - float transparency; -}__attribute__((packed)); + struct WorldMaterial { bool passable = false; @@@ -119,9 -64,13 +124,12 @@@ private unsigned int seed; unsigned int layers; - unsigned int height; - unsigned int width; - - std::vector>> data; + unsigned int unitSize; - std::vector solidLayers; - std::vector drawLayers; ++ std::vector> solidLayers; ++ std::vector> drawLayers; + - std::unordered_map string_registry; - std::vector registry; ++ std::vector> ++ getIntersectingPlanes(glm::vec2 origin, glm::vec2 dir); protected: // RENDER @@@ -150,13 -115,6 +158,14 @@@ public /* 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); }; /**