room.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**************************************************************************/
  2. /* room.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 ROOM_H
  31. #define ROOM_H
  32. #include "core/local_vector.h"
  33. #include "core/rid.h"
  34. #include "spatial.h"
  35. class Portal;
  36. class Room : public Spatial {
  37. GDCLASS(Room, Spatial);
  38. friend class RoomManager;
  39. friend class RoomGroup;
  40. friend class Portal;
  41. friend class RoomGizmoPlugin;
  42. friend class RoomEditorPlugin;
  43. friend class RoomSpatialGizmo;
  44. RID _room_rid;
  45. public:
  46. struct SimplifyInfo {
  47. SimplifyInfo() { set_simplify(0.5); }
  48. void set_simplify(real_t p_value, real_t p_room_size = 0.0);
  49. bool add_plane_if_unique(LocalVector<Plane, int32_t> &r_planes, const Plane &p) const;
  50. real_t _plane_simplify = 0.5;
  51. real_t _plane_simplify_dot = 0.98;
  52. real_t _plane_simplify_dist = 0.08;
  53. };
  54. Room();
  55. ~Room();
  56. void set_room_simplify(real_t p_value);
  57. real_t get_room_simplify() const { return _simplify_info._plane_simplify; }
  58. // whether to use the room manager default
  59. void set_use_default_simplify(bool p_use);
  60. bool get_use_default_simplify() const { return _use_default_simplify; }
  61. void set_points(const PoolVector<Vector3> &p_points);
  62. PoolVector<Vector3> get_points() const;
  63. // primarily for the gizmo
  64. void set_point(int p_idx, const Vector3 &p_point);
  65. // editor only
  66. PoolVector<Vector3> generate_points();
  67. String get_configuration_warning() const;
  68. private:
  69. // call during each conversion
  70. void clear();
  71. void _changed(bool p_regenerate_bounds = false);
  72. template <class T>
  73. static bool detect_nodes_of_type(const Node *p_node, bool p_ignore_first_node = true);
  74. template <typename T>
  75. static bool detect_nodes_using_lambda(const Node *p_node, T p_lambda, bool p_ignore_first_node = true);
  76. // note this is client side, and does not use the final planes stored in the PortalRenderer
  77. bool contains_point(const Vector3 &p_pt) const;
  78. // planes forming convex hull of room
  79. LocalVector<Plane, int32_t> _planes;
  80. // preliminary planes are created during the first conversion pass,
  81. // they do not include the portals, and are used for identifying auto
  82. // linkage of rooms by portals
  83. LocalVector<Plane, int32_t> _preliminary_planes;
  84. Geometry::MeshData _bound_mesh_data;
  85. AABB _aabb;
  86. // editable points making up the bound
  87. PoolVector<Vector3> _bound_pts;
  88. #ifdef TOOLS_ENABLED
  89. // to help with editing, when converting, we can generate overlap zones
  90. // that occur between rooms. Ideally these should not occur, as rooms
  91. // should be convex and non-overlapping. But if they do occur, they should
  92. // be minimized.
  93. Vector<Geometry::MeshData> _gizmo_overlap_zones;
  94. #endif
  95. // makes sure lrooms are not converted more than once per
  96. // call to rooms_convert
  97. int _conversion_tick = -1;
  98. // room ID during conversion, used for matching portals links to rooms
  99. int _room_ID;
  100. // room priority allows rooms to be placed inside other rooms,
  101. // such as a house on a landscape room.
  102. // If the camera is inside more than one room, the higher priority room
  103. // will *win* (e.g. house, rather than landscape)
  104. int _room_priority = 0;
  105. // a room may be in one or several roomgroups
  106. LocalVector<int, int32_t> _roomgroups;
  107. // list of portal ids from or to this room, just used in conversion to determine room bound
  108. LocalVector<int, int32_t> _portals;
  109. // each room now stores simplification data
  110. SimplifyInfo _simplify_info;
  111. bool _use_default_simplify = true;
  112. protected:
  113. static void _bind_methods();
  114. void _notification(int p_what);
  115. };
  116. template <class T>
  117. bool Room::detect_nodes_of_type(const Node *p_node, bool p_ignore_first_node) {
  118. if (Object::cast_to<T>(p_node) && (!p_ignore_first_node)) {
  119. return true;
  120. }
  121. for (int n = 0; n < p_node->get_child_count(); n++) {
  122. if (detect_nodes_of_type<T>(p_node->get_child(n), false)) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. template <typename T>
  129. bool Room::detect_nodes_using_lambda(const Node *p_node, T p_lambda, bool p_ignore_first_node) {
  130. if (p_lambda(p_node) && !p_ignore_first_node) {
  131. return true;
  132. }
  133. for (int n = 0; n < p_node->get_child_count(); n++) {
  134. if (detect_nodes_using_lambda(p_node->get_child(n), p_lambda, false)) {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. #endif // ROOM_H