[Registry] Now able to register elements from a Lua table.

This commit is contained in:
Quentin Bazin 2018-12-20 13:31:32 +01:00
parent 2f4a9eac44
commit ca953b6661
4 changed files with 203 additions and 81 deletions

View File

@ -17,26 +17,24 @@
#include <memory>
#include <vector>
#include <sol.hpp>
#include "Block.hpp"
#include "Item.hpp"
#include "Recipe.hpp"
class Registry {
public:
template<typename T, typename... Args>
auto registerBlock(Args &&...args) -> typename std::enable_if<std::is_base_of<Block, T>::value, Block*>::type {
return m_blocks.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)).get();
}
void registerBlockFromTable(const sol::table &table);
template<typename T, typename... Args>
auto registerItem(Args &&...args) -> typename std::enable_if<std::is_base_of<Item, T>::value, Item*>::type {
return m_items.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)).get();
}
template<typename T, typename... Args>
auto registerRecipe(Args &&...args) -> typename std::enable_if<std::is_base_of<Recipe, T>::value, Recipe*>::type {
return m_recipes.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)).get();
}
void registerItemFromTable(const sol::table &table);
void registerCraftingRecipeFromTable(const sol::table &table);
void registerSmeltingRecipeFromTable(const sol::table &table);
const Block &getBlock(std::size_t id) const { return *m_blocks.at(id).get(); }
const Item &getItem(std::size_t id) const { return *m_items.at(id).get(); }
@ -47,6 +45,16 @@ class Registry {
static void setInstance(Registry &instance) { s_instance = &instance; }
private:
template<typename T, typename... Args>
auto registerBlock(Args &&...args) -> typename std::enable_if<std::is_base_of<Block, T>::value, Block*>::type {
return m_blocks.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)).get();
}
template<typename T, typename... Args>
auto registerRecipe(Args &&...args) -> typename std::enable_if<std::is_base_of<Recipe, T>::value, Recipe*>::type {
return m_recipes.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)).get();
}
static Registry *s_instance;
std::vector<std::unique_ptr<Block>> m_blocks;

View File

@ -1,41 +1,133 @@
registry:register_block(0, 0, "Air")
registry:register_block(1, 37, "Dirt")
registry:register_block({
id = 0,
texture = 0,
name = "Air",
})
local cobblestone = registry:register_block(2, 38, "Cobblestone")
cobblestone.hardness = 2
cobblestone.harvest_requirements = 1
registry:register_block({
id = 1,
texture = 37,
name = "Dirt",
})
registry:register_block(3, 226, "Grass"):set_item_drop(1, 1)
registry:register_block(4, 266, "Leaves").hardness = 0.5
registry:register_block(5, 277, "Wood").hardness = 2
registry:register_block({
id = 2,
texture = 38,
name = "Cobblestone",
hardness = 2,
harvest_requirements = 1,
})
local stone = registry:register_block(6, 402, "Stone")
stone.hardness = 1.5
stone.harvest_requirements = 1
stone:set_item_drop(2, 1)
registry:register_block({
id = 3,
texture = 226,
name = "Grass",
item_drop = {
id = 1,
amount = 1
}
})
registry:register_block(7, 369, "Sand")
registry:register_block(8, 457, "Water")
registry:register_block(9, 168, "Glass")
registry:register_block({
id = 4,
texture = 266,
name = "Leaves",
hardness = 0.5,
})
local coalOre = registry:register_block(10, 36, "Coal Ore")
coalOre.hardness = 3
coalOre.harvest_requirements = 1
coalOre:set_item_drop(38, 1)
registry:register_block({
id = 5,
texture = 277,
name = "Wood",
hardness = 2
})
registry:register_block(11, 316, "Planks")
registry:register_block(12, 218, "Glowstone")
registry:register_block(13, 77, "Workbench")
registry:register_block(14, 164, "Furnace")
registry:register_block({
id = 6,
texture = 402,
name = "Stone",
hardness = 1.5,
harvest_requirements = 1,
item_drop = {
id = 2,
amount = 1
},
})
local ironOre = registry:register_block(15, 254, "Iron Ore")
ironOre.hardness = 3
ironOre.harvest_requirements = 1
registry:register_block({
id = 7,
texture = 369,
name = "Sand"
})
registry:register_block(16, 316, "Plank Slab")
registry:register_block({
id = 8,
texture = 457,
name = "Water",
})
registry:register_block({
id = 9,
texture = 168,
name = "Glass",
})
registry:register_block({
id = 10,
texture = 36,
name = "Coal Ore",
hardness = 3,
harvest_requirements = 1,
item_drop = {
id = 38,
amount = 1
},
})
registry:register_block({
id = 11,
texture = 316,
name = "Planks",
})
registry:register_block({
id = 12,
texture = 218,
name = "Glowstone",
})
registry:register_block({
id = 13,
texture = 77,
name = "Workbench",
})
registry:register_block({
id = 14,
texture = 164,
name = "Furnace",
})
registry:register_block({
id = 15,
texture = 254,
name = "Iron Ore",
hardness = 3,
harvest_requirements = 1,
})
registry:register_block({
id = 16,
texture = 316,
name = "Plank Slab"
})
for i = 17, 31 do
registry:register_block(i, 4, "Undefined")
registry:register_block({
id = i,
texture = 4,
name = "Undefined"
})
end
registry:register_item_block(0, "")

