Added a _chunk_added virtual method to voxel world.

This commit is contained in:
Relintai 2020-04-06 13:55:22 +02:00
parent b4c61ac674
commit c691c13277
3 changed files with 16 additions and 0 deletions

View File

@ -46,6 +46,14 @@ Ref<VoxelChunk> VoxelWorldDefault::_create_chunk(int x, int y, int z, Ref<VoxelC
return VoxelWorld::_create_chunk(x, y, z, chunk);
}
void VoxelWorldDefault::_chunk_added(Ref<VoxelChunk> chunk) {
Ref<VoxelChunkDefault> c = chunk;
if (c.is_valid()) {
c->set_build_flags(get_build_flags());
}
}
VoxelWorldDefault::VoxelWorldDefault() {
_build_flags = VoxelChunkDefault::BUILD_FLAG_CREATE_COLLIDER | VoxelChunkDefault::BUILD_FLAG_CREATE_LODS;
@ -57,6 +65,8 @@ VoxelWorldDefault ::~VoxelWorldDefault() {
}
void VoxelWorldDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("_chunk_added", "chunk"), &VoxelWorldDefault::_chunk_added);
ClassDB::bind_method(D_METHOD("get_build_flags"), &VoxelWorldDefault::get_build_flags);
ClassDB::bind_method(D_METHOD("set_build_flags", "value"), &VoxelWorldDefault::set_build_flags);
ADD_PROPERTY(PropertyInfo(Variant::INT, "build_flags", PROPERTY_HINT_FLAGS, VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS), "set_build_flags", "get_build_flags");

View File

@ -37,6 +37,7 @@ public:
protected:
Ref<VoxelChunk> _create_chunk(int x, int y, int z, Ref<VoxelChunk> p_chunk);
virtual void _chunk_added(Ref<VoxelChunk> chunk);
static void _bind_methods();

View File

@ -175,6 +175,9 @@ void VoxelWorld::add_chunk(Ref<VoxelChunk> chunk, const int x, const int y, cons
_chunks_vector.push_back(chunk);
chunk->enter_tree();
if (has_method("_chunk_added"))
call("_chunk_added", chunk);
}
bool VoxelWorld::has_chunk(const int x, const int y, const int z) const {
return _chunks.has(IntPos(x, y, z));
@ -622,6 +625,8 @@ void VoxelWorld::_bind_methods() {
ClassDB::bind_method(D_METHOD("clear_world_areas"), &VoxelWorld::clear_world_areas);
ClassDB::bind_method(D_METHOD("get_world_area_count"), &VoxelWorld::get_world_area_count);
BIND_VMETHOD(MethodInfo("_chunk_added", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk")));
ClassDB::bind_method(D_METHOD("add_chunk", "chunk", "x", "y", "z"), &VoxelWorld::add_chunk);
ClassDB::bind_method(D_METHOD("has_chunk", "x", "y", "z"), &VoxelWorld::has_chunk);
ClassDB::bind_method(D_METHOD("get_chunk", "x", "y", "z"), &VoxelWorld::get_chunk);