[Registry] Blocks, item and recipe data loaded from XML files.

This commit is contained in:
Quentin Bazin 2018-06-30 02:23:50 +02:00
parent 31ea124dce
commit dbb6fb5736
5 changed files with 216 additions and 164 deletions

View File

@ -0,0 +1,26 @@
<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" />
<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">
<drop_item 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">
<drop_item id="" 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" />
</blocks>

View File

@ -0,0 +1,26 @@
<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="Stick" textureID="324" />
<item id="17" name="Stone Axe" textureID="325" miningSpeed="4" harvestCapability="4"/>
<item id="18" name="Stone Hoe" textureID="326" />
<item id="19" name="Stone Pickaxe" textureID="327" miningSpeed="4" harvestCapability="1" />
<item id="20" name="Stone Shovel" textureID="328" miningSpeed="4" harvestCapability="2" />
<item id="21" name="Stone Sword" textureID="329" />
<item id="22" name="Coal" textureID="111" />
<item id="23" name="Iron Ingot" textureID="232" />
</items>

View File

@ -0,0 +1,92 @@
<recipes>
<recipe>
<pattern string="##" />
<pattern string="#|" />
<pattern string=" |" />
<key char="#" item="2" />
<key char="|" item="16" />
<result item="17" amount="1" />
</recipe>
<recipe>
<pattern string="##" />
<pattern string=" |" />
<pattern string=" |" />
<key char="#" item="2" />
<key char="|" item="16" />
<result item="18" amount="1" />
</recipe>
<recipe>
<pattern string="###" />
<pattern string=" | " />
<pattern string=" | " />
<key char="#" item="2" />
<key char="|" item="16" />
<result item="19" amount="1" />
</recipe>
<recipe>
<pattern string="#" />
<pattern string="|" />
<pattern string="|" />
<key char="#" item="2" />
<key char="|" item="16" />
<result item="20" amount="1" />
</recipe>
<recipe>
<pattern string="#" />
<pattern string="#" />
<pattern string="|" />
<key char="#" item="2" />
<key char="|" item="16" />
<result item="21" amount="1" />
</recipe>
<recipe>
<pattern string="#" />
<pattern string="#" />
<key char="#" item="11" />
<result item="16" amount="4" />
</recipe>
<recipe>
<pattern string="#" />
<key char="#" item="5" />
<result item="11" amount="4" />
</recipe>
<recipe>
<pattern string="##" />
<pattern string="##" />
<key char="#" item="11" />
<result item="13" amount="1" />
</recipe>
<recipe>
<pattern string="###" />
<pattern string="# #" />
<pattern string="###" />
<key char="#" item="2" />
<result item="14" amount="1" />
</recipe>
</recipes>

View File

@ -35,6 +35,7 @@ void Application::init() {
Registry::setInstance(m_registry);
m_registry.registerBlocks();
m_registry.registerItems();
m_registry.registerRecipes();
m_stateStack.push<GameState>();
}

View File

