animation_node_state_machine.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 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_reset(bool p_reset);
  74. bool is_reset() const;
  75. void set_xfade_curve(const Ref<Curve> &p_curve);
  76. Ref<Curve> get_xfade_curve() const;
  77. void set_priority(int p_priority);
  78. int get_priority() const;
  79. AnimationNodeStateMachineTransition();
  80. };
  81. VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::SwitchMode)
  82. VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::AdvanceMode)
  83. class AnimationNodeStateMachinePlayback;
  84. class AnimationNodeStateMachine : public AnimationRootNode {
  85. GDCLASS(AnimationNodeStateMachine, AnimationRootNode);
  86. public:
  87. enum StateMachineType {
  88. STATE_MACHINE_TYPE_ROOT,
  89. STATE_MACHINE_TYPE_NESTED,
  90. STATE_MACHINE_TYPE_GROUPED,
  91. };
  92. private:
  93. friend class AnimationNodeStateMachinePlayback;
  94. StateMachineType state_machine_type = STATE_MACHINE_TYPE_ROOT;
  95. struct State {
  96. Ref<AnimationRootNode> node;
  97. Vector2 position;
  98. };
  99. HashMap<StringName, State> states;
  100. bool allow_transition_to_self = false;
  101. bool reset_ends = false;
  102. struct Transition {
  103. StringName from;
  104. StringName to;
  105. Ref<AnimationNodeStateMachineTransition> transition;
  106. };
  107. Vector<Transition> transitions;
  108. StringName playback = "playback";
  109. bool updating_transitions = false;
  110. Vector2 graph_offset;
  111. void _remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition);
  112. void _rename_transitions(const StringName &p_name, const StringName &p_new_name);
  113. bool _can_connect(const StringName &p_name);
  114. protected:
  115. static void _bind_methods();
  116. bool _set(const StringName &p_name, const Variant &p_value);
  117. bool _get(const StringName &p_name, Variant &r_ret) const;
  118. void _get_property_list(List<PropertyInfo> *p_list) const;
  119. void _validate_property(PropertyInfo &p_property) const;
  120. bool _check_advance_condition(const Ref<AnimationNodeStateMachine> p_state_machine, const Ref<AnimationNodeStateMachineTransition> p_transition) const;
  121. virtual void _tree_changed() override;
  122. virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) override;
  123. virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node) override;
  124. virtual void reset_state() override;
  125. public:
  126. StringName start_node = "Start";
  127. StringName end_node = "End";
  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. void get_node_list(List<StringName> *r_nodes) const;
  139. void set_node_position(const StringName &p_name, const Vector2 &p_position);
  140. Vector2 get_node_position(const StringName &p_name) const;
  141. virtual void get_child_nodes(List<ChildNode> *r_child_nodes) override;
  142. bool has_transition(const StringName &p_from, const StringName &p_to) const;
  143. bool has_transition_from(const StringName &p_from) const;
  144. bool has_transition_to(const StringName &p_to) const;
  145. int find_transition(const StringName &p_from, const StringName &p_to) const;
  146. Vector<int> find_transition_from(const StringName &p_from) const;
  147. Vector<int> find_transition_to(const StringName &p_to) const;
  148. void add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition);
  149. Ref<AnimationNodeStateMachineTransition> get_transition(int p_transition) const;
  150. StringName get_transition_from(int p_transition) const;
  151. StringName get_transition_to(int p_transition) const;
  152. int get_transition_count() const;
  153. bool is_transition_across_group(int p_transition) const;
  154. void remove_transition_by_index(const int p_transition);
  155. void remove_transition(const StringName &p_from, const StringName &p_to);
  156. void set_state_machine_type(StateMachineType p_state_machine_type);
  157. StateMachineType get_state_machine_type() const;
  158. void set_allow_transition_to_self(bool p_enable);
  159. bool is_allow_transition_to_self() const;
  160. void set_reset_ends(bool p_enable);
  161. bool are_ends_reset() const;
  162. bool can_edit_node(const StringName &p_name) const;
  163. void set_graph_offset(const Vector2 &p_offset);
  164. Vector2 get_graph_offset() const;
  165. virtual double _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  166. virtual String get_caption() const override;
  167. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) const override;
  168. AnimationNodeStateMachine();
  169. };
  170. VARIANT_ENUM_CAST(AnimationNodeStateMachine::StateMachineType);
  171. class AnimationNodeStateMachinePlayback : public Resource {
  172. GDCLASS(AnimationNodeStateMachinePlayback, Resource);
  173. friend class AnimationNodeStateMachine;
  174. struct AStarCost {
  175. float distance = 0.0;
  176. StringName prev;
  177. };
  178. struct TransitionInfo {
  179. StringName from;
  180. StringName to;
  181. StringName next;
  182. };
  183. struct NextInfo {
  184. StringName node;
  185. double xfade;
  186. Ref<Curve> curve;
  187. AnimationNodeStateMachineTransition::SwitchMode switch_mode;
  188. bool is_reset;
  189. };
  190. struct ChildStateMachineInfo {
  191. Ref<AnimationNodeStateMachinePlayback> playback;
  192. Vector<StringName> path;
  193. bool is_reset = false;
  194. };
  195. Ref<AnimationNodeStateMachineTransition> default_transition;
  196. String base_path;
  197. double len_fade_from = 0.0;
  198. double pos_fade_from = 0.0;
  199. double len_current = 0.0;
  200. double pos_current = 0.0;
  201. StringName current;
  202. Ref<Curve> current_curve;
  203. Ref<AnimationNodeStateMachineTransition> group_start_transition;
  204. Ref<AnimationNodeStateMachineTransition> group_end_transition;
  205. StringName fading_from;
  206. float fading_time = 0.0;
  207. float fading_pos = 0.0;
  208. Vector<StringName> path;
  209. bool playing = false;
  210. StringName start_request;
  211. StringName travel_request;
  212. bool reset_request = false;
  213. bool reset_request_on_teleport = false;
  214. bool _reset_request_for_fading_from = false;
  215. bool next_request = false;
  216. bool stop_request = false;
  217. bool teleport_request = false;
  218. bool is_grouped = false;
  219. void _travel_main(const StringName &p_state, bool p_reset_on_teleport = true);
  220. void _start_main(const StringName &p_state, bool p_reset = true);
  221. void _next_main();
  222. void _stop_main();
  223. 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);
  224. String _validate_path(AnimationNodeStateMachine *p_state_machine, const String &p_path);
  225. bool _travel(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, bool p_test_only);
  226. void _start(AnimationNodeStateMachine *p_state_machine);
  227. void _clear_path_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_test_only);
  228. 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);
  229. void _start_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_test_only);
  230. double process(const String &p_base_path, AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only);
  231. double _process(const String &p_base_path, AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only);
  232. bool _check_advance_condition(const Ref<AnimationNodeStateMachine> p_state_machine, const Ref<AnimationNodeStateMachineTransition> p_transition) const;
  233. bool _transition_to_next_recursive(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_test_only);
  234. NextInfo _find_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine) const;
  235. 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;
  236. bool _can_transition_to_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, NextInfo p_next, bool p_test_only);
  237. void _set_current(AnimationNodeStateMachine *p_state_machine, const StringName &p_state);
  238. void _set_grouped(bool p_is_grouped);
  239. void _set_base_path(const String &p_base_path);
  240. Ref<AnimationNodeStateMachinePlayback> _get_parent_playback(AnimationTree *p_tree) const;
  241. Ref<AnimationNodeStateMachine> _get_parent_state_machine(AnimationTree *p_tree) const;
  242. Ref<AnimationNodeStateMachineTransition> _get_group_start_transition() const;
  243. Ref<AnimationNodeStateMachineTransition> _get_group_end_transition() const;
  244. TypedArray<StringName> _get_travel_path() const;
  245. protected:
  246. static void _bind_methods();
  247. public:
  248. void travel(const StringName &p_state, bool p_reset_on_teleport = true);
  249. void start(const StringName &p_state, bool p_reset = true);
  250. void next();
  251. void stop();
  252. bool is_playing() const;
  253. bool is_end() const;
  254. StringName get_current_node() const;
  255. StringName get_fading_from_node() const;
  256. Vector<StringName> get_travel_path() const;
  257. float get_current_play_pos() const;
  258. float get_current_length() const;
  259. float get_fade_from_play_pos() const;
  260. float get_fade_from_length() const;
  261. float get_fading_time() const;
  262. float get_fading_pos() const;
  263. void clear_path();
  264. void push_path(const StringName &p_state);
  265. AnimationNodeStateMachinePlayback();
  266. };
  267. #endif // ANIMATION_NODE_STATE_MACHINE_H