BlockMaterial struct + minor refactor and docs

This commit is contained in:
MihailRis
2024-03-07 10:55:19 +03:00
parent f8e02e1b3b
commit 8658a170bc
4 changed files with 86 additions and 43 deletions
+18 -19
View File
@@ -10,8 +10,7 @@
#include "ContentPack.h" #include "ContentPack.h"
#include "../logic/scripting/scripting.h" #include "../logic/scripting/scripting.h"
ContentBuilder::~ContentBuilder() { ContentBuilder::~ContentBuilder() {}
}
void ContentBuilder::add(Block* def) { void ContentBuilder::add(Block* def) {
checkIdentifier(def->name); checkIdentifier(def->name);
@@ -127,25 +126,25 @@ Content* ContentBuilder::build() {
ContentIndices::ContentIndices( ContentIndices::ContentIndices(
std::vector<Block*> blockDefs, std::vector<Block*> blockDefs,
std::vector<ItemDef*> itemDefs) std::vector<ItemDef*> itemDefs
: blockDefs(blockDefs), ) : blockDefs(blockDefs),
itemDefs(itemDefs) { itemDefs(itemDefs)
} {}
Content::Content(ContentIndices* indices, Content::Content(
std::unique_ptr<DrawGroups> drawGroups, ContentIndices* indices,
std::unordered_map<std::string, Block*> blockDefs, std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, ItemDef*> itemDefs, std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs) std::unordered_map<std::string, ItemDef*> itemDefs,
: blockDefs(blockDefs), std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs
itemDefs(itemDefs), ) : blockDefs(blockDefs),
indices(indices), itemDefs(itemDefs),
packs(std::move(packs)), indices(indices),
drawGroups(std::move(drawGroups)) { packs(std::move(packs)),
} drawGroups(std::move(drawGroups))
{}
Content::~Content() { Content::~Content() {}
}
Block* Content::findBlock(std::string id) const { Block* Content::findBlock(std::string id) const {
auto found = blockDefs.find(id); auto found = blockDefs.find(id);
+12 -8
View File
@@ -65,13 +65,15 @@ public:
Content* build(); Content* build();
}; };
/* Runtime defs cache: indices */ /// @brief Runtime defs cache: indices
class ContentIndices { class ContentIndices {
std::vector<Block*> blockDefs; std::vector<Block*> blockDefs;
std::vector<ItemDef*> itemDefs; std::vector<ItemDef*> itemDefs;
public: public:
ContentIndices(std::vector<Block*> blockDefs, ContentIndices(
std::vector<ItemDef*> itemDefs); std::vector<Block*> blockDefs,
std::vector<ItemDef*> itemDefs
);
inline Block* getBlockDef(blockid_t id) const { inline Block* getBlockDef(blockid_t id) const {
if (id >= blockDefs.size()) if (id >= blockDefs.size())
@@ -112,11 +114,13 @@ class Content {
public: public:
std::unique_ptr<DrawGroups> const drawGroups; std::unique_ptr<DrawGroups> const drawGroups;
Content(ContentIndices* indices, Content(
std::unique_ptr<DrawGroups> drawGroups, ContentIndices* indices,
std::unordered_map<std::string, Block*> blockDefs, std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, ItemDef*> itemDefs, std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs); std::unordered_map<std::string, ItemDef*> itemDefs,
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs
);
~Content(); ~Content();
inline ContentIndices* getIndices() const { inline ContentIndices* getIndices() const {
+3 -3
View File
@@ -19,9 +19,9 @@ struct contententry {
// TODO: make it unified for all types of content // TODO: make it unified for all types of content
/* Content indices lookup table or report /// @brief Content indices lookup table or report
used to convert world with different indices /// used to convert world with different indices
Building with indices.json */ /// Building with indices.json
class ContentLUT { class ContentLUT {
std::vector<blockid_t> blocks; std::vector<blockid_t> blocks;
std::vector<std::string> blockNames; std::vector<std::string> blockNames;
+53 -13
View File
@@ -43,11 +43,8 @@ struct CoordSystem {
void transform(AABB& aabb) const; void transform(AABB& aabb) const;
static bool isVectorHasNegatives(glm::ivec3 vec) { inline bool isVectorHasNegatives(glm::ivec3 vec) {
if (vec.x < 0 || vec.y < 0 || vec.z < 0) { return (vec.x < 0 || vec.y < 0 || vec.z < 0);
return true;
}
else return false;
} }
}; };
@@ -56,56 +53,99 @@ struct BlockRotProfile {
std::string name; std::string name;
CoordSystem variants[MAX_COUNT]; CoordSystem variants[MAX_COUNT];
/* Wood logs, pillars, pipes */ /// @brief Wood logs, pillars, pipes
static const BlockRotProfile PIPE; static const BlockRotProfile PIPE;
/* Doors, signs and other panes */ /// @brief Doors, signs and other panes
static const BlockRotProfile PANE; static const BlockRotProfile PANE;
}; };
enum class BlockModel { enum class BlockModel {
none, // invisible /// @brief invisible
block, // default shape none,
xsprite, // X-shape (grass) /// @brief default cube shape
aabb, // box shaped as block hitbox block,
/// @brief X-shape (grass)
xsprite,
/// @brief box shape sized as block hitbox
aabb,
/// @brief custom model defined in json
custom custom
}; };
using BoxModel = AABB; using BoxModel = AABB;
/// @brief Common kit of block properties applied to groups of blocks
struct BlockMaterial {
std::string name;
std::string stepsSound;
std::string placeSound;
std::string breakSound;
};
/// @brief Block properties definition
class Block { class Block {
public: public:
/// @brief Block string id (with prefix included)
std::string const name; std::string const name;
// 0 1 2 3 4 5 /// @brief Textures set applied to block sides
std::string textureFaces[6]; // -x,x, -y,y, -z,z std::string textureFaces[6]; // -x,x, -y,y, -z,z
std::vector<std::string> modelTextures = {}; std::vector<std::string> modelTextures = {};
std::vector<BoxModel> modelBoxes = {}; std::vector<BoxModel> modelBoxes = {};
std::vector<glm::vec3> modelExtraPoints = {}; //initially made for tetragons std::vector<glm::vec3> modelExtraPoints = {}; //initially made for tetragons
std::vector<UVRegion> modelUVs = {}; // boxes' tex-UVs also there std::vector<UVRegion> modelUVs = {}; // boxes' tex-UVs also there
/// @brief Light emission R, G, B, S (sky lights: sun, moon, radioactive clouds)
uint8_t emission[4] {0, 0, 0, 0}; uint8_t emission[4] {0, 0, 0, 0};
/// @brief Influences visible block sides for transparent blocks
uint8_t drawGroup = 0; uint8_t drawGroup = 0;
/// @brief Block model type
BlockModel model = BlockModel::block; BlockModel model = BlockModel::block;
/// @brief Does the block passing lights into itself
bool lightPassing = false; bool lightPassing = false;
/// @brief Does the block passing top-down sky lights into itself
bool skyLightPassing = false; bool skyLightPassing = false;
/// @brief Is the block a physical obstacle
bool obstacle = true; bool obstacle = true;
/// @brief Can the block be selected
bool selectable = true; bool selectable = true;
/// @brief Can the block be replaced with other.
/// Examples of replaceable blocks: air, flower, water
bool replaceable = false; bool replaceable = false;
/// @brief Can player destroy the block
bool breakable = true; bool breakable = true;
/// @brief Can the block be oriented different ways
bool rotatable = false; bool rotatable = false;
/// @brief Can the block exist without physical support be a solid block below
bool grounded = false; bool grounded = false;
/// @brief Turns off block item generation
bool hidden = false; bool hidden = false;
/// @brief Set of block physical hitboxes
std::vector<AABB> hitboxes; std::vector<AABB> hitboxes;
/// @brief Set of available block rotations (coord-systems)
BlockRotProfile rotations; BlockRotProfile rotations;
/// @brief Item will be picked on MMB click on the block
std::string pickingItem = name+BLOCK_ITEM_SUFFIX; std::string pickingItem = name+BLOCK_ITEM_SUFFIX;
std::string scriptName = name.substr(name.find(':')+1); /// @brief Block script name in blocks/ without extension
std::string scriptName = name.substr(name.find(':')+1);
/// @brief Default block layout will be used by hud.open_block(...)
std::string uiLayout = name; std::string uiLayout = name;
/// @brief Block inventory size. 0 - no inventory
uint inventorySize = 0; uint inventorySize = 0;
struct { struct {
/// @brief block runtime integer id
blockid_t id; blockid_t id;
/// @brief is the block completely opaque for render and raycast
bool solid = true; bool solid = true;
/// @brief does the block emit any lights
bool emissive = false; bool emissive = false;
/// @brief set of hitboxes sets with all coord-systems precalculated
std::vector<AABB> hitboxes[BlockRotProfile::MAX_COUNT]; std::vector<AABB> hitboxes[BlockRotProfile::MAX_COUNT];
/// @brief set of block callbacks flags
block_funcs_set funcsset {}; block_funcs_set funcsset {};
/// @brief picking item integer id
itemid_t pickingItem = 0; itemid_t pickingItem = 0;
} rt; } rt;