mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(block): add block texture types and sounds
Introduce BlockTextureType and BlockSound, parse texture and sound configs from block JSON, and add data() accessors to BlockManager.
This commit is contained in:
@@ -42,6 +42,18 @@ struct LookBlock {
|
||||
glm::ivec3 normal;
|
||||
};
|
||||
|
||||
enum class BlockTextureType {
|
||||
CUBOID,
|
||||
CROSS
|
||||
|
||||
};
|
||||
|
||||
struct BlockSound {
|
||||
std::optional<ResourceLocation> break_s;
|
||||
std::optional<ResourceLocation> place;
|
||||
std::optional<ResourceLocation> walk;
|
||||
};
|
||||
|
||||
struct BlockData {
|
||||
ResourceLocation name{};
|
||||
BlockType id = 0;
|
||||
@@ -57,6 +69,14 @@ struct BlockData {
|
||||
bool is_blend = false;
|
||||
bool is_transitional = false;
|
||||
float roughness = 1.0f;
|
||||
|
||||
BlockTextureType texture_type = BlockTextureType::CUBOID;
|
||||
|
||||
std::optional<ResourceLocation> texture_path;
|
||||
std::optional<ResourceLocation> normal;
|
||||
|
||||
BlockSound sound;
|
||||
|
||||
BlockData() = default;
|
||||
};
|
||||
|
||||
|
||||
@@ -27,6 +27,10 @@ public:
|
||||
static BlockType id_from_name(std::string_view name);
|
||||
static BlockType id_from_name(const ResourceLocation& name);
|
||||
|
||||
static const BlockData& data(std::string_view name);
|
||||
static const BlockData& data(const ResourceLocation& location);
|
||||
static const BlockData& data(BlockType type);
|
||||
|
||||
private:
|
||||
using BlockMap = tbb::concurrent_hash_map<BlockType, BlockData>;
|
||||
using acc = BlockMap::accessor;
|
||||
|
||||
@@ -171,6 +171,7 @@ void BlockManager::init() {
|
||||
data.name.to_string());
|
||||
continue;
|
||||
}
|
||||
|
||||
auto& properties = doc["properties"];
|
||||
|
||||
Tools::get_json_value(properties, "is_liquid", data.is_liquid);
|
||||
@@ -186,6 +187,49 @@ void BlockManager::init() {
|
||||
data.is_transitional);
|
||||
Tools::get_json_value(properties, "roughness", data.roughness);
|
||||
|
||||
if (doc.HasMember("texture") && doc["texture"].IsObject()) {
|
||||
auto& texture = doc["texture"];
|
||||
std::string s;
|
||||
if (Tools::get_json_value(texture, "type", s)) {
|
||||
if (s == "cuboid") {
|
||||
data.texture_type = BlockTextureType::CUBOID;
|
||||
} else if (s == "cross") {
|
||||
data.texture_type = BlockTextureType::CROSS;
|
||||
} else {
|
||||
Logger::error("Block {} texture type {} unknown",
|
||||
data.name.to_string(), s);
|
||||
ASSERT(false);
|
||||
}
|
||||
} else {
|
||||
Logger::error("Block {} need texture type",
|
||||
data.name.to_string());
|
||||
ASSERT(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Tools::get_json_value(texture, "path", s)) {
|
||||
data.texture_path = ResourceLocation::parse(s);
|
||||
}
|
||||
|
||||
if (Tools::get_json_value(texture, "normal", s)) {
|
||||
data.normal = ResourceLocation::parse(s);
|
||||
}
|
||||
}
|
||||
|
||||
if (doc.HasMember("sounds") && doc["sounds"].IsObject()) {
|
||||
auto& sound = doc["sounds"];
|
||||
std::string s;
|
||||
if (Tools::get_json_value(sound, "break", s)) {
|
||||
data.sound.break_s = ResourceLocation::parse(s);
|
||||
}
|
||||
if (Tools::get_json_value(sound, "walk", s)) {
|
||||
data.sound.walk = ResourceLocation::parse(s);
|
||||
}
|
||||
if (Tools::get_json_value(sound, "place", s)) {
|
||||
data.sound.place = ResourceLocation::parse(s);
|
||||
}
|
||||
}
|
||||
|
||||
const auto LOCATION = data.name;
|
||||
const auto IS_CROSS_PLANE = data.is_cross_plane;
|
||||
const auto ID = data.id;
|
||||
@@ -231,6 +275,37 @@ BlockType BlockManager::id_from_name(const ResourceLocation& name) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const BlockData& BlockManager::data(std::string_view name) {
|
||||
auto s = ResourceLocation::parse(name);
|
||||
if (!s) {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
return data(*s);
|
||||
}
|
||||
|
||||
const BlockData& BlockManager::data(const ResourceLocation& location) {
|
||||
|
||||
IDMap::const_accessor cacc;
|
||||
if (!m_id_map.find(cacc, location)) {
|
||||
Logger::error("Can't find block {} id", location.to_string());
|
||||
ASSERT(false);
|
||||
return EMPTY;
|
||||
}
|
||||
return data(cacc->second);
|
||||
}
|
||||
|
||||
const BlockData& BlockManager::data(BlockType type) {
|
||||
cacc c;
|
||||
if (!m_datas.find(c, type)) {
|
||||
Logger::error("Can't find block {} data", type);
|
||||
ASSERT(false);
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
return c->second;
|
||||
}
|
||||
|
||||
void BlockManager::set_up_cross_plane_map(
|
||||
const std::vector<std::pair<bool, BlockType>>& types) {
|
||||
unsigned cur_id = 0;
|
||||
|
||||
Reference in New Issue
Block a user