Cleaned up the prop spawning api in chunk and world.

This commit is contained in:
Relintai 2020-04-15 13:06:45 +02:00
parent a63ae2183a
commit dd0fa1d7ea
8 changed files with 66 additions and 16 deletions

View File

@ -928,6 +928,12 @@ void VoxelChunkDefault::_world_transform_changed() {
update_transforms();
}
//Props
void VoxelChunkDefault::_add_prop(Ref<VoxelChunkPropData> prop) {
ERR_FAIL_MSG("TODO! _add_prop not yet implemented, send prop to the relevant chunk");
}
//Lights
void VoxelChunkDefault::_bake_lights() {
clear_baked_lights();
@ -1494,6 +1500,9 @@ void VoxelChunkDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("_process", "delta"), &VoxelChunkDefault::_process);
ClassDB::bind_method(D_METHOD("_physics_process", "delta"), &VoxelChunkDefault::_physics_process);
//Props
ClassDB::bind_method(D_METHOD("_add_prop", "prop"), &VoxelChunkDefault::_add_prop);
//lights
ClassDB::bind_method(D_METHOD("_bake_lights"), &VoxelChunkDefault::_bake_lights);
ClassDB::bind_method(D_METHOD("_bake_light", "light"), &VoxelChunkDefault::_bake_light);

View File

@ -252,6 +252,9 @@ protected:
virtual void _physics_process(float delta);
virtual void _world_transform_changed();
//Props
virtual void _add_prop(Ref<VoxelChunkPropData> prop);
//lights
virtual void _bake_lights();
virtual void _bake_light(Ref<VoxelLight> light);

View File

