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