2018-06-23 23:05:36 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: Registry.hpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 23/06/2018 22:32:51
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#ifndef REGISTRY_HPP_
|
|
|
|
#define REGISTRY_HPP_
|
|
|
|
|
2018-06-24 01:09:32 +02:00
|
|
|
#include <memory>
|
2018-06-23 23:05:36 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2018-12-20 13:31:32 +01:00
|
|
|
#include <sol.hpp>
|
|
|
|
|
2018-06-23 23:05:36 +02:00
|
|
|
#include "Block.hpp"
|
|
|
|
#include "Item.hpp"
|
2018-06-30 03:47:39 +02:00
|
|
|
#include "Recipe.hpp"
|
2018-06-23 23:05:36 +02:00
|
|
|
|
|
|
|
class Registry {
|
|
|
|
public:
|
2018-12-20 13:31:32 +01:00
|
|
|
void registerBlockFromTable(const sol::table &table);
|
2018-12-20 14:00:18 +01:00
|
|
|
void registerItemFromTable(const sol::table &table);
|
|
|
|
void registerCraftingRecipeFromTable(const sol::table &table);
|
|
|
|
void registerSmeltingRecipeFromTable(const sol::table &table);
|
2018-06-23 23:05:36 +02:00
|
|
|
|
2018-12-28 21:23:26 +01:00
|
|
|
const Block &getBlock(std::size_t id) const { return m_blocks.at(id); }
|
|
|
|
const Item &getItem(std::size_t id) const { return m_items.at(id); }
|
|
|
|
|
2018-12-28 21:30:17 +01:00
|
|
|
const Block &getBlock(const std::string &name);
|
|
|
|
const Item &getItem(const std::string &name);
|
2018-06-23 23:05:36 +02:00
|
|
|
|
2018-06-30 03:47:39 +02:00
|
|
|
const Recipe *getRecipe(const Inventory &inventory) const;
|
2018-06-23 23:05:36 +02:00
|
|
|
|
|
|
|
static Registry &getInstance() { return *s_instance; }
|
|
|
|
static void setInstance(Registry &instance) { s_instance = &instance; }
|
|
|
|
|
|
|
|
private:
|
2018-12-28 21:30:17 +01:00
|
|
|
Block ®isterBlock(u32 textureID, const std::string &name, const std::string &label);
|
|
|
|
Item ®isterItem(u32 textureID, const std::string &name, const std::string &label);
|
2018-12-25 23:14:39 +01:00
|
|
|
|
2018-12-20 13:31:32 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-06-23 23:05:36 +02:00
|
|
|
static Registry *s_instance;
|
|
|
|
|
2018-12-28 21:23:26 +01:00
|
|
|
std::vector<Block> m_blocks;
|
|
|
|
std::vector<Item> m_items;
|
2018-06-30 03:47:39 +02:00
|
|
|
std::vector<std::unique_ptr<Recipe>> m_recipes;
|
2018-12-28 21:23:26 +01:00
|
|
|
|
|
|
|
std::unordered_map<std::string, u32> m_blocksID;
|
|
|
|
std::unordered_map<std::string, u32> m_itemsID;
|
2018-06-23 23:05:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // REGISTRY_HPP_
|