From f14faead25b545b52b0771563dd31b180fc49d75 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 25 Oct 2020 17:05:15 +0100 Subject: [PATCH] Add voxel structure storage to VoxelChunk aswell. --- world/voxel_chunk.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ world/voxel_chunk.h | 15 ++++++++++ 2 files changed, 81 insertions(+) diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index 022edca..98c6e54 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -29,6 +29,7 @@ SOFTWARE. #include "../defines.h" #include "jobs/voxel_job.h" +#include "voxel_structure.h" #if THREAD_POOL_PRESENT #include "../../thread_pool/thread_pool.h" @@ -566,6 +567,59 @@ _FORCE_INLINE_ int VoxelChunk::get_data_size() const { return _data_size_x * _data_size_y * _data_size_z; } +//Voxel Structures + +Ref VoxelChunk::voxel_structure_get(const int index) const { + ERR_FAIL_INDEX_V(index, _voxel_structures.size(), Ref()); + + return _voxel_structures.get(index); +} +void VoxelChunk::voxel_structure_add(const Ref &structure) { + _voxel_structures.push_back(structure); +} +void VoxelChunk::voxel_structure_remove(const Ref &structure) { + if (!structure.is_valid()) + return; + + int index = _voxel_structures.find(structure); + + if (index != -1) + _voxel_structures.remove(index); +} +void VoxelChunk::voxel_structure_remove_index(const int index) { + ERR_FAIL_INDEX(index, _voxel_structures.size()); + + _voxel_structures.remove(index); +} +void VoxelChunk::voxel_structure_clear() { + _voxel_structures.clear(); +} +int VoxelChunk::voxel_structure_get_count() const { + return _voxel_structures.size(); +} +void VoxelChunk::voxel_structure_add_at_position(Ref structure, const Vector3 &world_position) { + ERR_FAIL_COND(!structure.is_valid()); + + structure->set_position_x(static_cast(world_position.x / _voxel_scale)); + structure->set_position_y(static_cast(world_position.y / _voxel_scale)); + structure->set_position_z(static_cast(world_position.z / _voxel_scale)); + + voxel_structure_add(structure); +} + +Vector VoxelChunk::voxel_structures_get() { + VARIANT_ARRAY_GET(_voxel_structures); +} +void VoxelChunk::voxel_structures_set(const Vector &structures) { + voxel_structure_clear(); + + for (int i = 0; i < structures.size(); ++i) { + Ref structure = Ref(structures[i]); + + voxel_structure_add(structure); + } +} + void VoxelChunk::build() { ERR_FAIL_COND(!INSTANCE_VALIDATE(get_voxel_world())); ERR_FAIL_COND(!get_voxel_world()->is_inside_tree()); @@ -1308,6 +1362,18 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("get_data_index", "x", "y", "z"), &VoxelChunk::get_data_index); ClassDB::bind_method(D_METHOD("get_data_size"), &VoxelChunk::get_data_size); + ClassDB::bind_method(D_METHOD("voxel_structure_get", "index"), &VoxelChunk::voxel_structure_get); + ClassDB::bind_method(D_METHOD("voxel_structure_add", "structure"), &VoxelChunk::voxel_structure_add); + ClassDB::bind_method(D_METHOD("voxel_structure_remove", "structure"), &VoxelChunk::voxel_structure_remove); + ClassDB::bind_method(D_METHOD("voxel_structure_remove_index", "index"), &VoxelChunk::voxel_structure_remove_index); + ClassDB::bind_method(D_METHOD("voxel_structure_clear"), &VoxelChunk::voxel_structure_clear); + ClassDB::bind_method(D_METHOD("voxel_structure_get_count"), &VoxelChunk::voxel_structure_get_count); + ClassDB::bind_method(D_METHOD("voxel_structure_add_at_position", "structure", "world_position"), &VoxelChunk::voxel_structure_add_at_position); + + ClassDB::bind_method(D_METHOD("voxel_structures_get"), &VoxelChunk::voxel_structures_get); + ClassDB::bind_method(D_METHOD("voxel_structures_set"), &VoxelChunk::voxel_structures_set); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "voxel_structures", PROPERTY_HINT_NONE, "17/17:VoxelStructure", PROPERTY_USAGE_DEFAULT, "VoxelStructure"), "voxel_structures_set", "voxel_structures_get"); + //Meshes #if PROPS_PRESENT diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index ef91fe6..21e94c5 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -62,6 +62,7 @@ include_pool_vector class VoxelJob; class VoxelWorld; +class VoxelStructure; class VoxelChunk : public Resource { GDCLASS(VoxelChunk, Resource); @@ -185,6 +186,18 @@ public: int get_data_index(const int x, const int y, const int z) const; int get_data_size() const; + //Voxel Structures + Ref voxel_structure_get(const int index) const; + void voxel_structure_add(const Ref &structure); + void voxel_structure_remove(const Ref &structure); + void voxel_structure_remove_index(const int index); + void voxel_structure_clear(); + int voxel_structure_get_count() const; + void voxel_structure_add_at_position(Ref structure, const Vector3 &world_position); + + Vector voxel_structures_get(); + void voxel_structures_set(const Vector &structures); + //Meshing void build(); void clear(); @@ -355,6 +368,8 @@ protected: Ref _library; + Vector > _voxel_structures; + #if PROPS_PRESENT Vector _props; #endif