[Inventory] Now supports using 'addStack' on a subset.

This commit is contained in:
Quentin Bazin 2020-03-01 12:28:26 +01:00
parent 4d9a2fbe7e
commit c092e53116
6 changed files with 24 additions and 8 deletions

View File

@ -27,12 +27,15 @@
#include "ClientCommandHandler.hpp"
#include "InventoryWidget.hpp"
void InventoryWidget::init(Inventory &inventory, unsigned int offset, unsigned int size) {
void InventoryWidget::init(Inventory &inventory, u16 offset, u16 size) {
m_inventory = &inventory;
m_itemWidgets.clear();
for (u16 i = 0 ; i < (size > 0 ? size : inventory.width() * inventory.height()) ; ++i) {
m_offset = offset;
m_size = size > 0 ? size : inventory.width() * inventory.height();
for (u16 i = 0 ; i < m_size ; ++i) {
m_itemWidgets.emplace_back(inventory, (i + offset) % inventory.width(), (i + offset) / inventory.width(), this);
ItemWidget &widget = m_itemWidgets.back();
@ -92,7 +95,7 @@ void InventoryWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractI
}
bool InventoryWidget::receiveItemStack(const ItemWidget *itemStack) {
bool stackAdded = m_inventory->addStack(itemStack->stack().item().stringID(), itemStack->stack().amount());
bool stackAdded = m_inventory->addStack(itemStack->stack().item().stringID(), itemStack->stack().amount(), m_offset, m_size);
if (stackAdded)
sendUpdatePacket();

View File

@ -39,7 +39,7 @@ class InventoryWidget : public AbstractInventoryWidget {
InventoryWidget(ClientCommandHandler &client, Widget *parent = nullptr)
: AbstractInventoryWidget(parent), m_client(client) {}
void init(Inventory &inventory, unsigned int offset = 0, unsigned int size = 0);
void init(Inventory &inventory, u16 offset = 0, u16 size = 0);
void onMouseEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget, bool isReadOnly = false);
@ -64,6 +64,9 @@ class InventoryWidget : public AbstractInventoryWidget {
u16 m_inventoryWidth = 0;
u16 m_inventoryHeight = 0;
u16 m_offset = 0;
u16 m_size = 0;
std::vector<ItemWidget> m_itemWidgets;
ItemWidget *m_currentItemWidget = nullptr;

View File

@ -32,8 +32,8 @@ void Inventory::setStack(u16 x, u16 y, const std::string &stringID, u16 amount)
m_hasChanged = true;
}
bool Inventory::addStack(const std::string &stringID, u16 amount) {
for (std::size_t i = 0 ; i < m_items.size() ; ++i) {
bool Inventory::addStack(const std::string &stringID, u16 amount, u16 offset, u16 size) {
for (std::size_t i = offset ; i < (size ? offset + size : m_items.size()) ; ++i) {
if (m_items[i].item().id() == 0) {
m_items[i] = ItemStack(stringID, amount);
m_hasChanged = true;
@ -49,6 +49,11 @@ bool Inventory::addStack(const std::string &stringID, u16 amount) {
return false;
}
// NOTE: This fonction is only used by Lua since default parameters don't work properly
bool Inventory::addStack2(const std::string &stringID, u16 amount) {
return addStack(stringID, amount, 0, 0);
}
void Inventory::clearStack(u16 x, u16 y) {
setStack(x, y, "_:air", 0);
}

View File

@ -43,7 +43,8 @@ class Inventory : public ISerializable {
const ItemStack &getStack(u16 x, u16 y) const { return m_items.at(x + y * m_width); }
ItemStack &getStackRef(u16 x, u16 y) { return m_items.at(x + y * m_width); }
void setStack(u16 x, u16 y, const std::string &stringID, u16 amount = 1);
bool addStack(const std::string &stringID, u16 amount = 1);
bool addStack(const std::string &stringID, u16 amount = 1, u16 offset = 0, u16 size = 0);
bool addStack2(const std::string &stringID, u16 amount = 1);
void clearStack(u16 x, u16 y);
void serialize(sf::Packet &packet) const override;

View File

@ -97,6 +97,8 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
size = {x = 9, y = 3},
offset = 9,
count = 9 * 3,
shift_destination = "inv_hotbar",
}
gui:inventory {
@ -109,6 +111,8 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
size = {x = 9, y = 1},
offset = 0,
count = 9,
shift_destination = "inv_main",
}
gui:crafting {

View File

@ -93,7 +93,7 @@ void ScriptEngine::initUsertypes() {
);
m_lua.new_usertype<Inventory>("Inventory",
"add_stack", &Inventory::addStack,
"add_stack", sol::overload(&Inventory::addStack, &Inventory::addStack2),
"get_stack", &Inventory::getStack,
"set_stack", &Inventory::setStack
);