animation_node_state_machine.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**************************************************************************/
  2. /* animation_node_state_machine.h */
  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. #pragma once
  31. #include "core/math/expression.h"
  32. #include "scene/animation/animation_tree.h"
  33. class AnimationNodeStateMachineTransition : public Resource {
  34. GDCLASS(AnimationNodeStateMachineTransition, Resource);
  35. public:
  36. enum SwitchMode {
  37. SWITCH_MODE_IMMEDIATE,
  38. SWITCH_MODE_SYNC,
  39. SWITCH_MODE_AT_END,
  40. };
  41. enum AdvanceMode {
  42. ADVANCE_MODE_DISABLED,
  43. ADVANCE_MODE_ENABLED,
  44. ADVANCE_MODE_AUTO,
  45. };
  46. private:
  47. SwitchMode switch_mode = SWITCH_MODE_IMMEDIATE;
  48. AdvanceMode advance_mode = ADVANCE_MODE_ENABLED;
  49. StringName advance_condition;
  50. StringName advance_condition_name;
  51. float xfade_time = 0.0;
  52. Ref<Curve> xfade_curve;
  53. bool break_loop_at_end = false;
  54. bool reset = true;
  55. int priority = 1;
  56. String advance_expression;
  57. friend class AnimationNodeStateMachinePlayback;
  58. Ref<Expression> expression;
  59. protected:
  60. static void _bind_methods();
  61. public:
  62. void set_switch_mode(SwitchMode p_mode);
  63. SwitchMode get_switch_mode() const;
  64. void set_advance_mode(AdvanceMode p_mode);
  65. AdvanceMode get_advance_mode() const;
  66. void set_advance_condition(const StringName &p_condition);
  67. StringName get_advance_condition() const;
  68. StringName get_advance_condition_name() const;
  69. void set_advance_expression(const String &p_expression);
  70. String get_advance_expression() const;
  71. void set_xfade_time(float p_xfade);
  72. float get_xfade_time() const;
  73. void set_break_loop_at_end(bool p_enable);
  74. bool is_loop_broken_at_end() const;
  75. void set_reset(bool p_reset);
  76. bool is_reset() const;
  77. void set_xfade_curve(const Ref<Curve> &p_curve);
  78. Ref<Curve> get_xfade_curve() const;
  79. void set_priority(int p_priority);
  80. int get_priority() const;
  81. AnimationNodeStateMachineTransition();
  82. };
  83. VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::SwitchMode)
  84. VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::AdvanceMode)
  85. class AnimationNodeStateMachinePlayback;
  86. class AnimationNodeStateMachine : public AnimationRootNode {
  87. GDCLASS(AnimationNodeStateMachine, AnimationRootNode);
  88. public:
  89. enum StateMachineType {
  90. STATE_MACHINE_TYPE_ROOT,
  91. STATE_MACHINE_TYPE_NESTED,
  92. STATE_MACHINE_TYPE_GROUPED,
  93. };
  94. private:
  95. friend class AnimationNodeStateMachinePlayback;
  96. StateMachineType state_machine_type = STATE_MACHINE_TYPE_ROOT;
  97. struct State {
  98. Ref<AnimationRootNode> node;
  99. Vector2 position;
  100. };
  101. HashMap<StringName, State> states;
  102. bool allow_transition_to_self = false;
  103. bool reset_ends = false;
  104. struct Transition {
  105. StringName from;
  106. StringName to;
  107. Ref<AnimationNodeStateMachineTransition> transition;
  108. };
  109. Vector<Transition> transitions;
  110. StringName playback = "playback";
  111. bool updating_transitions = false;
  112. Vector2 graph_offset;
  113. void _remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition);
  114. void _rename_transitions(const StringName &p_name, const StringName &p_new_name);
  115. bool _can_connect(const StringName &p_name);
  116. protected:
  117. static void _bind_methods();
  118. bool _set(const StringName &p_name, const Variant &p_value);
  119. bool _get(const StringName &p_name, Variant &r_ret) const;
  120. void _get_property_list(List<PropertyInfo> *p_list) const;
  121. void _validate_property(PropertyInfo &p_property) const;
  122. bool _check_advance_condition(const Ref<AnimationNodeStateMachine> p_state_machine, const Ref<AnimationNodeStateMachineTransition> p_transition) const;
  123. virtual void _tree_changed() override;
  124. virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) override;
  125. virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node) override;
  126. virtual void reset_state() override;
  127. public:
  128. virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
  129. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  130. virtual bool is_parameter_read_only(const StringName &p_parameter) const override;
  131. void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2());
  132. void replace_node(const StringName &p_name, Ref<AnimationNode> p_node);
  133. Ref<AnimationNode> get_node(const StringName &p_name) const;
  134. void remove_node(const StringName &p_name);
  135. void rename_node(const StringName &p_name, const StringName &p_new_name);
  136. bool has_node(const StringName &p_name) const;
  137. StringName get_node_name(const Ref<AnimationNode> &p_node) const;
  138. LocalVector<StringName> get_node_list() const;
  139. TypedArray<StringName> get_node_list_as_typed_array() const;
  140. void set_node_position(const StringName &p_name, const Vector2 &p_position);
  141. Vector2 get_node_position(const StringName &p_name) const;
  142. virtual void get_child_nodes(List<ChildNode> *r_child_nodes) override;
  143. bool has_transition(const StringName &p_from, const StringName &p_to) const;
  144. bool has_transition_from(const StringName &p_from) const;
  145. bool has_transition_to(const StringName &p_to) const;
  146. int find_transition(const StringName &p_from, const StringName &p_to) const;
  147. Vector<int> find_transition_from(const StringName &p_from) const;
  148. Vector<int> find_transition_to(const StringName &p_to) const;
  149. void add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition);
  150. Ref<AnimationNodeStateMachineTransition> get_transition(int p_transition) const;
  151. StringName get_transition_from(int p_transition) const;
  152. StringName get_transition_to(int p_transition) const;
  153. int get_transition_count() const;
  154. bool is_transition_across_group(int p_transition) const;
  155. void remove_transition_by_index(const int p_transition);
  156. void remove_transition(const StringName &p_from, const StringName &p_to);
  157. void set_state_machine_type(StateMachineType p_state_machine_type);
  158. StateMachineType get_state_machine_type() const;
  159. void set_allow_transition_to_self(bool p_enable);
  160. bool is_allow_transition_to_self() const;
  161. void set_reset_ends(bool p_enable);
  162. bool are_ends_reset() const;
  163. bool can_edit_node(const StringName &p_name) const;
  164. void set_graph_offset(const Vector2 &p_offset);
  165. Vector2 get_graph_offset() const;
  166. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  167. virtual String get_caption() const override;
  168. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) const override;
  169. #ifdef TOOLS_ENABLED
  170. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  171. #endif
  172. Vector<StringName> get_nodes_with_transitions_from(const StringName &p_node) const;
  173. Vector<StringName> get_nodes_with_transitions_to(const StringName &p_node) const;
  174. AnimationNodeStateMachine();
  175. };
  176. VARIANT_ENUM_CAST(AnimationNodeStateMachine::StateMachineType);
  177. class AnimationNodeStateMachinePlayback : public Resource {
  178. GDCLASS(AnimationNodeStateMachinePlayback, Resource);
  179. friend class AnimationNodeStateMachine;
  180. struct AStarCost {
  181. float distance = 0.0;
  182. StringName prev;
  183. };
  184. struct TransitionInfo {
  185. StringName from;
  186. StringName to;
  187. StringName next;
  188. };
  189. struct NextInfo {
  190. StringName node;
  191. double xfade;
  192. Ref<Curve> curve;
  193. AnimationNodeStateMachineTransition::SwitchMode switch_mode;
  194. bool is_reset;
  195. bool break_loop_at_end;
  196. };
  197. struct ChildStateMachineInfo {
  198. Ref<AnimationNodeStateMachinePlayback> playback;
  199. Vector<StringName> path;
  200. bool is_reset = false;
  201. };
  202. Ref<AnimationNodeStateMachineTransition> default_transition;
  203. String base_path;
  204. AnimationNode::NodeTimeInfo current_nti;
  205. StringName current;
  206. Ref<Curve> current_curve;
  207. Ref<AnimationNodeStateMachineTransition> group_start_transition;
  208. Ref<AnimationNodeStateMachineTransition> group_end_transition;
  209. AnimationNode::NodeTimeInfo fadeing_from_nti;
  210. StringName fading_from;
  211. float fading_time = 0.0;
  212. float fading_pos = 0.0;
  213. Vector<StringName> path;
  214. bool playing = false;
  215. StringName start_request;
  216. StringName travel_request;
  217. bool reset_request = false;
  218. bool reset_request_on_teleport = false;
  219. bool _reset_request_for_fading_from = false;
  220. bool next_request = false;
  221. bool stop_request = false;
  222. bool teleport_request = false;
  223. bool is_grouped = false;
  224. void _clear_fading(AnimationNodeStateMachine *p_state_machine, const StringName &p_state);
  225. void _signal_state_change(AnimationTree *p_animation_tree, const StringName &p_state, bool p_started);
  226. void _travel_main(const StringName &p_state, bool p_reset_on_teleport = true);
  227. void _start_main(const StringName &p_state, bool p_reset = true);
  228. void _next_main();
  229. void _stop_main();
  230. bool _make_travel_path(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, Vector<StringName> &r_path, bool p_test_only);
  231. String _validate_path(AnimationNodeStateMachine *p_state_machine, const String &p_path);
  232. bool _travel(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, bool p_test_only);
  233. void _start(AnimationNodeStateMachine *p_state_machine);
  234. void _clear_path_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_test_only);
  235. bool _travel_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_is_allow_transition_to_self, bool p_is_parent_same_state, bool p_test_only);
  236. void _start_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_test_only);
  237. AnimationNode::NodeTimeInfo process(AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only);
  238. AnimationNode::NodeTimeInfo _process(AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only);
  239. bool _check_advance_condition(const Ref<AnimationNodeStateMachine> p_state_machine, const Ref<AnimationNodeStateMachineTransition> p_transition) const;
  240. bool _transition_to_next_recursive(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, double p_delta, bool p_test_only);
  241. NextInfo _find_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine) const;
  242. Ref<AnimationNodeStateMachineTransition> _check_group_transition(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const AnimationNodeStateMachine::Transition &p_transition, Ref<AnimationNodeStateMachine> &r_state_machine, bool &r_bypass) const;
  243. bool _can_transition_to_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, NextInfo p_next, bool p_test_only);
  244. void _set_current(AnimationNodeStateMachine *p_state_machine, const StringName &p_state);
  245. void _set_grouped(bool p_is_grouped);
  246. void _set_base_path(const String &p_base_path);
  247. Ref<AnimationNodeStateMachinePlayback> _get_parent_playback(AnimationTree *p_tree) const;
  248. Ref<AnimationNodeStateMachine> _get_parent_state_machine(AnimationTree *p_tree) const;
  249. Ref<AnimationNodeStateMachineTransition> _get_group_start_transition() const;
  250. Ref<AnimationNodeStateMachineTransition> _get_group_end_transition() const;
  251. TypedArray<StringName> _get_travel_path() const;
  252. protected:
  253. static void _bind_methods();
  254. public:
  255. void travel(const StringName &p_state, bool p_reset_on_teleport = true);
  256. void start(const StringName &p_state, bool p_reset = true);
  257. void next();
  258. void stop();
  259. bool is_playing() const;
  260. bool is_end() const;
  261. StringName get_current_node() const;
  262. StringName get_fading_from_node() const;
  263. Vector<StringName> get_travel_path() const;
  264. float get_current_play_pos() const;
  265. float get_current_length() const;
  266. float get_fade_from_play_pos() const;
  267. float get_fade_from_length() const;
  268. float get_fading_time() const;
  269. float get_fading_pos() const;
  270. void clear_path();
  271. void push_path(const StringName &p_state);
  272. AnimationNodeStateMachinePlayback();
  273. };