Nooncraft/src/item.h

105 lines
2.0 KiB
C
Raw Permalink Normal View History

2022-07-16 23:02:04 +02:00
// nooncraft.item: Item storage and properties
#pragma once
#include <cstdint>
2022-07-17 16:33:55 +02:00
#include "render.h"
2022-07-16 23:02:04 +02:00
enum class ToolKind: uint8_t {
Pickaxe, Axe, Shovel, Hoe,
};
2022-07-17 16:23:23 +02:00
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,
};
2022-07-17 16:23:23 +02:00
#include "block.h"
struct ItemInfo
{
char const *name;
2022-07-17 16:33:55 +02:00
GlyphCluster cluster;
2022-07-17 16:23:23 +02:00
bool isTool;
ToolKind toolKind;
/* Wooden, Stone, Iron, Gold, Diamond */
uint8_t toolLevel;
uint8_t stackSize;
2022-07-17 16:23:23 +02:00
};
struct Item
{
bool isBlock;
union {
Block block;
ItemID itemID;
2022-07-17 16:23:23 +02:00
};
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;
2022-07-17 16:23:23 +02:00
};
struct BlockInfo;
namespace Nooncraft {
/* TODO: Move as methods of Item */
2022-07-17 16:23:23 +02:00
ItemInfo *getItemInfo(Item const &i);
BlockInfo *getItemBlockInfo(Item const &i);
2022-07-17 16:33:55 +02:00
GlyphCluster getItemCluster(Item const &i);
char const *getItemName(Item const &i);
2022-07-17 16:23:23 +02:00
} /* 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);
}
};