View File

@ -23,6 +23,70 @@
Registry *Registry::s_instance = nullptr;
void Registry::registerBlockFromTable(const sol::table &table) {
u32 id = table["id"].get<u32>();
u32 textureID = table["texture"].get<u32>();
std::string name = table["name"].get<std::string>();
Block *block = nullptr;
if (id == BlockType::Workbench) block = registerBlock<BlockWorkbench>();
else if (id == BlockType::Furnace) block = registerBlock<BlockFurnace>();
else if (id == BlockType::Water) block = registerBlock<BlockWater>();
else block = registerBlock<Block>(id, textureID, name);
block->setHarvestRequirements(table["harvest_requirements"].get_or(0));
block->setHardness(table["hardness"].get_or(1.0f));
sol::optional<sol::table> itemDrop = table["item_drop"];
if (itemDrop != sol::nullopt) {
u16 id = table["id"];
u16 amount = table["amoun"];
block->setItemDrop(id, amount);
}
}
void Registry::registerItemFromTable(const sol::table &table) {
}
void Registry::registerCraftingRecipeFromTable(const sol::table &table) {
sol::table resultTable = table["result"];
sol::table patternTable = table["pattern"];
sol::table keysTable = table["keys"];
ItemStack result = {
resultTable["item"].get<u16>(),
resultTable["amount"].get<u16>()
};
std::vector<std::string> pattern;
for (auto &it : patternTable)
pattern.emplace_back(it.second.as<std::string>());
std::map<char, std::vector<u32>> keys;
for (auto &it : keysTable) {
keys.emplace(it.first.as<char>(), std::vector<u32>{it.second.as<u32>()});
}
registerRecipe<CraftingRecipe>(pattern, keys, result);
}
void Registry::registerSmeltingRecipeFromTable(const sol::table &table) {
sol::table inputTable = table["input"];
sol::table outputTable = table["output"];
ItemStack input = {
inputTable["item"].get<u16>(),
inputTable["amount"].get<u16>()
};
ItemStack output = {
outputTable["item"].get<u16>(),
outputTable["amount"].get<u16>()
};
registerRecipe<SmeltingRecipe>(input, output);
}
const Recipe *Registry::getRecipe(const Inventory &inventory) const {
for (auto &recipe : m_recipes) {
if (recipe->isMatching(inventory))

View File

@ -72,57 +72,15 @@ void ScriptEngine::initUsertypes() {
);
m_lua.new_usertype<Registry>("Registry",
"register_block", [] (Registry *reg, u32 id, u32 textureID, const std::string &name)
-> Block*
{
if (id == BlockType::Workbench) return reg->registerBlock<BlockWorkbench>();
else if (id == BlockType::Furnace) return reg->registerBlock<BlockFurnace>();
else if (id == BlockType::Water) return reg->registerBlock<BlockWater>();
else return reg->registerBlock<Block>(id, textureID, name);
},
"register_block", &Registry::registerBlockFromTable,
"register_item_block", [] (Registry *reg, u32 id, const std::string &name) {
return reg->registerItem<ItemBlock>(id, id, name);
},
"register_item", [] (Registry *reg, u32 id, const std::string &name, u32 textureID) {
return reg->registerItem<Item>(id, textureID, name);
},
"register_crafting_recipe", [] (Registry *reg, const sol::table &recipeDefinition) {
sol::table resultTable = recipeDefinition["result"];
sol::table patternTable = recipeDefinition["pattern"];
sol::table keysTable = recipeDefinition["keys"];
ItemStack result = {
resultTable["item"].get<u16>(),
resultTable["amount"].get<u16>()
};
std::vector<std::string> pattern;
for (auto &it : patternTable)
pattern.emplace_back(it.second.as<std::string>());
std::map<char, std::vector<u32>> keys;
for (auto &it : keysTable) {
keys.emplace(it.first.as<char>(), std::vector<u32>{it.second.as<u32>()});
}
reg->registerRecipe<CraftingRecipe>(pattern, keys, result);
},
"register_smelting_recipe", [] (Registry *reg, const sol::table &recipeDefinition) {
sol::table inputTable = recipeDefinition["input"];
sol::table outputTable = recipeDefinition["output"];
ItemStack input = {
inputTable["item"].get<u16>(),
inputTable["amount"].get<u16>()
};
ItemStack output = {
outputTable["item"].get<u16>(),
outputTable["amount"].get<u16>()
};
reg->registerRecipe<SmeltingRecipe>(input, output);
}
"register_crafting_recipe", &Registry::registerCraftingRecipeFromTable,
"register_smelting_recipe", &Registry::registerSmeltingRecipeFromTable
);
}