[CraftingWidget] Added for more control on craft result inventory.

This commit is contained in:
Quentin Bazin 2018-06-25 02:39:23 +02:00
parent 0f779421a4
commit 68e3d1493d
4 changed files with 94 additions and 35 deletions

View File

@ -0,0 +1,35 @@
/*
* =====================================================================================
*
* Filename: CraftingWidget.hpp
*
* Description:
*
* Created: 25/06/2018 02:26:18
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef CRAFTINGWIDGET_HPP_
#define CRAFTINGWIDGET_HPP_
#include "InventoryWidget.hpp"
class CraftingWidget : public Widget {
public:
CraftingWidget(Widget *parent = nullptr);
void onEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget);
private:
void draw(RenderTarget &target, RenderStates states) const override;
Inventory m_craftingInventory{3, 3};
InventoryWidget m_craftingInventoryWidget{this};
Inventory m_craftingResultInventory{1, 1};
InventoryWidget m_craftingResultInventoryWidget{this};
};
#endif // CRAFTINGWIDGET_HPP_

View File

@ -14,7 +14,7 @@
#ifndef WORKBENCHWIDGET_HPP_
#define WORKBENCHWIDGET_HPP_
#include "InventoryWidget.hpp"
#include "CraftingWidget.hpp"
class WorkbenchWidget : public Widget {
public:
@ -27,11 +27,7 @@ class WorkbenchWidget : public Widget {
Image m_background;
Inventory m_craftingInventory{3, 3};
InventoryWidget m_craftingInventoryWidget{this};
Inventory m_craftingResultInventory{1, 1};
InventoryWidget m_craftingResultInventoryWidget{this};
CraftingWidget m_craftingWidget{this};
Inventory &m_playerInventory;
InventoryWidget m_playerInventoryWidget{this};

View File

@ -0,0 +1,53 @@
/*
* =====================================================================================
*
* Filename: CraftingWidget.cpp
*
* Description:
*
* Created: 25/06/2018 02:27:35
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "CraftingWidget.hpp"
#include "Registry.hpp"
CraftingWidget::CraftingWidget(Widget *parent) : Widget(parent) {
m_craftingInventoryWidget.init(m_craftingInventory);
m_craftingInventoryWidget.setPosition(29, 16, 0);
m_craftingResultInventoryWidget.init(m_craftingResultInventory);
m_craftingResultInventoryWidget.setPosition(122, 34, 0);
}
void CraftingWidget::onEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget) {
m_craftingInventoryWidget.onEvent(event, mouseItemWidget);
m_craftingResultInventoryWidget.onEvent(event, mouseItemWidget);
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 CraftingWidget::draw(RenderTarget &target, RenderStates states) const {
applyTransform(states);
target.draw(m_craftingInventoryWidget, states);
target.draw(m_craftingResultInventoryWidget, states);
}

View File

@ -12,7 +12,6 @@
* =====================================================================================
*/
#include "Config.hpp"
#include "Registry.hpp"
#include "WorkbenchWidget.hpp"
WorkbenchWidget::WorkbenchWidget(Inventory &playerInventory, Inventory &hotbarInventory, Widget *parent)
@ -21,12 +20,6 @@ WorkbenchWidget::WorkbenchWidget(Inventory &playerInventory, Inventory &hotbarIn
m_background.load("texture-workbench");
m_background.setClipRect(0, 0, 176, 166);
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);
@ -38,31 +31,13 @@ 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_craftingWidget.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 {
@ -70,8 +45,8 @@ 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_craftingWidget, states);
target.draw(m_playerInventoryWidget, states);
target.draw(m_hotbarInventoryWidget, states);