diff options
Diffstat (limited to 'scene/animation/animation_node_state_machine.cpp')
-rw-r--r-- | scene/animation/animation_node_state_machine.cpp | 146 |
1 files changed, 55 insertions, 91 deletions
diff --git a/scene/animation/animation_node_state_machine.cpp b/scene/animation/animation_node_state_machine.cpp index 9f5e06c43d..9fc1dbd0c6 100644 --- a/scene/animation/animation_node_state_machine.cpp +++ b/scene/animation/animation_node_state_machine.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,12 +33,10 @@ ///////////////////////////////////////////////// void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) { - switch_mode = p_mode; } AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const { - return switch_mode; } @@ -59,7 +57,7 @@ void AnimationNodeStateMachineTransition::set_advance_condition(const StringName } else { advance_condition_name = StringName(); } - emit_signal("advance_condition_changed"); + emit_signal(SNAME("advance_condition_changed")); } StringName AnimationNodeStateMachineTransition::get_advance_condition() const { @@ -71,7 +69,6 @@ StringName AnimationNodeStateMachineTransition::get_advance_condition_name() con } void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) { - ERR_FAIL_COND(p_xfade < 0); xfade = p_xfade; emit_changed(); @@ -118,7 +115,7 @@ void AnimationNodeStateMachineTransition::_bind_methods() { ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority); ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority); - ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,AtEnd"), "set_switch_mode", "get_switch_mode"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,At End"), "set_switch_mode", "get_switch_mode"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_advance"), "set_auto_advance", "has_auto_advance"); ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "advance_condition"), "set_advance_condition", "get_advance_condition"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01"), "set_xfade_time", "get_xfade_time"); @@ -133,18 +130,11 @@ void AnimationNodeStateMachineTransition::_bind_methods() { } AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() { - - switch_mode = SWITCH_MODE_IMMEDIATE; - auto_advance = false; - xfade = 0; - disabled = false; - priority = 1; } //////////////////////////////////////////////////////// void AnimationNodeStateMachinePlayback::travel(const StringName &p_state) { - start_request_travel = true; start_request = p_state; stop_request = false; @@ -155,39 +145,45 @@ void AnimationNodeStateMachinePlayback::start(const StringName &p_state) { start_request = p_state; stop_request = false; } -void AnimationNodeStateMachinePlayback::stop() { +void AnimationNodeStateMachinePlayback::stop() { stop_request = true; } + bool AnimationNodeStateMachinePlayback::is_playing() const { return playing; } + StringName AnimationNodeStateMachinePlayback::get_current_node() const { return current; } + StringName AnimationNodeStateMachinePlayback::get_blend_from_node() const { return fading_from; } + Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const { return path; } + float AnimationNodeStateMachinePlayback::get_current_play_pos() const { return pos_current; } + float AnimationNodeStateMachinePlayback::get_current_length() const { return len_current; } bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) { - ERR_FAIL_COND_V(!playing, false); ERR_FAIL_COND_V(!p_state_machine->states.has(p_travel), false); ERR_FAIL_COND_V(!p_state_machine->states.has(current), false); path.clear(); //a new one will be needed - if (current == p_travel) + if (current == p_travel) { return true; //nothing to do + } loops_current = 0; // reset loops, so fade does not happen immediately @@ -219,7 +215,6 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_sta //begin astar bool found_route = false; while (!found_route) { - if (open_list.size() == 0) { return false; //no path found } @@ -229,7 +224,6 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_sta float least_cost = 1e20; for (List<int>::Element *E = open_list.front(); E; E = E->next()) { - float cost = cost_map[p_state_machine->transitions[E->get()].to].distance; cost += p_state_machine->states[p_state_machine->transitions[E->get()].to].position.distance_to(target_pos); @@ -287,13 +281,12 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_sta at = cost_map[at].prev; } - path.invert(); + path.reverse(); return true; } -float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, float p_time, bool p_seek) { - +double AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, double p_time, bool p_seek) { //if not playing and it can restart, then restart if (!playing && start_request == StringName()) { if (!stop_request && p_state_machine->start_node) { @@ -324,7 +317,7 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st // stopped, invalid state String node_name = start_request; start_request = StringName(); //clear start request - ERR_FAIL_V_MSG(0, "Can't travel to '" + node_name + "' if state machine is not playing."); + ERR_FAIL_V_MSG(0, "Can't travel to '" + node_name + "' if state machine is not playing. Maybe you need to enable Autoplay on Load for one of the nodes in your state machine or call .start() first?"); } } else { if (!_travel(p_state_machine, start_request)) { @@ -347,7 +340,6 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st bool do_start = (p_seek && p_time == 0) || play_start || current == StringName(); if (do_start) { - if (p_state_machine->start_node != StringName() && p_seek && p_time == 0) { current = p_state_machine->start_node; } @@ -365,7 +357,6 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st float fade_blend = 1.0; if (fading_from != StringName()) { - if (!p_state_machine->states.has(fading_from)) { fading_from = StringName(); } else { @@ -382,7 +373,6 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st float rem = p_state_machine->blend_node(current, p_state_machine->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false); if (fading_from != StringName()) { - 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); } @@ -403,11 +393,10 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st //find next StringName next; - float next_xfade = 0; + float next_xfade = 0.0; AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE; if (path.size()) { - for (int i = 0; i < p_state_machine->transitions.size(); i++) { if (p_state_machine->transitions[i].from == current && p_state_machine->transitions[i].to == path[0]) { next_xfade = p_state_machine->transitions[i].transition->get_xfade_time(); @@ -419,7 +408,6 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st float priority_best = 1e20; int auto_advance_to = -1; for (int i = 0; i < p_state_machine->transitions.size(); i++) { - bool auto_advance = false; if (p_state_machine->transitions[i].transition->has_auto_advance()) { auto_advance = true; @@ -430,7 +418,6 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st } if (p_state_machine->transitions[i].from == current && auto_advance) { - if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) { priority_best = p_state_machine->transitions[i].transition->get_priority(); auto_advance_to = i; @@ -447,7 +434,6 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st //if next, see when to transition if (next != StringName()) { - bool goto_next = false; if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) { @@ -492,7 +478,6 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st //compute time left for transitions by using the end node if (p_state_machine->end_node != StringName() && p_state_machine->end_node != current) { - 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); } @@ -500,27 +485,18 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_st } void AnimationNodeStateMachinePlayback::_bind_methods() { - ClassDB::bind_method(D_METHOD("travel", "to_node"), &AnimationNodeStateMachinePlayback::travel); ClassDB::bind_method(D_METHOD("start", "node"), &AnimationNodeStateMachinePlayback::start); ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop); ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing); ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node); + ClassDB::bind_method(D_METHOD("get_current_play_position"), &AnimationNodeStateMachinePlayback::get_current_play_pos); + ClassDB::bind_method(D_METHOD("get_current_length"), &AnimationNodeStateMachinePlayback::get_current_length); ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path); } AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() { - set_local_to_scene(true); //only one per instanced scene - - playing = false; - len_current = 0; - fading_time = 0; - stop_request = false; - len_total = 0.0; - pos_current = 0.0; - loops_current = 0; - fading_pos = 0.0; - start_request_travel = false; + set_local_to_scene(true); //only one per instantiated scene } /////////////////////////////////////////////////////// @@ -536,16 +512,15 @@ void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) c } advance_conditions.sort_custom<StringName::AlphCompare>(); - for (List<StringName>::Element *E = advance_conditions.front(); E; E = E->next()) { - r_list->push_back(PropertyInfo(Variant::BOOL, E->get())); + for (const StringName &E : advance_conditions) { + r_list->push_back(PropertyInfo(Variant::BOOL, E)); } } Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const { - if (p_parameter == playback) { Ref<AnimationNodeStateMachinePlayback> p; - p.instance(); + p.instantiate(); return p; } else { return false; //advance condition @@ -553,7 +528,6 @@ Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName } void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) { - ERR_FAIL_COND(states.has(p_name)); ERR_FAIL_COND(p_node.is_null()); ERR_FAIL_COND(String(p_name).find("/") != -1); @@ -565,13 +539,12 @@ void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<Animation states[p_name] = state; emit_changed(); - emit_signal("tree_changed"); + emit_signal(SNAME("tree_changed")); p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED); } void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) { - ERR_FAIL_COND(states.has(p_name) == false); ERR_FAIL_COND(p_node.is_null()); ERR_FAIL_COND(String(p_name).find("/") != -1); @@ -579,20 +552,19 @@ void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<Anima { Ref<AnimationNode> node = states[p_name].node; if (node.is_valid()) { - node->disconnect_compat("tree_changed", this, "_tree_changed"); + node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed)); } } states[p_name].node = p_node; emit_changed(); - emit_signal("tree_changed"); + emit_signal(SNAME("tree_changed")); - p_node->connect_compat("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED); + p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED); } Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const { - ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>()); return states[p_name].node; @@ -628,8 +600,8 @@ void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) bool AnimationNodeStateMachine::has_node(const StringName &p_name) const { return states.has(p_name); } -void AnimationNodeStateMachine::remove_node(const StringName &p_name) { +void AnimationNodeStateMachine::remove_node(const StringName &p_name) { ERR_FAIL_COND(!states.has(p_name)); { @@ -664,11 +636,10 @@ void AnimationNodeStateMachine::remove_node(const StringName &p_name) { }*/ emit_changed(); - emit_signal("tree_changed"); + emit_signal(SNAME("tree_changed")); } void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) { - ERR_FAIL_COND(!states.has(p_name)); ERR_FAIL_COND(states.has(p_new_name)); @@ -698,42 +669,40 @@ void AnimationNodeStateMachine::rename_node(const StringName &p_name, const Stri }*/ //path.clear(); //clear path - emit_signal("tree_changed"); + emit_signal(SNAME("tree_changed")); } void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const { - List<StringName> nodes; for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) { nodes.push_back(E->key()); } nodes.sort_custom<StringName::AlphCompare>(); - for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) { - r_nodes->push_back(E->get()); + for (const StringName &E : nodes) { + r_nodes->push_back(E); } } bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const { - for (int i = 0; i < transitions.size(); i++) { - if (transitions[i].from == p_from && transitions[i].to == p_to) + if (transitions[i].from == p_from && transitions[i].to == p_to) { return true; + } } return false; } int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const { - for (int i = 0; i < transitions.size(); i++) { - if (transitions[i].from == p_from && transitions[i].to == p_to) + if (transitions[i].from == p_from && transitions[i].to == p_to) { return i; + } } return -1; } void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) { - ERR_FAIL_COND(p_from == p_to); ERR_FAIL_COND(!states.has(p_from)); ERR_FAIL_COND(!states.has(p_to)); @@ -757,23 +726,22 @@ Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transiti ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>()); return transitions[p_transition].transition; } -StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const { +StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const { ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName()); return transitions[p_transition].from; } -StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const { +StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const { ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName()); return transitions[p_transition].to; } int AnimationNodeStateMachine::get_transition_count() const { - return transitions.size(); } -void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) { +void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) { for (int i = 0; i < transitions.size(); i++) { if (transitions[i].from == p_from && transitions[i].to == p_to) { transitions.write[i].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed)); @@ -788,7 +756,6 @@ void AnimationNodeStateMachine::remove_transition(const StringName &p_from, cons } void AnimationNodeStateMachine::remove_transition_by_index(int p_transition) { - ERR_FAIL_INDEX(p_transition, transitions.size()); transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed)); transitions.remove(p_transition); @@ -798,24 +765,20 @@ void AnimationNodeStateMachine::remove_transition_by_index(int p_transition) { } void AnimationNodeStateMachine::set_start_node(const StringName &p_node) { - ERR_FAIL_COND(p_node != StringName() && !states.has(p_node)); start_node = p_node; } String AnimationNodeStateMachine::get_start_node() const { - return start_node; } void AnimationNodeStateMachine::set_end_node(const StringName &p_node) { - ERR_FAIL_COND(p_node != StringName() && !states.has(p_node)); end_node = p_node; } String AnimationNodeStateMachine::get_end_node() const { - return end_node; } @@ -827,8 +790,7 @@ Vector2 AnimationNodeStateMachine::get_graph_offset() const { return graph_offset; } -float AnimationNodeStateMachine::process(float p_time, bool p_seek) { - +double AnimationNodeStateMachine::process(double p_time, bool p_seek) { Ref<AnimationNodeStateMachinePlayback> playback = get_parameter(this->playback); ERR_FAIL_COND_V(playback.is_null(), 0.0); @@ -847,7 +809,6 @@ Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName } bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) { - String name = p_name; if (name.begins_with("states/")) { String node_name = name.get_slicec('/', 1); @@ -862,14 +823,12 @@ bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_ } if (what == "position") { - if (states.has(node_name)) { states[node_name].position = p_value; } return true; } } else if (name == "transitions") { - Array trans = p_value; ERR_FAIL_COND_V(trans.size() % 3 != 0, false); @@ -892,7 +851,6 @@ bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_ } bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const { - String name = p_name; if (name.begins_with("states/")) { String node_name = name.get_slicec('/', 1); @@ -906,7 +864,6 @@ bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) c } if (what == "position") { - if (states.has(node_name)) { r_ret = states[node_name].position; return true; @@ -937,16 +894,15 @@ bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) c return false; } -void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const { +void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const { List<StringName> names; for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) { names.push_back(E->key()); } names.sort_custom<StringName::AlphCompare>(); - for (List<StringName>::Element *E = names.front(); E; E = E->next()) { - String name = E->get(); + for (const StringName &name : names) { p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NOEDITOR)); p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); } @@ -957,23 +913,33 @@ void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) c p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); } +void AnimationNodeStateMachine::reset_state() { + states.clear(); + transitions.clear(); + playback = "playback"; + start_node = StringName(); + end_node = StringName(); + graph_offset = Vector2(); + + emit_changed(); + emit_signal(SNAME("tree_changed")); +} + void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) { ERR_FAIL_COND(!states.has(p_name)); states[p_name].position = p_position; } Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const { - ERR_FAIL_COND_V(!states.has(p_name), Vector2()); return states[p_name].position; } void AnimationNodeStateMachine::_tree_changed() { - emit_signal("tree_changed"); + emit_signal(SNAME("tree_changed")); } void AnimationNodeStateMachine::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2())); ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node); ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node); @@ -1005,6 +971,4 @@ void AnimationNodeStateMachine::_bind_methods() { } AnimationNodeStateMachine::AnimationNodeStateMachine() { - - playback = "playback"; } |