proximity_group.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*************************************************************************/
  2. /* proximity_group.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. {
  35. const int size = 16;
  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. };
  49. if (E) {
  50. clear_groups(); // call until we go through the whole list
  51. };
  52. };
  53. void ProximityGroup::update_groups() {
  54. if (grid_radius == Vector3(0, 0, 0))
  55. return;
  56. ++group_version;
  57. Vector3 pos = get_global_transform().get_origin();
  58. Vector3 vcell = pos / cell_size;
  59. int cell[3] = { Math::fast_ftoi(vcell.x), Math::fast_ftoi(vcell.y), Math::fast_ftoi(vcell.z) };
  60. add_groups(cell, group_name, 0);
  61. clear_groups();
  62. };
  63. void ProximityGroup::add_groups(int *p_cell, String p_base, int p_depth) {
  64. p_base = p_base + "|";
  65. if (grid_radius[p_depth] == 0) {
  66. if (p_depth == 2) {
  67. _new_group(p_base);
  68. } else {
  69. add_groups(p_cell, p_base, p_depth + 1);
  70. };
  71. };
  72. int start = p_cell[p_depth] - grid_radius[p_depth];
  73. int end = p_cell[p_depth] + grid_radius[p_depth];
  74. for (int i = start; i <= end; i++) {
  75. String gname = p_base + itos(i);
  76. if (p_depth == 2) {
  77. _new_group(gname);
  78. } else {
  79. add_groups(p_cell, gname, p_depth + 1);
  80. };
  81. };
  82. };
  83. void ProximityGroup::_new_group(StringName p_name) {
  84. const Map<StringName, uint32_t>::Element *E = groups.find(p_name);
  85. if (!E) {
  86. add_to_group(p_name);
  87. };
  88. groups[p_name] = group_version;
  89. };
  90. void ProximityGroup::_notification(int p_what) {
  91. switch (p_what) {
  92. case NOTIFICATION_EXIT_TREE:
  93. ++group_version;
  94. clear_groups();
  95. break;
  96. case NOTIFICATION_TRANSFORM_CHANGED:
  97. update_groups();
  98. break;
  99. };
  100. };
  101. void ProximityGroup::broadcast(String p_name, Variant p_params) {
  102. Map<StringName, uint32_t>::Element *E;
  103. E = groups.front();
  104. while (E) {
  105. get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFAULT, E->key(), "_proximity_group_broadcast", p_name, p_params);
  106. E = E->next();
  107. };
  108. };
  109. void ProximityGroup::_proximity_group_broadcast(String p_name, Variant p_params) {
  110. if (dispatch_mode == MODE_PROXY) {
  111. get_parent()->call(p_name, p_params);
  112. } else {
  113. emit_signal("broadcast", p_name, p_params);
  114. };
  115. };
  116. void ProximityGroup::set_group_name(const String &p_group_name) {
  117. group_name = p_group_name;
  118. };
  119. String ProximityGroup::get_group_name() const {
  120. return group_name;
  121. };
  122. void ProximityGroup::set_dispatch_mode(DispatchMode p_mode) {
  123. dispatch_mode = p_mode;
  124. };
  125. ProximityGroup::DispatchMode ProximityGroup::get_dispatch_mode() const {
  126. return dispatch_mode;
  127. };
  128. void ProximityGroup::set_grid_radius(const Vector3 &p_radius) {
  129. grid_radius = p_radius;
  130. };
  131. Vector3 ProximityGroup::get_grid_radius() const {
  132. return grid_radius;
  133. };
  134. void ProximityGroup::_bind_methods() {
  135. ClassDB::bind_method(D_METHOD("set_group_name", "name"), &ProximityGroup::set_group_name);
  136. ClassDB::bind_method(D_METHOD("get_group_name"), &ProximityGroup::get_group_name);
  137. ClassDB::bind_method(D_METHOD("set_dispatch_mode", "mode"), &ProximityGroup::set_dispatch_mode);
  138. ClassDB::bind_method(D_METHOD("get_dispatch_mode"), &ProximityGroup::get_dispatch_mode);
  139. ClassDB::bind_method(D_METHOD("set_grid_radius", "radius"), &ProximityGroup::set_grid_radius);
  140. ClassDB::bind_method(D_METHOD("get_grid_radius"), &ProximityGroup::get_grid_radius);
  141. ClassDB::bind_method(D_METHOD("broadcast", "name", "parameters"), &ProximityGroup::broadcast);
  142. ClassDB::bind_method(D_METHOD("_proximity_group_broadcast", "name", "params"), &ProximityGroup::_proximity_group_broadcast);
  143. ADD_PROPERTY(PropertyInfo(Variant::STRING, "group_name"), "set_group_name", "get_group_name");
  144. ADD_PROPERTY(PropertyInfo(Variant::INT, "dispatch_mode", PROPERTY_HINT_ENUM, "Proxy,Signal"), "set_dispatch_mode", "get_dispatch_mode");
  145. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "grid_radius"), "set_grid_radius", "get_grid_radius");
  146. ADD_SIGNAL(MethodInfo("broadcast", PropertyInfo(Variant::STRING, "group_name"), PropertyInfo(Variant::ARRAY, "parameters")));
  147. BIND_ENUM_CONSTANT(MODE_PROXY);
  148. BIND_ENUM_CONSTANT(MODE_SIGNAL);
  149. };
  150. ProximityGroup::ProximityGroup() {
  151. group_version = 0;
  152. dispatch_mode = MODE_PROXY;
  153. grid_radius = Vector3(1, 1, 1);
  154. set_notify_transform(true);
  155. };
  156. ProximityGroup::~ProximityGroup(){
  157. };