proximity_group.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*************************************************************************/
  2. /* proximity_group.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "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::set_group_name(String p_group_name) {
  91. group_name = p_group_name;
  92. };
  93. void ProximityGroup::_notification(int p_what) {
  94. switch (p_what) {
  95. case NOTIFICATION_EXIT_TREE:
  96. ++group_version;
  97. clear_groups();
  98. break;
  99. case NOTIFICATION_TRANSFORM_CHANGED:
  100. update_groups();
  101. break;
  102. };
  103. };
  104. void ProximityGroup::broadcast(String p_name, Variant p_params) {
  105. Map<StringName, uint32_t>::Element *E;
  106. E = groups.front();
  107. while (E) {
  108. get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFAULT, E->key(), "_proximity_group_broadcast", p_name, p_params);
  109. E = E->next();
  110. };
  111. };
  112. void ProximityGroup::_proximity_group_broadcast(String p_name, Variant p_params) {
  113. if (dispatch_mode == MODE_PROXY) {
  114. get_parent()->call(p_name, p_params);
  115. } else {
  116. emit_signal("broadcast", p_name, p_params);
  117. };
  118. };
  119. void ProximityGroup::set_dispatch_mode(int p_mode) {
  120. dispatch_mode = (DispatchMode)p_mode;
  121. };
  122. void ProximityGroup::set_grid_radius(const Vector3 &p_radius) {
  123. grid_radius = p_radius;
  124. };
  125. Vector3 ProximityGroup::get_grid_radius() const {
  126. return grid_radius;
  127. };
  128. void ProximityGroup::_bind_methods() {
  129. ClassDB::bind_method(D_METHOD("set_group_name", "name"), &ProximityGroup::set_group_name);
  130. ClassDB::bind_method(D_METHOD("broadcast", "name", "parameters"), &ProximityGroup::broadcast);
  131. ClassDB::bind_method(D_METHOD("set_dispatch_mode", "mode"), &ProximityGroup::set_dispatch_mode);
  132. ClassDB::bind_method(D_METHOD("_proximity_group_broadcast", "name", "params"), &ProximityGroup::_proximity_group_broadcast);
  133. ClassDB::bind_method(D_METHOD("set_grid_radius", "radius"), &ProximityGroup::set_grid_radius);
  134. ClassDB::bind_method(D_METHOD("get_grid_radius"), &ProximityGroup::get_grid_radius);
  135. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "grid_radius"), "set_grid_radius", "get_grid_radius");
  136. ADD_SIGNAL(MethodInfo("broadcast", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::ARRAY, "parameters")));
  137. };
  138. ProximityGroup::ProximityGroup() {
  139. group_version = 0;
  140. dispatch_mode = MODE_PROXY;
  141. grid_radius = Vector3(1, 1, 1);
  142. set_notify_transform(true);
  143. };
  144. ProximityGroup::~ProximityGroup(){
  145. };