aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-13 01:52:26 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-13 01:52:26 -0400
commit54cf2f1f9a044a0d0def92c1904b24d3c3647d36 (patch)
tree4d24c079da281d1144b9019b6156d2d6d0c4a4fd
parentdb388db12d8dc26c9f2d21a4c4e9ea9b0af1cf29 (diff)
Lua can now register materials for worlds during world registration
-rw-r--r--Scripts/world.lua38
-rw-r--r--src/script.cpp5
-rw-r--r--src/world.cpp37
-rw-r--r--src/world.hpp33
4 files changed, 84 insertions, 29 deletions
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>("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>();
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 <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
@@ -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<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_
-