@ -627,30 +627,42 @@ void VoxelChunk::clear_baked_lights() {
}
void VoxelChunk::add_prop(Ref<VoxelChunkPropData> prop) {
ERR_FAIL_COND(!prop.is_valid());
ERR_FAIL_COND(prop->get_owner().is_valid());
prop->set_owner(Ref<VoxelChunk>(this));
_props.push_back(prop);
if (has_method("_prop_added"))
call("_prop_added", prop);
}
Ref<VoxelChunkPropData> VoxelChunk::get_prop(int index) {
ERR_FAIL_INDEX_V(index, _props.size(), Ref<VoxelChunkPropData>());
return _props.get(index);
}
int VoxelChunk::get_prop_count() {
return _props.size();
}
void VoxelChunk::remove_prop(int index) {
return _props.remove(index);
ERR_FAIL_INDEX(index, _props.size());
Ref<VoxelChunkPropData> prop = _props.get(index);
if (prop.is_valid())
prop->set_owner(Ref<VoxelChunk>());
_props.remove(index);
}
void VoxelChunk::clear_props() {
_props.clear();
}
for (int i = 0; i < _props.size(); ++i) {
Ref<VoxelChunkPropData> prop = _props.get(i);
void VoxelChunk::free_spawn_props() {
for (int i = 0; i < _spawned_props.size(); ++i) {
_spawned_props[i]->queue_delete();
if (prop.is_valid())
prop->set_owner(Ref<VoxelChunk>());
}
_spawned_props.clear();
_props.clear();
}
void VoxelChunk::enter_tree() {
@ -729,7 +741,7 @@ VoxelChunk::~VoxelChunk() {
_library.unref();
}
_props.clear();
clear_props();
for (int i = 0; i < _channels.size(); ++i) {
uint8_t *ch = _channels[i];
@ -958,8 +970,6 @@ void VoxelChunk::_bind_methods() {
ClassDB::bind_method(D_METHOD("remove_prop", "index"), &VoxelChunk::remove_prop);
ClassDB::bind_method(D_METHOD("clear_props"), &VoxelChunk::clear_props);
ClassDB::bind_method(D_METHOD("free_spawn_props"), &VoxelChunk::free_spawn_props);
ClassDB::bind_method(D_METHOD("create_meshers"), &VoxelChunk::create_meshers);
ClassDB::bind_method(D_METHOD("_world_transform_changed"), &VoxelChunk::_world_transform_changed);

View File

@ -187,8 +187,7 @@ public:
void remove_prop(int index);
void clear_props();
void free_spawn_props();
//handlers
void enter_tree();
void exit_tree();
void process(float delta);
@ -250,9 +249,6 @@ protected:
//mergeable props
Vector<Ref<VoxelChunkPropData> > _props;
//spawned props
Vector<Node *> _spawned_props;
Transform _transform;
};

View File

@ -24,6 +24,13 @@ SOFTWARE.
#include "voxel_chunk.h"
Ref<VoxelChunk> VoxelChunkPropData::get_owner() {
return _owner;
}
void VoxelChunkPropData::set_owner(const Ref<VoxelChunk> &chunk) {
_owner = chunk;
}
int VoxelChunkPropData::get_scene_id() const {
return _scene_id;
}
@ -66,10 +73,15 @@ VoxelChunkPropData::VoxelChunkPropData() {
_scene_id = 0;
}
VoxelChunkPropData::~VoxelChunkPropData() {
_owner.unref();
_scene.unref();
}
void VoxelChunkPropData::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_owner"), &VoxelChunkPropData::get_owner);
ClassDB::bind_method(D_METHOD("set_owner", "value"), &VoxelChunkPropData::set_owner);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk"), "set_owner", "get_owner");
ClassDB::bind_method(D_METHOD("get_scene_id"), &VoxelChunkPropData::get_scene_id);
ClassDB::bind_method(D_METHOD("set_scene_id", "value"), &VoxelChunkPropData::set_scene_id);
ADD_PROPERTY(PropertyInfo(Variant::INT, "scene_id"), "set_scene_id", "get_scene_id");

View File

@ -37,6 +37,9 @@ class VoxelChunkPropData : public Resource {
GDCLASS(VoxelChunkPropData, Resource);
public:
Ref<VoxelChunk> get_owner();
void set_owner(const Ref<VoxelChunk> &chunk);
int get_scene_id() const;
void set_scene_id(const int id);
@ -61,6 +64,7 @@ protected:
static void _bind_methods();
private:
Ref<VoxelChunk> _owner;
int _scene_id;
Transform _transform;
Node *_spawned_prop;

View File

@ -23,6 +23,7 @@ SOFTWARE.
#include "voxel_world.h"
#include "voxel_chunk.h"
#include "voxel_chunk_prop_data.h"
#include "core/version.h"
@ -413,8 +414,14 @@ void VoxelWorld::set_chunks(const Vector<Variant> &chunks) {
}
}
//Lights
//Props
void VoxelWorld::add_prop(Ref<VoxelChunkPropData> prop) {
ERR_FAIL_COND(!has_method("_add_prop"));
call("_add_prop", prop);
}
//Lights
void VoxelWorld::add_light(const Ref<VoxelLight> &light) {
_lights.push_back(light);
@ -763,6 +770,11 @@ void VoxelWorld::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_position_walkable", "position"), &VoxelWorld::is_position_walkable);
ClassDB::bind_method(D_METHOD("on_chunk_mesh_generation_finished", "chunk"), &VoxelWorld::on_chunk_mesh_generation_finished);
//Props
BIND_VMETHOD(MethodInfo("_add_prop", PropertyInfo(Variant::OBJECT, "prop", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunkPropData")));
ClassDB::bind_method(D_METHOD("add_prop", "prop"), &VoxelWorld::add_prop);
//Lights
ClassDB::bind_method(D_METHOD("add_light", "light"), &VoxelWorld::add_light);
ClassDB::bind_method(D_METHOD("get_light", "index"), &VoxelWorld::get_light);

View File

@ -46,6 +46,7 @@ typedef class Node3D Spatial;
#include "core/os/os.h"
class VoxelChunk;
class VoxelChunkPropData;
class VoxelWorld : public Navigation {
GDCLASS(VoxelWorld, Navigation);
@ -142,6 +143,9 @@ public:
Vector<Variant> get_chunks();
void set_chunks(const Vector<Variant> &chunks);
//Props
void add_prop(Ref<VoxelChunkPropData> prop);
//Lights
void add_light(const Ref<VoxelLight> &light);
Ref<VoxelLight> get_light(const int index);