1 /*************************************************************************/
2 /* animation_node_state_machine.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 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
31 #include "animation_node_state_machine.h"
32
33 /////////////////////////////////////////////////
34
set_switch_mode(SwitchMode p_mode)35 void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) {
36
37 switch_mode = p_mode;
38 }
39
get_switch_mode() const40 AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const {
41
42 return switch_mode;
43 }
44
set_auto_advance(bool p_enable)45 void AnimationNodeStateMachineTransition::set_auto_advance(bool p_enable) {
46 auto_advance = p_enable;
47 }
48
has_auto_advance() const49 bool AnimationNodeStateMachineTransition::has_auto_advance() const {
50 return auto_advance;
51 }
52
set_advance_condition(const StringName & p_condition)53 void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {
54 String cs = p_condition;
55 ERR_FAIL_COND(cs.find("/") != -1 || cs.find(":") != -1);
56 advance_condition = p_condition;
57 if (cs != String()) {
58 advance_condition_name = "conditions/" + cs;
59 } else {
60 advance_condition_name = StringName();
61 }
62 emit_signal("advance_condition_changed");
63 }
64
get_advance_condition() const65 StringName AnimationNodeStateMachineTransition::get_advance_condition() const {
66 return advance_condition;
67 }
68
get_advance_condition_name() const69 StringName AnimationNodeStateMachineTransition::get_advance_condition_name() const {
70 return advance_condition_name;
71 }
72
set_xfade_time(float p_xfade)73 void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {
74
75 ERR_FAIL_COND(p_xfade < 0);
76 xfade = p_xfade;
77 emit_changed();
78 }
79
get_xfade_time() const80 float AnimationNodeStateMachineTransition::get_xfade_time() const {
81 return xfade;
82 }
83
set_disabled(bool p_disabled)84 void AnimationNodeStateMachineTransition::set_disabled(bool p_disabled) {
85 disabled = p_disabled;
86 emit_changed();
87 }
88
is_disabled() const89 bool AnimationNodeStateMachineTransition::is_disabled() const {
90 return disabled;
91 }
92
set_priority(int p_priority)93 void AnimationNodeStateMachineTransition::set_priority(int p_priority) {
94 priority = p_priority;
95 emit_changed();
96 }
97
get_priority() const98 int AnimationNodeStateMachineTransition::get_priority() const {
99 return priority;
100 }
101
_bind_methods()102 void AnimationNodeStateMachineTransition::_bind_methods() {
103 ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);
104 ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);
105
106 ClassDB::bind_method(D_METHOD("set_auto_advance", "auto_advance"), &AnimationNodeStateMachineTransition::set_auto_advance);
107 ClassDB::bind_method(D_METHOD("has_auto_advance"), &AnimationNodeStateMachineTransition::has_auto_advance);
108
109 ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);
110 ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);
111
112 ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);
113 ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);
114
115 ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &AnimationNodeStateMachineTransition::set_disabled);
116 ClassDB::bind_method(D_METHOD("is_disabled"), &AnimationNodeStateMachineTransition::is_disabled);
117
118 ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);
119 ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);
120
121 ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,AtEnd"), "set_switch_mode", "get_switch_mode");
122 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_advance"), "set_auto_advance", "has_auto_advance");
123 ADD_PROPERTY(PropertyInfo(Variant::STRING, "advance_condition"), "set_advance_condition", "get_advance_condition");
124 ADD_PROPERTY(PropertyInfo(Variant::REAL, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01"), "set_xfade_time", "get_xfade_time");
125 ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");
126 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
127
128 BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);
129 BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);
130 BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);
131
132 ADD_SIGNAL(MethodInfo("advance_condition_changed"));
133 }
134
AnimationNodeStateMachineTransition()135 AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {
136
137 switch_mode = SWITCH_MODE_IMMEDIATE;
138 auto_advance = false;
139 xfade = 0;
140 disabled = false;
141 priority = 1;
142 }
143
144 ////////////////////////////////////////////////////////
145
travel(const StringName & p_state)146 void AnimationNodeStateMachinePlayback::travel(const StringName &p_state) {
147
148 start_request_travel = true;
149 start_request = p_state;
150 stop_request = false;
151 }
152
start(const StringName & p_state)153 void AnimationNodeStateMachinePlayback::start(const StringName &p_state) {
154 start_request_travel = false;
155 start_request = p_state;
156 stop_request = false;
157 }
stop()158 void AnimationNodeStateMachinePlayback::stop() {
159
160 stop_request = true;
161 }
is_playing() const162 bool AnimationNodeStateMachinePlayback::is_playing() const {
163 return playing;
164 }
get_current_node() const165 StringName AnimationNodeStateMachinePlayback::get_current_node() const {
166 return current;
167 }
get_blend_from_node() const168 StringName AnimationNodeStateMachinePlayback::get_blend_from_node() const {
169 return fading_from;
170 }
get_travel_path() const171 Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {
172 return path;
173 }
get_current_play_pos() const174 float AnimationNodeStateMachinePlayback::get_current_play_pos() const {
175 return pos_current;
176 }
get_current_length() const177 float AnimationNodeStateMachinePlayback::get_current_length() const {
178 return len_current;
179 }
180
_travel(AnimationNodeStateMachine * p_state_machine,const StringName & p_travel)181 bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) {
182
183 ERR_FAIL_COND_V(!playing, false);
184 ERR_FAIL_COND_V(!p_state_machine->states.has(p_travel), false);
185 ERR_FAIL_COND_V(!p_state_machine->states.has(current), false);
186
187 path.clear(); //a new one will be needed
188
189 if (current == p_travel)
190 return true; //nothing to do
191
192 loops_current = 0; // reset loops, so fade does not happen immediately
193
194 Vector2 current_pos = p_state_machine->states[current].position;
195 Vector2 target_pos = p_state_machine->states[p_travel].position;
196
197 Map<StringName, AStarCost> cost_map;
198
199 List<int> open_list;
200
201 //build open list
202 for (int i = 0; i < p_state_machine->transitions.size(); i++) {
203 if (p_state_machine->transitions[i].from == current) {
204 open_list.push_back(i);
205 float cost = p_state_machine->states[p_state_machine->transitions[i].to].position.distance_to(current_pos);
206 cost *= p_state_machine->transitions[i].transition->get_priority();
207 AStarCost ap;
208 ap.prev = current;
209 ap.distance = cost;
210 cost_map[p_state_machine->transitions[i].to] = ap;
211
212 if (p_state_machine->transitions[i].to == p_travel) { //prematurely found it! :D
213 path.push_back(p_travel);
214 return true;
215 }
216 }
217 }
218
219 //begin astar
220 bool found_route = false;
221 while (!found_route) {
222
223 if (open_list.size() == 0) {
224 return false; //no path found
225 }
226
227 //find the last cost transition
228 List<int>::Element *least_cost_transition = NULL;
229 float least_cost = 1e20;
230
231 for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
232
233 float cost = cost_map[p_state_machine->transitions[E->get()].to].distance;
234 cost += p_state_machine->states[p_state_machine->transitions[E->get()].to].position.distance_to(target_pos);
235
236 if (cost < least_cost) {
237 least_cost_transition = E;
238 least_cost = cost;
239 }
240 }
241
242 StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].from;
243 StringName transition = p_state_machine->transitions[least_cost_transition->get()].to;
244
245 for (int i = 0; i < p_state_machine->transitions.size(); i++) {
246 if (p_state_machine->transitions[i].from != transition || p_state_machine->transitions[i].to == transition_prev) {
247 continue; //not interested on those
248 }
249
250 float distance = p_state_machine->states[p_state_machine->transitions[i].from].position.distance_to(p_state_machine->states[p_state_machine->transitions[i].to].position);
251 distance *= p_state_machine->transitions[i].transition->get_priority();
252 distance += cost_map[p_state_machine->transitions[i].from].distance;
253
254 if (cost_map.has(p_state_machine->transitions[i].to)) {
255 //oh this was visited already, can we win the cost?
256 if (distance < cost_map[p_state_machine->transitions[i].to].distance) {
257 cost_map[p_state_machine->transitions[i].to].distance = distance;
258 cost_map[p_state_machine->transitions[i].to].prev = p_state_machine->transitions[i].from;
259 }
260 } else {
261 //add to open list
262 AStarCost ac;
263 ac.prev = p_state_machine->transitions[i].from;
264 ac.distance = distance;
265 cost_map[p_state_machine->transitions[i].to] = ac;
266
267 open_list.push_back(i);
268
269 if (p_state_machine->transitions[i].to == p_travel) {
270 found_route = true;
271 break;
272 }
273 }
274 }
275
276 if (found_route) {
277 break;
278 }
279
280 open_list.erase(least_cost_transition);
281 }
282
283 //make path
284 StringName at = p_travel;
285 while (at != current) {
286 path.push_back(at);
287 at = cost_map[at].prev;
288 }
289
290 path.invert();
291
292 return true;
293 }
294
process(AnimationNodeStateMachine * p_state_machine,float p_time,bool p_seek)295 float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, float p_time, bool p_seek) {
296
297 //if not playing and it can restart, then restart
298 if (!playing && start_request == StringName()) {
299 if (!stop_request && p_state_machine->start_node) {
300 start(p_state_machine->start_node);
301 } else {
302 return 0;
303 }
304 }
305
306 if (playing && stop_request) {
307 stop_request = false;
308 playing = false;
309 return 0;
310 }
311
312 bool play_start = false;
313
314 if (start_request != StringName()) {
315 if (start_request_travel) {
316 if (!playing) {
317 if (!stop_request && p_state_machine->start_node) {
318 // can restart, just postpone traveling
319 path.clear();
320 current = p_state_machine->start_node;
321 playing = true;
322 play_start = true;
323 } else {
324 // stopped, invalid state
325 String node_name = start_request;
326 start_request = StringName(); //clear start request
327 ERR_FAIL_V_MSG(0, "Can't travel to '" + node_name + "' if state machine is not playing.");
328 }
329 } else {
330 if (!_travel(p_state_machine, start_request)) {
331 // can't travel, then teleport
332 path.clear();
333 current = start_request;
334 }
335 start_request = StringName(); //clear start request
336 }
337 } else {
338 // teleport to start
339 path.clear();
340 current = start_request;
341 playing = true;
342 play_start = true;
343 start_request = StringName(); //clear start request
344 }
345 }
346
347 bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
348
349 if (do_start) {
350
351 if (p_state_machine->start_node != StringName() && p_seek && p_time == 0) {
352 current = p_state_machine->start_node;
353 }
354
355 len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false);
356 pos_current = 0;
357 loops_current = 0;
358 }
359
360 if (!p_state_machine->states.has(current)) {
361 playing = false; //current does not exist
362 current = StringName();
363 return 0;
364 }
365 float fade_blend = 1.0;
366
367 if (fading_from != StringName()) {
368
369 if (!p_state_machine->states.has(fading_from)) {
370 fading_from = StringName();
371 } else {
372 if (!p_seek) {
373 fading_pos += p_time;
374 }
375 fade_blend = MIN(1.0, fading_pos / fading_time);
376 if (fade_blend >= 1.0) {
377 fading_from = StringName();
378 }
379 }
380 }
381
382 float rem = p_state_machine->blend_node(current, p_state_machine->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false);
383
384 if (fading_from != StringName()) {
385
386 p_state_machine->blend_node(fading_from, p_state_machine->states[fading_from].node, p_time, p_seek, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, false);
387 }
388
389 //guess playback position
390 if (rem > len_current) { // weird but ok
391 len_current = rem;
392 }
393
394 { //advance and loop check
395
396 float next_pos = len_current - rem;
397
398 if (next_pos < pos_current) {
399 loops_current++;
400 }
401 pos_current = next_pos; //looped
402 }
403
404 //find next
405 StringName next;
406 float next_xfade = 0;
407 AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
408
409 if (path.size()) {
410
411 for (int i = 0; i < p_state_machine->transitions.size(); i++) {
412 if (p_state_machine->transitions[i].from == current && p_state_machine->transitions[i].to == path[0]) {
413 next_xfade = p_state_machine->transitions[i].transition->get_xfade_time();
414 switch_mode = p_state_machine->transitions[i].transition->get_switch_mode();
415 next = path[0];
416 }
417 }
418 } else {
419 float priority_best = 1e20;
420 int auto_advance_to = -1;
421 for (int i = 0; i < p_state_machine->transitions.size(); i++) {
422
423 bool auto_advance = false;
424 if (p_state_machine->transitions[i].transition->has_auto_advance()) {
425 auto_advance = true;
426 }
427 StringName advance_condition_name = p_state_machine->transitions[i].transition->get_advance_condition_name();
428 if (advance_condition_name != StringName() && bool(p_state_machine->get_parameter(advance_condition_name))) {
429 auto_advance = true;
430 }
431
432 if (p_state_machine->transitions[i].from == current && auto_advance) {
433
434 if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) {
435 priority_best = p_state_machine->transitions[i].transition->get_priority();
436 auto_advance_to = i;
437 }
438 }
439 }
440
441 if (auto_advance_to != -1) {
442 next = p_state_machine->transitions[auto_advance_to].to;
443 next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time();
444 switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode();
445 }
446 }
447
448 //if next, see when to transition
449 if (next != StringName()) {
450
451 bool goto_next = false;
452
453 if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
454 goto_next = next_xfade >= (len_current - pos_current) || loops_current > 0;
455 if (loops_current > 0) {
456 next_xfade = 0;
457 }
458 } else {
459 goto_next = fading_from == StringName();
460 }
461
462 if (goto_next) { //loops should be used because fade time may be too small or zero and animation may have looped
463
464 if (next_xfade) {
465 //time to fade, baby
466 fading_from = current;
467 fading_time = next_xfade;
468 fading_pos = 0;
469 } else {
470 fading_from = StringName();
471 fading_pos = 0;
472 }
473
474 if (path.size()) { //if it came from path, remove path
475 path.remove(0);
476 }
477 current = next;
478 if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
479 len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
480 pos_current = MIN(pos_current, len_current);
481 p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false);
482
483 } else {
484 len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
485 pos_current = 0;
486 }
487
488 rem = len_current; //so it does not show 0 on transition
489 loops_current = 0;
490 }
491 }
492
493 //compute time left for transitions by using the end node
494 if (p_state_machine->end_node != StringName() && p_state_machine->end_node != current) {
495
496 rem = p_state_machine->blend_node(p_state_machine->end_node, p_state_machine->states[p_state_machine->end_node].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
497 }
498
499 return rem;
500 }
501
_bind_methods()502 void AnimationNodeStateMachinePlayback::_bind_methods() {
503
504 ClassDB::bind_method(D_METHOD("travel", "to_node"), &AnimationNodeStateMachinePlayback::travel);
505 ClassDB::bind_method(D_METHOD("start", "node"), &AnimationNodeStateMachinePlayback::start);
506 ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
507 ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
508 ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
509 ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
510 }
511
AnimationNodeStateMachinePlayback()512 AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
513 set_local_to_scene(true); //only one per instanced scene
514
515 playing = false;
516 len_current = 0;
517 fading_time = 0;
518 stop_request = false;
519 }
520
521 ///////////////////////////////////////////////////////
522
get_parameter_list(List<PropertyInfo> * r_list) const523 void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
524 r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
525 List<StringName> advance_conditions;
526 for (int i = 0; i < transitions.size(); i++) {
527 StringName ac = transitions[i].transition->get_advance_condition_name();
528 if (ac != StringName() && advance_conditions.find(ac) == NULL) {
529 advance_conditions.push_back(ac);
530 }
531 }
532
533 advance_conditions.sort_custom<StringName::AlphCompare>();
534 for (List<StringName>::Element *E = advance_conditions.front(); E; E = E->next()) {
535 r_list->push_back(PropertyInfo(Variant::BOOL, E->get()));
536 }
537 }
538
get_parameter_default_value(const StringName & p_parameter) const539 Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
540
541 if (p_parameter == playback) {
542 Ref<AnimationNodeStateMachinePlayback> p;
543 p.instance();
544 return p;
545 } else {
546 return false; //advance condition
547 }
548 }
549
add_node(const StringName & p_name,Ref<AnimationNode> p_node,const Vector2 & p_position)550 void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
551
552 ERR_FAIL_COND(states.has(p_name));
553 ERR_FAIL_COND(p_node.is_null());
554 ERR_FAIL_COND(String(p_name).find("/") != -1);
555
556 State state;
557 state.node = p_node;
558 state.position = p_position;
559
560 states[p_name] = state;
561
562 emit_changed();
563 emit_signal("tree_changed");
564
565 p_node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
566 }
567
replace_node(const StringName & p_name,Ref<AnimationNode> p_node)568 void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
569
570 ERR_FAIL_COND(states.has(p_name) == false);
571 ERR_FAIL_COND(p_node.is_null());
572 ERR_FAIL_COND(String(p_name).find("/") != -1);
573
574 {
575 Ref<AnimationNode> node = states[p_name].node;
576 if (node.is_valid()) {
577 node->disconnect("tree_changed", this, "_tree_changed");
578 }
579 }
580
581 states[p_name].node = p_node;
582
583 emit_changed();
584 emit_signal("tree_changed");
585
586 p_node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
587 }
588
get_node(const StringName & p_name) const589 Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
590
591 ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
592
593 return states[p_name].node;
594 }
595
get_node_name(const Ref<AnimationNode> & p_node) const596 StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
597 for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
598 if (E->get().node == p_node) {
599 return E->key();
600 }
601 }
602
603 ERR_FAIL_V(StringName());
604 }
605
get_child_nodes(List<ChildNode> * r_child_nodes)606 void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
607 Vector<StringName> nodes;
608
609 for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
610 nodes.push_back(E->key());
611 }
612
613 nodes.sort_custom<StringName::AlphCompare>();
614
615 for (int i = 0; i < nodes.size(); i++) {
616 ChildNode cn;
617 cn.name = nodes[i];
618 cn.node = states[cn.name].node;
619 r_child_nodes->push_back(cn);
620 }
621 }
622
has_node(const StringName & p_name) const623 bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
624 return states.has(p_name);
625 }
remove_node(const StringName & p_name)626 void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
627
628 ERR_FAIL_COND(!states.has(p_name));
629
630 {
631 Ref<AnimationNode> node = states[p_name].node;
632
633 ERR_FAIL_COND(node.is_null());
634
635 node->disconnect("tree_changed", this, "_tree_changed");
636 }
637
638 states.erase(p_name);
639 //path.erase(p_name);
640
641 for (int i = 0; i < transitions.size(); i++) {
642 if (transitions[i].from == p_name || transitions[i].to == p_name) {
643 transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
644 transitions.remove(i);
645 i--;
646 }
647 }
648
649 if (start_node == p_name) {
650 start_node = StringName();
651 }
652
653 if (end_node == p_name) {
654 end_node = StringName();
655 }
656
657 /*if (playing && current == p_name) {
658 stop();
659 }*/
660
661 emit_changed();
662 emit_signal("tree_changed");
663 }
664
rename_node(const StringName & p_name,const StringName & p_new_name)665 void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
666
667 ERR_FAIL_COND(!states.has(p_name));
668 ERR_FAIL_COND(states.has(p_new_name));
669
670 states[p_new_name] = states[p_name];
671 states.erase(p_name);
672
673 for (int i = 0; i < transitions.size(); i++) {
674 if (transitions[i].from == p_name) {
675 transitions.write[i].from = p_new_name;
676 }
677
678 if (transitions[i].to == p_name) {
679 transitions.write[i].to = p_new_name;
680 }
681 }
682
683 if (start_node == p_name) {
684 start_node = p_new_name;
685 }
686
687 if (end_node == p_name) {
688 end_node = p_new_name;
689 }
690
691 /*if (playing && current == p_name) {
692 current = p_new_name;
693 }*/
694
695 //path.clear(); //clear path
696 emit_signal("tree_changed");
697 }
698
get_node_list(List<StringName> * r_nodes) const699 void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
700
701 List<StringName> nodes;
702 for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
703 nodes.push_back(E->key());
704 }
705 nodes.sort_custom<StringName::AlphCompare>();
706
707 for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) {
708 r_nodes->push_back(E->get());
709 }
710 }
711
has_transition(const StringName & p_from,const StringName & p_to) const712 bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
713
714 for (int i = 0; i < transitions.size(); i++) {
715 if (transitions[i].from == p_from && transitions[i].to == p_to)
716 return true;
717 }
718 return false;
719 }
720
find_transition(const StringName & p_from,const StringName & p_to) const721 int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
722
723 for (int i = 0; i < transitions.size(); i++) {
724 if (transitions[i].from == p_from && transitions[i].to == p_to)
725 return i;
726 }
727 return -1;
728 }
729
add_transition(const StringName & p_from,const StringName & p_to,const Ref<AnimationNodeStateMachineTransition> & p_transition)730 void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
731
732 ERR_FAIL_COND(p_from == p_to);
733 ERR_FAIL_COND(!states.has(p_from));
734 ERR_FAIL_COND(!states.has(p_to));
735 ERR_FAIL_COND(p_transition.is_null());
736
737 for (int i = 0; i < transitions.size(); i++) {
738 ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);
739 }
740
741 Transition tr;
742 tr.from = p_from;
743 tr.to = p_to;
744 tr.transition = p_transition;
745
746 tr.transition->connect("advance_condition_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
747
748 transitions.push_back(tr);
749 }
750
get_transition(int p_transition) const751 Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
752 ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
753 return transitions[p_transition].transition;
754 }
get_transition_from(int p_transition) const755 StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
756
757 ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
758 return transitions[p_transition].from;
759 }
get_transition_to(int p_transition) const760 StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
761
762 ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
763 return transitions[p_transition].to;
764 }
765
get_transition_count() const766 int AnimationNodeStateMachine::get_transition_count() const {
767
768 return transitions.size();
769 }
remove_transition(const StringName & p_from,const StringName & p_to)770 void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
771
772 for (int i = 0; i < transitions.size(); i++) {
773 if (transitions[i].from == p_from && transitions[i].to == p_to) {
774 transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
775 transitions.remove(i);
776 return;
777 }
778 }
779
780 /*if (playing) {
781 path.clear();
782 }*/
783 }
784
remove_transition_by_index(int p_transition)785 void AnimationNodeStateMachine::remove_transition_by_index(int p_transition) {
786
787 ERR_FAIL_INDEX(p_transition, transitions.size());
788 transitions.write[p_transition].transition->disconnect("advance_condition_changed", this, "_tree_changed");
789 transitions.remove(p_transition);
790 /*if (playing) {
791 path.clear();
792 }*/
793 }
794
set_start_node(const StringName & p_node)795 void AnimationNodeStateMachine::set_start_node(const StringName &p_node) {
796
797 ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
798 start_node = p_node;
799 }
800
get_start_node() const801 String AnimationNodeStateMachine::get_start_node() const {
802
803 return start_node;
804 }
805
set_end_node(const StringName & p_node)806 void AnimationNodeStateMachine::set_end_node(const StringName &p_node) {
807
808 ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
809 end_node = p_node;
810 }
811
get_end_node() const812 String AnimationNodeStateMachine::get_end_node() const {
813
814 return end_node;
815 }
816
set_graph_offset(const Vector2 & p_offset)817 void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
818 graph_offset = p_offset;
819 }
820
get_graph_offset() const821 Vector2 AnimationNodeStateMachine::get_graph_offset() const {
822 return graph_offset;
823 }
824
process(float p_time,bool p_seek)825 float AnimationNodeStateMachine::process(float p_time, bool p_seek) {
826
827 Ref<AnimationNodeStateMachinePlayback> playback = get_parameter(this->playback);
828 ERR_FAIL_COND_V(playback.is_null(), 0.0);
829
830 return playback->process(this, p_time, p_seek);
831 }
832
get_caption() const833 String AnimationNodeStateMachine::get_caption() const {
834 return "StateMachine";
835 }
836
_notification(int p_what)837 void AnimationNodeStateMachine::_notification(int p_what) {
838 }
839
get_child_by_name(const StringName & p_name)840 Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
841 return get_node(p_name);
842 }
843
_set(const StringName & p_name,const Variant & p_value)844 bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
845
846 String name = p_name;
847 if (name.begins_with("states/")) {
848 String node_name = name.get_slicec('/', 1);
849 String what = name.get_slicec('/', 2);
850
851 if (what == "node") {
852 Ref<AnimationNode> anode = p_value;
853 if (anode.is_valid()) {
854 add_node(node_name, p_value);
855 }
856 return true;
857 }
858
859 if (what == "position") {
860
861 if (states.has(node_name)) {
862 states[node_name].position = p_value;
863 }
864 return true;
865 }
866 } else if (name == "transitions") {
867
868 Array trans = p_value;
869 ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
870
871 for (int i = 0; i < trans.size(); i += 3) {
872 add_transition(trans[i], trans[i + 1], trans[i + 2]);
873 }
874 return true;
875 } else if (name == "start_node") {
876 set_start_node(p_value);
877 return true;
878 } else if (name == "end_node") {
879 set_end_node(p_value);
880 return true;
881 } else if (name == "graph_offset") {
882 set_graph_offset(p_value);
883 return true;
884 }
885
886 return false;
887 }
888
_get(const StringName & p_name,Variant & r_ret) const889 bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
890
891 String name = p_name;
892 if (name.begins_with("states/")) {
893 String node_name = name.get_slicec('/', 1);
894 String what = name.get_slicec('/', 2);
895
896 if (what == "node") {
897 if (states.has(node_name)) {
898 r_ret = states[node_name].node;
899 return true;
900 }
901 }
902
903 if (what == "position") {
904
905 if (states.has(node_name)) {
906 r_ret = states[node_name].position;
907 return true;
908 }
909 }
910 } else if (name == "transitions") {
911 Array trans;
912 trans.resize(transitions.size() * 3);
913
914 for (int i = 0; i < transitions.size(); i++) {
915 trans[i * 3 + 0] = transitions[i].from;
916 trans[i * 3 + 1] = transitions[i].to;
917 trans[i * 3 + 2] = transitions[i].transition;
918 }
919
920 r_ret = trans;
921 return true;
922 } else if (name == "start_node") {
923 r_ret = get_start_node();
924 return true;
925 } else if (name == "end_node") {
926 r_ret = get_end_node();
927 return true;
928 } else if (name == "graph_offset") {
929 r_ret = get_graph_offset();
930 return true;
931 }
932
933 return false;
934 }
_get_property_list(List<PropertyInfo> * p_list) const935 void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
936
937 List<StringName> names;
938 for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
939 names.push_back(E->key());
940 }
941 names.sort_custom<StringName::AlphCompare>();
942
943 for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
944 String name = E->get();
945 p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NOEDITOR));
946 p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
947 }
948
949 p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
950 p_list->push_back(PropertyInfo(Variant::STRING, "start_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
951 p_list->push_back(PropertyInfo(Variant::STRING, "end_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
952 p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
953 }
954
set_node_position(const StringName & p_name,const Vector2 & p_position)955 void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
956 ERR_FAIL_COND(!states.has(p_name));
957 states[p_name].position = p_position;
958 }
959
get_node_position(const StringName & p_name) const960 Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
961
962 ERR_FAIL_COND_V(!states.has(p_name), Vector2());
963 return states[p_name].position;
964 }
965
_tree_changed()966 void AnimationNodeStateMachine::_tree_changed() {
967 emit_signal("tree_changed");
968 }
969
_bind_methods()970 void AnimationNodeStateMachine::_bind_methods() {
971
972 ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
973 ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
974 ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
975 ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
976 ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
977 ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
978 ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
979
980 ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
981 ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
982
983 ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
984 ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
985 ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
986 ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
987 ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
988 ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
989 ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
990 ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
991
992 ClassDB::bind_method(D_METHOD("set_start_node", "name"), &AnimationNodeStateMachine::set_start_node);
993 ClassDB::bind_method(D_METHOD("get_start_node"), &AnimationNodeStateMachine::get_start_node);
994
995 ClassDB::bind_method(D_METHOD("set_end_node", "name"), &AnimationNodeStateMachine::set_end_node);
996 ClassDB::bind_method(D_METHOD("get_end_node"), &AnimationNodeStateMachine::get_end_node);
997
998 ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
999 ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
1000
1001 ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeStateMachine::_tree_changed);
1002 }
1003
AnimationNodeStateMachine()1004 AnimationNodeStateMachine::AnimationNodeStateMachine() {
1005
1006 playback = "playback";
1007 }
1008