[CraftingRecipe] Added. [Registry] Basic axe recipe added. [WorkbenchWidget] Recipe support added.

This commit is contained in:
Quentin Bazin 2018-06-24 07:11:50 +02:00
parent f2dc9775ad
commit 1de5c04b14
7 changed files with 115 additions and 3 deletions

View File

@ -19,6 +19,7 @@
#include "Block.hpp"
#include "Item.hpp"
#include "CraftingRecipe.hpp"
class Registry {
public:
@ -32,11 +33,14 @@ class Registry {
m_items.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
}
void registerBlocks();
void registerItems();
void registerRecipes();
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(); }
void registerBlocks();
void registerItems();
const CraftingRecipe *getRecipe(const Inventory &inventory) const;
static Registry &getInstance() { return *s_instance; }
static void setInstance(Registry &instance) { s_instance = &instance; }
@ -46,6 +50,7 @@ class Registry {
std::vector<std::unique_ptr<Block>> m_blocks;
std::vector<std::unique_ptr<Item>> m_items;
std::vector<CraftingRecipe> m_recipes;
};
#endif // REGISTRY_HPP_

View File

@ -30,6 +30,9 @@ class WorkbenchWidget : public Widget {
Inventory m_craftingInventory{3, 3};
InventoryWidget m_craftingInventoryWidget{this};
Inventory m_craftingResultInventory{1, 1};
InventoryWidget m_craftingResultInventoryWidget{this};
Inventory &m_playerInventory;
InventoryWidget m_playerInventoryWidget{this};

View File

@ -0,0 +1,34 @@
/*
* =====================================================================================
*
* Filename: CraftingRecipe.hpp
*
* Description:
*
* Created: 24/06/2018 06:38:11
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef CRAFTINGRECIPE_HPP_
#define CRAFTINGRECIPE_HPP_
#include <array>
#include "Inventory.hpp"
class CraftingRecipe {
public:
CraftingRecipe(const std::array<u32, 9> &recipe, const ItemStack &result);
bool isMatching(const Inventory &inventory) const;
const ItemStack &result() const { return m_result; }
private:
std::array<u32, 9> m_recipe;
ItemStack m_result;
};
#endif // CRAFTINGRECIPE_HPP_

View File

@ -24,7 +24,7 @@ class Inventory {
Inventory(u16 width, u16 height)
: m_width(width), m_height(height) { m_items.resize(width * height); }
const ItemStack &getStack(u16 x, u16 y) { return m_items.at(x + y * m_width); }
const ItemStack &getStack(u16 x, u16 y) const { return m_items.at(x + y * m_width); }
void setStack(u16 x, u16 y, u16 id, u16 amount = 1);
void addStack(u16 id, u16 amount = 1);

View File

@ -77,5 +77,21 @@ void Registry::registerItems() {
registerItem<Item>(28);
registerItem<Item>(29);
registerItem<Item>(30);
// FIXME: Move this to Application or load from XML file
registerRecipes();
}
void Registry::registerRecipes() {
m_recipes.emplace_back(std::array<u32, 9>{6, 6, 0, 6, 25, 0, 0, 25, 0}, ItemStack{26});
// m_recipes.emplace_back(std::array<u32, 9>{0, 0, 0, 0, 0, 0, 0, 0, 0}, ItemStack{1});
}
const CraftingRecipe *Registry::getRecipe(const Inventory &inventory) const {
for (const CraftingRecipe &recipe : m_recipes) {
if (recipe.isMatching(inventory))
return &recipe;
}
return nullptr;
}

View File

@ -12,6 +12,7 @@
* =====================================================================================
*/
#include "Config.hpp"
#include "Registry.hpp"
#include "WorkbenchWidget.hpp"
WorkbenchWidget::WorkbenchWidget(Inventory &playerInventory, Inventory &hotbarInventory, Widget *parent)
@ -23,6 +24,9 @@ WorkbenchWidget::WorkbenchWidget(Inventory &playerInventory, Inventory &hotbarIn
m_craftingInventoryWidget.init(m_craftingInventory);
m_craftingInventoryWidget.setPosition(29, 16, 0);
m_craftingResultInventoryWidget.init(m_craftingResultInventory);
m_craftingResultInventoryWidget.setPosition(122, 34, 0);
m_playerInventoryWidget.init(m_playerInventory);
m_playerInventoryWidget.setPosition(7, 83, 0);
@ -34,12 +38,31 @@ WorkbenchWidget::WorkbenchWidget(Inventory &playerInventory, Inventory &hotbarIn
SCREEN_HEIGHT / 3.0 / 2.0 - m_background.clipRect().height / 2.0, 0);
}
#include "Debug.hpp"
void WorkbenchWidget::onEvent(const SDL_Event &event) {
m_craftingInventoryWidget.onEvent(event, m_mouseItemWidget);
m_craftingResultInventoryWidget.onEvent(event, m_mouseItemWidget);
m_playerInventoryWidget.onEvent(event, m_mouseItemWidget);
m_hotbarInventoryWidget.onEvent(event, m_mouseItemWidget);
m_mouseItemWidget.onEvent(event);
const CraftingRecipe *recipe = Registry::getInstance().getRecipe(m_craftingInventory);
if (recipe != nullptr && !m_craftingResultInventory.getStack(0, 0).item().id()) {
m_craftingResultInventory.setStack(0, 0, recipe->result().item().id(), recipe->result().amount());
m_craftingResultInventoryWidget.init(m_craftingResultInventory);
// FIXME: Make a subclass CraftingInventory to handle source items and crafting result
// Source elements should be destroyed if crafting result is taken
// Crafting result should be read-only
for (u8 i = 0 ; i < 9 ; ++i) {
const ItemStack &stack = m_craftingInventory.getStack(i % 3, i / 3);
if (stack.item().id()) {
m_craftingInventory.setStack(i % 3, i / 3, (stack.amount() > 1) ? stack.item().id() : 0, stack.amount() - 1);
}
}
m_craftingInventoryWidget.init(m_craftingInventory);
}
}
void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const {
@ -48,6 +71,7 @@ void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const {
target.draw(m_background, states);
target.draw(m_craftingInventoryWidget, states);
target.draw(m_craftingResultInventoryWidget, states);
target.draw(m_playerInventoryWidget, states);
target.draw(m_hotbarInventoryWidget, states);

View File

@ -0,0 +1,30 @@
/*
* =====================================================================================
*
* Filename: CraftingRecipe.cpp
*
* Description:
*
* Created: 24/06/2018 06:39:53
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "CraftingRecipe.hpp"
CraftingRecipe::CraftingRecipe(const std::array<u32, 9> &recipe, const ItemStack &result) {
m_recipe = recipe;
m_result = result;
}
bool CraftingRecipe::isMatching(const Inventory &inventory) const {
for (u16 x = 0 ; x < 3 ; ++x) {
for (u16 y = 0 ; y < 3 ; ++y) {
if (m_recipe.at(x + y * 3) != inventory.getStack(x, y).item().id())
return false;
}
}
return true;
}