collision_object_2d.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************/
  2. /* collision_object_2d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef COLLISION_OBJECT_2D_H
  31. #define COLLISION_OBJECT_2D_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/main/viewport.h"
  34. #include "scene/resources/shape_2d.h"
  35. #include "servers/physics_server_2d.h"
  36. class CollisionObject2D : public Node2D {
  37. GDCLASS(CollisionObject2D, Node2D);
  38. public:
  39. enum DisableMode {
  40. DISABLE_MODE_REMOVE,
  41. DISABLE_MODE_MAKE_STATIC,
  42. DISABLE_MODE_KEEP_ACTIVE,
  43. };
  44. private:
  45. uint32_t collision_layer = 1;
  46. uint32_t collision_mask = 1;
  47. real_t collision_priority = 1.0;
  48. bool area = false;
  49. RID rid;
  50. uint32_t callback_lock = 0;
  51. bool pickable = false;
  52. DisableMode disable_mode = DISABLE_MODE_REMOVE;
  53. PhysicsServer2D::BodyMode body_mode = PhysicsServer2D::BODY_MODE_STATIC;
  54. struct ShapeData {
  55. ObjectID owner_id;
  56. Transform2D xform;
  57. struct Shape {
  58. Ref<Shape2D> shape;
  59. int index = 0;
  60. };
  61. Vector<Shape> shapes;
  62. bool disabled = false;
  63. bool one_way_collision = false;
  64. real_t one_way_collision_margin = 0.0;
  65. };
  66. int total_subshapes = 0;
  67. RBMap<uint32_t, ShapeData> shapes;
  68. bool only_update_transform_changes = false; // This is used for sync to physics.
  69. void _apply_disabled();
  70. void _apply_enabled();
  71. protected:
  72. _FORCE_INLINE_ void lock_callback() { callback_lock++; }
  73. _FORCE_INLINE_ void unlock_callback() {
  74. ERR_FAIL_COND(callback_lock == 0);
  75. callback_lock--;
  76. }
  77. CollisionObject2D(RID p_rid, bool p_area);
  78. void _notification(int p_what);
  79. static void _bind_methods();
  80. void _update_pickable();
  81. friend class Viewport;
  82. void _input_event_call(Viewport *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape);
  83. void _mouse_enter();
  84. void _mouse_exit();
  85. void _mouse_shape_enter(int p_shape);
  86. void _mouse_shape_exit(int p_shape);
  87. void set_only_update_transform_changes(bool p_enable);
  88. bool is_only_update_transform_changes_enabled() const;
  89. void set_body_mode(PhysicsServer2D::BodyMode p_mode);
  90. GDVIRTUAL3(_input_event, Viewport *, Ref<InputEvent>, int)
  91. GDVIRTUAL0(_mouse_enter)
  92. GDVIRTUAL0(_mouse_exit)
  93. GDVIRTUAL1(_mouse_shape_enter, int)
  94. GDVIRTUAL1(_mouse_shape_exit, int)
  95. public:
  96. void set_collision_layer(uint32_t p_layer);
  97. uint32_t get_collision_layer() const;
  98. void set_collision_mask(uint32_t p_mask);
  99. uint32_t get_collision_mask() const;
  100. void set_collision_layer_value(int p_layer_number, bool p_value);
  101. bool get_collision_layer_value(int p_layer_number) const;
  102. void set_collision_mask_value(int p_layer_number, bool p_value);
  103. bool get_collision_mask_value(int p_layer_number) const;
  104. void set_collision_priority(real_t p_priority);
  105. real_t get_collision_priority() const;
  106. void set_disable_mode(DisableMode p_mode);
  107. DisableMode get_disable_mode() const;
  108. uint32_t create_shape_owner(Object *p_owner);
  109. void remove_shape_owner(uint32_t owner);
  110. void get_shape_owners(List<uint32_t> *r_owners);
  111. PackedInt32Array _get_shape_owners();
  112. void shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform);
  113. Transform2D shape_owner_get_transform(uint32_t p_owner) const;
  114. Object *shape_owner_get_owner(uint32_t p_owner) const;
  115. void shape_owner_set_disabled(uint32_t p_owner, bool p_disabled);
  116. bool is_shape_owner_disabled(uint32_t p_owner) const;
  117. void shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable);
  118. bool is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const;
  119. void shape_owner_set_one_way_collision_margin(uint32_t p_owner, real_t p_margin);
  120. real_t get_shape_owner_one_way_collision_margin(uint32_t p_owner) const;
  121. void shape_owner_add_shape(uint32_t p_owner, const Ref<Shape2D> &p_shape);
  122. int shape_owner_get_shape_count(uint32_t p_owner) const;
  123. Ref<Shape2D> shape_owner_get_shape(uint32_t p_owner, int p_shape) const;
  124. int shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const;
  125. void shape_owner_remove_shape(uint32_t p_owner, int p_shape);
  126. void shape_owner_clear_shapes(uint32_t p_owner);
  127. uint32_t shape_find_owner(int p_shape_index) const;
  128. void set_pickable(bool p_enabled);
  129. bool is_pickable() const;
  130. PackedStringArray get_configuration_warnings() const override;
  131. _FORCE_INLINE_ RID get_rid() const { return rid; }
  132. CollisionObject2D();
  133. ~CollisionObject2D();
  134. };
  135. VARIANT_ENUM_CAST(CollisionObject2D::DisableMode);
  136. #endif // COLLISION_OBJECT_2D_H