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