Use StringName for voxel names

master
Marc Gilleron 2020-07-26 18:37:34 +01:00
parent f9e2d9cc12
commit fd9122a0de
4 changed files with 25 additions and 3 deletions

View File

@ -449,6 +449,7 @@ void Voxel::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_collision_aabbs", "aabbs"), &Voxel::_b_set_collision_aabbs);
ClassDB::bind_method(D_METHOD("get_collision_aabbs"), &Voxel::_b_get_collision_aabbs);
// TODO Update to StringName in Godot 4
ADD_PROPERTY(PropertyInfo(Variant::STRING, "voxel_name"), "set_voxel_name", "get_voxel_name");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent"), "set_transparent", "is_transparent");

View File

@ -30,7 +30,7 @@ public:
// Properties
Ref<Voxel> set_voxel_name(String name);
_FORCE_INLINE_ String get_voxel_name() const { return _name; }
_FORCE_INLINE_ StringName get_voxel_name() const { return _name; }
Ref<Voxel> set_id(int id);
_FORCE_INLINE_ int get_id() const { return _id; }
@ -98,7 +98,6 @@ private:
void clear_geometry();
Ref<Voxel> set_cube_geometry(float sy = 1);
//Ref<Voxel> set_xquad_geometry(Vector2 atlas_pos);
Array _b_get_collision_aabbs() const;
void _b_set_collision_aabbs(Array array);
@ -108,7 +107,7 @@ private:
// Identifiers
int _id;
String _name; // TODO StringName?
StringName _name;
// Properties
int _material_id;

View File

@ -30,6 +30,16 @@ void VoxelLibrary::set_voxel_count(unsigned int type_count) {
_change_notify();
}
int VoxelLibrary::get_voxel_index_from_name(StringName name) const {
for (size_t i = 0; i < _voxel_types.size(); ++i) {
const Ref<Voxel> &v = _voxel_types[i];
if (v->get_name() == name) {
return i;
}
}
return -1;
}
void VoxelLibrary::load_default() {
set_voxel_count(2);
create_voxel(0, "air")->set_transparent(true);
@ -382,6 +392,9 @@ void VoxelLibrary::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_voxel_count", "count"), &VoxelLibrary::set_voxel_count);
ClassDB::bind_method(D_METHOD("get_voxel_count"), &VoxelLibrary::get_voxel_count);
ClassDB::bind_method(D_METHOD("get_voxel_index_from_name", "name"), &VoxelLibrary::get_voxel_index_from_name);
ClassDB::bind_method(D_METHOD("get_voxel_by_name", "name"), &VoxelLibrary::_b_get_voxel_by_name);
ClassDB::bind_method(D_METHOD("bake"), &VoxelLibrary::bake);
ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_size"), "set_atlas_size", "get_atlas_size");
@ -394,3 +407,9 @@ Ref<Voxel> VoxelLibrary::_b_get_voxel(unsigned int id) {
ERR_FAIL_COND_V(id >= _voxel_types.size(), Ref<Voxel>());
return _voxel_types[id];
}
Ref<Voxel> VoxelLibrary::_b_get_voxel_by_name(StringName name) {
int id = get_voxel_index_from_name(name);
ERR_FAIL_COND_V(id == -1, Ref<Voxel>());
return _voxel_types[id];
}

View File

@ -41,6 +41,8 @@ public:
bool get_side_pattern_occlusion(unsigned int pattern_a, unsigned int pattern_b) const;
int get_voxel_index_from_name(StringName name) const;
private:
void generate_side_culling_matrix();
@ -51,6 +53,7 @@ private:
void _get_property_list(List<PropertyInfo> *p_list) const;
Ref<Voxel> _b_get_voxel(unsigned int id);
Ref<Voxel> _b_get_voxel_by_name(StringName name);
private:
std::vector<Ref<Voxel> > _voxel_types;