animatable_body_2d.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**************************************************************************/
  2. /* animatable_body_2d.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 "animatable_body_2d.h"
  31. void AnimatableBody2D::set_sync_to_physics(bool p_enable) {
  32. if (sync_to_physics == p_enable) {
  33. return;
  34. }
  35. sync_to_physics = p_enable;
  36. _update_kinematic_motion();
  37. }
  38. bool AnimatableBody2D::is_sync_to_physics_enabled() const {
  39. return sync_to_physics;
  40. }
  41. void AnimatableBody2D::_update_kinematic_motion() {
  42. #ifdef TOOLS_ENABLED
  43. if (Engine::get_singleton()->is_editor_hint()) {
  44. return;
  45. }
  46. #endif
  47. if (sync_to_physics) {
  48. PhysicsServer2D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &AnimatableBody2D::_body_state_changed));
  49. set_only_update_transform_changes(true);
  50. set_notify_local_transform(true);
  51. } else {
  52. PhysicsServer2D::get_singleton()->body_set_state_sync_callback(get_rid(), Callable());
  53. set_only_update_transform_changes(false);
  54. set_notify_local_transform(false);
  55. }
  56. }
  57. void AnimatableBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
  58. if (!sync_to_physics) {
  59. return;
  60. }
  61. last_valid_transform = p_state->get_transform();
  62. set_notify_local_transform(false);
  63. set_global_transform(last_valid_transform);
  64. set_notify_local_transform(true);
  65. }
  66. void AnimatableBody2D::_notification(int p_what) {
  67. switch (p_what) {
  68. case NOTIFICATION_ENTER_TREE: {
  69. last_valid_transform = get_global_transform();
  70. _update_kinematic_motion();
  71. } break;
  72. case NOTIFICATION_EXIT_TREE: {
  73. set_only_update_transform_changes(false);
  74. set_notify_local_transform(false);
  75. } break;
  76. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  77. // Used by sync to physics, send the new transform to the physics...
  78. Transform2D new_transform = get_global_transform();
  79. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_TRANSFORM, new_transform);
  80. // ... but then revert changes.
  81. set_notify_local_transform(false);
  82. set_global_transform(last_valid_transform);
  83. set_notify_local_transform(true);
  84. } break;
  85. }
  86. }
  87. void AnimatableBody2D::_bind_methods() {
  88. ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &AnimatableBody2D::set_sync_to_physics);
  89. ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &AnimatableBody2D::is_sync_to_physics_enabled);
  90. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");
  91. }
  92. AnimatableBody2D::AnimatableBody2D() :
  93. StaticBody2D(PhysicsServer2D::BODY_MODE_KINEMATIC) {
  94. }