From 2acbf8689a46e62c87556880edcfba272a352175 Mon Sep 17 00:00:00 2001 From: Nelonn Date: Sun, 10 Dec 2023 17:23:30 +0300 Subject: [PATCH] Template for future items --- res/content/base/items/stone.json | 2 ++ res/content/base/package.json | 3 +++ src/content/Content.cpp | 45 +++++++++++++++++++++++++++---- src/content/Content.h | 28 +++++++++++++++++-- src/content/ContentLoader.cpp | 21 +++++++++++++++ src/content/ContentLoader.h | 2 ++ src/item/Item.cpp | 7 +++++ src/item/Item.h | 21 +++++++++++++++ src/item/ItemStack.cpp | 8 ++++++ src/item/ItemStack.h | 18 +++++++++++++ src/typedefs.h | 1 + 11 files changed, 149 insertions(+), 7 deletions(-) create mode 100644 res/content/base/items/stone.json create mode 100644 src/item/Item.cpp create mode 100644 src/item/Item.h create mode 100644 src/item/ItemStack.cpp create mode 100644 src/item/ItemStack.h diff --git a/res/content/base/items/stone.json b/res/content/base/items/stone.json new file mode 100644 index 000000000..7a73a41bf --- /dev/null +++ b/res/content/base/items/stone.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/res/content/base/package.json b/res/content/base/package.json index f131099f3..20e580f0d 100644 --- a/res/content/base/package.json +++ b/res/content/base/package.json @@ -25,5 +25,8 @@ "pane", "pipe", "lightbulb" + ], + "items": [ + "stone" ] } diff --git a/src/content/Content.cpp b/src/content/Content.cpp index c7ddf25bc..d597be89b 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -3,6 +3,7 @@ #include #include +#include "../item/Item.h" #include "../voxels/Block.h" using glm::vec3; @@ -18,8 +19,17 @@ void ContentBuilder::add(Block* def) { blockIds.push_back(def->name); } +void ContentBuilder::add(Item* def) { + if (itemDefs.find(def->name) != itemDefs.end()) { + throw std::runtime_error("item name duplicate: "+def->name); + } + itemDefs[def->name] = def; + itemIds.push_back(def->name); +} + Content* ContentBuilder::build() { vector blockDefsIndices; + vector itemDefsIndices; DrawGroups* groups = new DrawGroups; for (const string& name : blockIds) { Block* def = blockDefs[name]; @@ -42,20 +52,29 @@ Content* ContentBuilder::build() { if (groups->find(def->drawGroup) == groups->end()) { groups->insert(def->drawGroup); } + } + for (const string& name : itemIds) { + Item* def = itemDefs[name]; + // Generating runtime info + def->rt.id = itemDefsIndices.size(); + itemDefsIndices.push_back(def); } - ContentIndices* indices = new ContentIndices(blockDefsIndices); - return new Content(indices, groups, blockDefs); + // TODO: item ids + ContentIndices* indices = new ContentIndices(blockDefsIndices, itemDefsIndices); + return new Content(indices, groups, blockDefs, itemDefs); } -ContentIndices::ContentIndices(vector blockDefs) - : blockDefs(blockDefs) { +ContentIndices::ContentIndices(vector blockDefs, vector itemDefs) + : blockDefs(blockDefs), itemDefs(itemDefs) { } Content::Content(ContentIndices* indices, DrawGroups* drawGroups, - unordered_map blockDefs) + unordered_map blockDefs, + unordered_map itemDefs) : blockDefs(blockDefs), + itemDefs(itemDefs), indices(indices), drawGroups(drawGroups) { } @@ -79,3 +98,19 @@ Block* Content::requireBlock(string id) const { } return found->second; } + +Item* Content::findItem(string id) const { + auto found = itemDefs.find(id); + if (found == itemDefs.end()) { + return nullptr; + } + return found->second; +} + +Item* Content::requireItem(string id) const { + auto found = itemDefs.find(id); + if (found == itemDefs.end()) { + throw std::runtime_error("missing item "+id); + } + return found->second; +} diff --git a/src/content/Content.h b/src/content/Content.h index f54f7fe5b..2ce9ebb67 100644 --- a/src/content/Content.h +++ b/src/content/Content.h @@ -10,13 +10,17 @@ typedef std::set DrawGroups; class Block; +class Item; class Content; class ContentBuilder { std::unordered_map blockDefs; + std::unordered_map itemDefs; std::vector blockIds; + std::vector itemIds; public: void add(Block* def); + void add(Item* def); Content* build(); }; @@ -25,8 +29,9 @@ class ContentBuilder { class ContentIndices { // blockDefs must be a plain vector with block id used as index std::vector blockDefs; + std::vector itemDefs; public: - ContentIndices(std::vector blockDefs); + ContentIndices(std::vector blockDefs, std::vector itemDefs); inline Block* getBlockDef(blockid_t id) const { if (id >= blockDefs.size()) @@ -42,21 +47,40 @@ class ContentIndices { const Block* const* getBlockDefs() const { return blockDefs.data(); } + + inline Item* getItemDef(itemid_t id) const { + if (id >= itemDefs.size()) + return nullptr; + return itemDefs[id]; + } + + inline size_t countItemDefs() const { + return itemDefs.size(); + } + + const Item* const* getItemDefs() const { + return itemDefs.data(); + } }; /* Content is a definitions repository */ class Content { std::unordered_map blockDefs; + std::unordered_map itemDefs; public: ContentIndices* const indices; DrawGroups* const drawGroups; Content(ContentIndices* indices, DrawGroups* drawGroups, - std::unordered_map blockDefs); + std::unordered_map blockDefs, + std::unordered_map itemDefs); ~Content(); Block* findBlock(std::string id) const; Block* requireBlock(std::string id) const; + + Item* findItem(std::string id) const; + Item* requireItem(std::string id) const; }; #endif // CONTENT_CONTENT_H_ \ No newline at end of file diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 39d0d0913..9936b8bc7 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -5,6 +5,7 @@ #include #include "Content.h" +#include "../item/Item.h" #include "../voxels/Block.h" #include "../files/files.h" #include "../coders/json.h" @@ -95,6 +96,15 @@ Block* ContentLoader::loadBlock(string name, path file) { return def.release(); } +Item* ContentLoader::loadItem(string name, path file) { + unique_ptr root(files::read_json(file)); + unique_ptr def(new Item(name)); + + // TODO: load item properties + + return def.release(); +} + void ContentLoader::load(ContentBuilder* builder) { cout << "-- loading content " << folder << endl; @@ -127,4 +137,15 @@ void ContentLoader::load(ContentBuilder* builder) { builder->add(loadBlock(id+":"+name, blockfile)); } } + + json::JArray* itemsarr = root->arr("items"); + if (itemsarr) { + cout << " items: " << itemsarr->size() << endl; + for (uint i = 0; i < itemsarr->size(); i++) { + string name = itemsarr->str(i); + cout << " loading item " << id << ":" << name << endl; + path itemfile = folder/path("items/"+name+".json"); + builder->add(loadItem(id+":"+name, itemfile)); + } + } } diff --git a/src/content/ContentLoader.h b/src/content/ContentLoader.h index 5395b288e..35c548226 100644 --- a/src/content/ContentLoader.h +++ b/src/content/ContentLoader.h @@ -5,6 +5,7 @@ #include class Block; +class Item; class ContentBuilder; class ContentLoader { @@ -13,6 +14,7 @@ class ContentLoader { ContentLoader(std::filesystem::path folder); Block* loadBlock(std::string name, std::filesystem::path file); + Item* loadItem(std::string name, std::filesystem::path file); void load(ContentBuilder* builder); }; diff --git a/src/item/Item.cpp b/src/item/Item.cpp new file mode 100644 index 000000000..5cc6a616d --- /dev/null +++ b/src/item/Item.cpp @@ -0,0 +1,7 @@ +#include "Item.h" + +#include + +Item::Item(std::string name) { + this->name = std::move(name); +} diff --git a/src/item/Item.h b/src/item/Item.h new file mode 100644 index 000000000..20925dbaf --- /dev/null +++ b/src/item/Item.h @@ -0,0 +1,21 @@ +#ifndef ITEM_H_ +#define ITEM_H_ + +#include +#include "../typedefs.h" + +class Item { +public: + std::string name; + + // TODO: item properties + + struct { + itemid_t id; + } rt; + + explicit Item(std::string name); +}; + + +#endif //ITEM_H_ diff --git a/src/item/ItemStack.cpp b/src/item/ItemStack.cpp new file mode 100644 index 000000000..0794be485 --- /dev/null +++ b/src/item/ItemStack.cpp @@ -0,0 +1,8 @@ +#include "ItemStack.h" + +ItemStack::ItemStack(Item *item, int count) { + this->item = item; + this->count = count; +} + +ItemStack::ItemStack(Item *item) : ItemStack(item, 1) {} diff --git a/src/item/ItemStack.h b/src/item/ItemStack.h new file mode 100644 index 000000000..aa7e7c6e2 --- /dev/null +++ b/src/item/ItemStack.h @@ -0,0 +1,18 @@ +#ifndef ITEMSTACK_H_ +#define ITEMSTACK_H_ + +#include + +class Item; + +class ItemStack { +public: + Item* item; + int count; + + ItemStack(Item* item, int count); + explicit ItemStack(Item* item); +}; + + +#endif //ITEMSTACK_H_ diff --git a/src/typedefs.h b/src/typedefs.h index 920015c60..9ee747edc 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -8,6 +8,7 @@ typedef unsigned int uint; typedef unsigned char ubyte; typedef uint8_t blockid_t; +typedef uint8_t itemid_t; typedef uint16_t light_t; #endif