summaryrefslogtreecommitdiff
path: root/scene/animation/animation_node_state_machine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/animation/animation_node_state_machine.cpp')
-rw-r--r--scene/animation/animation_node_state_machine.cpp128
1 files changed, 66 insertions, 62 deletions
diff --git a/scene/animation/animation_node_state_machine.cpp b/scene/animation/animation_node_state_machine.cpp
index 5df3da93e1..f1ce948c43 100644
--- a/scene/animation/animation_node_state_machine.cpp
+++ b/scene/animation/animation_node_state_machine.cpp
@@ -178,11 +178,11 @@ float AnimationNodeStateMachinePlayback::get_current_length() const {
return len_current;
}
-bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, const StringName &p_travel) {
+bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) {
ERR_FAIL_COND_V(!playing, false);
- ERR_FAIL_COND_V(!sm->states.has(p_travel), false);
- ERR_FAIL_COND_V(!sm->states.has(current), 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
@@ -191,25 +191,25 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, c
loops_current = 0; // reset loops, so fade does not happen immediately
- Vector2 current_pos = sm->states[current].position;
- Vector2 target_pos = sm->states[p_travel].position;
+ Vector2 current_pos = p_state_machine->states[current].position;
+ Vector2 target_pos = p_state_machine->states[p_travel].position;
Map<StringName, AStarCost> cost_map;
List<int> open_list;
//build open list
- for (int i = 0; i < sm->transitions.size(); i++) {
- if (sm->transitions[i].from == current) {
+ for (int i = 0; i < p_state_machine->transitions.size(); i++) {
+ if (p_state_machine->transitions[i].from == current) {
open_list.push_back(i);
- float cost = sm->states[sm->transitions[i].to].position.distance_to(current_pos);
- cost *= sm->transitions[i].transition->get_priority();
+ float cost = p_state_machine->states[p_state_machine->transitions[i].to].position.distance_to(current_pos);
+ cost *= p_state_machine->transitions[i].transition->get_priority();
AStarCost ap;
ap.prev = current;
ap.distance = cost;
- cost_map[sm->transitions[i].to] = ap;
+ cost_map[p_state_machine->transitions[i].to] = ap;
- if (sm->transitions[i].to == p_travel) { //prematurely found it! :D
+ if (p_state_machine->transitions[i].to == p_travel) { //prematurely found it! :D
path.push_back(p_travel);
return true;
}
@@ -230,42 +230,42 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, c
for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
- float cost = cost_map[sm->transitions[E->get()].to].distance;
- cost += sm->states[sm->transitions[E->get()].to].position.distance_to(target_pos);
+ 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);
if (cost < least_cost) {
least_cost_transition = E;
}
}
- StringName transition_prev = sm->transitions[least_cost_transition->get()].from;
- StringName transition = sm->transitions[least_cost_transition->get()].to;
+ StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].from;
+ StringName transition = p_state_machine->transitions[least_cost_transition->get()].to;
- for (int i = 0; i < sm->transitions.size(); i++) {
- if (sm->transitions[i].from != transition || sm->transitions[i].to == transition_prev) {
+ for (int i = 0; i < p_state_machine->transitions.size(); i++) {
+ if (p_state_machine->transitions[i].from != transition || p_state_machine->transitions[i].to == transition_prev) {
continue; //not interested on those
}
- float distance = sm->states[sm->transitions[i].from].position.distance_to(sm->states[sm->transitions[i].to].position);
- distance *= sm->transitions[i].transition->get_priority();
- distance += cost_map[sm->transitions[i].from].distance;
+ 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);
+ distance *= p_state_machine->transitions[i].transition->get_priority();
+ distance += cost_map[p_state_machine->transitions[i].from].distance;
- if (cost_map.has(sm->transitions[i].to)) {
+ if (cost_map.has(p_state_machine->transitions[i].to)) {
//oh this was visited already, can we win the cost?
- if (distance < cost_map[sm->transitions[i].to].distance) {
- cost_map[sm->transitions[i].to].distance = distance;
- cost_map[sm->transitions[i].to].prev = sm->transitions[i].from;
+ if (distance < cost_map[p_state_machine->transitions[i].to].distance) {
+ cost_map[p_state_machine->transitions[i].to].distance = distance;
+ cost_map[p_state_machine->transitions[i].to].prev = p_state_machine->transitions[i].from;
}
} else {
//add to open list
AStarCost ac;
- ac.prev = sm->transitions[i].from;
+ ac.prev = p_state_machine->transitions[i].from;
ac.distance = distance;
- cost_map[sm->transitions[i].to] = ac;
+ cost_map[p_state_machine->transitions[i].to] = ac;
open_list.push_back(i);
- if (sm->transitions[i].to == p_travel) {
+ if (p_state_machine->transitions[i].to == p_travel) {
found_route = true;
break;
}
@@ -291,12 +291,12 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, c
return true;
}
-float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, float p_time, bool p_seek) {
+float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, float p_time, bool p_seek) {
//if not playing and it can restart, then restart
if (!playing && start_request == StringName()) {
- if (!stop_request && sm->start_node) {
- start(sm->start_node);
+ if (!stop_request && p_state_machine->start_node) {
+ start(p_state_machine->start_node);
} else {
return 0;
}
@@ -314,12 +314,13 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm,
if (start_request_travel) {
if (!playing) {
+ String node_name = start_request;
start_request = StringName();
- ERR_EXPLAIN("Can't travel to '" + String(start_request) + "' if state machine is not active.");
+ ERR_EXPLAIN("Can't travel to '" + node_name + "' if state machine is not playing.");
ERR_FAIL_V(0);
}
- if (!_travel(sm, start_request)) {
+ if (!_travel(p_state_machine, start_request)) {
//can't travel, then teleport
path.clear();
current = start_request;
@@ -338,16 +339,16 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm,
if (do_start) {
- if (sm->start_node != StringName() && p_seek && p_time == 0) {
- current = sm->start_node;
+ if (p_state_machine->start_node != StringName() && p_seek && p_time == 0) {
+ current = p_state_machine->start_node;
}
- len_current = sm->blend_node(current, sm->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false);
+ len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false);
pos_current = 0;
loops_current = 0;
}
- if (!sm->states.has(current)) {
+ if (!p_state_machine->states.has(current)) {
playing = false; //current does not exist
current = StringName();
return 0;
@@ -356,7 +357,7 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm,
if (fading_from != StringName()) {
- if (!sm->states.has(fading_from)) {
+ if (!p_state_machine->states.has(fading_from)) {
fading_from = StringName();
} else {
if (!p_seek) {
@@ -369,11 +370,11 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm,
}
}
- float rem = sm->blend_node(current, sm->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false);
+ 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()) {
- sm->blend_node(fading_from, sm->states[fading_from].node, p_time, p_seek, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, false);
+ 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);
}
//guess playback position
@@ -398,40 +399,40 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm,
if (path.size()) {
- for (int i = 0; i < sm->transitions.size(); i++) {
- if (sm->transitions[i].from == current && sm->transitions[i].to == path[0]) {
- next_xfade = sm->transitions[i].transition->get_xfade_time();
- switch_mode = sm->transitions[i].transition->get_switch_mode();
+ 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();
+ switch_mode = p_state_machine->transitions[i].transition->get_switch_mode();
next = path[0];
}
}
} else {
float priority_best = 1e20;
int auto_advance_to = -1;
- for (int i = 0; i < sm->transitions.size(); i++) {
+ for (int i = 0; i < p_state_machine->transitions.size(); i++) {
bool auto_advance = false;
- if (sm->transitions[i].transition->has_auto_advance()) {
+ if (p_state_machine->transitions[i].transition->has_auto_advance()) {
auto_advance = true;
}
- StringName advance_condition_name = sm->transitions[i].transition->get_advance_condition_name();
- if (advance_condition_name != StringName() && bool(sm->get_parameter(advance_condition_name))) {
+ StringName advance_condition_name = p_state_machine->transitions[i].transition->get_advance_condition_name();
+ if (advance_condition_name != StringName() && bool(p_state_machine->get_parameter(advance_condition_name))) {
auto_advance = true;
}
- if (sm->transitions[i].from == current && auto_advance) {
+ if (p_state_machine->transitions[i].from == current && auto_advance) {
- if (sm->transitions[i].transition->get_priority() <= priority_best) {
- priority_best = sm->transitions[i].transition->get_priority();
+ 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;
}
}
}
if (auto_advance_to != -1) {
- next = sm->transitions[auto_advance_to].to;
- next_xfade = sm->transitions[auto_advance_to].transition->get_xfade_time();
- switch_mode = sm->transitions[auto_advance_to].transition->get_switch_mode();
+ next = p_state_machine->transitions[auto_advance_to].to;
+ next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time();
+ switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode();
}
}
@@ -440,13 +441,13 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm,
bool goto_next = false;
- if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE) {
- goto_next = fading_from == StringName();
- } else {
+ if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
goto_next = next_xfade >= (len_current - pos_current) || loops_current > 0;
if (loops_current > 0) {
next_xfade = 0;
}
+ } else {
+ goto_next = fading_from == StringName();
}
if (goto_next) { //loops should be used because fade time may be too small or zero and animation may have looped
@@ -466,12 +467,12 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm,
}
current = next;
if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
- len_current = sm->blend_node(current, sm->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
+ len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
pos_current = MIN(pos_current, len_current);
- sm->blend_node(current, sm->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false);
+ p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false);
} else {
- len_current = sm->blend_node(current, sm->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
+ len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
pos_current = 0;
}
@@ -481,9 +482,9 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm,
}
//compute time left for transitions by using the end node
- if (sm->end_node != StringName() && sm->end_node != current) {
+ if (p_state_machine->end_node != StringName() && p_state_machine->end_node != current) {
- rem = sm->blend_node(sm->end_node, sm->states[sm->end_node].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
+ 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);
}
return rem;
@@ -598,6 +599,9 @@ void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
{
Ref<AnimationNode> node = states[p_name].node;
+
+ ERR_FAIL_COND(node.is_null());
+
node->disconnect("tree_changed", this, "_tree_changed");
}
@@ -960,7 +964,7 @@ void AnimationNodeStateMachine::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_end_node", "name"), &AnimationNodeStateMachine::set_end_node);
ClassDB::bind_method(D_METHOD("get_end_node"), &AnimationNodeStateMachine::get_end_node);
- ClassDB::bind_method(D_METHOD("set_graph_offset", "name"), &AnimationNodeStateMachine::set_graph_offset);
+ ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeStateMachine::_tree_changed);