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

This commit is contained in:
Quentin Bazin 2018-12-20 14:00:18 +01:00
parent ca953b6661
commit 5d1b024d64
4 changed files with 103 additions and 60 deletions

View File

@ -26,16 +26,16 @@
class Registry {
public:
void registerBlockFromTable(const sol::table &table);
void registerItemFromTable(const sol::table &table);
void registerCraftingRecipeFromTable(const sol::table &table);
void registerSmeltingRecipeFromTable(const sol::table &table);
// FIXME: Should be private
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();
}
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(); }

View File

@ -130,69 +130,103 @@ for i = 17, 31 do
})
end
registry:register_item_block(0, "")
registry:register_item_block(1, "Dirt")
registry:register_item_block(2, "Cobblestone")
registry:register_item_block(3, "Grass")
registry:register_item_block(4, "Leaves")
registry:register_item_block(5, "Wood")
registry:register_item_block(6, "Stone")
registry:register_item_block(7, "Sand")
registry:register_item_block(8, "Water")
registry:register_item_block(9, "Glass")
registry:register_item_block(10, "Coal Ore")
registry:register_item_block(11, "Planks")
registry:register_item_block(12, "Glowstone")
registry:register_item_block(13, "Workbench")
registry:register_item_block(14, "Furnace")
registry:register_item_block(15, "Iron Ore")
registry:register_item_block(16, "Plank Slab")
registry:register_item({
id = 32,
name = "Stick",
texture = 324,
})
for i = 17, 31 do
registry:register_item_block(i, "Undefined")
end
registry:register_item({
id = 33,
name = "Stone Axe",
texture = 325,
mining_speed = 4,
harvest_capability = 4,
})
registry:register_item(32, "Stick", 324)
registry:register_item({
id = 34,
name = "Stone Hoe",
texture = 326,
})
local stone_axe = registry:register_item(33, "Stone Axe", 325)
stone_axe.mining_speed = 4
stone_axe.harvest_capability = 4
registry:register_item({
id = 35,
name = "Stone Pickaxe",
texture = 327,
mining_speed = 4,
harvest_capability = 1,
})
registry:register_item(34, "Stone Hoe", 326)
registry:register_item({
id = 36,
name = "Stone Shovel",
texture = 328,
mining_speed = 4,
harvest_capability = 2,
})
local stone_pickaxe = registry:register_item(35, "Stone Pickaxe", 327)
stone_pickaxe.mining_speed = 4
stone_pickaxe.harvest_capability = 1
registry:register_item({
id = 37,
name = "Stone Sword",
texture = 329,
})
local stoneShovel = registry:register_item(36, "Stone Shovel", 328)
stoneShovel.mining_speed = 4
stoneShovel.harvest_capability = 2
registry:register_item({
id = 38,
name = "Coal",
texture = 111,
is_fuel = true,
burn_time = 1600,
})
registry:register_item(37, "Stone Sword", 329)
registry:register_item({
id = 39,
name = "Iron Ingot",
texture = 232,
})
local coal = registry:register_item(38, "Coal", 111)
coal.is_fuel = true
coal.burn_time = 1600
registry:register_item({
id = 40,
name = "Charcoal",
texture = 41,
is_fuel = true,
burn_time = 1600,
})
registry:register_item(39, "Iron Ingot", 232)
registry:register_item({
id = 41,
name = "Wooden Axe",
texture = 337,
mining_speed = 2,
harvest_capability = 4,
})
local charcoal = registry:register_item(40, "Charcoal", 41)
charcoal.is_fuel = true
charcoal.burn_time = 1600
registry:register_item({
id = 42,
name = "Wooden Hoe",
texture = 338,
})
local woodenAxe = registry:register_item(41, "Wooden Axe", 337)
woodenAxe.mining_speed = 2
woodenAxe.harvest_capability = 4
registry:register_item({
id = 43,
name = "Wooden Pickaxe",
texture = 339,
mining_speed = 2,
harvest_capability = 1,
})
registry:register_item(42, "Wooden Hoe", 338)
registry:register_item({
id = 44,
name = "Wooden Shovel",
texture = 340,
mining_speed = 2,
harvest_capability = 2,
})
local woodenPickaxe = registry:register_item(43, "Wooden Pickaxe", 339)
woodenPickaxe.mining_speed = 2
woodenPickaxe.harvest_capability = 1
local woodenShovel = registry:register_item(44, "Wooden Shovel", 340)
woodenShovel.mining_speed = 2
woodenShovel.harvest_capability = 2
registry:register_item(45, "Wooden Sword", 341)
registry:register_item({
id = 45,
name = "Wooden Sword",
texture = 341,
})

View File

@ -40,12 +40,23 @@ void Registry::registerBlockFromTable(const sol::table &table) {
sol::optional<sol::table> itemDrop = table["item_drop"];
if (itemDrop != sol::nullopt) {
u16 id = table["id"];
u16 amount = table["amoun"];
u16 amount = table["amount"];
block->setItemDrop(id, amount);
}
registerItem<ItemBlock>(id, id, name);
}
void Registry::registerItemFromTable(const sol::table &table) {
u32 id = table["id"].get<u32>();
u32 textureID = table["texture"].get<u32>();
std::string name = table["name"].get<std::string>();
Item *item = registerItem<Item>(id, textureID, name);
item->setIsFuel(table["is_fuel"].get_or(false));
item->setBurnTime(table["burn_time"].get_or(0));
item->setHarvestCapability(table["harvest_capability"].get_or(0));
item->setMiningSpeed(table["mining_speed"].get_or(1));
}
void Registry::registerCraftingRecipeFromTable(const sol::table &table) {

View File

@ -76,9 +76,7 @@ void ScriptEngine::initUsertypes() {
"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_item", &Registry::registerItemFromTable,
"register_crafting_recipe", &Registry::registerCraftingRecipeFromTable,
"register_smelting_recipe", &Registry::registerSmeltingRecipeFromTable
);