From 3f7b4226629934d3c2532abdd1c09c7d74406dde Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 6 Nov 2019 03:37:22 +0100 Subject: [PATCH] VoxelChunk is s Spatial now (again). --- world/voxel_chunk.cpp | 2 +- world/voxel_chunk.h | 8 +++---- world/voxel_world.cpp | 53 ++++++++++++++++++++++++------------------- world/voxel_world.h | 13 ++++++----- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index 2465d3e..adc9513 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -278,7 +278,7 @@ void VoxelChunk::next_phase() { if (_current_build_phase >= BUILD_PHASE_MAX) { _current_build_phase = BUILD_PHASE_DONE; - emit_signal("mesh_generation_finished", Ref(this)); + emit_signal("mesh_generation_finished", this); return; } diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 279313d..9edcafc 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -1,9 +1,9 @@ #ifndef VOXEL_CHUNK_H #define VOXEL_CHUNK_H +#include "scene/3d/spatial.h" + #include "core/engine.h" -#include "core/object.h" -#include "core/reference.h" #include "core/ustring.h" #include "scene/3d/mesh_instance.h" #include "scene/resources/packed_scene.h" @@ -35,8 +35,8 @@ class VoxelWorld; -class VoxelChunk : public Reference { - GDCLASS(VoxelChunk, Reference); +class VoxelChunk : public Spatial { + GDCLASS(VoxelChunk, Spatial); public: //Properties diff --git a/world/voxel_world.cpp b/world/voxel_world.cpp index b0461c5..19afe84 100644 --- a/world/voxel_world.cpp +++ b/world/voxel_world.cpp @@ -31,10 +31,10 @@ void VoxelWorld::set_library(const Ref library) { } Ref VoxelWorld::get_level_generator() const { - return _level_generator; + return _level_generator; } void VoxelWorld::set_level_generator(const Ref level_generator) { - _level_generator = level_generator; + _level_generator = level_generator; } float VoxelWorld::get_voxel_scale() const { @@ -69,36 +69,43 @@ void VoxelWorld::set_player_bind(Node *player) { set_player(Object::cast_to(player)); } -void VoxelWorld::add_chunk(Ref chunk, const int x, const int y, const int z) { +void VoxelWorld::add_chunk(VoxelChunk *chunk, const int x, const int y, const int z) { chunk->set_chunk_position(x, y, z); _chunks.set(Vector3i(x, y, z), chunk); _chunks_vector.push_back(chunk); } -Ref VoxelWorld::get_chunk(const int x, const int y, const int z) const { - const Ref *chunk = _chunks.getptr(Vector3i(x, y, z)); +void VoxelWorld::add_chunk_bind(Node *chunk, const int x, const int y, const int z) { + VoxelChunk *v = Object::cast_to(chunk); - return Ref(chunk); + ERR_FAIL_COND(!ObjectDB::instance_validate(v)); + + add_chunk(v, x, y, z); } -Ref VoxelWorld::remove_chunk(const int x, const int y, const int z) { - Ref *chunk = _chunks.getptr(Vector3i(x, y, z)); +VoxelChunk *VoxelWorld::get_chunk(const int x, const int y, const int z) const { + if (_chunks.has(Vector3i(x, y, z))) + return _chunks.get(Vector3i(x, y, z)); - Ref c(chunk); + return NULL; +} +VoxelChunk *VoxelWorld::remove_chunk(const int x, const int y, const int z) { + ERR_FAIL_COND_V(!_chunks.has(Vector3i(x, y, z)), NULL); - if (c.is_valid()) { - - for (int i = 0; i < _chunks_vector.size(); ++i) { - if (_chunks_vector.get(i) == c) { - _chunks_vector.remove(i); - break; - } + VoxelChunk *chunk = _chunks.get(Vector3i(x, y, z)); + + for (int i = 0; i < _chunks_vector.size(); ++i) { + if (_chunks_vector.get(i) == chunk) { + _chunks_vector.remove(i); + break; } } - return c; + return chunk; } -Ref VoxelWorld::get_chunk_index(const int index) { +VoxelChunk *VoxelWorld::get_chunk_index(const int index) { + ERR_FAIL_INDEX_V(index, _chunks_vector.size(), NULL); + return _chunks_vector.get(index); } int VoxelWorld::get_chunk_count() const { @@ -107,7 +114,7 @@ int VoxelWorld::get_chunk_count() const { void VoxelWorld::clear_chunks() { for (int i = 0; i < _chunks_vector.size(); ++i) { - _chunks_vector.get(i)->free_chunk(); + _chunks_vector.get(i)->queue_delete(); } _chunks_vector.clear(); @@ -148,11 +155,11 @@ void VoxelWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("get_library"), &VoxelWorld::get_library); ClassDB::bind_method(D_METHOD("set_library", "library"), &VoxelWorld::set_library); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "VoxelmanLibrary"), "set_library", "get_library"); - - ClassDB::bind_method(D_METHOD("get_level_generator"), &VoxelWorld::get_level_generator); + + ClassDB::bind_method(D_METHOD("get_level_generator"), &VoxelWorld::get_level_generator); ClassDB::bind_method(D_METHOD("set_level_generator", "level_generator"), &VoxelWorld::set_level_generator); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "level_generator", PROPERTY_HINT_RESOURCE_TYPE, "VoxelmanLevelGenerator"), "set_level_generator", "get_level_generator"); - + ClassDB::bind_method(D_METHOD("get_voxel_scale"), &VoxelWorld::get_voxel_scale); ClassDB::bind_method(D_METHOD("set_voxel_scale", "value"), &VoxelWorld::set_voxel_scale); ADD_PROPERTY(PropertyInfo(Variant::REAL, "voxel_scale"), "set_voxel_scale", "get_voxel_scale"); @@ -169,7 +176,7 @@ void VoxelWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("set_player", "player"), &VoxelWorld::set_player_bind); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "player", PROPERTY_HINT_RESOURCE_TYPE, "Spatial"), "set_player", "get_player"); - ClassDB::bind_method(D_METHOD("add_chunk", "chunk", "x", "y", "z"), &VoxelWorld::add_chunk); + 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); diff --git a/world/voxel_world.h b/world/voxel_world.h index b03e56e..42e5484 100644 --- a/world/voxel_world.h +++ b/world/voxel_world.h @@ -43,11 +43,12 @@ public: void set_player(Spatial *player); void set_player_bind(Node *player); - void add_chunk(Ref chunk, const int x, const int y, const int z); - Ref get_chunk(const int x, const int y, const int z) const; - Ref remove_chunk(const int x, const int y, const int z); + void add_chunk(VoxelChunk *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 *remove_chunk(const int x, const int y, const int z); - Ref get_chunk_index(const int index); + VoxelChunk *get_chunk_index(const int index); int get_chunk_count() const; void clear_chunks(); @@ -65,8 +66,8 @@ private: float _voxel_scale; int _chunk_spawn_range; - HashMap, Vector3iHasher> _chunks; - Vector > _chunks_vector; + HashMap _chunks; + Vector _chunks_vector; NodePath _player_path; Spatial *_player;