diff --git a/world/voxel_world.cpp b/world/voxel_world.cpp index b330e14..cde2a60 100644 --- a/world/voxel_world.cpp +++ b/world/voxel_world.cpp @@ -443,6 +443,26 @@ int VoxelWorld::_get_channel_index_info(const VoxelWorld::ChannelTypeInfo channe return -1; } +void VoxelWorld::_set_voxel_with_tool(const bool mode_add, const Vector3 hit_position, const Vector3 hit_normal, const int selected_voxel, const int isolevel) { + Vector3 pos; + + if (mode_add) { + pos = (hit_position + (Vector3(0.1, 0.1, 0.1) * hit_normal * get_voxel_scale())); + } else { + pos = (hit_position + (Vector3(0.1, 0.1, 0.1) * -hit_normal * get_voxel_scale())); + } + + int channel_type = get_channel_index_info(VoxelWorld::CHANNEL_TYPE_INFO_TYPE); + int channel_isolevel = get_channel_index_info(VoxelWorld::CHANNEL_TYPE_INFO_ISOLEVEL); + + if (channel_isolevel == -1) { + set_voxel_at_world_position(pos, selected_voxel, channel_type); + } else { + set_voxel_at_world_position(pos, selected_voxel, channel_type, false); + set_voxel_at_world_position(pos, isolevel, channel_isolevel); + } +} + bool VoxelWorld::can_chunk_do_build_step() { if (_max_frame_chunk_build_steps == 0) { return true; @@ -802,6 +822,10 @@ Ref VoxelWorld::get_or_create_chunk_at_world_position(const Vector3 return get_or_create_chunk(x, y, z); } +void VoxelWorld::set_voxel_with_tool(const bool mode_add, const Vector3 hit_position, const Vector3 hit_normal, const int selected_voxel, const int isolevel) { + call("_set_voxel_with_tool", mode_add, hit_position, hit_normal, selected_voxel, isolevel); +} + int VoxelWorld::get_channel_index_info(const VoxelWorld::ChannelTypeInfo channel_type) { return call("_get_channel_index_info", channel_type); } @@ -1143,6 +1167,16 @@ void VoxelWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("get_channel_index_info", "channel_type"), &VoxelWorld::get_channel_index_info); ClassDB::bind_method(D_METHOD("_get_channel_index_info", "channel_type"), &VoxelWorld::_get_channel_index_info); + BIND_VMETHOD(MethodInfo("_set_voxel_with_tool", + PropertyInfo(Variant::BOOL, "mode_add"), + PropertyInfo(Variant::VECTOR3, "hit_position"), + PropertyInfo(Variant::VECTOR3, "hit_normal"), + PropertyInfo(Variant::INT, "selected_voxel"), + PropertyInfo(Variant::INT, "isolevel"))); + + ClassDB::bind_method(D_METHOD("set_voxel_with_tool", "mode_add", "hit_position", "hit_normal", "selected_voxel", "isolevel"), &VoxelWorld::set_voxel_with_tool); + ClassDB::bind_method(D_METHOD("_set_voxel_with_tool", "mode_add", "hit_position", "hit_normal", "selected_voxel", "isolevel"), &VoxelWorld::_set_voxel_with_tool); + BIND_ENUM_CONSTANT(CHANNEL_TYPE_INFO_TYPE); BIND_ENUM_CONSTANT(CHANNEL_TYPE_INFO_ISOLEVEL); BIND_ENUM_CONSTANT(CHANNEL_TYPE_INFO_LIQUID_FLOW); diff --git a/world/voxel_world.h b/world/voxel_world.h index def6246..0bd2de6 100644 --- a/world/voxel_world.h +++ b/world/voxel_world.h @@ -179,6 +179,7 @@ public: void set_voxel_at_world_position(const Vector3 &world_position, const uint8_t data, const int channel_index, const bool rebuild = true); Ref get_chunk_at_world_position(const Vector3 &world_position); Ref get_or_create_chunk_at_world_position(const Vector3 &world_position); + void set_voxel_with_tool(const bool mode_add, const Vector3 hit_position, const Vector3 hit_normal, const int selected_voxel, const int isolevel); int get_channel_index_info(const ChannelTypeInfo channel_type); @@ -189,6 +190,7 @@ protected: virtual void _generate_chunk(Ref chunk); virtual Ref _create_chunk(int x, int y, int z, Ref p_chunk); virtual int _get_channel_index_info(const ChannelTypeInfo channel_type); + virtual void _set_voxel_with_tool(const bool mode_add, const Vector3 hit_position, const Vector3 hit_normal, const int selected_voxel, const int isolevel); virtual void _notification(int p_what); static void _bind_methods(); diff --git a/world/voxel_world_editor.cpp b/world/voxel_world_editor.cpp index e45a82e..9ae62f4 100644 --- a/world/voxel_world_editor.cpp +++ b/world/voxel_world_editor.cpp @@ -86,7 +86,6 @@ bool VoxelWorldEditor::do_input_action(Camera *p_camera, const Point2 &p_point, PhysicsDirectSpaceState::RayResult res; if (ss->intersect_ray(from, to, res)) { - Vector3 pos; int selected_voxel = 0; int channel = 0; @@ -96,22 +95,18 @@ bool VoxelWorldEditor::do_input_action(Camera *p_camera, const Point2 &p_point, return false; int isolevel = _current_isolevel; + bool mode_add = false; if (_tool_mode == TOOL_MODE_ADD) { - pos = (res.position + (Vector3(0.1, 0.1, 0.1) * res.normal)); selected_voxel = _selected_type + 1; + mode_add = true; } else if (_tool_mode == TOOL_MODE_REMOVE) { - pos = (res.position + (Vector3(0.1, 0.1, 0.1) * -res.normal)); selected_voxel = 0; isolevel = 0; + mode_add = false; } - if (_channel_isolevel == -1) { - _world->set_voxel_at_world_position(pos, selected_voxel, channel); - } else { - _world->set_voxel_at_world_position(pos, selected_voxel, channel, false); - _world->set_voxel_at_world_position(pos, isolevel, _channel_isolevel); - } + _world->set_voxel_with_tool(mode_add, res.position, res.normal, selected_voxel, isolevel); return true; }