diff options
author | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-09-12 18:16:06 -0400 |
---|---|---|
committer | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-09-12 18:16:06 -0400 |
commit | 2564533a4860a8452abc27ba05115ca11ed4a787 (patch) | |
tree | 67c5dfb2e892f0d492d977b8a5fa42321f2fa4a5 | |
parent | 8f0db67c5fdbc1e7b8759f44b45ad64caf336cb5 (diff) |
Ability to pass world data into Lua
-rw-r--r-- | Scripts/world.lua | 17 | ||||
-rw-r--r-- | src/engine.cpp | 6 | ||||
-rw-r--r-- | src/script.cpp | 8 | ||||
-rw-r--r-- | src/texture.hpp | 2 | ||||
-rw-r--r-- | src/world.cpp | 25 | ||||
-rw-r--r-- | src/world.hpp | 41 |
6 files changed, 86 insertions, 13 deletions
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<Scripted>([](entityx::Entity, Scripted &f){ f.cleanup(); }); - entities.each<EventListener>([](entityx::Entity, EventListener &f){ + entities.each<Scripted>([](entityx::Entity, Scripted &f) { f.cleanup(); }); + entities.each<EventListener>([](entityx::Entity, EventListener &f) { + f.cleanup(); }); + systems.system<WorldSystem>()->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<Physics(void), Physics()>(), "standing", &Physics::standing); + lua.new_usertype<World>("World", + sol::constructors<World(sol::object), World(void)>(), + "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<sol::table>(); 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<std::vector<std::vector<unsigned int>>> data; std::vector<unsigned int> registry; - std::vector<std::vector<unsigned int>> 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. |