Cleaned up VoxelStructure, and added an api for them into VoxelWorld.

This commit is contained in:
Relintai 2020-04-16 13:40:39 +02:00
parent dd0fa1d7ea
commit d985610574
4 changed files with 139 additions and 51 deletions

View File

@ -29,36 +29,42 @@ void VoxelStructure::set_use_aabb(const bool value) {
_use_aabb = value;
}
AABB VoxelStructure::get_aabb() const {
return _aabb;
AABB VoxelStructure::get_chunk_aabb() const {
return _chunk_aabb;
}
void VoxelStructure::set_aabb(const AABB &value) {
_aabb = value;
void VoxelStructure::set_chunk_aabb(const AABB &value) {
_chunk_aabb = value;
}
int VoxelStructure::get_world_position_x() const {
return _world_position_x;
int VoxelStructure::get_position_x() const {
return _position_x;
}
void VoxelStructure::set_world_position_x(const int value) {
_world_position_x = value;
void VoxelStructure::set_position_x(const int value) {
_position_x = value;
}
int VoxelStructure::get_world_position_y() const {
return _world_position_y;
int VoxelStructure::get_position_y() const {
return _position_y;
}
void VoxelStructure::set_world_position_y(const int value) {
_world_position_y = value;
void VoxelStructure::set_position_y(const int value) {
_position_y = value;
}
int VoxelStructure::get_world_position_z() const {
return _world_position_z;
int VoxelStructure::get_position_z() const {
return _position_z;
}
void VoxelStructure::set_world_position_z(const int value) {
_world_position_z = value;
void VoxelStructure::set_position_z(const int value) {
_position_z = value;
}
void VoxelStructure::write_to_chunk(Node *chunk) {
ERR_FAIL_COND(Object::cast_to<VoxelChunk>(chunk) == NULL);
void VoxelStructure::set_position(const int x, const int y, const int z) {
_position_x = x;
_position_y = y;
_position_z = z;
}
void VoxelStructure::write_to_chunk(Ref<VoxelChunk> chunk) {
ERR_FAIL_COND(!chunk.is_valid());
if (has_method("_write_to_chunk"))
call("_write_to_chunk", chunk);
@ -66,9 +72,9 @@ void VoxelStructure::write_to_chunk(Node *chunk) {
VoxelStructure::VoxelStructure() {
_use_aabb = true;
_world_position_x = 0;
_world_position_y = 0;
_world_position_z = 0;
_position_x = 0;
_position_y = 0;
_position_z = 0;
}
VoxelStructure::~VoxelStructure() {
@ -81,21 +87,23 @@ void VoxelStructure::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_use_aabb", "value"), &VoxelStructure::set_use_aabb);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_aabb"), "set_use_aabb", "get_use_aabb");
ClassDB::bind_method(D_METHOD("get_aabb"), &VoxelStructure::get_aabb);
ClassDB::bind_method(D_METHOD("set_aabb", "value"), &VoxelStructure::set_aabb);
ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb"), "set_aabb", "get_aabb");
ClassDB::bind_method(D_METHOD("get_chunk_aabb"), &VoxelStructure::get_chunk_aabb);
ClassDB::bind_method(D_METHOD("set_chunk_aabb", "value"), &VoxelStructure::set_chunk_aabb);
ADD_PROPERTY(PropertyInfo(Variant::AABB, "chunk_aabb"), "set_chunk_aabb", "get_chunk_aabb");
ClassDB::bind_method(D_METHOD("get_world_position_x"), &VoxelStructure::get_world_position_x);
ClassDB::bind_method(D_METHOD("set_world_position_x", "value"), &VoxelStructure::set_world_position_x);
ADD_PROPERTY(PropertyInfo(Variant::INT, "world_position_x"), "set_world_position_x", "get_world_position_x");
ClassDB::bind_method(D_METHOD("get_position_x"), &VoxelStructure::get_position_x);
ClassDB::bind_method(D_METHOD("set_position_x", "value"), &VoxelStructure::set_position_x);
ADD_PROPERTY(PropertyInfo(Variant::INT, "position_x"), "set_position_x", "get_position_x");
ClassDB::bind_method(D_METHOD("get_world_position_y"), &VoxelStructure::get_world_position_y);
ClassDB::bind_method(D_METHOD("set_world_position_y", "value"), &VoxelStructure::set_world_position_y);
ADD_PROPERTY(PropertyInfo(Variant::INT, "world_position_y"), "set_world_position_y", "get_world_position_y");
ClassDB::bind_method(D_METHOD("get_position_y"), &VoxelStructure::get_position_y);
ClassDB::bind_method(D_METHOD("set_position_y", "value"), &VoxelStructure::set_position_y);
ADD_PROPERTY(PropertyInfo(Variant::INT, "position_y"), "set_position_y", "get_position_y");
ClassDB::bind_method(D_METHOD("get_world_position_z"), &VoxelStructure::get_world_position_z);
ClassDB::bind_method(D_METHOD("set_world_position_z", "value"), &VoxelStructure::set_world_position_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "world_position_z"), "set_world_position_z", "get_world_position_z");
ClassDB::bind_method(D_METHOD("get_position_z"), &VoxelStructure::get_position_z);
ClassDB::bind_method(D_METHOD("set_position_z", "value"), &VoxelStructure::set_position_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "position_z"), "set_position_z", "get_position_z");
ClassDB::bind_method(D_METHOD("set_position", "x", "y", "z"), &VoxelStructure::set_position);
ClassDB::bind_method(D_METHOD("write_to_chunk", "chunk"), &VoxelStructure::write_to_chunk);
}

View File

@ -23,7 +23,7 @@ SOFTWARE.
#ifndef VOXEL_STRUCTURE_H
#define VOXEL_STRUCTURE_H
#include "core/reference.h"
#include "core/resource.h"
#include "core/version.h"
@ -40,26 +40,28 @@ using PoolVector = Vector<N>;
#include "core/math/aabb.h"
#include "voxel_chunk.h"
class VoxelStructure : public Reference {
GDCLASS(VoxelStructure, Reference);
class VoxelStructure : public Resource {
GDCLASS(VoxelStructure, Resource);
public:
bool get_use_aabb() const;
void set_use_aabb(const bool value);
AABB get_aabb() const;
void set_aabb(const AABB &value);
AABB get_chunk_aabb() const;
void set_chunk_aabb(const AABB &value);
int get_world_position_x() const;
void set_world_position_x(const int value);
int get_position_x() const;
void set_position_x(const int value);
int get_world_position_y() const;
void set_world_position_y(const int value);
int get_position_y() const;
void set_position_y(const int value);
int get_world_position_z() const;
void set_world_position_z(const int value);
int get_position_z() const;
void set_position_z(const int value);
void write_to_chunk(Node *chunk);
void set_position(const int x, const int y, const int z);
void write_to_chunk(Ref<VoxelChunk> chunk);
VoxelStructure();
~VoxelStructure();
@ -69,11 +71,11 @@ protected:
private:
bool _use_aabb;
AABB _aabb;
AABB _chunk_aabb;
int _world_position_x;
int _world_position_y;
int _world_position_z;
int _position_x;
int _position_y;
int _position_z;
};
#endif

View File

@ -24,6 +24,7 @@ SOFTWARE.
#include "voxel_chunk.h"
#include "voxel_chunk_prop_data.h"
#include "voxel_structure.h"
#include "core/version.h"
@ -152,7 +153,7 @@ Ref<WorldArea> VoxelWorld::get_world_area(const int index) const {
return _world_areas.get(index);
}
void VoxelWorld::add_world_area(Ref<WorldArea> area) {
void VoxelWorld::add_world_area(const Ref<WorldArea> &area) {
_world_areas.push_back(area);
}
void VoxelWorld::remove_world_area(const int index) {
@ -167,6 +168,48 @@ int VoxelWorld::get_world_area_count() const {
return _world_areas.size();
}
//Voxel Structures
Ref<VoxelStructure> VoxelWorld::get_voxel_structure(const int index) const {
ERR_FAIL_INDEX_V(index, _voxel_structures.size(), Ref<WorldArea>());
return _voxel_structures.get(index);
}
void VoxelWorld::add_voxel_structure(const Ref<VoxelStructure> &structure) {
ERR_FAIL_COND(!structure.is_valid());
_voxel_structures.push_back(structure);
}
void VoxelWorld::remove_voxel_structure(const Ref<VoxelStructure> &structure) {
if (!structure.is_valid())
return;
int index = _voxel_structures.find(structure);
if (index != -1)
_voxel_structures.remove(index);
}
void VoxelWorld::remove_voxel_structure_index(const int index) {
ERR_FAIL_INDEX(index, _voxel_structures.size());
_voxel_structures.remove(index);
}
void VoxelWorld::clear_voxel_structures() {
_voxel_structures.clear();
}
int VoxelWorld::get_voxel_structure_count() const {
return _voxel_structures.size();
}
void VoxelWorld::add_voxel_structure_at_position(Ref<VoxelStructure> structure, const Vector3 &world_position) {
ERR_FAIL_COND(!structure.is_valid());
structure->set_position_x(static_cast<int>(world_position.x / _voxel_scale));
structure->set_position_y(static_cast<int>(world_position.y / _voxel_scale));
structure->set_position_z(static_cast<int>(world_position.z / _voxel_scale));
add_voxel_structure(structure);
}
void VoxelWorld::add_chunk(Ref<VoxelChunk> chunk, const int x, const int y, const int z) {
ERR_FAIL_COND(!chunk.is_valid());
@ -522,6 +565,7 @@ VoxelWorld ::~VoxelWorld() {
_chunks.clear();
_chunks_vector.clear();
_world_areas.clear();
_voxel_structures.clear();
_library.unref();
_level_generator.unref();
@ -539,6 +583,20 @@ void VoxelWorld::_generate_chunk(Ref<VoxelChunk> chunk) {
ERR_FAIL_COND(!_level_generator.is_valid());
_level_generator->generate_chunk(chunk);
for (int i = 0; i < _voxel_structures.size(); ++i) {
Ref<VoxelStructure> structure = _voxel_structures.get(i);
if (!structure.is_valid())
continue;
if (structure->get_use_aabb()) {
if (structure->get_chunk_aabb().has_point(Vector3(chunk->get_position_x(), chunk->get_position_y(), chunk->get_position_z())))
structure->write_to_chunk(chunk);
} else {
structure->write_to_chunk(chunk);
}
}
}
void VoxelWorld::_notification(int p_what) {
@ -730,6 +788,14 @@ void VoxelWorld::_bind_methods() {
ClassDB::bind_method(D_METHOD("clear_world_areas"), &VoxelWorld::clear_world_areas);
ClassDB::bind_method(D_METHOD("get_world_area_count"), &VoxelWorld::get_world_area_count);
ClassDB::bind_method(D_METHOD("get_voxel_structure", "index"), &VoxelWorld::get_voxel_structure);
ClassDB::bind_method(D_METHOD("add_voxel_structure", "structure"), &VoxelWorld::add_voxel_structure);
ClassDB::bind_method(D_METHOD("remove_voxel_structure", "structure"), &VoxelWorld::remove_voxel_structure);
ClassDB::bind_method(D_METHOD("remove_voxel_structure_index", "index"), &VoxelWorld::remove_voxel_structure_index);
ClassDB::bind_method(D_METHOD("clear_voxel_structures"), &VoxelWorld::clear_voxel_structures);
ClassDB::bind_method(D_METHOD("get_voxel_structure_count"), &VoxelWorld::get_voxel_structure_count);
ClassDB::bind_method(D_METHOD("add_voxel_structure_at_position", "structure", "world_position"), &VoxelWorld::add_voxel_structure_at_position);
BIND_VMETHOD(MethodInfo("_chunk_added", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk")));
ClassDB::bind_method(D_METHOD("add_chunk", "chunk", "x", "y", "z"), &VoxelWorld::add_chunk);

View File

@ -45,6 +45,7 @@ typedef class Node3D Spatial;
#include "core/os/os.h"
class VoxelStructure;
class VoxelChunk;
class VoxelChunkPropData;
@ -103,11 +104,20 @@ public:
//World Areas
Ref<WorldArea> get_world_area(const int index) const;
void add_world_area(Ref<WorldArea> area);
void add_world_area(const Ref<WorldArea> &area);
void remove_world_area(const int index);
void clear_world_areas();
int get_world_area_count() const;
//Voxel Structures
Ref<VoxelStructure> get_voxel_structure(const int index) const;
void add_voxel_structure(const Ref<VoxelStructure> &structure);
void remove_voxel_structure(const Ref<VoxelStructure> &structure);
void remove_voxel_structure_index(const int index);
void clear_voxel_structures();
int get_voxel_structure_count() const;
void add_voxel_structure_at_position(Ref<VoxelStructure> structure, const Vector3 &world_position);
//Chunks
void add_chunk(Ref<VoxelChunk> chunk, const int x, const int y, const int z);
bool has_chunk(const int x, const int y, const int z) const;
@ -221,6 +231,8 @@ private:
Vector<Ref<WorldArea> > _world_areas;
Vector<Ref<VoxelStructure> > _voxel_structures;
NodePath _player_path;
Spatial *_player;