From 2b095e30f7786526da9339ccada3aae7f2ab5df4 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Tue, 10 Sep 2019 23:55:54 -0400 Subject: Started world system --- src/script.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/script.cpp') diff --git a/src/script.cpp b/src/script.cpp index a6f0968..e3aca3e 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -87,9 +87,12 @@ void ScriptSystem::doFile(void) void ScriptSystem::scriptExport(void) { - std::function func = + 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(), "x", &Position::x, @@ -125,7 +128,8 @@ void ScriptSystem::scriptExport(void) "standing", &Physics::standing); auto gamespace = lua["game"].get_or_create(); - gamespace.set_function("spawn", func); + gamespace.set_function("spawn", entitySpawn); + gamespace.set_function("worldRegister", worldRegister); } sol::table ScriptSystem::spawn(sol::object param) -- cgit v1.2.3 From 8f0db67c5fdbc1e7b8759f44b45ad64caf336cb5 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Wed, 11 Sep 2019 01:46:14 -0400 Subject: World system now has a pointer to the current world, and worlds created in Lua are returned to Lua as pointers --- src/script.cpp | 2 +- src/world.cpp | 6 ++++-- src/world.hpp | 7 ++++--- 3 files changed, 9 insertions(+), 6 deletions(-) (limited to 'src/script.cpp') diff --git a/src/script.cpp b/src/script.cpp index e3aca3e..f52391f 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -90,7 +90,7 @@ void ScriptSystem::scriptExport(void) std::function entitySpawn = [this](sol::table t){ return spawn(t);}; - std::function worldRegister = + std::function worldRegister = [this](sol::object t){ return worldSystem.addWorld(t); }; lua.new_usertype("Position", diff --git a/src/world.cpp b/src/world.cpp index 8b71316..63769a9 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -38,10 +38,12 @@ World::World(sol::object ref) * WORLD SYSTEM * ******************/ -unsigned int WorldSystem::addWorld(sol::object t) +World* WorldSystem::addWorld(sol::object t) { worlds.push_back(World(t)); - return worlds.size()-1; + if (currentWorld == nullptr) + currentWorld = &(worlds.back()); + return &(worlds.back()); } void WorldSystem::configure([[maybe_unused]]entityx::EntityManager& entities, diff --git a/src/world.hpp b/src/world.hpp index 7c66e6b..4ceca94 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -52,13 +52,14 @@ class WorldSystem : public entityx::System { private: std::vector worlds; - //World& currentWorld; + World* currentWorld; public: - WorldSystem(void) {} + WorldSystem(void): + currentWorld(nullptr) {} ~WorldSystem(void) {} - unsigned int addWorld(sol::object); + World* addWorld(sol::object); /** * Prepares the system for running. -- cgit v1.2.3 From 2564533a4860a8452abc27ba05115ca11ed4a787 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Thu, 12 Sep 2019 18:16:06 -0400 Subject: Ability to pass world data into Lua --- Scripts/world.lua | 17 +++++++++++------ src/engine.cpp | 6 ++++-- src/script.cpp | 8 ++++++++ src/texture.hpp | 2 +- src/world.cpp | 25 ++++++++++++++++++++++++- src/world.hpp | 41 ++++++++++++++++++++++++++++++++++++++--- 6 files changed, 86 insertions(+), 13 deletions(-) (limited to 'src/script.cpp') diff --git a/Scripts/world.lua b/Scripts/world.lua index a6224ab..54fcdc0 100644 --- a/Scripts/world.lua +++ b/Scripts/world.lua @@ -20,24 +20,29 @@ world = { Layers = 2, Generate = function(self) math.randomseed(self.Seed) - self.data = {} + --self.data = {} for Z = 0,self.Layers do - self.data[Z] = {} + --self.data[Z] = {} for X = 0,250 do - self.data[Z][X] = {} + --self.data[Z][X] = {} YGen = math.floor(6*math.sin(X/20) + Z) + 64 YDepth = math.random(2,5) for Y = 0,128 do if Y == YGen then - self.data[Z][X][Y] = 0 + --self.data[Z][X][Y] = 0 + self:setData(X, Y, Z, 0); elseif Y < YGen and Y > (YGen - YDepth) then - self.data[Z][X][Y] = 1 + --self.data[Z][X][Y] = 1 + self:setData(X, Y, Z, 1); elseif Y < YGen then - self.data[Z][X][Y] = 2 + --self.data[Z][X][Y] = 2 + self:setData(X, Y, Z, 2); end + --print(X..","..Y..","..Z); end end end + print("Done with world gen"); end } diff --git a/src/engine.cpp b/src/engine.cpp index ecc6501..6e6a0e5 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -147,9 +147,11 @@ void Engine::run(void) GameState::save("save.json", entities); // Remove all Lua references from entities - entities.each([](entityx::Entity, Scripted &f){ f.cleanup(); }); - entities.each([](entityx::Entity, EventListener &f){ + entities.each([](entityx::Entity, Scripted &f) { f.cleanup(); }); + entities.each([](entityx::Entity, EventListener &f) { + f.cleanup(); }); + systems.system()->cleanup(); } bool Engine::shouldRun(void) diff --git a/src/script.cpp b/src/script.cpp index f52391f..7d04375 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -127,6 +127,14 @@ void ScriptSystem::scriptExport(void) sol::constructors(), "standing", &Physics::standing); + lua.new_usertype("World", + sol::constructors(), + "generate", &World::generate, + "Seed", sol::property(&World::setSeed, &World::getSeed), + "Layers", sol::property(&World::setLayers, &World::getLayers), + "setData", &World::setData); + + auto gamespace = lua["game"].get_or_create(); gamespace.set_function("spawn", entitySpawn); gamespace.set_function("worldRegister", worldRegister); diff --git a/src/texture.hpp b/src/texture.hpp index 4483d0f..25fed54 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -31,7 +31,7 @@ class Texture { private: public: - GLuint tex; + GLuint tex = 0; int width; int height; Texture() {}; diff --git a/src/world.cpp b/src/world.cpp index 63769a9..9e289ea 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -26,11 +26,34 @@ World::World(sol::object ref) { if (ref.get_type() == sol::type::table) { - + sol::table tab = ref; + if (tab["Seed"] == sol::type::number) { + seed = tab["Seed"]; + } + if (tab["Layers"] == sol::type::number) { + layers = tab["Layers"]; + } + 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; } + generate(this); +} + +void World::setData(unsigned int x, + unsigned int y, + unsigned int z, + unsigned int d) +{ + (void)x; + (void)y; + (void)z; + (void)d; + + // TODO actually do stuff here } diff --git a/src/world.hpp b/src/world.hpp index 4ceca94..c0e6098 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -37,11 +37,39 @@ struct WorldMaterial class World { private: + unsigned int seed; + unsigned int layers; + + std::vector>> data; std::vector registry; - std::vector> worldData; public: + World() {} World(sol::object ref); - ~World() {} + ~World() { + generate = sol::nil; + registry.clear(); + data.clear(); + } + + sol::function generate; + + /* SEED */ + unsigned int getSeed() {return seed;} + void setSeed(unsigned int s) {seed = s;} + + /* LAYERS */ + unsigned int getLayers() {return layers;} + // If we change the amount of layers, we have to regenerate the world + void setLayers(unsigned int l) { + layers = l; + generate(); + } + + /* DATA */ + void setData(unsigned int x, + unsigned int y, + unsigned int z, + unsigned int d); }; /** @@ -57,9 +85,16 @@ public: WorldSystem(void): currentWorld(nullptr) {} - ~WorldSystem(void) {} + ~WorldSystem(void) { + currentWorld = nullptr; + worlds.clear(); + } World* addWorld(sol::object); + void cleanup() + { + worlds.clear(); + } /** * Prepares the system for running. -- cgit v1.2.3 From 54cf2f1f9a044a0d0def92c1904b24d3c3647d36 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Fri, 13 Sep 2019 01:52:26 -0400 Subject: Lua can now register materials for worlds during world registration --- Scripts/world.lua | 38 ++++++++++++++++++++++---------------- src/script.cpp | 5 +++-- src/world.cpp | 37 ++++++++++++++++++++++++++++++++----- src/world.hpp | 33 +++++++++++++++++++++++++++------ 4 files changed, 84 insertions(+), 29 deletions(-) (limited to 'src/script.cpp') diff --git a/Scripts/world.lua b/Scripts/world.lua index 54fcdc0..655cdab 100644 --- a/Scripts/world.lua +++ b/Scripts/world.lua @@ -1,23 +1,29 @@ world = { - Registry = { - grass = { - id = 0, + 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) + self:registerMaterial("grass", { texture = "Assets/grass.png", normal = "Assets/grass_normal" - }, - dirt = { - id = 1, + }); + self:registerMaterial("dirt", { texture = "Assets/dirt.png", normal = "Assets/dirt_normal.png" - }, - stone = { - id = 2, + }); + self:registerMaterial("stone", { texture = "Assets/stone.png", normal = "Assets/dirt_normal.png" - } - }, - Seed = 5345345, - Layers = 2, + }); + self:registerMaterial("flower", { + texture = "Assets/flower.png", + normal = "Assets/flower_normal.png", + passable = true + }); + end, + Generate = function(self) math.randomseed(self.Seed) --self.data = {} @@ -30,13 +36,13 @@ world = { for Y = 0,128 do if Y == YGen then --self.data[Z][X][Y] = 0 - self:setData(X, Y, Z, 0); + self:setData(X, Y, Z, "grass"); elseif Y < YGen and Y > (YGen - YDepth) then --self.data[Z][X][Y] = 1 - self:setData(X, Y, Z, 1); + self:setData(X, Y, Z, "dirt"); elseif Y < YGen then --self.data[Z][X][Y] = 2 - self:setData(X, Y, Z, 2); + self:setData(X, Y, Z, "stone"); end --print(X..","..Y..","..Z); end diff --git a/src/script.cpp b/src/script.cpp index 7d04375..da365e1 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -129,10 +129,11 @@ void ScriptSystem::scriptExport(void) lua.new_usertype("World", sol::constructors(), - "generate", &World::generate, + "Generate", &World::generate, "Seed", sol::property(&World::setSeed, &World::getSeed), "Layers", sol::property(&World::setLayers, &World::getLayers), - "setData", &World::setData); + "setData", &World::setData, + "registerMaterial", &World::registerMaterial); auto gamespace = lua["game"].get_or_create(); diff --git a/src/world.cpp b/src/world.cpp index 9e289ea..dc9f75b 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -2,7 +2,6 @@ * @file world.cpp * * Copyright (C) 2019 Belle-Isle, Andrew - * Author: Belle-Isle, Andrew * * 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 @@ -23,30 +22,37 @@ /***************** * WORLD CLASS * *****************/ -World::World(sol::object ref) +World::World(sol::object param) { - if (ref.get_type() == sol::type::table) { - sol::table tab = ref; + 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; } + + registerMat(this); generate(this); } void World::setData(unsigned int x, unsigned int y, unsigned int z, - unsigned int d) + std::string d) { (void)x; (void)y; @@ -56,6 +62,27 @@ void World::setData(unsigned int x, // TODO actually do stuff here } +void World::registerMaterial(std::string name, sol::object data) +{ + if (data.get_type() == sol::type::table) { + sol::table tab = data; + + // Make sure this material has not been registered before + auto it = string_registry.find(name); + if (it == string_registry.end()) { + string_registry.emplace(name, registry.size()); + registry.push_back(WorldMaterial(tab)); + } else { + std::cerr << "Material: " << name + << " was already registered" << std::endl; + } + + } else { + // TODO better logging + std::cerr << "Material registration must have a table" << std::endl; + } +} + /****************** * WORLD SYSTEM * diff --git a/src/world.hpp b/src/world.hpp index c0e6098..cb9995e 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -30,8 +30,25 @@ struct WorldMaterial { + bool passable = false; + Texture texture; Texture normal; + + WorldMaterial(sol::table tab) { + if (tab["texture"] == sol::type::string) { + std::string tex = tab["texture"]; + texture = Texture(tex); + } + if (tab["normal"] == sol::type::string) { + std::string nor = tab["normal"]; + normal = Texture(nor); + } + + if (tab["passable"] == sol::type::boolean) { + passable = tab["passable"]; + } + } }; class World @@ -41,17 +58,22 @@ private: unsigned int layers; std::vector>> data; - std::vector registry; + + std::unordered_map string_registry; + std::vector registry; + public: World() {} World(sol::object ref); ~World() { + registerMat = sol::nil; generate = sol::nil; registry.clear(); data.clear(); } sol::function generate; + sol::function registerMat; /* SEED */ unsigned int getSeed() {return seed;} @@ -66,10 +88,10 @@ public: } /* DATA */ - void setData(unsigned int x, - unsigned int y, - unsigned int z, - unsigned int d); + void setData(unsigned int, unsigned int, unsigned int, std::string); + + /* REGISTRY */ + void registerMaterial(std::string, sol::object); }; /** @@ -111,4 +133,3 @@ public: }; #endif // SYSTEM_WORLD_HPP_ - -- cgit v1.2.3 From 0e19992b820158f8e9c0fc16ddc372a5ea596f53 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Sat, 14 Sep 2019 00:30:05 -0400 Subject: Lua can now modify world size and world data --- Scripts/world.lua | 8 +++-- src/script.cpp | 5 ++-- src/world.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++---------- src/world.hpp | 37 +++++++++++++---------- 4 files changed, 104 insertions(+), 36 deletions(-) (limited to 'src/script.cpp') diff --git a/Scripts/world.lua b/Scripts/world.lua index 655cdab..f87f108 100644 --- a/Scripts/world.lua +++ b/Scripts/world.lua @@ -26,14 +26,15 @@ world = { Generate = function(self) math.randomseed(self.Seed) + xsize, ysize, zsize = self:setSize(250, 128, 3) --self.data = {} - for Z = 0,self.Layers do + for Z = 0,zsize-1 do --self.data[Z] = {} - for X = 0,250 do + for X = 0,xsize-1 do --self.data[Z][X] = {} YGen = math.floor(6*math.sin(X/20) + Z) + 64 YDepth = math.random(2,5) - for Y = 0,128 do + for Y = 0,ysize-1 do if Y == YGen then --self.data[Z][X][Y] = 0 self:setData(X, Y, Z, "grass"); @@ -48,6 +49,7 @@ world = { end end end + self:setData(1000, 1345, 5, "grass"); -- Test error checking print("Done with world gen"); end } diff --git a/src/script.cpp b/src/script.cpp index da365e1..cf5b2dd 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -131,9 +131,10 @@ void ScriptSystem::scriptExport(void) sol::constructors(), "Generate", &World::generate, "Seed", sol::property(&World::setSeed, &World::getSeed), - "Layers", sol::property(&World::setLayers, &World::getLayers), "setData", &World::setData, - "registerMaterial", &World::registerMaterial); + "registerMaterial", &World::registerMaterial, + "setSize", &World::setSize, + "getSize", &World::getSize); auto gamespace = lua["game"].get_or_create(); diff --git a/src/world.cpp b/src/world.cpp index dc9f75b..eb101f2 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -22,6 +22,7 @@ /***************** * WORLD CLASS * *****************/ +/* CONSTRUCTORS */ World::World(sol::object param) { if (param.get_type() == sol::type::table) { @@ -45,23 +46,13 @@ World::World(sol::object param) std::cerr << "World paramaters must be stored in a table" << std::endl; } - registerMat(this); - generate(this); -} - -void World::setData(unsigned int x, - unsigned int y, - unsigned int z, - std::string d) -{ - (void)x; - (void)y; - (void)z; - (void)d; - - // TODO actually do stuff here + if (registerMat != sol::nil) + registerMat(this); + if (generate != sol::nil) + generate(this); } +/* REGISTRY */ void World::registerMaterial(std::string name, sol::object data) { if (data.get_type() == sol::type::table) { @@ -83,6 +74,75 @@ void World::registerMaterial(std::string name, sol::object data) } } +/* DATA */ +void World::setData(unsigned int x, + unsigned int y, + unsigned int z, + std::string d) +{ + unsigned int discovered = -1; + + auto found = string_registry.find(d); + if (found != string_registry.end()) + discovered = found->second; + + try { + data.at(z).at(x).at(y) = discovered; + } catch (std::out_of_range &oor) { + // Make sure any assignments that are outsize specified world size are + // caught to avoid any seg faults + std::cerr << "Unable to set data at: " + << x << "," << y << "," << z + << " Exception: " << oor.what() << std::endl; + } +} + +/* SIZE */ +std::tuple +World::setSize(unsigned int x, unsigned int y, unsigned int z) +{ + width = x; + height = y; + layers = z; + + data = std::vector>> + (z, std::vector> + (x,std::vector + (y, -1) + ) + ); + + return {width, height, layers}; +} + +std::tuple +World::getSize() +{ + return {width, height, layers}; +} + +/* RENDERING */ +void World::generateMesh() +{ + const unsigned int coordLength = 3 + // x, y, z + 2 + // texture coords + 1; // transparency + + (void)coordLength; +} + +/* SEED */ +unsigned int World::getSeed() +{ + return seed; +} + +unsigned int World::setSeed(unsigned int s) +{ + seed = s; + return seed; +} + /****************** * WORLD SYSTEM * diff --git a/src/world.hpp b/src/world.hpp index cb9995e..7a7461f 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -44,7 +44,6 @@ struct WorldMaterial std::string nor = tab["normal"]; normal = Texture(nor); } - if (tab["passable"] == sol::type::boolean) { passable = tab["passable"]; } @@ -57,12 +56,20 @@ private: unsigned int seed; unsigned int layers; + unsigned int height; + unsigned int width; + std::vector>> data; std::unordered_map string_registry; std::vector registry; + std::vector mesh; public: + /* VARS */ + sol::function generate; + sol::function registerMat; + World() {} World(sol::object ref); ~World() { @@ -72,26 +79,24 @@ public: data.clear(); } - sol::function generate; - sol::function registerMat; - /* SEED */ - unsigned int getSeed() {return seed;} - void setSeed(unsigned int s) {seed = s;} - - /* LAYERS */ - unsigned int getLayers() {return layers;} - // If we change the amount of layers, we have to regenerate the world - void setLayers(unsigned int l) { - layers = l; - generate(); - } + unsigned int getSeed(); + unsigned int setSeed(unsigned int); + + /* REGISTRY */ + void registerMaterial(std::string, sol::object); /* DATA */ void setData(unsigned int, unsigned int, unsigned int, std::string); - /* REGISTRY */ - void registerMaterial(std::string, sol::object); + /* SIZE */ + std::tuple setSize(unsigned int, + unsigned int, + unsigned int); + std::tuple getSize(); + + /* RENDERING */ + void generateMesh(); }; /** -- cgit v1.2.3