Merge pull request #2 from MarkSuckerberg/inventory-adjustment

Inventory Display Tweak
This commit is contained in:
Shweet
2025-12-30 16:47:51 -08:00
committed by GitHub
5 changed files with 132 additions and 58 deletions

View File

@@ -123,8 +123,7 @@ namespace game
((Item::SPAWN_Y_MAX - Item::SPAWN_Y_MIN) * math::random()) + Item::SPAWN_Y_MIN); ((Item::SPAWN_Y_MAX - Item::SPAWN_Y_MIN) * math::random()) + Item::SPAWN_Y_MIN);
items.emplace_back(&resources.anm2s[anm2::ITEMS], position, type); items.emplace_back(&resources.anm2s[anm2::ITEMS], position, type);
inventory.values[type]--; inventory.adjust_item(type, -1);
if (inventory.values[type] == 0) inventory.values.erase(type);
type = Item::NONE; type = Item::NONE;
inventory.isQueued = false; inventory.isQueued = false;
} }
@@ -159,7 +158,7 @@ namespace game
{ {
if (Item::queuedReturnItem->state == Item::DEFAULT) if (Item::queuedReturnItem->state == Item::DEFAULT)
{ {
inventory.values[item.type]++; inventory.adjust_item(item.type);
resources.sound_play(audio::RETURN); resources.sound_play(audio::RETURN);
} }
else else

View File

@@ -9,6 +9,31 @@ using namespace glm;
namespace game::window namespace game::window
{ {
Inventory::Inventory()
{
count = 0;
for (auto& [type, quantity] : values)
{
count += quantity;
}
}
void Inventory::adjust_item(Item::Type type, int quantity)
{
values[type] += quantity;
count += quantity;
}
void Inventory::set_item(Item::Type type, int value)
{
count -= values[type];
values[type] = value;
count += value;
}
int Inventory::get_item(Item::Type type) { return values.contains(type) ? values[type] : 0; }
void Inventory::update(Resources& resources, Character& character, GameData& gameData) void Inventory::update(Resources& resources, Character& character, GameData& gameData)
{ {
auto& texture = resources.anm2s[anm2::ITEMS].content.spritesheets.at(0).texture; auto& texture = resources.anm2s[anm2::ITEMS].content.spritesheets.at(0).texture;
@@ -18,9 +43,13 @@ namespace game::window
auto cursorPos = ImGui::GetCursorPos(); auto cursorPos = ImGui::GetCursorPos();
auto cursorStartX = ImGui::GetCursorPosX(); auto cursorStartX = ImGui::GetCursorPosX();
for (auto& [type, quantity] : values) for (int index = 0; index < Item::ITEM_COUNT; index++)
{ {
if (quantity == 0) continue; auto type = (Item::Type)index;
auto seen = values.contains(type);
//Hide invalid items and impossible-to-obtain items when not seen.
if (!seen && (Item::CATEGORIES[type] == Item::INVALID || Item::RARITIES[type] == Item::IMPOSSIBLE)) continue;
auto columns = (int)(texture.size.x / ITEM_SIZE.x); auto columns = (int)(texture.size.x / ITEM_SIZE.x);
auto crop = vec2(type % columns, type / columns) * ITEM_SIZE; auto crop = vec2(type % columns, type / columns) * ITEM_SIZE;
@@ -30,23 +59,24 @@ namespace game::window
ImGui::PushID(type); ImGui::PushID(type);
ImGui::SetCursorPos(cursorPos); ImGui::SetCursorPos(cursorPos);
auto cursorScreenPos = ImGui::GetCursorScreenPos(); auto cursorScreenPos = ImGui::GetCursorScreenPos();
auto quantity = 0;
if (seen)
{
quantity = values[type];
ImGui::BeginDisabled(quantity < 1);
if (ImGui::ImageButton("##Image Button", texture.id, IMAGE_SIZE, imgui::to_imvec2(uvMin), if (ImGui::ImageButton("##Image Button", texture.id, IMAGE_SIZE, imgui::to_imvec2(uvMin),
imgui::to_imvec2(uvMax))) imgui::to_imvec2(uvMax)) &&
quantity > 0)
{ {
queuedItemType = type; queuedItemType = type;
isQueued = true; isQueued = true;
resources.sound_play(audio::SUMMON); resources.sound_play(audio::SUMMON);
} }
ImGui::EndDisabled();
auto increment = ImGui::GetItemRectSize().x + ImGui::GetStyle().ItemSpacing.x;
cursorPos.x += increment;
if (cursorPos.x + increment > ImGui::GetContentRegionAvail().x)
{
cursorPos.x = cursorStartX;
cursorPos.y += increment;
}
if (ImGui::BeginItemTooltip()) if (ImGui::BeginItemTooltip())
{ {
@@ -77,7 +107,6 @@ namespace game::window
ImGui::PopStyleColor(); ImGui::PopStyleColor();
ImGui::EndTooltip(); ImGui::EndTooltip();
} }
ImGui::PopID();
ImGui::PushFont(resources.font.get(), Font::BIG); ImGui::PushFont(resources.font.get(), Font::BIG);
auto text = std::format("x{}", quantity); auto text = std::format("x{}", quantity);
@@ -87,8 +116,41 @@ namespace game::window
text.c_str()); text.c_str());
ImGui::PopFont(); ImGui::PopFont();
} }
else
{
ImGui::BeginDisabled(true);
ImGui::ImageButton("##Image Button", texture.id, IMAGE_SIZE, imgui::to_imvec2(uvMin), imgui::to_imvec2(uvMax),
ImVec4(0, 0, 0, 0), ImVec4(0, 0, 0, 1));
ImGui::EndDisabled();
if (values.empty()) ImGui::Text("Check the \"Play\" tab to earn rewards!"); if (ImGui::BeginItemTooltip())
{
ImGui::TextUnformatted("??? (x0)");
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetColorU32(imgui::to_imvec4(GRAY)));
ImGui::TextUnformatted("-- ??? (\?\?\?) --");
ImGui::Separator();
ImGui::TextUnformatted("???");
ImGui::PopStyleColor();
ImGui::EndTooltip();
}
}
auto increment = ImGui::GetItemRectSize().x + ImGui::GetStyle().ItemSpacing.x;
cursorPos.x += increment;
if (cursorPos.x + increment > ImGui::GetContentRegionAvail().x)
{
cursorPos.x = cursorStartX;
cursorPos.y += increment;
}
ImGui::PopID();
}
if (count == 0) ImGui::Text("Check the \"Play\" tab to earn rewards!");
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} }

View File

@@ -11,20 +11,26 @@ namespace game::window
{ {
class Inventory class Inventory
{ {
public: private:
static constexpr auto ITEM_SIZE = glm::vec2(48, 48);
static constexpr auto IMAGE_SIZE = ImVec2(48, 48);
static constexpr auto BUTTON_ROUNDING = 32.0f;
std::map<Item::Type, int> values = {{Item::POKE_PUFF_BASIC_SWEET, 1}, std::map<Item::Type, int> values = {{Item::POKE_PUFF_BASIC_SWEET, 1},
{Item::POKE_PUFF_BASIC_CITRUS, 1}, {Item::POKE_PUFF_BASIC_CITRUS, 1},
{Item::POKE_PUFF_BASIC_MINT, 1}, {Item::POKE_PUFF_BASIC_MINT, 1},
{Item::POKE_PUFF_BASIC_MOCHA, 1}, {Item::POKE_PUFF_BASIC_MOCHA, 1},
{Item::POKE_PUFF_BASIC_SPICE, 1}}; {Item::POKE_PUFF_BASIC_SPICE, 1}};
public:
static constexpr auto ITEM_SIZE = glm::vec2(48, 48);
static constexpr auto IMAGE_SIZE = ImVec2(48, 48);
static constexpr auto BUTTON_ROUNDING = 32.0f;
int count;
bool isQueued{}; bool isQueued{};
Item::Type queuedItemType{}; Item::Type queuedItemType{};
Inventory();
void update(Resources&, Character&, GameData&); void update(Resources&, Character&, GameData&);
void adjust_item(Item::Type, int = 1);
void set_item(Item::Type, int);
int get_item(Item::Type);
}; };
} }

View File

@@ -123,9 +123,16 @@ namespace game::window
ImGui::PushItemWidth(100); ImGui::PushItemWidth(100);
for (int i = 0; i < Item::ITEM_COUNT; i++) for (int i = 0; i < Item::ITEM_COUNT; i++)
{ {
if (i == Item::INVALID) continue; if (Item::CATEGORIES[i] == Item::INVALID) continue;
ImGui::PushID(i); ImGui::PushID(i);
ImGui::DragInt(Item::NAMES[i], &inventory.values[(Item::Type)i], 0.1f, 0, 999);
//TODO: Probably a cleaner way to do this, maybe
int value = inventory.get_item((Item::Type)i);
if (ImGui::DragInt(Item::NAMES[i], &value, 0.1f, 0, 999))
{
inventory.set_item((Item::Type)i, value);
}
ImGui::PopID(); ImGui::PopID();
} }
ImGui::PopItemWidth(); ImGui::PopItemWidth();

View File

@@ -203,7 +203,7 @@ namespace game::window
resources.sound_play(audio::HIGH_SCORE_BIG); resources.sound_play(audio::HIGH_SCORE_BIG);
isHighScoreBigAchieved = true; isHighScoreBigAchieved = true;
inventory.values[Item::POKE_PUFF_SUPREME_HONOR]++; inventory.adjust_item(Item::POKE_PUFF_SUPREME_HONOR);
auto toastItemPosition = auto toastItemPosition =
ImVec2(math::random_in_range(barMax.x + ITEM_SIZE.x, barMax.x + (size.x * 0.5f) - ITEM_SIZE.x), ImVec2(math::random_in_range(barMax.x + ITEM_SIZE.x, barMax.x + (size.x * 0.5f) - ITEM_SIZE.x),
@@ -265,7 +265,7 @@ namespace game::window
resources.sound_play(audio::FALL); resources.sound_play(audio::FALL);
resources.sound_play(RARITY_SOUNDS.at(rarity)); resources.sound_play(RARITY_SOUNDS.at(rarity));
inventory.values[rewardType]++; inventory.adjust_item(rewardType);
auto toastItemPosition = auto toastItemPosition =
ImVec2(math::random_in_range(barMax.x + ITEM_SIZE.x, barMax.x + (size.x * 0.5f) - ITEM_SIZE.x), ImVec2(math::random_in_range(barMax.x + ITEM_SIZE.x, barMax.x + (size.x * 0.5f) - ITEM_SIZE.x),