]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Lua can now register materials for worlds during world registration
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Fri, 13 Sep 2019 05:52:26 +0000 (01:52 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Fri, 13 Sep 2019 05:52:26 +0000 (01:52 -0400)
Scripts/world.lua
src/script.cpp
src/world.cpp
src/world.hpp

index 54fcdc06696b48e5759e3399c3d3e860e70f97cc..655cdab95e491043577f231fb94c977eec37196d 100644 (file)
@@ -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
index 7d043750e9c4a2164c8a893f6cf7de82db4774fe..da365e1d39c9efbe67acb004ec621a2d5c8c6991 100644 (file)
@@ -129,10 +129,11 @@ void ScriptSystem::scriptExport(void)
 
     lua.new_usertype<World>("World",
             sol::constructors<World(sol::object), World(void)>(),
-            "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<sol::table>();
index 9e289eac8aad1e7f9bda804b46b40476e3f9cdd0..dc9f75b01c36cac8e83732a20f6eee2a48caff45 100644 (file)
@@ -2,7 +2,6 @@
  * @file world.cpp
  *
  * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
- * Author: 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
 /*****************
 *  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  *
index c0e6098321817bf1666900a0f841c30a0f981582..cb9995e2a2e545e0da119d9966e3add243c08e50 100644 (file)
 
 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<std::vector<std::vector<unsigned int>>> data;
-    std::vector<unsigned int> registry;
+
+    std::unordered_map<std::string, unsigned int> string_registry;
+    std::vector<WorldMaterial> 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_
-