VoxelChunk is s Spatial now (again).

This commit is contained in:
Relintai 2019-11-06 03:37:22 +01:00
parent 9bc8ccd0e7
commit 3f7b422662
4 changed files with 42 additions and 34 deletions

View File

@ -278,7 +278,7 @@ void VoxelChunk::next_phase() {
if (_current_build_phase >= BUILD_PHASE_MAX) { if (_current_build_phase >= BUILD_PHASE_MAX) {
_current_build_phase = BUILD_PHASE_DONE; _current_build_phase = BUILD_PHASE_DONE;
emit_signal("mesh_generation_finished", Ref<VoxelChunk>(this)); emit_signal("mesh_generation_finished", this);
return; return;
} }

View File

@ -1,9 +1,9 @@
#ifndef VOXEL_CHUNK_H #ifndef VOXEL_CHUNK_H
#define VOXEL_CHUNK_H #define VOXEL_CHUNK_H
#include "scene/3d/spatial.h"
#include "core/engine.h" #include "core/engine.h"
#include "core/object.h"
#include "core/reference.h"
#include "core/ustring.h" #include "core/ustring.h"
#include "scene/3d/mesh_instance.h" #include "scene/3d/mesh_instance.h"
#include "scene/resources/packed_scene.h" #include "scene/resources/packed_scene.h"
@ -35,8 +35,8 @@
class VoxelWorld; class VoxelWorld;
class VoxelChunk : public Reference { class VoxelChunk : public Spatial {
GDCLASS(VoxelChunk, Reference); GDCLASS(VoxelChunk, Spatial);
public: public:
//Properties //Properties

View File

@ -31,10 +31,10 @@ void VoxelWorld::set_library(const Ref<VoxelmanLibrary> library) {
} }
Ref<VoxelmanLevelGenerator> VoxelWorld::get_level_generator() const { Ref<VoxelmanLevelGenerator> VoxelWorld::get_level_generator() const {
return _level_generator; return _level_generator;
} }
void VoxelWorld::set_level_generator(const Ref<VoxelmanLevelGenerator> level_generator) { void VoxelWorld::set_level_generator(const Ref<VoxelmanLevelGenerator> level_generator) {
_level_generator = level_generator; _level_generator = level_generator;
} }
float VoxelWorld::get_voxel_scale() const { float VoxelWorld::get_voxel_scale() const {
@ -69,36 +69,43 @@ void VoxelWorld::set_player_bind(Node *player) {
set_player(Object::cast_to<Spatial>(player)); set_player(Object::cast_to<Spatial>(player));
} }
void VoxelWorld::add_chunk(Ref<VoxelChunk> 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); chunk->set_chunk_position(x, y, z);
_chunks.set(Vector3i(x, y, z), chunk); _chunks.set(Vector3i(x, y, z), chunk);
_chunks_vector.push_back(chunk); _chunks_vector.push_back(chunk);
} }
Ref<VoxelChunk> VoxelWorld::get_chunk(const int x, const int y, const int z) const { void VoxelWorld::add_chunk_bind(Node *chunk, const int x, const int y, const int z) {
const Ref<VoxelChunk> *chunk = _chunks.getptr(Vector3i(x, y, z)); VoxelChunk *v = Object::cast_to<VoxelChunk>(chunk);
return Ref<VoxelChunk>(chunk); ERR_FAIL_COND(!ObjectDB::instance_validate(v));
add_chunk(v, x, y, z);
} }
Ref<VoxelChunk> VoxelWorld::remove_chunk(const int x, const int y, const int z) { VoxelChunk *VoxelWorld::get_chunk(const int x, const int y, const int z) const {
Ref<VoxelChunk> *chunk = _chunks.getptr(Vector3i(x, y, z)); if (_chunks.has(Vector3i(x, y, z)))
return _chunks.get(Vector3i(x, y, z));
Ref<VoxelChunk> 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()) { VoxelChunk *chunk = _chunks.get(Vector3i(x, y, z));
for (int i = 0; i < _chunks_vector.size(); ++i) { for (int i = 0; i < _chunks_vector.size(); ++i) {
if (_chunks_vector.get(i) == c) { if (_chunks_vector.get(i) == chunk) {
_chunks_vector.remove(i); _chunks_vector.remove(i);
break; break;
}
} }
} }
return c; return chunk;
} }
Ref<VoxelChunk> 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); return _chunks_vector.get(index);
} }
int VoxelWorld::get_chunk_count() const { int VoxelWorld::get_chunk_count() const {
@ -107,7 +114,7 @@ int VoxelWorld::get_chunk_count() const {
void VoxelWorld::clear_chunks() { void VoxelWorld::clear_chunks() {
for (int i = 0; i < _chunks_vector.size(); ++i) { for (int i = 0; i < _chunks_vector.size(); ++i) {
_chunks_vector.get(i)->free_chunk(); _chunks_vector.get(i)->queue_delete();
} }
_chunks_vector.clear(); _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("get_library"), &VoxelWorld::get_library);
ClassDB::bind_method(D_METHOD("set_library", "library"), &VoxelWorld::set_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"); 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); 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"); 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("get_voxel_scale"), &VoxelWorld::get_voxel_scale);
ClassDB::bind_method(D_METHOD("set_voxel_scale", "value"), &VoxelWorld::set_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"); 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); 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"); 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("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);

View File

@ -43,11 +43,12 @@ public:
void set_player(Spatial *player); void set_player(Spatial *player);
void set_player_bind(Node *player); void set_player_bind(Node *player);
void add_chunk(Ref<VoxelChunk> chunk, const int x, const int y, const int z); void add_chunk(VoxelChunk *chunk, const int x, const int y, const int z);
Ref<VoxelChunk> get_chunk(const int x, const int y, const int z) const; void add_chunk_bind(Node *chunk, const int x, const int y, const int z);
Ref<VoxelChunk> remove_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<VoxelChunk> get_chunk_index(const int index); VoxelChunk *get_chunk_index(const int index);
int get_chunk_count() const; int get_chunk_count() const;
void clear_chunks(); void clear_chunks();
@ -65,8 +66,8 @@ private:
float _voxel_scale; float _voxel_scale;
int _chunk_spawn_range; int _chunk_spawn_range;
HashMap<Vector3i, Ref<VoxelChunk>, Vector3iHasher> _chunks; HashMap<Vector3i, VoxelChunk *, Vector3iHasher> _chunks;
Vector<Ref<VoxelChunk> > _chunks_vector; Vector<VoxelChunk *> _chunks_vector;
NodePath _player_path; NodePath _player_path;
Spatial *_player; Spatial *_player;