refactor
This commit is contained in:
parent
b1740bf7eb
commit
4306573320
@ -108,21 +108,21 @@ std::shared_ptr<UINode> HudElement::getNode() const {
|
||||
|
||||
std::shared_ptr<InventoryView> Hud::createContentAccess() {
|
||||
auto& content = frontend.getLevel().content;
|
||||
auto indices = content.getIndices();
|
||||
auto& indices = *content.getIndices();
|
||||
auto inventory = player.getInventory();
|
||||
|
||||
size_t itemsCount = indices->items.count();
|
||||
size_t itemsCount = indices.items.count();
|
||||
auto accessInventory = std::make_shared<Inventory>(0, itemsCount);
|
||||
for (size_t id = 1; id < itemsCount; id++) {
|
||||
accessInventory->getSlot(id-1).set(ItemStack(id, 1));
|
||||
}
|
||||
|
||||
SlotLayout slotLayout(-1, glm::vec2(), false, true, nullptr,
|
||||
[=](uint, ItemStack& item) {
|
||||
[inventory, &indices](uint, ItemStack& item) {
|
||||
auto copy = ItemStack(item);
|
||||
inventory->move(copy, indices);
|
||||
},
|
||||
[=](uint, ItemStack& item) {
|
||||
[this, inventory](uint, ItemStack& item) {
|
||||
inventory->getSlot(player.getChosenSlot()).set(item);
|
||||
});
|
||||
|
||||
@ -494,12 +494,12 @@ void Hud::dropExchangeSlot() {
|
||||
|
||||
auto indices = frontend.getLevel().content.getIndices();
|
||||
if (auto invView = std::dynamic_pointer_cast<InventoryView>(blockUI)) {
|
||||
invView->getInventory()->move(stack, indices);
|
||||
invView->getInventory()->move(stack, *indices);
|
||||
}
|
||||
if (stack.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
player.getInventory()->move(stack, indices);
|
||||
player.getInventory()->move(stack, *indices);
|
||||
if (!stack.isEmpty()) {
|
||||
logger.warning() << "discard item [" << stack.getItemId() << ":"
|
||||
<< stack.getCount();
|
||||
|
||||
@ -219,7 +219,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
|
||||
return;
|
||||
}
|
||||
if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) {
|
||||
stack.move(grabbed, content->getIndices());
|
||||
stack.move(grabbed, *content->getIndices());
|
||||
} else {
|
||||
if (layout.itemSource) {
|
||||
if (grabbed.isEmpty()) {
|
||||
|
||||
@ -37,7 +37,7 @@ size_t Inventory::findSlotByItem(
|
||||
}
|
||||
|
||||
void Inventory::move(
|
||||
ItemStack& item, const ContentIndices* indices, size_t begin, size_t end
|
||||
ItemStack& item, const ContentIndices& indices, size_t begin, size_t end
|
||||
) {
|
||||
end = std::min(slots.size(), end);
|
||||
for (size_t i = begin; i < end && !item.isEmpty(); i++) {
|
||||
|
||||
@ -32,7 +32,7 @@ public:
|
||||
|
||||
void move(
|
||||
ItemStack& item,
|
||||
const ContentIndices* indices,
|
||||
const ContentIndices& indices,
|
||||
size_t begin = 0,
|
||||
size_t end = -1
|
||||
);
|
||||
|
||||
@ -3,9 +3,6 @@
|
||||
#include "content/Content.hpp"
|
||||
#include "ItemDef.hpp"
|
||||
|
||||
ItemStack::ItemStack() : item(ITEM_EMPTY), count(0) {
|
||||
}
|
||||
|
||||
ItemStack::ItemStack(itemid_t item, itemcount_t count)
|
||||
: item(item), count(count) {
|
||||
}
|
||||
@ -28,9 +25,9 @@ bool ItemStack::accepts(const ItemStack& other) const {
|
||||
return item == other.getItemId();
|
||||
}
|
||||
|
||||
void ItemStack::move(ItemStack& item, const ContentIndices* indices) {
|
||||
auto& def = indices->items.require(item.getItemId());
|
||||
int count = std::min(item.count, def.stackSize - this->count);
|
||||
void ItemStack::move(ItemStack& item, const ContentIndices& indices) {
|
||||
auto& def = indices.items.require(item.getItemId());
|
||||
itemcount_t count = std::min(item.count, def.stackSize - this->count);
|
||||
if (isEmpty()) {
|
||||
set(ItemStack(item.getItemId(), count));
|
||||
} else {
|
||||
|
||||
@ -6,10 +6,10 @@
|
||||
class ContentIndices;
|
||||
|
||||
class ItemStack {
|
||||
itemid_t item;
|
||||
itemcount_t count;
|
||||
itemid_t item = ITEM_EMPTY;
|
||||
itemcount_t count = 0;
|
||||
public:
|
||||
ItemStack();
|
||||
ItemStack() = default;
|
||||
|
||||
ItemStack(itemid_t item, itemcount_t count);
|
||||
|
||||
@ -17,7 +17,12 @@ public:
|
||||
void setCount(itemcount_t count);
|
||||
|
||||
bool accepts(const ItemStack& item) const;
|
||||
void move(ItemStack& item, const ContentIndices* indices);
|
||||
|
||||
/// @brief Move items from one stack to another.
|
||||
/// If the target stack is completely filled, the source stack will be reduced.
|
||||
/// @param item source stack
|
||||
/// @param indices content indices
|
||||
void move(ItemStack& item, const ContentIndices& indices);
|
||||
|
||||
inline void clear() {
|
||||
set(ItemStack(0, 0));
|
||||
|
||||
@ -80,7 +80,7 @@ static int l_add(lua::State* L) {
|
||||
|
||||
auto& inv = get_inventory(invid);
|
||||
ItemStack item(itemid, count);
|
||||
inv.move(item, indices);
|
||||
inv.move(item, *indices);
|
||||
return lua::pushinteger(L, item.getCount());
|
||||
}
|
||||
|
||||
@ -144,9 +144,9 @@ static int l_move(lua::State* L) {
|
||||
auto& invB = get_inventory(invBid, 3);
|
||||
auto& slot = invA.getSlot(slotAid);
|
||||
if (slotBid == -1) {
|
||||
invB.move(slot, content->getIndices());
|
||||
invB.move(slot, *content->getIndices());
|
||||
} else {
|
||||
invB.move(slot, content->getIndices(), slotBid, slotBid + 1);
|
||||
invB.move(slot, *content->getIndices(), slotBid, slotBid + 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -163,9 +163,9 @@ static int l_move_range(lua::State* L) {
|
||||
auto invB = get_inventory(invBid, 3);
|
||||
auto& slot = invA.getSlot(slotAid);
|
||||
if (slotBegin == -1) {
|
||||
invB.move(slot, content->getIndices());
|
||||
invB.move(slot, *content->getIndices());
|
||||
} else {
|
||||
invB.move(slot, content->getIndices(), slotBegin, slotEnd);
|
||||
invB.move(slot, *content->getIndices(), slotBegin, slotEnd);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user