Skip to content

Commit

Permalink
Template for future items
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelonn committed Dec 10, 2023
1 parent c2ab95d commit 2acbf86
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 7 deletions.
2 changes: 2 additions & 0 deletions res/content/base/items/stone.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
3 changes: 3 additions & 0 deletions res/content/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
"pane",
"pipe",
"lightbulb"
],
"items": [
"stone"
]
}
45 changes: 40 additions & 5 deletions src/content/Content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdexcept>
#include <glm/glm.hpp>

#include "../item/Item.h"
#include "../voxels/Block.h"

using glm::vec3;
Expand All @@ -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<Block*> blockDefsIndices;
vector<Item*> itemDefsIndices;
DrawGroups* groups = new DrawGroups;
for (const string& name : blockIds) {
Block* def = blockDefs[name];
Expand All @@ -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<Block*> blockDefs)
: blockDefs(blockDefs) {
ContentIndices::ContentIndices(vector<Block*> blockDefs, vector<Item*> itemDefs)
: blockDefs(blockDefs), itemDefs(itemDefs) {
}

Content::Content(ContentIndices* indices, DrawGroups* drawGroups,
unordered_map<string, Block*> blockDefs)
unordered_map<string, Block*> blockDefs,
unordered_map<string, Item*> itemDefs)
: blockDefs(blockDefs),
itemDefs(itemDefs),
indices(indices),
drawGroups(drawGroups) {
}
Expand All @@ -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;
}
28 changes: 26 additions & 2 deletions src/content/Content.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
typedef std::set<unsigned char> DrawGroups;

class Block;
class Item;
class Content;

class ContentBuilder {
std::unordered_map<std::string, Block*> blockDefs;
std::unordered_map<std::string, Item*> itemDefs;
std::vector<std::string> blockIds;
std::vector<std::string> itemIds;
public:
void add(Block* def);
void add(Item* def);

Content* build();
};
Expand All @@ -25,8 +29,9 @@ class ContentBuilder {
class ContentIndices {
// blockDefs must be a plain vector with block id used as index
std::vector<Block*> blockDefs;
std::vector<Item*> itemDefs;
public:
ContentIndices(std::vector<Block*> blockDefs);
ContentIndices(std::vector<Block*> blockDefs, std::vector<Item*> itemDefs);

inline Block* getBlockDef(blockid_t id) const {
if (id >= blockDefs.size())
Expand All @@ -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<std::string, Block*> blockDefs;
std::unordered_map<std::string, Item*> itemDefs;
public:
ContentIndices* const indices;
DrawGroups* const drawGroups;

Content(ContentIndices* indices, DrawGroups* drawGroups,
std::unordered_map<std::string, Block*> blockDefs);
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, Item*> 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_
21 changes: 21 additions & 0 deletions src/content/ContentLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>

#include "Content.h"
#include "../item/Item.h"
#include "../voxels/Block.h"
#include "../files/files.h"
#include "../coders/json.h"
Expand Down Expand Up @@ -95,6 +96,15 @@ Block* ContentLoader::loadBlock(string name, path file) {
return def.release();
}

Item* ContentLoader::loadItem(string name, path file) {
unique_ptr<json::JObject> root(files::read_json(file));
unique_ptr<Item> def(new Item(name));

// TODO: load item properties

return def.release();
}

void ContentLoader::load(ContentBuilder* builder) {
cout << "-- loading content " << folder << endl;

Expand Down Expand Up @@ -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));
}
}
}
2 changes: 2 additions & 0 deletions src/content/ContentLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <filesystem>

class Block;
class Item;
class ContentBuilder;

class ContentLoader {
Expand All @@ -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);
};

Expand Down
7 changes: 7 additions & 0 deletions src/item/Item.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "Item.h"

#include <utility>

Item::Item(std::string name) {
this->name = std::move(name);
}
21 changes: 21 additions & 0 deletions src/item/Item.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef ITEM_H_
#define ITEM_H_

#include <string>
#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_
8 changes: 8 additions & 0 deletions src/item/ItemStack.cpp
Original file line number Diff line number Diff line change
@@ -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) {}
18 changes: 18 additions & 0 deletions src/item/ItemStack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ITEMSTACK_H_
#define ITEMSTACK_H_

#include <string>

class Item;

class ItemStack {
public:
Item* item;
int count;

ItemStack(Item* item, int count);
explicit ItemStack(Item* item);
};


#endif //ITEMSTACK_H_
1 change: 1 addition & 0 deletions src/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2acbf86

Please sign in to comment.