// nooncraft.item: Item storage and properties #pragma once #include #include "render.h" enum class ToolKind: uint8_t { Pickaxe, Axe, Shovel, Hoe, }; enum class ItemType: uint8_t { Block, Item, Tool, }; /* TODO: Generate Item IDs dynamically */ enum class ItemID: int16_t { COAL = 0, IRON_INGOT = 1, GOLD_INGOT = 2, DIAMOND = 3, WOODEN_PICKAXE = 4, }; #include "block.h" struct ItemInfo { char const *name; GlyphCluster cluster; bool isTool; ToolKind toolKind; /* Wooden, Stone, Iron, Gold, Diamond */ uint8_t toolLevel; uint8_t stackSize; }; struct Item { bool isBlock; union { Block block; ItemID itemID; }; Item(): isBlock {false}, itemID {-1} {} Item(ItemID id): isBlock {false}, itemID {(int)id} {} bool isNull() const { return !isBlock && (int)itemID == -1; } constexpr bool operator==(Item const &other) { return isBlock == other.isBlock && (isBlock ? block == other.block : itemID == other.itemID); } /* Complex access methods */ int stackSize() const; }; struct BlockInfo; namespace Nooncraft { /* TODO: Move as methods of Item */ ItemInfo *getItemInfo(Item const &i); BlockInfo *getItemBlockInfo(Item const &i); GlyphCluster getItemCluster(Item const &i); char const *getItemName(Item const &i); } /* namespace Nooncraft */ struct ItemStack { Item item; unsigned int qty; ItemStack(): item {}, qty {0} {} ItemStack(Item i): item {i}, qty {1} {} int stackSize() const { return item.stackSize(); } bool isFull() const { return (int)qty >= item.stackSize(); } bool isNull() const { return item.isNull(); } bool canReceiveExtraItems(int count) const { return (int)qty + count <= item.stackSize(); } char const *name() const { return Nooncraft::getItemName(item); } GlyphCluster cluster() const { return Nooncraft::getItemCluster(item); } };