Added index-based remove chunk function, also fixed remove_chunk, now it properly removes the given chunk from the chunk map.

This commit is contained in:
Relintai 2020-03-04 10:48:53 +01:00
parent ac75c78be1
commit d431ca4daa
2 changed files with 23 additions and 3 deletions

View File

@ -59,7 +59,6 @@ void VoxelWorld::set_data_margin_end(const int value) {
_data_margin_end = value; _data_margin_end = value;
} }
int VoxelWorld::get_current_seed() const { int VoxelWorld::get_current_seed() const {
return _current_seed; return _current_seed;
} }
@ -167,9 +166,11 @@ VoxelChunk *VoxelWorld::get_chunk(const int x, const int y, const int z) const {
return NULL; return NULL;
} }
VoxelChunk *VoxelWorld::remove_chunk(const int x, const int y, const int z) { 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) { for (int i = 0; i < _chunks_vector.size(); ++i) {
if (_chunks_vector.get(i) == chunk) { 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; 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("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("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", "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_index", "index"), &VoxelWorld::get_chunk_index);
ClassDB::bind_method(D_METHOD("get_chunk_count"), &VoxelWorld::get_chunk_count); ClassDB::bind_method(D_METHOD("get_chunk_count"), &VoxelWorld::get_chunk_count);

View File

@ -92,6 +92,7 @@ public:
void add_chunk_bind(Node *chunk, const int x, const int y, const int z); 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 *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(const int x, const int y, const int z);
VoxelChunk *remove_chunk_index(const int index);
VoxelChunk *get_chunk_index(const int index); VoxelChunk *get_chunk_index(const int index);
int get_chunk_count() const; int get_chunk_count() const;
@ -141,6 +142,12 @@ public:
y = p_y; y = p_y;
z = p_z; z = p_z;
} }
IntPos(const Vector3 &p) {
x = p.x;
y = p.y;
z = p.z;
}
}; };
struct IntPosHasher { struct IntPosHasher {