animation_node_state_machine.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "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. private:
  42. SwitchMode switch_mode;
  43. bool auto_advance;
  44. StringName advance_condition;
  45. StringName advance_condition_name;
  46. float xfade;
  47. bool disabled;
  48. int priority;
  49. protected:
  50. static void _bind_methods();
  51. public:
  52. void set_switch_mode(SwitchMode p_mode);
  53. SwitchMode get_switch_mode() const;
  54. void set_auto_advance(bool p_enable);
  55. bool has_auto_advance() const;
  56. void set_advance_condition(const StringName &p_condition);
  57. StringName get_advance_condition() const;
  58. StringName get_advance_condition_name() const;
  59. void set_xfade_time(float p_xfade);
  60. float get_xfade_time() const;
  61. void set_disabled(bool p_disabled);
  62. bool is_disabled() const;
  63. void set_priority(int p_priority);
  64. int get_priority() const;
  65. AnimationNodeStateMachineTransition();
  66. };
  67. VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::SwitchMode)
  68. class AnimationNodeStateMachine;
  69. class AnimationNodeStateMachinePlayback : public Resource {
  70. GDCLASS(AnimationNodeStateMachinePlayback, Resource);
  71. friend class AnimationNodeStateMachine;
  72. struct AStarCost {
  73. float distance;
  74. StringName prev;
  75. };
  76. float len_total;
  77. float len_current;
  78. float pos_current;
  79. int loops_current;
  80. StringName current;
  81. StringName fading_from;
  82. float fading_time;
  83. float fading_pos;
  84. Vector<StringName> path;
  85. bool playing;
  86. StringName start_request;
  87. bool start_request_travel;
  88. bool stop_request;
  89. bool _travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel);
  90. float process(AnimationNodeStateMachine *p_state_machine, float p_time, bool p_seek);
  91. protected:
  92. static void _bind_methods();
  93. public:
  94. void travel(const StringName &p_state);
  95. void start(const StringName &p_state);
  96. void stop();
  97. bool is_playing() const;
  98. StringName get_current_node() const;
  99. StringName get_blend_from_node() const;
  100. Vector<StringName> get_travel_path() const;
  101. float get_current_play_pos() const;
  102. float get_current_length() const;
  103. AnimationNodeStateMachinePlayback();
  104. };
  105. class AnimationNodeStateMachine : public AnimationRootNode {
  106. GDCLASS(AnimationNodeStateMachine, AnimationRootNode);
  107. private:
  108. friend class AnimationNodeStateMachinePlayback;
  109. struct State {
  110. Ref<AnimationRootNode> node;
  111. Vector2 position;
  112. };
  113. Map<StringName, State> states;
  114. struct Transition {
  115. StringName from;
  116. StringName to;
  117. Ref<AnimationNodeStateMachineTransition> transition;
  118. };
  119. Vector<Transition> transitions;
  120. StringName playback;
  121. StringName start_node;
  122. StringName end_node;
  123. Vector2 graph_offset;
  124. void _tree_changed();
  125. protected:
  126. void _notification(int p_what);
  127. static void _bind_methods();
  128. bool _set(const StringName &p_name, const Variant &p_value);
  129. bool _get(const StringName &p_name, Variant &r_ret) const;
  130. void _get_property_list(List<PropertyInfo> *p_list) const;
  131. public:
  132. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  133. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  134. void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2());
  135. Ref<AnimationNode> get_node(const StringName &p_name) const;
  136. void remove_node(const StringName &p_name);
  137. void rename_node(const StringName &p_name, const StringName &p_new_name);
  138. bool has_node(const StringName &p_name) const;
  139. StringName get_node_name(const Ref<AnimationNode> &p_node) const;
  140. void get_node_list(List<StringName> *r_nodes) const;
  141. void set_node_position(const StringName &p_name, const Vector2 &p_position);
  142. Vector2 get_node_position(const StringName &p_name) const;
  143. virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
  144. bool has_transition(const StringName &p_from, const StringName &p_to) const;
  145. int find_transition(const StringName &p_from, const StringName &p_to) const;
  146. void add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition);
  147. Ref<AnimationNodeStateMachineTransition> get_transition(int p_transition) const;
  148. StringName get_transition_from(int p_transition) const;
  149. StringName get_transition_to(int p_transition) const;
  150. int get_transition_count() const;
  151. void remove_transition_by_index(int p_transition);
  152. void remove_transition(const StringName &p_from, const StringName &p_to);
  153. void set_start_node(const StringName &p_node);
  154. String get_start_node() const;
  155. void set_end_node(const StringName &p_node);
  156. String get_end_node() const;
  157. void set_graph_offset(const Vector2 &p_offset);
  158. Vector2 get_graph_offset() const;
  159. virtual float process(float p_time, bool p_seek);
  160. virtual String get_caption() const;
  161. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);
  162. AnimationNodeStateMachine();
  163. };
  164. #endif // ANIMATION_NODE_STATE_MACHINE_H