godot_voxel/voxel_library.cpp

45 lines
1.2 KiB
C++
Raw Normal View History

#include "voxel_library.h"
2017-08-12 16:19:39 -07:00
VoxelLibrary::VoxelLibrary()
: Reference(), _atlas_size(1) {
2017-03-24 17:23:36 -07:00
// Defaults
2016-12-31 19:40:16 -08:00
create_voxel(0, "air")->set_transparent(true);
2017-03-24 17:23:36 -07:00
create_voxel(1, "solid")->set_transparent(false)->set_cube_geometry();
}
VoxelLibrary::~VoxelLibrary() {
2016-12-31 19:40:16 -08:00
for (unsigned int i = 0; i < MAX_VOXEL_TYPES; ++i) {
if (_voxel_types[i].is_valid()) {
_voxel_types[i]->set_library_ptr(NULL);
}
}
}
void VoxelLibrary::set_atlas_size(int s) {
2016-12-31 19:40:16 -08:00
ERR_FAIL_COND(s <= 0);
_atlas_size = s;
}
Ref<Voxel> VoxelLibrary::create_voxel(int id, String name) {
2016-12-31 19:40:16 -08:00
ERR_FAIL_COND_V(id < 0 || id >= MAX_VOXEL_TYPES, Ref<Voxel>());
Ref<Voxel> voxel(memnew(Voxel));
voxel->set_library_ptr(this);
voxel->set_id(id);
voxel->set_name(name);
_voxel_types[id] = voxel;
return voxel;
}
2016-12-31 20:23:22 -08: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-12 15:08:53 -07:00
ClassDB::bind_method(D_METHOD("create_voxel", "id", "name"), &VoxelLibrary::create_voxel);
2017-03-24 17:23:36 -07:00
ClassDB::bind_method(D_METHOD("get_voxel", "id"), &VoxelLibrary::_get_voxel_bind);
2016-12-31 20:23:22 -08:00
2017-03-24 17:23:36 -07:00
ClassDB::bind_method(D_METHOD("set_atlas_size", "square_size"), &VoxelLibrary::set_atlas_size);
}