@ -17,199 +17,106 @@
#include "BlockFurnace.hpp"
#include "BlockWater.hpp"
#include "BlockWorkbench.hpp"
#include "XMLFile.hpp"
Registry *Registry::s_instance = nullptr;
void Registry::registerBlocks() {
registerBlock<Block>(BlockType::Air, 0);
registerBlock<Block>(BlockType::Dirt, 37);
XMLFile doc("resources/config/blocks.xml");
auto &cobblestoneBlock = registerBlock<Block>(BlockType::Cobblestone, 38);
cobblestoneBlock.setHarvestRequirements(1);
cobblestoneBlock.setHardness(2);
tinyxml2::XMLElement *blockElement = doc.FirstChildElement("blocks").FirstChildElement("block").ToElement();
while (blockElement) {
u16 id = blockElement->UnsignedAttribute("id");
// const char *name = blockElement->Attribute("name");
registerBlock<Block>(BlockType::Grass, 226);
registerBlock<Block>(BlockType::Leaves, 266).setHardness(0.5);
registerBlock<Block>(BlockType::Wood, 277).setHardness(2);
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 &stoneBlock = registerBlock<Block>(BlockType::Stone, 402);
stoneBlock.setItemDrop(ItemType::Cobblestone);
stoneBlock.setHardness(1.5);
stoneBlock.setHarvestRequirements(1);
auto &block = registerBlock<Block>(id, textureID);
registerBlock<Block>(BlockType::Sand, 369);
registerBlock<BlockWater>();
registerBlock<Block>(BlockType::Glass, 168);
float hardness = 0;
if (blockElement->QueryFloatAttribute("hardness", &hardness) == tinyxml2::XMLError::XML_SUCCESS)
block.setHardness(hardness);
auto &coalBlock = registerBlock<Block>(BlockType::CoalOre, 36);
coalBlock.setItemDrop(ItemType::Coal);
coalBlock.setHardness(3);
coalBlock.setHarvestRequirements(1);
unsigned int harvestRequirements = 0;
if (blockElement->QueryUnsignedAttribute("harvestRequirements", &harvestRequirements) == tinyxml2::XMLError::XML_SUCCESS)
block.setHarvestRequirements(harvestRequirements);
}
registerBlock<Block>(BlockType::Planks, 316);
registerBlock<Block>(BlockType::Glowstone, 218);
registerBlock<BlockWorkbench>();
registerBlock<BlockFurnace>();
registerBlock<Block>(BlockType::IronOre, 254);
blockElement = blockElement->NextSiblingElement("block");
}
}
void Registry::registerItems() {
registerItem<ItemBlock>(ItemType::Air, BlockType::Air, "");
registerItem<ItemBlock>(ItemType::Dirt, BlockType::Dirt, "Dirt");
registerItem<ItemBlock>(ItemType::Cobblestone, BlockType::Cobblestone, "Cobblestone");
registerItem<ItemBlock>(ItemType::Grass, BlockType::Grass, "Grass");
registerItem<ItemBlock>(ItemType::Leaves, BlockType::Leaves, "Leaves");
registerItem<ItemBlock>(ItemType::Wood, BlockType::Wood, "Wood");
registerItem<ItemBlock>(ItemType::Stone, BlockType::Stone, "Stone");
registerItem<ItemBlock>(ItemType::Sand, BlockType::Sand, "Sand");
registerItem<ItemBlock>(ItemType::Water, BlockType::Water, "Water");
registerItem<ItemBlock>(ItemType::Glass, BlockType::Glass, "Glass");
registerItem<ItemBlock>(ItemType::CoalOre, BlockType::CoalOre, "Coal Ore");
registerItem<ItemBlock>(ItemType::Planks, BlockType::Planks, "Planks");
registerItem<ItemBlock>(ItemType::Glowstone, BlockType::Glowstone, "Glowstone");
registerItem<ItemBlock>(ItemType::Workbench, BlockType::Workbench, "Workbench");
registerItem<ItemBlock>(ItemType::Furnace, BlockType::Furnace, "Furnace");
registerItem<ItemBlock>(ItemType::IronOre, BlockType::IronOre, "Iron Ore");
XMLFile doc("resources/config/items.xml");
registerItem<Item>(ItemType::Stick, 324, "Stick");
tinyxml2::XMLElement *itemElement = doc.FirstChildElement("items").FirstChildElement("item").ToElement();
while (itemElement) {
u16 id = itemElement->UnsignedAttribute("id");
const char *name = itemElement->Attribute("name");
auto &stoneAxe = registerItem<Item>(ItemType::StoneAxe, 325, "Stone Axe");
stoneAxe.setHarvestCapability(4);
stoneAxe.setMiningSpeed(4);
unsigned int textureID;
if (itemElement->QueryUnsignedAttribute("textureID", &textureID) == tinyxml2::XMLError::XML_SUCCESS) {
auto &item = registerItem<Item>(id, textureID, name);
registerItem<Item>(ItemType::StoneHoe, 326, "Stone Hoe");
float miningSpeed = 0;
if (itemElement->QueryFloatAttribute("miningSpeed", &miningSpeed) == tinyxml2::XMLError::XML_SUCCESS)
item.setMiningSpeed(miningSpeed);
auto &stonePickaxe = registerItem<Item>(ItemType::StonePickaxe, 327, "Stone Pickaxe");
stonePickaxe.setHarvestCapability(1);
stonePickaxe.setMiningSpeed(4);
unsigned int harvestCapability = 0;
if (itemElement->QueryUnsignedAttribute("harvestCapability", &harvestCapability) == tinyxml2::XMLError::XML_SUCCESS)
item.setHarvestCapability(harvestCapability);
}
else {
registerItem<ItemBlock>(id, id, name);
}
registerItem<Item>(ItemType::StoneShovel, 328, "Stone Shovel").setHarvestCapability(2);
registerItem<Item>(ItemType::StoneSword, 329, "Stone Sword");
Item &itemCoal = registerItem<Item>(ItemType::Coal, 111, "Coal");
itemCoal.setIsFuel(true);
itemCoal.setBurnTime(1600);
registerItem<Item>(ItemType::IronIngot, 232, "Iron Ingot");
// FIXME: Move this to Application or load from XML file
registerRecipes();
itemElement = itemElement->NextSiblingElement("item");
}
}
void Registry::registerRecipes() {
// m_recipes.emplace_back(std::array<u32, 9>{2, 2, 0, 2, ItemType::Stick, 0, 0, ItemType::Stick, 0}, ItemStack{ItemType::StoneAxe});
// m_recipes.emplace_back(std::array<u32, 9>{2, 2, 0, 0, ItemType::Stick, 0, 0, ItemType::Stick, 0}, ItemStack{ItemType::StoneHoe});
// m_recipes.emplace_back(std::array<u32, 9>{2, 2, 2, 0, ItemType::Stick, 0, 0, ItemType::Stick, 0}, ItemStack{ItemType::StonePickaxe});
// m_recipes.emplace_back(std::array<u32, 9>{0, 2, 0, 0, ItemType::Stick, 0, 0, ItemType::Stick, 0}, ItemStack{ItemType::StoneShovel});
// m_recipes.emplace_back(std::array<u32, 9>{0, 2, 0, 0, 2, 0, 0, ItemType::Stick, 0}, ItemStack{ItemType::StoneSword});
//
// m_recipes.emplace_back(std::array<u32, 9>{ItemType::Wood, 0, 0, 0, 0, 0, 0, 0, 0}, ItemStack{ItemType::Planks, 4}, true);
// m_recipes.emplace_back(std::array<u32, 9>{ItemType::Planks, ItemType::Planks, 0, 0, 0, 0, 0, 0, 0}, ItemStack{ItemType::Stick, 4}, true);
//
// m_recipes.emplace_back(std::array<u32, 9>{
// ItemType::Cobblestone, ItemType::Cobblestone, ItemType::Cobblestone,
// ItemType::Cobblestone, 0, ItemType::Cobblestone,
// ItemType::Cobblestone, ItemType::Cobblestone, ItemType::Cobblestone,
// }, ItemStack{ItemType::Furnace});
//
// // FIXME: This recipe will only for in the top-left corner
// // Find a way to handle recipe size
// m_recipes.emplace_back(std::array<u32, 9>{
// ItemType::Planks, ItemType::Planks, 0,
// ItemType::Planks, ItemType::Planks, 0,
// 0, 0, 0
// }, ItemStack{ItemType::Workbench});
XMLFile doc("resources/config/recipes.xml");
m_recipes.emplace_back(std::vector<std::string>{
"##",
"#|",
" |"
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Cobblestone}},
{'|', {ItemType::Stick}},
},
ItemStack{ItemType::StoneAxe});
tinyxml2::XMLElement *recipeElement = doc.FirstChildElement("recipes").FirstChildElement("recipe").ToElement();
while (recipeElement) {
std::vector<std::string> pattern;
std::map<char, std::vector<u32>> keys;
ItemStack result;
bool isShapeless = false;
m_recipes.emplace_back(std::vector<std::string>{
"##",
" |",
" |"
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Cobblestone}},
{'|', {ItemType::Stick}},
},
ItemStack{ItemType::StoneHoe});
tinyxml2::XMLElement *patternElement = recipeElement->FirstChildElement("pattern");
while (patternElement) {
pattern.emplace_back(patternElement->Attribute("string"));
patternElement = patternElement->NextSiblingElement("pattern");
}
m_recipes.emplace_back(std::vector<std::string>{
"###",
" | ",
" | "
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Cobblestone}},
{'|', {ItemType::Stick}},
},
ItemStack{ItemType::StonePickaxe});
tinyxml2::XMLElement *keyElement = recipeElement->FirstChildElement("key");
while (keyElement) {
char ch = keyElement->Attribute("char")[0];
u32 item = keyElement->UnsignedAttribute("item");
m_recipes.emplace_back(std::vector<std::string>{
"#",
"|",
"|"
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Cobblestone}},
{'|', {ItemType::Stick}},
},
ItemStack{ItemType::StoneShovel});
std::vector<u32> items;
items.emplace_back(item);
keys.emplace(ch, items);
m_recipes.emplace_back(std::vector<std::string>{
"#",
"#",
"|"
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Cobblestone}},
{'|', {ItemType::Stick}},
},
ItemStack{ItemType::StoneSword});
keyElement = keyElement->NextSiblingElement("key");
}
m_recipes.emplace_back(std::vector<std::string>{
"#",
"#",
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Planks}},
},
ItemStack{ItemType::Stick, 4});
tinyxml2::XMLElement *resultElement = recipeElement->FirstChildElement("result");
if (resultElement) {
u16 item = resultElement->UnsignedAttribute("item");
u16 amount = resultElement->UnsignedAttribute("amount");
result = ItemStack{item, amount};
}
m_recipes.emplace_back(std::vector<std::string>{
"#",
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Wood}},
},
ItemStack{ItemType::Planks, 4});
m_recipes.emplace_back(pattern, keys, result, isShapeless);
m_recipes.emplace_back(std::vector<std::string>{
"##",
"##",
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Planks}},
},
ItemStack{ItemType::Workbench});
m_recipes.emplace_back(std::vector<std::string>{
"###",
"# #",
"###",
},
std::map<char, std::vector<u32>>{
{'#', {ItemType::Cobblestone}},
},
ItemStack{ItemType::Furnace});
recipeElement = recipeElement->NextSiblingElement("recipe");
}
}
const CraftingRecipe *Registry::getRecipe(const Inventory &inventory) const {