[ScriptEngine] Now able to register block/item properties.
This commit is contained in:
parent
a26a567e95
commit
2dadf9dc0a
@ -24,22 +24,20 @@
|
||||
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();
|
||||
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 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();
|
||||
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();
|
||||
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 registerBlocks();
|
||||
void registerItems();
|
||||
void registerRecipes();
|
||||
|
||||
const Block &getBlock(std::size_t id) const { return *m_blocks.at(id).get(); }
|
||||
|
@ -19,6 +19,7 @@
|
||||
class ScriptEngine {
|
||||
public:
|
||||
void init();
|
||||
void initUsertypes();
|
||||
|
||||
sol::state &lua() { return m_lua; }
|
||||
|
||||
|
@ -2,20 +2,38 @@ print("Hello from Lua!")
|
||||
|
||||
Registry:registerBlock(0, 0, "Air")
|
||||
Registry:registerBlock(1, 37, "Dirt")
|
||||
Registry:registerBlock(2, 38, "Cobblestone")
|
||||
Registry:registerBlock(3, 226, "Grass")
|
||||
Registry:registerBlock(4, 266, "Leaves")
|
||||
Registry:registerBlock(5, 277, "Wood")
|
||||
Registry:registerBlock(6, 402, "Stone")
|
||||
|
||||
local cobblestone = Registry:registerBlock(2, 38, "Cobblestone")
|
||||
cobblestone.hardness = 2
|
||||
cobblestone.harvestRequirements = 1
|
||||
|
||||
Registry:registerBlock(3, 226, "Grass"):setItemDrop(1, 1)
|
||||
Registry:registerBlock(4, 266, "Leaves").hardness = 0.5
|
||||
Registry:registerBlock(5, 277, "Wood").hardness = 2
|
||||
|
||||
local stone = Registry:registerBlock(6, 402, "Stone")
|
||||
stone.hardness = 1.5
|
||||
stone.harvestRequirements = 1
|
||||
stone:setItemDrop(2, 1)
|
||||
|
||||
Registry:registerBlock(7, 369, "Sand")
|
||||
Registry:registerBlock(8, 457, "Water")
|
||||
Registry:registerBlock(9, 168, "Glass")
|
||||
Registry:registerBlock(10, 36, "Coal Ore")
|
||||
|
||||
local coalOre = Registry:registerBlock(10, 36, "Coal Ore")
|
||||
coalOre.hardness = 3
|
||||
coalOre.harvestRequirements = 1
|
||||
coalOre:setItemDrop(38, 1)
|
||||
|
||||
Registry:registerBlock(11, 316, "Planks")
|
||||
Registry:registerBlock(12, 218, "Glowstone")
|
||||
Registry:registerBlock(13, 77, "Workbench")
|
||||
Registry:registerBlock(14, 164, "Furnace")
|
||||
Registry:registerBlock(15, 254, "Iron Ore")
|
||||
|
||||
local ironOre = Registry:registerBlock(15, 254, "Iron Ore")
|
||||
ironOre.hardness = 3
|
||||
ironOre.harvestRequirements = 1
|
||||
|
||||
Registry:registerBlock(16, 316, "Plank Slab")
|
||||
|
||||
for i = 17, 31 do
|
||||
@ -45,18 +63,40 @@ for i = 17, 31 do
|
||||
end
|
||||
|
||||
Registry:registerItem(32, "Stick", 324)
|
||||
Registry:registerItem(33, "Stone Axe", 325)
|
||||
|
||||
local stoneAxe = Registry:registerItem(33, "Stone Axe", 325)
|
||||
stoneAxe.miningSpeed = 4
|
||||
stoneAxe.harvestCapability = 4
|
||||
|
||||
Registry:registerItem(34, "Stone Hoe", 326)
|
||||
Registry:registerItem(35, "Stone Pickaxe", 327)
|
||||
Registry:registerItem(36, "Stone Shovel", 328)
|
||||
|
||||
local stonePickaxe = Registry:registerItem(35, "Stone Pickaxe", 327)
|
||||
stonePickaxe.miningSpeed = 4
|
||||
stonePickaxe.harvestCapability = 1
|
||||
|
||||
local stoneShovel = Registry:registerItem(36, "Stone Shovel", 328)
|
||||
stoneShovel.miningSpeed = 4
|
||||
stoneShovel.harvestCapability = 2
|
||||
|
||||
Registry:registerItem(37, "Stone Sword", 329)
|
||||
Registry:registerItem(38, "Coal", 111)
|
||||
Registry:registerItem(38, "Coal", 111).burnTime = 1600
|
||||
Registry:registerItem(39, "Iron Ingot", 232)
|
||||
Registry:registerItem(40, "Charcoal", 41)
|
||||
Registry:registerItem(41, "Wooden Axe", 337)
|
||||
Registry:registerItem(40, "Charcoal", 41).burnTime = 1600
|
||||
|
||||
local woodenAxe = Registry:registerItem(41, "Wooden Axe", 337)
|
||||
woodenAxe.miningSpeed = 2
|
||||
woodenAxe.harvestCapability = 4
|
||||
|
||||
Registry:registerItem(42, "Wooden Hoe", 338)
|
||||
Registry:registerItem(43, "Wooden Pickaxe", 339)
|
||||
Registry:registerItem(44, "Wooden Shovel", 340)
|
||||
|
||||
local woodenPickaxe = Registry:registerItem(43, "Wooden Pickaxe", 339)
|
||||
woodenPickaxe.miningSpeed = 2
|
||||
woodenPickaxe.harvestCapability = 1
|
||||
|
||||
local woodenShovel = Registry:registerItem(44, "Wooden Shovel", 340)
|
||||
woodenShovel.miningSpeed = 2
|
||||
woodenShovel.harvestCapability = 2
|
||||
|
||||
Registry:registerItem(45, "Wooden Sword", 341)
|
||||
|
||||
function init()
|
||||
|
@ -1,50 +0,0 @@
|
||||
<blocks>
|
||||
<block id="0" name="Air" />
|
||||
<block id="1" name="Dirt" textureID="37" />
|
||||
<block id="2" name="Cobblestone" textureID="38" hardness="2" harvestRequirements="1" />
|
||||
|
||||
<block id="3" name="Grass" textureID="226">
|
||||
<item_drop id="1" amount="1" />
|
||||
</block>
|
||||
|
||||
<block id="4" name="Leaves" textureID="266" hardness="0.5" />
|
||||
<block id="5" name="Wood" textureID="277" hardness="2" />
|
||||
|
||||
<block id="6" name="Stone" textureID="402" hardness="1.5" harvestRequirements="1">
|
||||
<item_drop id="2" amount="1" />
|
||||
</block>
|
||||
|
||||
<block id="7" name="Sand" textureID="369" />
|
||||
<block id="8" name="Water" />
|
||||
<block id="9" name="Glass" textureID="168" />
|
||||
|
||||
<block id="10" name="Coal Ore" textureID="36" hardness="3" harvestRequirements="1">
|
||||
<item_drop id="38" amount="1" />
|
||||
</block>
|
||||
|
||||
<block id="11" name="Planks" textureID="316" />
|
||||
<block id="12" name="Glowstone" textureID="218" />
|
||||
<block id="13" name="Workbench" />
|
||||
<block id="14" name="Furnace" />
|
||||
<block id="15" name="Iron Ore" textureID="254" hardness="3" harvestRequirements="1" />
|
||||
|
||||
<block id="16" name="Plank Slab" textureID="316">
|
||||
<box x="0" y="0" z="0" width="1" height="0.5" depth="1"/>
|
||||
</block>
|
||||
|
||||
<block id="17" name="Undefined" textureID="4" />
|
||||
<block id="18" name="Undefined" textureID="4" />
|
||||
<block id="19" name="Undefined" textureID="4" />
|
||||
<block id="20" name="Undefined" textureID="4" />
|
||||
<block id="21" name="Undefined" textureID="4" />
|
||||
<block id="22" name="Undefined" textureID="4" />
|
||||
<block id="23" name="Undefined" textureID="4" />
|
||||
<block id="24" name="Undefined" textureID="4" />
|
||||
<block id="25" name="Undefined" textureID="4" />
|
||||
<block id="26" name="Undefined" textureID="4" />
|
||||
<block id="27" name="Undefined" textureID="4" />
|
||||
<block id="28" name="Undefined" textureID="4" />
|
||||
<block id="29" name="Undefined" textureID="4" />
|
||||
<block id="30" name="Undefined" textureID="4" />
|
||||
<block id="31" name="Undefined" textureID="4" />
|
||||
</blocks>
|
@ -1,49 +0,0 @@
|
||||
<items>
|
||||
<item id="0" name="" />
|
||||
<item id="1" name="Dirt" />
|
||||
<item id="2" name="Cobblestone" />
|
||||
<item id="3" name="Grass" />
|
||||
<item id="4" name="Leaves" />
|
||||
<item id="5" name="Wood" />
|
||||
<item id="6" name="Stone" />
|
||||
<item id="7" name="Sand" />
|
||||
<item id="8" name="Water" />
|
||||
<item id="9" name="Glass" />
|
||||
<item id="10" name="Coal Ore" />
|
||||
<item id="11" name="Planks" />
|
||||
<item id="12" name="Glowstone" />
|
||||
<item id="13" name="Workbench" />
|
||||
<item id="14" name="Furnace" />
|
||||
<item id="15" name="Iron Ore" />
|
||||
<item id="16" name="Undefined" />
|
||||
<item id="17" name="Undefined" />
|
||||
<item id="18" name="Undefined" />
|
||||
<item id="19" name="Undefined" />
|
||||
<item id="20" name="Undefined" />
|
||||
<item id="21" name="Undefined" />
|
||||
<item id="22" name="Undefined" />
|
||||
<item id="23" name="Undefined" />
|
||||
<item id="24" name="Undefined" />
|
||||
<item id="25" name="Undefined" />
|
||||
<item id="26" name="Undefined" />
|
||||
<item id="27" name="Undefined" />
|
||||
<item id="28" name="Undefined" />
|
||||
<item id="29" name="Undefined" />
|
||||
<item id="30" name="Undefined" />
|
||||
<item id="31" name="Undefined" />
|
||||
|
||||
<item id="32" name="Stick" textureID="324" />
|
||||
<item id="33" name="Stone Axe" textureID="325" miningSpeed="4" harvestCapability="4"/>
|
||||
<item id="34" name="Stone Hoe" textureID="326" />
|
||||
<item id="35" name="Stone Pickaxe" textureID="327" miningSpeed="4" harvestCapability="1" />
|
||||
<item id="36" name="Stone Shovel" textureID="328" miningSpeed="4" harvestCapability="2" />
|
||||
<item id="37" name="Stone Sword" textureID="329" />
|
||||
<item id="38" name="Coal" textureID="111" burnTime="1600" />
|
||||
<item id="39" name="Iron Ingot" textureID="232" />
|
||||
<item id="40" name="Charcoal" textureID="41" burnTime="1600" />
|
||||
<item id="41" name="Wooden Axe" textureID="337" miningSpeed="2" harvestCapability="4" />
|
||||
<item id="42" name="Wooden Hoe" textureID="338" />
|
||||
<item id="43" name="Wooden Pickaxe" textureID="339" miningSpeed="2" harvestCapability="1" />
|
||||
<item id="44" name="Wooden Shovel" textureID="340" miningSpeed="2" harvestCapability="2" />
|
||||
<item id="45" name="Wooden Sword" textureID="341" />
|
||||
</items>
|
@ -23,84 +23,6 @@
|
||||
|
||||
Registry *Registry::s_instance = nullptr;
|
||||
|
||||
void Registry::registerBlocks() {
|
||||
XMLFile doc("resources/config/blocks.xml");
|
||||
|
||||
tinyxml2::XMLElement *blockElement = doc.FirstChildElement("blocks").FirstChildElement("block").ToElement();
|
||||
while (blockElement) {
|
||||
u16 id = blockElement->UnsignedAttribute("id");
|
||||
const char *name = blockElement->Attribute("name");
|
||||
|
||||
if (id == BlockType::Workbench) registerBlock<BlockWorkbench>();
|
||||
else if (id == BlockType::Furnace) registerBlock<BlockFurnace>();
|
||||
else if (id == BlockType::Water) registerBlock<BlockWater>();
|
||||
else {
|
||||
u16 textureID = blockElement->UnsignedAttribute("textureID");
|
||||
|
||||
auto &block = registerBlock<Block>(id, textureID, name);
|
||||
|
||||
float hardness = 0;
|
||||
if (blockElement->QueryFloatAttribute("hardness", &hardness) == tinyxml2::XMLError::XML_SUCCESS)
|
||||
block.setHardness(hardness);
|
||||
|
||||
unsigned int harvestRequirements = 0;
|
||||
if (blockElement->QueryUnsignedAttribute("harvestRequirements", &harvestRequirements) == tinyxml2::XMLError::XML_SUCCESS)
|
||||
block.setHarvestRequirements(harvestRequirements);
|
||||
|
||||
tinyxml2::XMLElement *itemDropElement = blockElement->FirstChildElement("item_drop");
|
||||
if (itemDropElement) {
|
||||
block.setItemDrop(itemDropElement->UnsignedAttribute("id"), itemDropElement->UnsignedAttribute("amount"));
|
||||
}
|
||||
|
||||
tinyxml2::XMLElement *boundingBoxElement = blockElement->FirstChildElement("box");
|
||||
if (boundingBoxElement) {
|
||||
block.setBoundingBox(FloatBox{boundingBoxElement->FloatAttribute("x"),
|
||||
boundingBoxElement->FloatAttribute("y"),
|
||||
boundingBoxElement->FloatAttribute("z"),
|
||||
boundingBoxElement->FloatAttribute("width"),
|
||||
boundingBoxElement->FloatAttribute("height"),
|
||||
boundingBoxElement->FloatAttribute("depth")});
|
||||
}
|
||||
}
|
||||
|
||||
blockElement = blockElement->NextSiblingElement("block");
|
||||
}
|
||||
}
|
||||
|
||||
void Registry::registerItems() {
|
||||
XMLFile doc("resources/config/items.xml");
|
||||
|
||||
tinyxml2::XMLElement *itemElement = doc.FirstChildElement("items").FirstChildElement("item").ToElement();
|
||||
while (itemElement) {
|
||||
u16 id = itemElement->UnsignedAttribute("id");
|
||||
const char *name = itemElement->Attribute("name");
|
||||
|
||||
unsigned int textureID;
|
||||
if (itemElement->QueryUnsignedAttribute("textureID", &textureID) == tinyxml2::XMLError::XML_SUCCESS) {
|
||||
auto &item = registerItem<Item>(id, textureID, name);
|
||||
|
||||
float miningSpeed = 0;
|
||||
if (itemElement->QueryFloatAttribute("miningSpeed", &miningSpeed) == tinyxml2::XMLError::XML_SUCCESS)
|
||||
item.setMiningSpeed(miningSpeed);
|
||||
|
||||
unsigned int harvestCapability = 0;
|
||||
if (itemElement->QueryUnsignedAttribute("harvestCapability", &harvestCapability) == tinyxml2::XMLError::XML_SUCCESS)
|
||||
item.setHarvestCapability(harvestCapability);
|
||||
|
||||
unsigned int burnTime = 0;
|
||||
if (itemElement->QueryUnsignedAttribute("burnTime", &burnTime) == tinyxml2::XMLError::XML_SUCCESS) {
|
||||
item.setIsFuel(true);
|
||||
item.setBurnTime(burnTime);
|
||||
}
|
||||
}
|
||||
else {
|
||||
registerItem<ItemBlock>(id, id, name);
|
||||
}
|
||||
|
||||
itemElement = itemElement->NextSiblingElement("item");
|
||||
}
|
||||
}
|
||||
|
||||
void Registry::registerRecipes() {
|
||||
XMLFile doc("resources/config/recipes.xml");
|
||||
|
||||
|
@ -16,13 +16,24 @@
|
||||
#include "Player.hpp"
|
||||
#include "Registry.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include "BlockFurnace.hpp"
|
||||
#include "BlockWater.hpp"
|
||||
#include "BlockWorkbench.hpp"
|
||||
|
||||
ScriptEngine *ScriptEngine::s_instance = nullptr;
|
||||
|
||||
void ScriptEngine::init() {
|
||||
setInstance(this);
|
||||
|
||||
initUsertypes();
|
||||
|
||||
m_lua["Registry"] = &Registry::getInstance();
|
||||
|
||||
m_lua.open_libraries();
|
||||
m_lua.safe_script_file("mods/test.lua");
|
||||
}
|
||||
|
||||
void ScriptEngine::initUsertypes() {
|
||||
m_lua.new_usertype<Player>("Player",
|
||||
"inventory", &Player::inventory);
|
||||
|
||||
@ -40,25 +51,35 @@ void ScriptEngine::init() {
|
||||
);
|
||||
|
||||
m_lua.new_usertype<Item>("Item",
|
||||
"name", &Item::name,
|
||||
"id", &Item::id
|
||||
"name", &Item::name,
|
||||
"id", &Item::id,
|
||||
"burnTime", sol::property(&Item::burnTime, &Item::setBurnTime),
|
||||
"harvestCapability", sol::property(&Item::harvestCapability, &Item::setHarvestCapability),
|
||||
"miningSpeed", sol::property(&Item::miningSpeed, &Item::setMiningSpeed)
|
||||
);
|
||||
|
||||
m_lua.new_usertype<Block>("Block",
|
||||
"id", &Block::id,
|
||||
"data", &Block::data,
|
||||
"name", sol::property(&Block::name, &Block::setName),
|
||||
"harvestRequirements", sol::property(&Block::harvestRequirements, &Block::setHarvestRequirements),
|
||||
"hardness", sol::property(&Block::hardness, &Block::setHardness),
|
||||
"setItemDrop", &Block::setItemDrop
|
||||
);
|
||||
|
||||
m_lua.new_usertype<Registry>("Registry",
|
||||
"registerBlock", [] (Registry *reg, u32 id, u32 textureID, const std::string &name) {
|
||||
reg->registerBlock<Block>(id, textureID, name);
|
||||
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);
|
||||
},
|
||||
"registerItemBlock", [] (Registry *reg, u32 id, const std::string &name) {
|
||||
reg->registerItem<ItemBlock>(id, id, name);
|
||||
return reg->registerItem<ItemBlock>(id, id, name);
|
||||
},
|
||||
"registerItem", [] (Registry *reg, u32 id, const std::string &name, u32 textureID) {
|
||||
reg->registerItem<Item>(id, textureID, name);
|
||||
return reg->registerItem<Item>(id, textureID, name);
|
||||
}
|
||||
);
|
||||
|
||||
m_lua["Registry"] = &Registry::getInstance();
|
||||
|
||||
m_lua.open_libraries();
|
||||
m_lua.script_file("mods/test.lua");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user