Nimbin[12]?SDL & Graphics / mapcreator / src/inventory/inventory.hpp

mapcreator git · master

SDL3 2.5D game and engine using assets, with map editor

sdl3 c++ game engine map-editor cmake · first commit 2025-10-31 · last commit 2026-09-07 (1 month ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-spieleentwicklung/map_creator

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gz
src/inventory/inventory.hpp 5 KB · 174 lines raw
#pragma once

#include <SDL3_ttf/SDL_ttf.h>

#include <unordered_map>
#include <deque>
#include <list>
#include <vector>
#include <memory>

#include "inventory/item.hpp"
#include "defaults/icons.hpp"
#include "vegetables/vegetables.hpp"


enum class InventoryObjectType : uint8_t {
    NONE,
    ITEM,
    VEGETABLE,
    SEED
};

struct Inventory_s {
    size_t id = 0;
    EquipmentSlot equipement_slot = EquipmentSlot::NONE;
    InventoryObjectType object_type;

    Inventory_s(size_t d, InventoryObjectType ot)
        : id(d), object_type(ot) {
    }
};

struct InventoryItem : Inventory_s {
    ItemType type = ItemType::HAND;
    int health_current = 0;
    int health_max     = 0;
    float speed        = 0;
    int level          = 0;
    float damage       = 0;
    InventoryItem(size_t d, ItemType t)
        : Inventory_s(d, InventoryObjectType::ITEM),
          type(t)
    {
        const ItemStats& info = Item::get(t);
        health_current  = info.base_health;
        health_max      = info.base_health;
        speed           = info.base_speed;
        damage          = info.base_damage;
        level           = 1;
        equipement_slot = info.equipement_slot;
    }
};

struct InventoryVegetable : Inventory_s {
    size_t count = 0;
    bool   is_seed = false;
    VegetableData::Type type;
    const ConsumingStatsBonus& stats_bonus;
    InventoryVegetable(size_t d, VegetableData::Type t, bool s)
        : Inventory_s(d, s ? InventoryObjectType::SEED : InventoryObjectType::VEGETABLE), count(0),
          is_seed(s), type(t),
          stats_bonus(VegetableData::crop_defs[static_cast<uint32_t>(t)].stats_bonus)
    { }
};


static constexpr int HOTBAR_SLOTS = 7; // 0 == HANDS

struct Equipement {
    size_t item_id;
    InventoryObjectType type;
};

struct InventorySelection {
    Inventory_s* item = nullptr;
    ResourceType resource = ResourceType::NONE;
    VegetableData::Type vegetable = VegetableData::Type::NONE;
    VegetableData::Type seed      = VegetableData::Type::NONE;
    Icon::TilePos tile;
    bool is_valid = false;
};


class Inventory {
    size_t next_item_id;
public:
    static bool draw_menu;

    static ItemType selected_build_item;
    static InventorySelection* selected_item;
    static bool clicked_button_craft;

    static Equipement
               hotbar[HOTBAR_SLOTS];
    static int selected_hotbar;
    static int selected_hotbar_last;
    static InventoryItem* hotbar_hands;


    // stackable items
    std::unordered_map<ResourceType, int>        resources;
    std::unordered_map<VegetableData::Type, InventoryVegetable>
                                                 vegetables;
    std::unordered_map<VegetableData::Type, InventoryVegetable>
                                                 seeds;

    std::list<std::unique_ptr<InventoryItem>>    items;

    std::unordered_map<EquipmentSlot, Equipement> equipment;

    InventorySelection selected_inventory;

    Inventory();
    Inventory(Inventory&& other);

    void add_Vegetable(VegetableData::Type, size_t amount, Messages*);
    int  remove_Vegetable(VegetableData::Type type, size_t amount);
    int  get_Vegetable(VegetableData::Type type) const;

    void add_Seed(VegetableData::Type, size_t amount, Messages*);
    bool remove_Seed(VegetableData::Type type, size_t amount);
    int  get_Seed(VegetableData::Type type) const;

    void add_Resource(ResourceType type, int amount, Messages* messages);
    bool remove_Resource(ResourceType type, int amount);
    int  get_Resource(ResourceType type) const;

    InventoryItem*
         add_Item(ItemType item, Messages*);
    bool remove_Item(ItemType item);
    bool has_Item(ItemType item) const;
    InventoryItem*
         get_ItemById(size_t id);
    InventoryVegetable&
         get_VegetableById(size_t id);
    InventoryVegetable&
         get_SeedById(size_t id);

    // EQUIPEMENT
    void Unequip(EquipmentSlot);
    void Equip(InventoryItem*);
    void Equip(InventoryVegetable*);

    // DRAW
    void draw_InventoryGrid(SDL_Renderer*);
    void draw_BuildMenu(SDL_Renderer*, std::vector<bool> researched_items);
    void draw_InfoPanel(SDL_Renderer*, TTF_Font*, ItemType, bool is_hovering_craftbutton);
    void draw_Menu(SDL_Renderer* renderer, SDL_Rect win_size, TTF_Font* font, std::vector<bool> researched_items, bool is_hovering_craftbutton);

    static bool
    cursor_OverInfoPanel(const SDL_FPoint& mouse);
    static bool
    cursor_OverBuildMenu(const SDL_FPoint& mouse);
    static bool
    cursor_OverInventoryGrid(const SDL_FPoint& mouse);

    static ItemType
    hit_BuildMenu(const SDL_FPoint& mouse, SDL_Rect win_size, std::vector<bool> researched_items);

    static bool
    cursor_OverCraftButton(const SDL_FPoint& mouse);

    void
    draw_Hotbar(SDL_Renderer* renderer, TTF_Font* font);

    bool set_HotbarItem(int slot, Inventory_s* item);
    Inventory_s* get_SelectedHotbarItem();

    InventorySelection
    hit_InventoryGrid(const SDL_FPoint& mouse);

    bool Save(const std::string &path) const;
    bool Load(const std::string &path);
};