From d431ca4daabd524cd5abf4926f84f860f661b09b Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 4 Mar 2020 10:48:53 +0100 Subject: [PATCH] Added index-based remove chunk function, also fixed remove_chunk, now it properly removes the given chunk from the chunk map. --- world/voxel_world.cpp | 19 ++++++++++++++++--- world/voxel_world.h | 7 +++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/world/voxel_world.cpp b/world/voxel_world.cpp index d9843a0..388ae0c 100644 --- a/world/voxel_world.cpp +++ b/world/voxel_world.cpp @@ -59,7 +59,6 @@ void VoxelWorld::set_data_margin_end(const int value) { _data_margin_end = value; } - int VoxelWorld::get_current_seed() const { return _current_seed; } @@ -167,9 +166,11 @@ VoxelChunk *VoxelWorld::get_chunk(const int x, const int y, const int z) const { return NULL; } VoxelChunk *VoxelWorld::remove_chunk(const int x, const int y, const int z) { - ERR_FAIL_COND_V(!_chunks.has(IntPos(x, y, z)), NULL); + IntPos pos(x, y, z); - VoxelChunk *chunk = _chunks.get(IntPos(x, y, z)); + ERR_FAIL_COND_V(!_chunks.has(pos), NULL); + + VoxelChunk *chunk = _chunks.get(pos); for (int i = 0; i < _chunks_vector.size(); ++i) { if (_chunks_vector.get(i) == chunk) { @@ -178,6 +179,17 @@ VoxelChunk *VoxelWorld::remove_chunk(const int x, const int y, const int z) { } } + _chunks.erase(pos); + + return chunk; +} +VoxelChunk *VoxelWorld::remove_chunk_index(const int index) { + ERR_FAIL_INDEX_V(index, _chunks_vector.size(), NULL); + + VoxelChunk *chunk = _chunks_vector.get(index); + _chunks_vector.remove(index); + _chunks.erase(IntPos(chunk->get_position())); + return chunk; } @@ -460,6 +472,7 @@ void VoxelWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("add_chunk", "chunk", "x", "y", "z"), &VoxelWorld::add_chunk_bind); ClassDB::bind_method(D_METHOD("get_chunk", "x", "y", "z"), &VoxelWorld::get_chunk); ClassDB::bind_method(D_METHOD("remove_chunk", "x", "y", "z"), &VoxelWorld::remove_chunk); + ClassDB::bind_method(D_METHOD("remove_chunk_index", "index"), &VoxelWorld::remove_chunk_index); ClassDB::bind_method(D_METHOD("get_chunk_index", "index"), &VoxelWorld::get_chunk_index); ClassDB::bind_method(D_METHOD("get_chunk_count"), &VoxelWorld::get_chunk_count); diff --git a/world/voxel_world.h b/world/voxel_world.h index 2ee946f..8a3b2a0 100644 --- a/world/voxel_world.h +++ b/world/voxel_world.h @@ -92,6 +92,7 @@ public: void add_chunk_bind(Node *chunk, const int x, const int y, const int z); VoxelChunk *get_chunk(const int x, const int y, const int z) const; VoxelChunk *remove_chunk(const int x, const int y, const int z); + VoxelChunk *remove_chunk_index(const int index); VoxelChunk *get_chunk_index(const int index); int get_chunk_count() const; @@ -141,6 +142,12 @@ public: y = p_y; z = p_z; } + + IntPos(const Vector3 &p) { + x = p.x; + y = p.y; + z = p.z; + } }; struct IntPosHasher {