Skip to content

Commit

Permalink
Change show_debug_collision to collision_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Sep 17, 2024
1 parent d63c93b commit a5e0910
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
41 changes: 22 additions & 19 deletions src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ void Terrain3D::_build_collision() {
return;
}
// Create collision only in game, unless showing debug
if (IS_EDITOR && !_show_debug_collision) {
if (IS_EDITOR && !_is_collision_editor()) {
return;
}
if (_data == nullptr) {
Expand All @@ -451,14 +451,14 @@ void Terrain3D::_build_collision() {
}
_destroy_collision();

if (!_show_debug_collision) {
if (!_is_collision_editor()) {
LOG(INFO, "Building collision with physics server");
_static_body = PhysicsServer3D::get_singleton()->body_create();
PhysicsServer3D::get_singleton()->body_set_mode(_static_body, PhysicsServer3D::BODY_MODE_STATIC);
PhysicsServer3D::get_singleton()->body_set_space(_static_body, get_world_3d()->get_space());
PhysicsServer3D::get_singleton()->body_attach_object_instance_id(_static_body, get_instance_id());
} else {
LOG(WARN, "Building debug collision. Disable this mode for releases");
LOG(WARN, "Building editor collision. Disable this mode for releases");
_debug_static_body = memnew(StaticBody3D);
_debug_static_body->set_name("StaticBody3D");
_debug_static_body->set_as_top_level(true);
Expand All @@ -476,11 +476,11 @@ void Terrain3D::_update_collision() {
return;
}
// Create collision only in game, unless showing debug
if (IS_EDITOR && !_show_debug_collision) {
if (IS_EDITOR && !_is_collision_editor()) {
return;
}
if ((!_show_debug_collision && !_static_body.is_valid()) ||
(_show_debug_collision && _debug_static_body == nullptr)) {
if ((!_is_collision_editor() && !_static_body.is_valid()) ||
(_is_collision_editor() && _debug_static_body == nullptr)) {
_build_collision();
}

Expand Down Expand Up @@ -567,7 +567,7 @@ void Terrain3D::_update_collision() {
global_pos + Vector3(_region_size, 0.f, _region_size) * .5f);
xform.scale(Vector3(_vertex_spacing, 1.f, _vertex_spacing));

if (!_show_debug_collision) {
if (!_is_collision_editor()) {
RID shape = PhysicsServer3D::get_singleton()->heightmap_shape_create();
Dictionary shape_data;
shape_data["width"] = shape_size;
Expand Down Expand Up @@ -925,19 +925,19 @@ void Terrain3D::set_collision_enabled(const bool p_enabled) {
}
}

void Terrain3D::set_show_debug_collision(const bool p_enabled) {
LOG(INFO, "Setting show collision: ", p_enabled);
_show_debug_collision = p_enabled;
_destroy_collision();
if (_data != nullptr && _show_debug_collision) {
void Terrain3D::set_collision_mode(CollisionMode p_mode) {
LOG(INFO, "Setting collision mode: ", p_mode);
if (_collision_mode != p_mode) {
_collision_mode = p_mode;
_destroy_collision();
_build_collision();
}
}

void Terrain3D::set_collision_layer(const uint32_t p_layers) {
LOG(INFO, "Setting collision layers: ", p_layers);
_collision_layer = p_layers;
if (_show_debug_collision) {
if (_is_collision_editor()) {
if (_debug_static_body != nullptr) {
_debug_static_body->set_collision_layer(_collision_layer);
}
Expand All @@ -951,7 +951,7 @@ void Terrain3D::set_collision_layer(const uint32_t p_layers) {
void Terrain3D::set_collision_mask(const uint32_t p_mask) {
LOG(INFO, "Setting collision mask: ", p_mask);
_collision_mask = p_mask;
if (_show_debug_collision) {
if (_is_collision_editor()) {
if (_debug_static_body != nullptr) {
_debug_static_body->set_collision_mask(_collision_mask);
}
Expand All @@ -965,7 +965,7 @@ void Terrain3D::set_collision_mask(const uint32_t p_mask) {
void Terrain3D::set_collision_priority(const real_t p_priority) {
LOG(INFO, "Setting collision priority: ", p_priority);
_collision_priority = p_priority;
if (_show_debug_collision) {
if (_is_collision_editor()) {
if (_debug_static_body != nullptr) {
_debug_static_body->set_collision_priority(_collision_priority);
}
Expand All @@ -977,7 +977,7 @@ void Terrain3D::set_collision_priority(const real_t p_priority) {
}

RID Terrain3D::get_collision_rid() const {
if (!_show_debug_collision) {
if (!_is_collision_editor()) {
return _static_body;
} else {
if (_debug_static_body != nullptr) {
Expand Down Expand Up @@ -1484,6 +1484,9 @@ void Terrain3D::_bind_methods() {
BIND_ENUM_CONSTANT(SIZE_1024);
//BIND_ENUM_CONSTANT(SIZE_2048);

BIND_ENUM_CONSTANT(FULL_GAME);
BIND_ENUM_CONSTANT(FULL_EDITOR);

ClassDB::bind_method(D_METHOD("get_version"), &Terrain3D::get_version);
ClassDB::bind_method(D_METHOD("set_debug_level", "level"), &Terrain3D::set_debug_level);
ClassDB::bind_method(D_METHOD("get_debug_level"), &Terrain3D::get_debug_level);
Expand Down Expand Up @@ -1528,8 +1531,8 @@ void Terrain3D::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_collision_enabled", "enabled"), &Terrain3D::set_collision_enabled);
ClassDB::bind_method(D_METHOD("get_collision_enabled"), &Terrain3D::get_collision_enabled);
ClassDB::bind_method(D_METHOD("set_show_debug_collision", "enabled"), &Terrain3D::set_show_debug_collision);
ClassDB::bind_method(D_METHOD("get_show_debug_collision"), &Terrain3D::get_show_debug_collision);
ClassDB::bind_method(D_METHOD("set_collision_mode", "mode"), &Terrain3D::set_collision_mode);
ClassDB::bind_method(D_METHOD("get_collision_mode"), &Terrain3D::get_collision_mode);
ClassDB::bind_method(D_METHOD("set_collision_layer", "layers"), &Terrain3D::set_collision_layer);
ClassDB::bind_method(D_METHOD("get_collision_layer"), &Terrain3D::get_collision_layer);
ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &Terrain3D::set_collision_mask);
Expand Down Expand Up @@ -1565,6 +1568,7 @@ void Terrain3D::_bind_methods() {

ADD_GROUP("Collision", "collision_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_enabled"), "set_collision_enabled", "get_collision_enabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mode", PROPERTY_HINT_ENUM, "Full / Game,Full / Editor"), "set_collision_mode", "get_collision_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
Expand All @@ -1575,7 +1579,6 @@ void Terrain3D::_bind_methods() {

ADD_GROUP("Debug", "debug_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_level", PROPERTY_HINT_ENUM, "Errors,Info,Debug,Debug Continuous"), "set_debug_level", "get_debug_level");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_show_collision"), "set_show_debug_collision", "get_show_debug_collision");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_show_region_labels"), "set_show_region_labels", "get_show_region_labels");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vertex_spacing", PROPERTY_HINT_RANGE, "0.25,10.0,0.05,or_greater"), "set_vertex_spacing", "get_vertex_spacing");

Expand Down
15 changes: 12 additions & 3 deletions src/terrain_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Terrain3D : public Node3D {
//SIZE_2048 = 2048,
};

enum CollisionMode {
//DYNAMIC_GAME,
//DYNAMIC_EDITOR,
FULL_GAME,
FULL_EDITOR,
};

private:
// Terrain state
String _version = "0.9.3-dev";
Expand Down Expand Up @@ -96,7 +103,7 @@ class Terrain3D : public Node3D {
RID _static_body;
StaticBody3D *_debug_static_body = nullptr;
bool _collision_enabled = true;
bool _show_debug_collision = false;
CollisionMode _collision_mode = FULL_GAME;
uint32_t _collision_layer = 1;
uint32_t _collision_mask = 1;
real_t _collision_priority = 1.0f;
Expand All @@ -116,6 +123,7 @@ class Terrain3D : public Node3D {
void _update_mesh_instances();
void _clear_meshes();

bool _is_collision_editor() const { return _collision_mode == FULL_EDITOR; }
void _build_collision();
void _update_collision();
void _destroy_collision();
Expand Down Expand Up @@ -184,8 +192,8 @@ class Terrain3D : public Node3D {
// Physics body settings
void set_collision_enabled(const bool p_enabled);
bool get_collision_enabled() const { return _collision_enabled; }
void set_show_debug_collision(const bool p_enabled);
bool get_show_debug_collision() const { return _show_debug_collision; }
void set_collision_mode(const CollisionMode p_mode);
bool get_collision_mode() const { return _collision_mode; }
void set_collision_layer(const uint32_t p_layers);
uint32_t get_collision_layer() const { return _collision_layer; };
void set_collision_mask(const uint32_t p_mask);
Expand Down Expand Up @@ -226,5 +234,6 @@ class Terrain3D : public Node3D {
};

VARIANT_ENUM_CAST(Terrain3D::RegionSize);
VARIANT_ENUM_CAST(Terrain3D::CollisionMode);

#endif // TERRAIN3D_CLASS_H

0 comments on commit a5e0910

Please sign in to comment.