proximity_group.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**************************************************************************/
  2. /* proximity_group.cpp */
  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. #include "proximity_group.h"
  31. #include "core/math/math_funcs.h"
  32. void ProximityGroup::_clear_groups() {
  33. Map<StringName, uint32_t>::Element *E;
  34. const int size = 16;
  35. do {
  36. StringName remove_list[size];
  37. E = groups.front();
  38. int num = 0;
  39. while (E && num < size) {
  40. if (E->get() != group_version) {
  41. remove_list[num++] = E->key();
  42. }
  43. E = E->next();
  44. }
  45. for (int i = 0; i < num; i++) {
  46. groups.erase(remove_list[i]);
  47. }
  48. } while (E);
  49. }
  50. void ProximityGroup::_update_groups() {
  51. if (grid_radius == Vector3(0, 0, 0)) {
  52. return;
  53. }
  54. ++group_version;
  55. Vector3 pos = get_global_transform().get_origin();
  56. Vector3 vcell = pos / cell_size;
  57. int cell[3] = { Math::fast_ftoi(vcell.x), Math::fast_ftoi(vcell.y), Math::fast_ftoi(vcell.z) };
  58. _add_groups(cell, group_name, 0);
  59. _clear_groups();
  60. }
  61. void ProximityGroup::_add_groups(int *p_cell, String p_base, int p_depth) {
  62. p_base = p_base + "|";
  63. if (grid_radius[p_depth] == 0) {
  64. if (p_depth == 2) {
  65. _new_group(p_base);
  66. } else {
  67. _add_groups(p_cell, p_base, p_depth + 1);
  68. }
  69. }
  70. int start = p_cell[p_depth] - grid_radius[p_depth];
  71. int end = p_cell[p_depth] + grid_radius[p_depth];
  72. for (int i = start; i <= end; i++) {
  73. String gname = p_base + itos(i);
  74. if (p_depth == 2) {
  75. _new_group(gname);
  76. } else {
  77. _add_groups(p_cell, gname, p_depth + 1);
  78. }
  79. }
  80. }
  81. void ProximityGroup::_new_group(StringName p_name) {
  82. const Map<StringName, uint32_t>::Element *E = groups.find(p_name);
  83. if (!E) {
  84. add_to_group(p_name);
  85. }
  86. groups[p_name] = group_version;
  87. }
  88. void ProximityGroup::_notification(int p_what) {
  89. switch (p_what) {
  90. case NOTIFICATION_EXIT_TREE:
  91. ++group_version;
  92. _clear_groups();
  93. break;
  94. case NOTIFICATION_TRANSFORM_CHANGED:
  95. _update_groups();
  96. break;
  97. }
  98. }
  99. void ProximityGroup::broadcast(String p_method, Variant p_parameters) {
  100. Map<StringName, uint32_t>::Element *E;
  101. E = groups.front();
  102. while (E) {
  103. get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFAULT, E->key(), "_proximity_group_broadcast", p_method, p_parameters);
  104. E = E->next();
  105. }
  106. }
  107. void ProximityGroup::_proximity_group_broadcast(String p_method, Variant p_parameters) {
  108. if (dispatch_mode == MODE_PROXY) {
  109. ERR_FAIL_COND(!is_inside_tree());
  110. get_parent()->call(p_method, p_parameters);
  111. } else {
  112. emit_signal("broadcast", p_method, p_parameters);
  113. }
  114. }
  115. void ProximityGroup::set_group_name(const String &p_group_name) {
  116. group_name = p_group_name;
  117. }
  118. String ProximityGroup::get_group_name() const {
  119. return group_name;
  120. }
  121. void ProximityGroup::set_dispatch_mode(DispatchMode p_mode) {
  122. dispatch_mode = p_mode;
  123. }
  124. ProximityGroup::DispatchMode ProximityGroup::get_dispatch_mode() const {
  125. return dispatch_mode;
  126. }
  127. void ProximityGroup::set_grid_radius(const Vector3 &p_radius) {
  128. grid_radius = p_radius;
  129. }
  130. Vector3 ProximityGroup::get_grid_radius() const {
  131. return grid_radius;
  132. }
  133. void ProximityGroup::_bind_methods() {
  134. ClassDB::bind_method(D_METHOD("set_group_name", "name"), &ProximityGroup::set_group_name);
  135. ClassDB::bind_method(D_METHOD("get_group_name"), &ProximityGroup::get_group_name);
  136. ClassDB::bind_method(D_METHOD("set_dispatch_mode", "mode"), &ProximityGroup::set_dispatch_mode);
  137. ClassDB::bind_method(D_METHOD("get_dispatch_mode"), &ProximityGroup::get_dispatch_mode);
  138. ClassDB::bind_method(D_METHOD("set_grid_radius", "radius"), &ProximityGroup::set_grid_radius);
  139. ClassDB::bind_method(D_METHOD("get_grid_radius"), &ProximityGroup::get_grid_radius);
  140. ClassDB::bind_method(D_METHOD("broadcast", "method", "parameters"), &ProximityGroup::broadcast);
  141. ClassDB::bind_method(D_METHOD("_proximity_group_broadcast", "method", "parameters"), &ProximityGroup::_proximity_group_broadcast);
  142. ADD_PROPERTY(PropertyInfo(Variant::STRING, "group_name"), "set_group_name", "get_group_name");
  143. ADD_PROPERTY(PropertyInfo(Variant::INT, "dispatch_mode", PROPERTY_HINT_ENUM, "Proxy,Signal"), "set_dispatch_mode", "get_dispatch_mode");
  144. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "grid_radius"), "set_grid_radius", "get_grid_radius");
  145. ADD_SIGNAL(MethodInfo("broadcast", PropertyInfo(Variant::STRING, "method"), PropertyInfo(Variant::ARRAY, "parameters")));
  146. BIND_ENUM_CONSTANT(MODE_PROXY);
  147. BIND_ENUM_CONSTANT(MODE_SIGNAL);
  148. }
  149. ProximityGroup::ProximityGroup() {
  150. group_version = 0;
  151. dispatch_mode = MODE_PROXY;
  152. cell_size = 1.0;
  153. grid_radius = Vector3(1, 1, 1);
  154. set_notify_transform(true);
  155. }