Skip to content

Commit

Permalink
Make restart in NodeStateMachine / NodeTransition optional
Browse files Browse the repository at this point in the history
  • Loading branch information
TokageItLab committed Jan 31, 2023
1 parent 5bcf016 commit 4525181
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
5 changes: 5 additions & 0 deletions doc/classes/AnimationNodeStateMachine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,9 @@
</description>
</method>
</methods>
<members>
<member name="allow_transition_to_self" type="bool" setter="set_allow_transition_to_self" getter="is_allow_transition_to_self" default="false">
If [code]true[/code], allows teleport to the self state with [method AnimationNodeStateMachinePlayback.travel]. When the reset option is enabled in [method AnimationNodeStateMachinePlayback.travel], the animation is restarted. If [code]false[/code], nothing happens on the teleportation to the self state.
</member>
</members>
</class>
3 changes: 3 additions & 0 deletions doc/classes/AnimationNodeTransition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
</method>
</methods>
<members>
<member name="allow_transition_to_self" type="bool" setter="set_allow_transition_to_self" getter="is_allow_transition_to_self" default="false">
If [code]true[/code], allows transition to the self state. When the reset option is enabled in input, the animation is restarted. If [code]false[/code], nothing happens on the transition to the self state.
</member>
<member name="input_count" type="int" setter="set_input_count" getter="get_input_count" default="0">
The number of enabled input ports for this node.
</member>
Expand Down
32 changes: 23 additions & 9 deletions scene/animation/animation_blend_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,14 @@ Ref<Curve> AnimationNodeTransition::get_xfade_curve() const {
return xfade_curve;
}

void AnimationNodeTransition::set_allow_transition_to_self(bool p_enable) {
allow_transition_to_self = p_enable;
}

bool AnimationNodeTransition::is_allow_transition_to_self() const {
return allow_transition_to_self;
}

double AnimationNodeTransition::process(double p_time, bool p_seek, bool p_is_external_seeking) {
String cur_transition_request = get_parameter(transition_request);
int cur_current_index = get_parameter(current_index);
Expand All @@ -815,20 +823,22 @@ double AnimationNodeTransition::process(double p_time, bool p_seek, bool p_is_ex
int new_idx = find_input(cur_transition_request);
if (new_idx >= 0) {
if (cur_current_index == new_idx) {
// Transition to same state.
restart = input_data[cur_current_index].reset;
cur_prev_xfading = 0;
set_parameter(prev_xfading, 0);
cur_prev_index = -1;
set_parameter(prev_index, -1);
if (allow_transition_to_self) {
// Transition to same state.
restart = input_data[cur_current_index].reset;
cur_prev_xfading = 0;
set_parameter(prev_xfading, 0);
cur_prev_index = -1;
set_parameter(prev_index, -1);
}
} else {
switched = true;
cur_prev_index = cur_current_index;
set_parameter(prev_index, cur_current_index);
cur_current_index = new_idx;
set_parameter(current_index, cur_current_index);
set_parameter(current_state, cur_transition_request);
}
cur_current_index = new_idx;
set_parameter(current_index, cur_current_index);
set_parameter(current_state, cur_transition_request);
} else {
ERR_PRINT("No such input: '" + cur_transition_request + "'");
}
Expand Down Expand Up @@ -932,8 +942,12 @@ void AnimationNodeTransition::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeTransition::set_xfade_curve);
ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeTransition::get_xfade_curve);

ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeTransition::set_allow_transition_to_self);
ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeTransition::is_allow_transition_to_self);

ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,120,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
ADD_PROPERTY(PropertyInfo(Variant::INT, "input_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED, "Inputs,input_"), "set_input_count", "get_input_count");
}

Expand Down
4 changes: 4 additions & 0 deletions scene/animation/animation_blend_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ class AnimationNodeTransition : public AnimationNodeSync {

double xfade_time = 0.0;
Ref<Curve> xfade_curve;
bool allow_transition_to_self = false;

protected:
bool _get(const StringName &p_path, Variant &r_ret) const;
Expand Down Expand Up @@ -325,6 +326,9 @@ class AnimationNodeTransition : public AnimationNodeSync {
void set_xfade_curve(const Ref<Curve> &p_curve);
Ref<Curve> get_xfade_curve() const;

void set_allow_transition_to_self(bool p_enable);
bool is_allow_transition_to_self() const;

double process(double p_time, bool p_seek, bool p_is_external_seeking) override;

AnimationNodeTransition();
Expand Down
15 changes: 14 additions & 1 deletion scene/animation/animation_node_state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_sta
path.clear(); //a new one will be needed

if (current == p_travel) {
return false; // Will teleport oneself (restart).
return !p_state_machine->is_allow_transition_to_self();
}

Vector2 current_pos = p_state_machine->states[current].position;
Expand Down Expand Up @@ -813,6 +813,14 @@ void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<Anima
p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
}

void AnimationNodeStateMachine::set_allow_transition_to_self(bool p_enable) {
allow_transition_to_self = p_enable;
}

bool AnimationNodeStateMachine::is_allow_transition_to_self() const {
return allow_transition_to_self;
}

bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
if (states.has(p_name)) {
return !(states[p_name].node->is_class("AnimationNodeStartState") || states[p_name].node->is_class("AnimationNodeEndState"));
Expand Down Expand Up @@ -1383,6 +1391,11 @@ void AnimationNodeStateMachine::_bind_methods() {

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("set_allow_transition_to_self", "enable"), &AnimationNodeStateMachine::set_allow_transition_to_self);
ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeStateMachine::is_allow_transition_to_self);

ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
}

AnimationNodeStateMachine::AnimationNodeStateMachine() {
Expand Down
4 changes: 4 additions & 0 deletions scene/animation/animation_node_state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class AnimationNodeStateMachine : public AnimationRootNode {
};

HashMap<StringName, State> states;
bool allow_transition_to_self = false;

struct Transition {
StringName from;
Expand Down Expand Up @@ -254,6 +255,9 @@ class AnimationNodeStateMachine : public AnimationRootNode {
void remove_transition_by_index(const int p_transition);
void remove_transition(const StringName &p_from, const StringName &p_to);

void set_allow_transition_to_self(bool p_enable);
bool is_allow_transition_to_self() const;

bool can_edit_node(const StringName &p_name) const;

AnimationNodeStateMachine *get_prev_state_machine() const;
Expand Down

0 comments on commit 4525181

Please sign in to comment.