area_sw.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* area_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "area_sw.h"
  30. #include "space_sw.h"
  31. #include "body_sw.h"
  32. AreaSW::BodyKey::BodyKey(BodySW *p_body, uint32_t p_body_shape,uint32_t p_area_shape) { rid=p_body->get_self(); instance_id=p_body->get_instance_id(); body_shape=p_body_shape; area_shape=p_area_shape; }
  33. void AreaSW::_shapes_changed() {
  34. }
  35. void AreaSW::set_transform(const Transform& p_transform) {
  36. if (!moved_list.in_list() && get_space())
  37. get_space()->area_add_to_moved_list(&moved_list);
  38. _set_transform(p_transform);
  39. }
  40. void AreaSW::set_space(SpaceSW *p_space) {
  41. if (get_space()) {
  42. if (monitor_query_list.in_list())
  43. get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
  44. if (moved_list.in_list())
  45. get_space()->area_remove_from_moved_list(&moved_list);
  46. }
  47. monitored_bodies.clear();
  48. _set_space(p_space);
  49. }
  50. void AreaSW::set_monitor_callback(ObjectID p_id, const StringName& p_method) {
  51. if (p_id==monitor_callback_id) {
  52. monitor_callback_method=p_method;
  53. return;
  54. }
  55. _unregister_shapes();
  56. monitor_callback_id=p_id;
  57. monitor_callback_method=p_method;
  58. monitored_bodies.clear();
  59. _shape_changed();
  60. }
  61. void AreaSW::set_space_override_mode(PhysicsServer::AreaSpaceOverrideMode p_mode) {
  62. bool do_override=p_mode!=PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED;
  63. if (do_override==(space_override_mode!=PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED))
  64. return;
  65. _unregister_shapes();
  66. space_override_mode=p_mode;
  67. _shape_changed();
  68. }
  69. void AreaSW::set_param(PhysicsServer::AreaParameter p_param, const Variant& p_value) {
  70. switch(p_param) {
  71. case PhysicsServer::AREA_PARAM_GRAVITY: gravity=p_value; ; break;
  72. case PhysicsServer::AREA_PARAM_GRAVITY_VECTOR: gravity_vector=p_value; ; break;
  73. case PhysicsServer::AREA_PARAM_GRAVITY_IS_POINT: gravity_is_point=p_value; ; break;
  74. case PhysicsServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION: point_attenuation=p_value; ; break;
  75. case PhysicsServer::AREA_PARAM_DENSITY: density=p_value; ; break;
  76. case PhysicsServer::AREA_PARAM_PRIORITY: priority=p_value; ; break;
  77. }
  78. }
  79. Variant AreaSW::get_param(PhysicsServer::AreaParameter p_param) const {
  80. switch(p_param) {
  81. case PhysicsServer::AREA_PARAM_GRAVITY: return gravity;
  82. case PhysicsServer::AREA_PARAM_GRAVITY_VECTOR: return gravity_vector;
  83. case PhysicsServer::AREA_PARAM_GRAVITY_IS_POINT: return gravity_is_point;
  84. case PhysicsServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION: return point_attenuation;
  85. case PhysicsServer::AREA_PARAM_DENSITY: return density;
  86. case PhysicsServer::AREA_PARAM_PRIORITY: return priority;
  87. }
  88. return Variant();
  89. }
  90. void AreaSW::_queue_monitor_update() {
  91. ERR_FAIL_COND(!get_space());
  92. if (!monitor_query_list.in_list())
  93. get_space()->area_add_to_monitor_query_list(&monitor_query_list);
  94. }
  95. void AreaSW::call_queries() {
  96. if (monitor_callback_id && !monitored_bodies.empty()) {
  97. Variant res[5];
  98. Variant *resptr[5];
  99. for(int i=0;i<5;i++)
  100. resptr[i]=&res[i];
  101. Object *obj = ObjectDB::get_instance(monitor_callback_id);
  102. if (!obj) {
  103. monitored_bodies.clear();
  104. monitor_callback_id=0;
  105. return;
  106. }
  107. for (Map<BodyKey,BodyState>::Element *E=monitored_bodies.front();E;E=E->next()) {
  108. if (E->get().state==0)
  109. continue; //nothing happened
  110. res[0]=E->get().state>0 ? PhysicsServer::AREA_BODY_ADDED : PhysicsServer::AREA_BODY_REMOVED;
  111. res[1]=E->key().rid;
  112. res[2]=E->key().instance_id;
  113. res[3]=E->key().body_shape;
  114. res[4]=E->key().area_shape;
  115. Variant::CallError ce;
  116. obj->call(monitor_callback_method,(const Variant**)resptr,5,ce);
  117. }
  118. }
  119. monitored_bodies.clear();
  120. //get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
  121. }
  122. AreaSW::AreaSW() : CollisionObjectSW(TYPE_AREA), monitor_query_list(this), moved_list(this) {
  123. _set_static(true); //areas are never active
  124. space_override_mode=PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED;
  125. gravity=9.80665;
  126. gravity_vector=Vector3(0,-1,0);
  127. gravity_is_point=false;
  128. point_attenuation=1;
  129. density=0.1;
  130. priority=0;
  131. }
  132. AreaSW::~AreaSW() {
  133. }