add variants (WIP)

This commit is contained in:
MihailRis
2025-07-13 21:28:57 +03:00
parent 01181b6403
commit 5478d121f0
16 changed files with 275 additions and 177 deletions
+14 -16
View File
@@ -108,34 +108,32 @@ const BlockRotProfile BlockRotProfile::STAIRS {
};
Block::Block(const std::string& name)
: name(name),
caption(util::id_to_caption(name)),
textureFaces {
TEXTURE_NOTFOUND,
TEXTURE_NOTFOUND,
TEXTURE_NOTFOUND,
TEXTURE_NOTFOUND,
TEXTURE_NOTFOUND,
TEXTURE_NOTFOUND
} {
: name(name), caption(util::id_to_caption(name)) {
for (int i = 0; i < defaults.textureFaces.size(); i++) {
defaults.textureFaces[i] = TEXTURE_NOTFOUND;
}
}
Block::~Block() {}
Block::~Block() = default;
Block::Block(std::string name, const std::string& texture)
: name(std::move(name)),
textureFaces {texture, texture, texture, texture, texture, texture} {
: name(std::move(name)) {
for (int i = 0; i < defaults.textureFaces.size(); i++) {
defaults.textureFaces[i] = TEXTURE_NOTFOUND;
}
}
void Block::cloneTo(Block& dst) {
dst.caption = caption;
for (int i = 0; i < 6; i++) {
dst.textureFaces[i] = textureFaces[i];
dst.defaults = defaults;
}
if (variants) {
dst.variants = std::make_unique<Variants>(*variants);
}
dst.material = material;
std::copy(&emission[0], &emission[3], dst.emission);
dst.size = size;
dst.model = model;
dst.drawGroup = drawGroup;
dst.lightPassing = lightPassing;
dst.skyLightPassing = skyLightPassing;
dst.shadeless = shadeless;
+31 -11
View File
@@ -10,6 +10,7 @@
#include "maths/aabb.hpp"
#include "typedefs.hpp"
#include "util/EnumMetadata.hpp"
#include "util/stack_vector.hpp"
#include "interfaces/Serializable.hpp"
struct ParticlesPreset;
@@ -33,6 +34,8 @@ inline constexpr uint BLOCK_AABB_GRID = 16;
inline constexpr size_t MAX_USER_BLOCK_FIELDS_SIZE = 240;
inline constexpr int BLOCK_MAX_VARIANTS = 16;
inline std::string DEFAULT_MATERIAL = "base:stone";
struct BlockFuncsSet {
@@ -141,6 +144,21 @@ struct BlockMaterial : Serializable {
void deserialize(const dv::value& src) override;
};
struct Variant {
/// @brief Block model
BlockModel model {};
/// @brief Textures set applied to block sides
std::array<std::string, 6> textureFaces; // -x,x, -y,y, -z,z
/// @brief Culling mode
CullingMode culling = CullingMode::DEFAULT;
/// @brief Influences visible block sides for transparent blocks
uint8_t drawGroup = 0;
};
struct Variants {
util::stack_vector<Variant, BLOCK_MAX_VARIANTS> variants;
};
/// @brief Block properties definition
class Block {
public:
@@ -149,8 +167,7 @@ public:
std::string caption;
/// @brief Textures set applied to block sides
std::array<std::string, 6> textureFaces; // -x,x, -y,y, -z,z
Variant defaults {};
dv::value properties = nullptr;
@@ -163,15 +180,6 @@ public:
glm::i8vec3 size {1, 1, 1};
/// @brief Influences visible block sides for transparent blocks
uint8_t drawGroup = 0;
/// @brief Block model
BlockModel model {};
/// @brief Culling mode
CullingMode culling = CullingMode::DEFAULT;
/// @brief Does the block passing lights into itself
bool lightPassing = false;
@@ -243,6 +251,8 @@ public:
std::unique_ptr<ParticlesPreset> particles;
std::unique_ptr<Variants> variants;
/// @brief Runtime indices (content indexing results)
struct {
/// @brief block runtime integer id
@@ -276,6 +286,16 @@ public:
void cloneTo(Block& dst);
constexpr const Variant& getVariant(uint8_t bits) const {
if (bits == 0 || variants == nullptr)
return defaults;
return variants->variants[(bits - 1) % variants->variants.size()];
}
constexpr const BlockModel& getModel(uint8_t bits) const {
return getVariant(bits).model;
}
static bool isReservedBlockField(std::string_view view);
};
+9
View File
@@ -56,6 +56,15 @@ public:
return voxels[vox_index(bx - x, by - y, bz - z, w, d)].id;
}
inline voxel pickBlock(int bx, int by, int bz) const {
if (bx < x || by < y || bz < z || bx >= x + w || by >= y + h ||
bz >= z + d) {
return {BLOCK_VOID, {}};
}
return voxels[vox_index(bx - x, by - y, bz - z, w, d)];
}
inline light_t pickLight(int bx, int by, int bz) const {
if (bx < x || by < y || bz < z || bx >= x + w || by >= y + h ||
bz >= z + d) {