2019-09-24 02:05:40 +02:00
|
|
|
#include "voxel_structure.h"
|
|
|
|
|
2019-09-25 01:49:35 +02:00
|
|
|
int VoxelStructure::get_chunk_size_x() const {
|
2019-11-18 21:53:33 +01:00
|
|
|
return _chunk_size_x;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
void VoxelStructure::set_chunk_size_x(const int value) {
|
2019-11-18 21:53:33 +01:00
|
|
|
_chunk_size_x = value;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelStructure::get_chunk_size_y() const {
|
2019-11-18 21:53:33 +01:00
|
|
|
return _chunk_size_y;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
void VoxelStructure::set_chunk_size_y(const int value) {
|
2019-11-18 21:53:33 +01:00
|
|
|
_chunk_size_y = value;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelStructure::get_chunk_size_z() const {
|
2019-11-18 21:53:33 +01:00
|
|
|
return _chunk_size_z;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
void VoxelStructure::set_chunk_size_z(const int value) {
|
2019-11-18 21:53:33 +01:00
|
|
|
_chunk_size_z = value;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelStructure::get_world_position_x() const {
|
2019-11-18 21:53:33 +01:00
|
|
|
return _world_position_x;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
void VoxelStructure::set_world_position_x(const int value) {
|
2019-11-18 21:53:33 +01:00
|
|
|
_world_position_x = value;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelStructure::get_world_position_y() const {
|
2019-11-18 21:53:33 +01:00
|
|
|
return _world_position_y;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
void VoxelStructure::set_world_position_y(const int value) {
|
2019-11-18 21:53:33 +01:00
|
|
|
_world_position_y = value;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelStructure::get_world_position_z() const {
|
2019-11-18 21:53:33 +01:00
|
|
|
return _world_position_z;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
void VoxelStructure::set_world_position_z(const int value) {
|
2019-11-18 21:53:33 +01:00
|
|
|
_world_position_z = value;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
VoxelChunk *VoxelStructure::get_chunk_voxel_pos(int x, int y, int z) {
|
2019-09-25 11:30:07 +02:00
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
VoxelChunk *b = get_chunk(x / _chunk_size_x, y / _chunk_size_y, z / _chunk_size_z);
|
|
|
|
/*
|
2019-09-25 11:30:07 +02:00
|
|
|
if (!b.is_valid()) {
|
|
|
|
b.instance();
|
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
add_chunk(b, x / _chunk_size_x, y / _chunk_size_y, z / _chunk_size_z);
|
2019-09-25 11:30:07 +02:00
|
|
|
}
|
2019-11-18 21:53:33 +01:00
|
|
|
*/
|
2019-09-25 11:30:07 +02:00
|
|
|
return b;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 11:30:07 +02:00
|
|
|
int VoxelStructure::get_voxel(int x, int y, int z, unsigned int channel_index) {
|
2019-11-18 21:53:33 +01:00
|
|
|
VoxelChunk *b = get_chunk_voxel_pos(x, y, z);
|
2019-09-25 11:30:07 +02:00
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
//ERR_FAIL_COND_V(!b.is_valid(), 0);
|
2019-09-25 01:49:35 +02:00
|
|
|
|
2019-09-25 11:30:07 +02:00
|
|
|
return b->get_voxel(x, y, z, channel_index);
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
void VoxelStructure::set_voxel(int value, int x, int y, int z, unsigned int channel_index) {
|
2019-11-18 21:53:33 +01:00
|
|
|
VoxelChunk *b = get_chunk_voxel_pos(x, y, z);
|
2019-09-25 11:30:07 +02:00
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
//ERR_FAIL_COND(!b.is_valid());
|
2019-09-25 11:30:07 +02:00
|
|
|
|
|
|
|
return b->set_voxel(value, x, y, z, channel_index);
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 22:22:41 +01:00
|
|
|
void add_chunk_bind(Node *chunk, const int x, const int y, const int z) {
|
|
|
|
|
|
|
|
}
|
2019-11-18 21:53:33 +01:00
|
|
|
void VoxelStructure::add_chunk(VoxelChunk *chunk, const int x, const int y, const int z) {
|
|
|
|
//_chunks.set(Vector3i(x, y, z), chunk);
|
2019-09-25 01:49:35 +02:00
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
//VoxelStructureChunk c;
|
|
|
|
//c.chunk = chunk;
|
|
|
|
//c.position = Vector3i(x, y, z);
|
2019-09-24 02:05:40 +02:00
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
//_chunks_vector.push_back(c);
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
2019-11-18 21:53:33 +01:00
|
|
|
VoxelChunk *VoxelStructure::get_chunk(const int x, const int y, const int z) {
|
|
|
|
//const VoxelChunk **chunk = _chunks.getptr(Vector3i(x, y, z));
|
2019-09-25 01:49:35 +02:00
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
//return Ref<VoxelBuffer>(chunk);
|
|
|
|
|
|
|
|
return NULL;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
2019-11-18 21:53:33 +01:00
|
|
|
VoxelChunk *VoxelStructure::remove_chunk(const int x, const int y, const int z) {
|
|
|
|
/*
|
|
|
|
VoxelChunk **chunk = _chunks.getptr(Vector3i(x, y, z));
|
2019-09-25 01:49:35 +02:00
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
VoxelChunk *c(chunk);
|
2019-09-25 01:49:35 +02:00
|
|
|
|
|
|
|
if (c.is_valid()) {
|
|
|
|
|
|
|
|
for (int i = 0; i < _chunks_vector.size(); ++i) {
|
2019-11-18 21:53:33 +01:00
|
|
|
if (_chunks_vector.get(i).chunk == c) {
|
2019-09-25 01:49:35 +02:00
|
|
|
_chunks_vector.remove(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
return c;*/
|
|
|
|
|
|
|
|
return NULL;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
VoxelChunk *VoxelStructure::get_chunk_index(const int index) {
|
|
|
|
//return _chunks_vector.get(index).chunk;
|
2019-11-18 22:22:41 +01:00
|
|
|
return NULL;
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
int VoxelStructure::get_chunk_count() const {
|
|
|
|
return _chunks_vector.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelStructure::clear_chunks() {
|
|
|
|
for (int i = 0; i < _chunks_vector.size(); ++i) {
|
2019-11-18 21:53:33 +01:00
|
|
|
//_chunks_vector.get(i).chunk.unref();
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
//_chunks_vector.clear();
|
2019-09-25 01:49:35 +02:00
|
|
|
|
2019-11-18 21:53:33 +01:00
|
|
|
//_chunks.clear();
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelStructure::VoxelStructure() {
|
2019-11-18 21:53:33 +01:00
|
|
|
//_chunk_size = Vector3i(16, 16, 16);
|
|
|
|
//_world_position = Vector3i(0, 0, 0);
|
2019-09-24 02:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelStructure::~VoxelStructure() {
|
2019-09-25 01:49:35 +02:00
|
|
|
for (int i = 0; i < _chunks_vector.size(); ++i) {
|
2019-11-18 21:53:33 +01:00
|
|
|
//_chunks_vector.get(i).chunk.unref();
|
2019-09-25 01:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_chunks.clear();
|
|
|
|
_chunks_vector.clear();
|
2019-09-24 02:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelStructure::_bind_methods() {
|
2019-09-25 01:49:35 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_chunk_size_x"), &VoxelStructure::get_chunk_size_x);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_chunk_size_x", "value"), &VoxelStructure::set_chunk_size_x);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_x"), "set_chunk_size_x", "get_chunk_size_x");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_chunk_size_y"), &VoxelStructure::get_chunk_size_y);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_chunk_size_y", "value"), &VoxelStructure::set_chunk_size_y);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_y"), "set_chunk_size_y", "get_chunk_size_y");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_chunk_size_z"), &VoxelStructure::get_chunk_size_z);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_chunk_size_z", "value"), &VoxelStructure::set_chunk_size_z);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_z"), "set_chunk_size_z", "get_chunk_size_z");
|
|
|
|
|
|
|
|
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_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_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");
|
|
|
|
|
2019-09-25 11:30:07 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_chunk_voxel_pos", "x", "y", "z"), &VoxelStructure::get_chunk_voxel_pos);
|
|
|
|
|
2019-09-25 01:49:35 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_voxel", "x", "y", "z", "channel_index"), &VoxelStructure::get_voxel, DEFVAL(0));
|
|
|
|
ClassDB::bind_method(D_METHOD("set_voxel", "value", "x", "y", "z", "channel_index"), &VoxelStructure::set_voxel, DEFVAL(0));
|
2019-11-18 22:40:58 +01:00
|
|
|
//ClassDB::bind_method(D_METHOD("set_voxel_v", "value", "pos", "channel_index"), &VoxelStructure::set_voxel_v, DEFVAL(0));
|
2019-09-25 01:49:35 +02:00
|
|
|
|
2019-11-18 22:40:58 +01:00
|
|
|
//ClassDB::bind_method(D_METHOD("add_chunk", "chunk", "x", "y", "z"), &VoxelStructure::add_chunk_bind);
|
2019-09-25 01:49:35 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_chunk", "x", "y", "z"), &VoxelStructure::get_chunk);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_chunk", "x", "y", "z"), &VoxelStructure::remove_chunk);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_chunk_index", "index"), &VoxelStructure::get_chunk_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_chunk_count"), &VoxelStructure::get_chunk_count);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_chunks"), &VoxelStructure::clear_chunks);
|
2019-09-24 02:05:40 +02:00
|
|
|
}
|