diff options
Diffstat (limited to 'scene/2d/collision_shape_2d.cpp')
-rw-r--r-- | scene/2d/collision_shape_2d.cpp | 107 |
1 files changed, 47 insertions, 60 deletions
diff --git a/scene/2d/collision_shape_2d.cpp b/scene/2d/collision_shape_2d.cpp index 37a49577b5..39e0017e92 100644 --- a/scene/2d/collision_shape_2d.cpp +++ b/scene/2d/collision_shape_2d.cpp @@ -28,14 +28,13 @@ /*************************************************************************/ #include "collision_shape_2d.h" #include "collision_object_2d.h" -#include "scene/resources/segment_shape_2d.h" -#include "scene/resources/shape_line_2d.h" -#include "scene/resources/circle_shape_2d.h" -#include "scene/resources/rectangle_shape_2d.h" #include "scene/resources/capsule_shape_2d.h" -#include "scene/resources/convex_polygon_shape_2d.h" +#include "scene/resources/circle_shape_2d.h" #include "scene/resources/concave_polygon_shape_2d.h" - +#include "scene/resources/convex_polygon_shape_2d.h" +#include "scene/resources/rectangle_shape_2d.h" +#include "scene/resources/segment_shape_2d.h" +#include "scene/resources/shape_line_2d.h" void CollisionShape2D::_add_to_collision_object(Object *p_obj) { @@ -44,12 +43,10 @@ void CollisionShape2D::_add_to_collision_object(Object *p_obj) { CollisionObject2D *co = p_obj->cast_to<CollisionObject2D>(); ERR_FAIL_COND(!co); - update_shape_index=co->get_shape_count(); - co->add_shape(shape,get_transform()); + update_shape_index = co->get_shape_count(); + co->add_shape(shape, get_transform()); if (trigger) - co->set_shape_as_trigger(co->get_shape_count()-1,true); - - + co->set_shape_as_trigger(co->get_shape_count() - 1, true); } void CollisionShape2D::_shape_changed() { @@ -60,7 +57,6 @@ void CollisionShape2D::_shape_changed() { void CollisionShape2D::_update_parent() { - Node *parent = get_parent(); if (!parent) return; @@ -72,15 +68,15 @@ void CollisionShape2D::_update_parent() { void CollisionShape2D::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_ENTER_TREE: { - unparenting=false; - can_update_body=get_tree()->is_editor_hint(); + unparenting = false; + can_update_body = get_tree()->is_editor_hint(); if (!get_tree()->is_editor_hint()) { //display above all else set_z_as_relative(false); - set_z(VS::CANVAS_ITEM_Z_MAX-1); + set_z(VS::CANVAS_ITEM_Z_MAX - 1); } } break; @@ -90,18 +86,17 @@ void CollisionShape2D::_notification(int p_what) { break; if (can_update_body) { _update_parent(); - } else if (update_shape_index>=0){ + } else if (update_shape_index >= 0) { CollisionObject2D *co = get_parent()->cast_to<CollisionObject2D>(); if (co) { - co->set_shape_transform(update_shape_index,get_transform()); + co->set_shape_transform(update_shape_index, get_transform()); } - } } break; case NOTIFICATION_EXIT_TREE: { - can_update_body=false; + can_update_body = false; } break; /* @@ -122,16 +117,13 @@ void CollisionShape2D::_notification(int p_what) { break; } - rect=Rect2(); - - - - Color draw_col=get_tree()->get_debug_collisions_color(); - shape->draw(get_canvas_item(),draw_col); + rect = Rect2(); + Color draw_col = get_tree()->get_debug_collisions_color(); + shape->draw(get_canvas_item(), draw_col); - rect=shape->get_rect(); - rect=rect.grow(3); + rect = shape->get_rect(); + rect = rect.grow(3); } break; case NOTIFICATION_UNPARENTED: { @@ -139,25 +131,24 @@ void CollisionShape2D::_notification(int p_what) { _update_parent(); } break; } - } -void CollisionShape2D::set_shape(const Ref<Shape2D>& p_shape) { +void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) { if (shape.is_valid()) - shape->disconnect("changed",this,"_shape_changed"); - shape=p_shape; + shape->disconnect("changed", this, "_shape_changed"); + shape = p_shape; update(); if (is_inside_tree() && can_update_body) _update_parent(); - if (is_inside_tree() && !can_update_body && update_shape_index>=0) { + if (is_inside_tree() && !can_update_body && update_shape_index >= 0) { CollisionObject2D *co = get_parent()->cast_to<CollisionObject2D>(); if (co) { - co->set_shape(update_shape_index,p_shape); + co->set_shape(update_shape_index, p_shape); } } if (shape.is_valid()) - shape->connect("changed",this,"_shape_changed"); + shape->connect("changed", this, "_shape_changed"); update_configuration_warning(); } @@ -174,30 +165,28 @@ Rect2 CollisionShape2D::get_item_rect() const { void CollisionShape2D::set_trigger(bool p_trigger) { - trigger=p_trigger; + trigger = p_trigger; if (can_update_body) { _update_parent(); - } else if (is_inside_tree() && update_shape_index>=0){ + } else if (is_inside_tree() && update_shape_index >= 0) { CollisionObject2D *co = get_parent()->cast_to<CollisionObject2D>(); if (co) { - co->set_shape_as_trigger(update_shape_index,p_trigger); + co->set_shape_as_trigger(update_shape_index, p_trigger); } } } -bool CollisionShape2D::is_trigger() const{ +bool CollisionShape2D::is_trigger() const { return trigger; } - void CollisionShape2D::_set_update_shape_index(int p_index) { - - update_shape_index=p_index; + update_shape_index = p_index; } -int CollisionShape2D::_get_update_shape_index() const{ +int CollisionShape2D::_get_update_shape_index() const { return update_shape_index; } @@ -215,33 +204,31 @@ String CollisionShape2D::get_configuration_warning() const { return String(); } - void CollisionShape2D::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_shape","shape"),&CollisionShape2D::set_shape); - ClassDB::bind_method(D_METHOD("get_shape"),&CollisionShape2D::get_shape); - ClassDB::bind_method(D_METHOD("_shape_changed"),&CollisionShape2D::_shape_changed); - ClassDB::bind_method(D_METHOD("_add_to_collision_object"),&CollisionShape2D::_add_to_collision_object); - ClassDB::bind_method(D_METHOD("set_trigger","enable"),&CollisionShape2D::set_trigger); - ClassDB::bind_method(D_METHOD("is_trigger"),&CollisionShape2D::is_trigger); - - ClassDB::bind_method(D_METHOD("_set_update_shape_index","index"),&CollisionShape2D::_set_update_shape_index); - ClassDB::bind_method(D_METHOD("_get_update_shape_index"),&CollisionShape2D::_get_update_shape_index); + ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape); + ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape); + ClassDB::bind_method(D_METHOD("_shape_changed"), &CollisionShape2D::_shape_changed); + ClassDB::bind_method(D_METHOD("_add_to_collision_object"), &CollisionShape2D::_add_to_collision_object); + ClassDB::bind_method(D_METHOD("set_trigger", "enable"), &CollisionShape2D::set_trigger); + ClassDB::bind_method(D_METHOD("is_trigger"), &CollisionShape2D::is_trigger); - ClassDB::bind_method(D_METHOD("get_collision_object_shape_index"),&CollisionShape2D::get_collision_object_shape_index); + ClassDB::bind_method(D_METHOD("_set_update_shape_index", "index"), &CollisionShape2D::_set_update_shape_index); + ClassDB::bind_method(D_METHOD("_get_update_shape_index"), &CollisionShape2D::_get_update_shape_index); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"shape",PROPERTY_HINT_RESOURCE_TYPE,"Shape2D"),"set_shape","get_shape"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL,"trigger"),"set_trigger","is_trigger"); - ADD_PROPERTY( PropertyInfo( Variant::INT, "_update_shape_index", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_NOEDITOR), "_set_update_shape_index", "_get_update_shape_index"); + ClassDB::bind_method(D_METHOD("get_collision_object_shape_index"), &CollisionShape2D::get_collision_object_shape_index); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trigger"), "set_trigger", "is_trigger"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "_update_shape_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_update_shape_index", "_get_update_shape_index"); } CollisionShape2D::CollisionShape2D() { - rect=Rect2(-Point2(10,10),Point2(20,20)); + rect = Rect2(-Point2(10, 10), Point2(20, 20)); set_notify_local_transform(true); - trigger=false; + trigger = false; unparenting = false; can_update_body = false; - update_shape_index=-1; + update_shape_index = -1; } |