godot_voxel/voxel_library.cpp

126 lines
3.3 KiB
C++
Raw Permalink Normal View History

#include "voxel_library.h"
VoxelLibrary::VoxelLibrary() :
2019-04-24 02:29:47 +02:00
Resource(),
_atlas_size(1) {
}
VoxelLibrary::~VoxelLibrary() {
// Handled with a WeakRef
// for (unsigned int i = 0; i < MAX_VOXEL_TYPES; ++i) {
// if (_voxel_types[i].is_valid()) {
// _voxel_types[i]->set_library(NULL);
// }
// }
}
int VoxelLibrary::get_voxel_count() const {
int count = 0;
2019-04-24 02:29:47 +02:00
for (int i = 0; i < MAX_VOXEL_TYPES; ++i) {
if (_voxel_types[i].is_valid())
++count;
}
return count;
}
void VoxelLibrary::load_default() {
create_voxel(0, "air")->set_transparent(true);
create_voxel(1, "solid")
->set_transparent(false)
->set_geometry_type(Voxel::GEOMETRY_CUBE);
}
// TODO Add a way to add voxels
bool VoxelLibrary::_set(const StringName &p_name, const Variant &p_value) {
2019-04-24 02:29:47 +02:00
// if(p_name == "voxels/max") {
2019-04-24 02:29:47 +02:00
// int v = p_value;
// _max_count = CLAMP(v, 0, MAX_VOXEL_TYPES);
// for(int i = _max_count; i < MAX_VOXEL_TYPES; ++i) {
// _voxel_types[i] = Ref<Voxel>();
// return true;
// }
2019-04-24 02:29:47 +02:00
// } else
if (p_name.operator String().begins_with("voxels/")) {
int idx = p_name.operator String().get_slicec('/', 1).to_int();
if (idx >= 0 && idx < MAX_VOXEL_TYPES) {
Ref<Voxel> voxel = p_value;
_voxel_types[idx] = voxel;
2019-04-24 02:29:47 +02:00
if (voxel.is_valid()) {
voxel->set_library(Ref<VoxelLibrary>(this));
2017-08-15 21:37:08 +02:00
voxel->set_id(idx);
}
// Note: if the voxel is set to null, we could set the previous one's library reference to null.
// however it Voxels use a weak reference, so it's not really needed
return true;
}
}
return false;
}
bool VoxelLibrary::_get(const StringName &p_name, Variant &r_ret) const {
2019-04-24 02:29:47 +02:00
// if(p_name == "voxels/max") {
2019-04-24 02:29:47 +02:00
// r_ret = _max_count;
// return true;
2019-04-24 02:29:47 +02:00
// } else
if (p_name.operator String().begins_with("voxels/")) {
int idx = p_name.operator String().get_slicec('/', 1).to_int();
if (idx >= 0 && idx < MAX_VOXEL_TYPES) {
r_ret = _voxel_types[idx];
return true;
2017-01-01 04:40:16 +01:00
}
}
return false;
}
void VoxelLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
//p_list->push_back(PropertyInfo(Variant::INT, "voxels/max"));
//for(int i = 0; i < _max_count; ++i) {
2019-04-24 02:29:47 +02:00
for (int i = 0; i < MAX_VOXEL_TYPES; ++i) {
p_list->push_back(PropertyInfo(Variant::OBJECT, "voxels/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "Voxel"));
}
}
void VoxelLibrary::set_atlas_size(int s) {
2017-01-01 04:40:16 +01:00
ERR_FAIL_COND(s <= 0);
_atlas_size = s;
}
Ref<Voxel> VoxelLibrary::create_voxel(int id, String name) {
2017-01-01 04:40:16 +01:00
ERR_FAIL_COND_V(id < 0 || id >= MAX_VOXEL_TYPES, Ref<Voxel>());
Ref<Voxel> voxel(memnew(Voxel));
voxel->set_library(Ref<VoxelLibrary>(this));
2017-01-01 04:40:16 +01:00
voxel->set_id(id);
voxel->set_voxel_name(name);
2017-01-01 04:40:16 +01:00
_voxel_types[id] = voxel;
return voxel;
}
2017-01-01 05:23:22 +01:00
Ref<Voxel> VoxelLibrary::_get_voxel_bind(int id) {
ERR_FAIL_COND_V(id < 0 || id >= MAX_VOXEL_TYPES, Ref<Voxel>());
return _voxel_types[id];
}
void VoxelLibrary::_bind_methods() {
2017-08-13 00:08:53 +02:00
ClassDB::bind_method(D_METHOD("create_voxel", "id", "name"), &VoxelLibrary::create_voxel);
2017-03-25 01:23:36 +01:00
ClassDB::bind_method(D_METHOD("get_voxel", "id"), &VoxelLibrary::_get_voxel_bind);
2017-01-01 05:23:22 +01:00
2017-03-25 01:23:36 +01:00
ClassDB::bind_method(D_METHOD("set_atlas_size", "square_size"), &VoxelLibrary::set_atlas_size);
ClassDB::bind_method(D_METHOD("get_atlas_size"), &VoxelLibrary::get_atlas_size);
ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_size"), "set_atlas_size", "get_atlas_size");
}