mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-05 11:01:49 +00:00
Introduced Content, ContentIndices, no more integer ids hardcoded, common refactor
This commit is contained in:
parent
f2f9343737
commit
3b03464d27
35 changed files with 472 additions and 279 deletions
50
src/content/Content.cpp
Normal file
50
src/content/Content.cpp
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include "Content.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "../voxels/Block.h"
|
||||
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::unordered_map;
|
||||
|
||||
void ContentBuilder::add(Block* def) {
|
||||
if (blockDefs.find(def->name) != blockDefs.end()) {
|
||||
throw std::runtime_error("block name duplicate: "+def->name);
|
||||
}
|
||||
blockDefs[def->name] = def;
|
||||
blockIds.push_back(def->name);
|
||||
}
|
||||
|
||||
Content* ContentBuilder::build() {
|
||||
vector<Block*> blockDefsIndices;
|
||||
for (const string& name : blockIds) {
|
||||
Block* def = blockDefs[name];
|
||||
def->id = blockDefsIndices.size();
|
||||
blockDefsIndices.push_back(def);
|
||||
}
|
||||
ContentIndices* indices = new ContentIndices(blockDefsIndices);
|
||||
return new Content(indices, blockDefs);
|
||||
}
|
||||
|
||||
ContentIndices::ContentIndices(vector<Block*> blockDefs)
|
||||
: blockDefs(blockDefs) {
|
||||
}
|
||||
|
||||
Content::Content(ContentIndices* indices,
|
||||
unordered_map<string, Block*> blockDefs)
|
||||
: blockDefs(blockDefs),
|
||||
indices(indices) {
|
||||
}
|
||||
|
||||
Content::~Content() {
|
||||
delete indices;
|
||||
}
|
||||
|
||||
Block* Content::require(std::string id) const {
|
||||
auto found = blockDefs.find(id);
|
||||
if (found == blockDefs.end()) {
|
||||
throw std::runtime_error("missing block "+id);
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue