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