From 5c0ab277155d53cf5b709b53b12fac97f17e0251 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 17 Apr 2020 10:15:50 +0200 Subject: [PATCH] Added 3 more helper functions to voxel world. --- world/voxel_world.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++- world/voxel_world.h | 4 +++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/world/voxel_world.cpp b/world/voxel_world.cpp index 12ec5b9..2120e66 100644 --- a/world/voxel_world.cpp +++ b/world/voxel_world.cpp @@ -259,7 +259,7 @@ Ref VoxelWorld::get_chunk(const int x, const int y, const int z) { if (_chunks.has(pos)) return _chunks.get(pos); - return NULL; + return Ref(); } Ref VoxelWorld::remove_chunk(const int x, const int y, const int z) { IntPos pos(x, y, z); @@ -562,6 +562,38 @@ void VoxelWorld::set_lights(const Vector &chunks) { } } +uint8_t VoxelWorld::get_voxel_at_world_position(const Vector3 &world_position, const int channel_index) { + Vector3 pos = world_position / get_voxel_scale(); + + //Note: floor is needed to handle negative numbers proiberly + int x = static_cast(Math::floor(pos.x / get_chunk_size_x() / get_voxel_scale())); + int y = static_cast(Math::floor(pos.y / get_chunk_size_y() / get_voxel_scale())); + int z = static_cast(Math::floor(pos.z / get_chunk_size_z() / get_voxel_scale())); + + int bx = static_cast(Math::floor(pos.x / get_voxel_scale())) % get_chunk_size_x(); + int by = static_cast(Math::floor(pos.y / get_voxel_scale())) % get_chunk_size_y(); + int bz = static_cast(Math::floor(pos.z / get_voxel_scale())) % get_chunk_size_z(); + + if (bx < 0) { + bx += get_chunk_size_x(); + } + + if (by < 0) { + by += get_chunk_size_y(); + } + + if (bz < 0) { + bz += get_chunk_size_z(); + } + + Ref chunk = get_chunk(x, y, z); + + if (chunk.is_valid()) + return chunk->get_voxel(bx, by, bz, channel_index); + + return 0; +} + void VoxelWorld::set_voxel_at_world_position(const Vector3 &world_position, const uint8_t data, const int channel_index) { Vector3 pos = world_position / get_voxel_scale(); @@ -631,6 +663,27 @@ void VoxelWorld::set_voxel_at_world_position(const Vector3 &world_position, cons chunk->build(); } +Ref VoxelWorld::get_chunk_at_world_position(const Vector3 &world_position) { + Vector3 pos = world_position / get_voxel_scale(); + + //Note: floor is needed to handle negative numbers proiberly + int x = static_cast(Math::floor(pos.x / get_chunk_size_x() / get_voxel_scale())); + int y = static_cast(Math::floor(pos.y / get_chunk_size_y() / get_voxel_scale())); + int z = static_cast(Math::floor(pos.z / get_chunk_size_z() / get_voxel_scale())); + + return get_chunk(x, y, z); +} +Ref VoxelWorld::get_or_create_chunk_at_world_position(const Vector3 &world_position) { + Vector3 pos = world_position / get_voxel_scale(); + + //Note: floor is needed to handle negative numbers proiberly + int x = static_cast(Math::floor(pos.x / get_chunk_size_x() / get_voxel_scale())); + int y = static_cast(Math::floor(pos.y / get_chunk_size_y() / get_voxel_scale())); + int z = static_cast(Math::floor(pos.z / get_chunk_size_z() / get_voxel_scale())); + + return get_or_create_chunk(x, y, z); +} + int VoxelWorld::get_channel_index_info(const VoxelWorld::ChannelTypeInfo channel_type) { return call("_get_channel_index_info", channel_type); } @@ -952,7 +1005,10 @@ void VoxelWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("get_lights"), &VoxelWorld::get_lights); ClassDB::bind_method(D_METHOD("set_lights", "chunks"), &VoxelWorld::set_lights); + ClassDB::bind_method(D_METHOD("get_voxel_at_world_position", "world_position", "channel_index"), &VoxelWorld::get_voxel_at_world_position); ClassDB::bind_method(D_METHOD("set_voxel_at_world_position", "world_position", "data", "channel_index"), &VoxelWorld::set_voxel_at_world_position); + ClassDB::bind_method(D_METHOD("get_chunk_at_world_position", "world_position"), &VoxelWorld::get_chunk_at_world_position); + ClassDB::bind_method(D_METHOD("get_or_create_chunk_at_world_position", "world_position"), &VoxelWorld::get_or_create_chunk_at_world_position); BIND_VMETHOD(MethodInfo("_get_channel_index_info", PropertyInfo(Variant::INT, "channel_type", PROPERTY_HINT_ENUM, BINDING_STRING_CHANNEL_TYPE_INFO))); diff --git a/world/voxel_world.h b/world/voxel_world.h index 2ed1261..a6befd7 100644 --- a/world/voxel_world.h +++ b/world/voxel_world.h @@ -180,7 +180,11 @@ public: void set_lights(const Vector &chunks); //Helpers + uint8_t get_voxel_at_world_position(const Vector3 &world_position, const int channel_index); void set_voxel_at_world_position(const Vector3 &world_position, const uint8_t data, const int channel_index); + Ref get_chunk_at_world_position(const Vector3 &world_position); + Ref get_or_create_chunk_at_world_position(const Vector3 &world_position); + int get_channel_index_info(const ChannelTypeInfo channel_type); VoxelWorld();