2018-08-29 22:38:13 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* animation_node_state_machine.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-08-29 22:38:13 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2018-06-25 21:21:57 +02:00
|
|
|
#ifndef ANIMATION_NODE_STATE_MACHINE_H
|
|
|
|
#define ANIMATION_NODE_STATE_MACHINE_H
|
|
|
|
|
2022-04-20 12:36:54 +02:00
|
|
|
#include "core/math/expression.h"
|
2018-06-25 23:40:24 +02:00
|
|
|
#include "scene/animation/animation_tree.h"
|
2018-06-25 21:21:57 +02:00
|
|
|
|
|
|
|
class AnimationNodeStateMachineTransition : public Resource {
|
2019-03-19 19:35:57 +01:00
|
|
|
GDCLASS(AnimationNodeStateMachineTransition, Resource);
|
|
|
|
|
2018-06-25 21:21:57 +02:00
|
|
|
public:
|
|
|
|
enum SwitchMode {
|
|
|
|
SWITCH_MODE_IMMEDIATE,
|
|
|
|
SWITCH_MODE_SYNC,
|
|
|
|
SWITCH_MODE_AT_END,
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2021-02-07 22:29:31 +01:00
|
|
|
SwitchMode switch_mode = SWITCH_MODE_IMMEDIATE;
|
|
|
|
bool auto_advance = false;
|
2018-08-20 18:38:18 +02:00
|
|
|
StringName advance_condition;
|
|
|
|
StringName advance_condition_name;
|
2021-02-07 22:29:31 +01:00
|
|
|
float xfade = 0.0;
|
|
|
|
bool disabled = false;
|
|
|
|
int priority = 1;
|
2022-04-20 12:36:54 +02:00
|
|
|
String advance_expression;
|
|
|
|
NodePath advance_expression_base_node;
|
|
|
|
|
|
|
|
friend class AnimationNodeStateMachinePlayback;
|
|
|
|
Ref<Expression> expression;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void set_switch_mode(SwitchMode p_mode);
|
|
|
|
SwitchMode get_switch_mode() const;
|
|
|
|
|
|
|
|
void set_auto_advance(bool p_enable);
|
|
|
|
bool has_auto_advance() const;
|
|
|
|
|
2018-08-20 18:38:18 +02:00
|
|
|
void set_advance_condition(const StringName &p_condition);
|
|
|
|
StringName get_advance_condition() const;
|
|
|
|
|
|
|
|
StringName get_advance_condition_name() const;
|
|
|
|
|
2022-04-20 12:36:54 +02:00
|
|
|
void set_advance_expression(const String &p_expression);
|
|
|
|
String get_advance_expression() const;
|
|
|
|
|
|
|
|
void set_advance_expression_base_node(const NodePath &p_expression_base_node);
|
|
|
|
NodePath get_advance_expression_base_node() const;
|
|
|
|
|
2018-06-25 21:21:57 +02:00
|
|
|
void set_xfade_time(float p_xfade);
|
|
|
|
float get_xfade_time() const;
|
|
|
|
|
|
|
|
void set_disabled(bool p_disabled);
|
|
|
|
bool is_disabled() const;
|
|
|
|
|
|
|
|
void set_priority(int p_priority);
|
|
|
|
int get_priority() const;
|
|
|
|
|
|
|
|
AnimationNodeStateMachineTransition();
|
|
|
|
};
|
|
|
|
|
|
|
|
VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::SwitchMode)
|
|
|
|
|
2018-08-20 18:38:18 +02:00
|
|
|
class AnimationNodeStateMachine;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
2018-08-20 18:38:18 +02:00
|
|
|
class AnimationNodeStateMachinePlayback : public Resource {
|
|
|
|
GDCLASS(AnimationNodeStateMachinePlayback, Resource);
|
2018-06-25 21:21:57 +02:00
|
|
|
|
2018-08-20 18:38:18 +02:00
|
|
|
friend class AnimationNodeStateMachine;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
|
|
|
struct AStarCost {
|
2021-02-07 22:29:31 +01:00
|
|
|
float distance = 0.0;
|
2018-06-25 21:21:57 +02:00
|
|
|
StringName prev;
|
|
|
|
};
|
|
|
|
|
2018-12-16 15:43:20 +01:00
|
|
|
struct Transition {
|
|
|
|
StringName from;
|
|
|
|
StringName to;
|
|
|
|
StringName next;
|
|
|
|
};
|
2018-06-25 21:21:57 +02:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
float len_current = 0.0;
|
|
|
|
float pos_current = 0.0;
|
2022-04-14 20:28:42 +02:00
|
|
|
bool end_loop = false;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
|
|
|
StringName current;
|
2018-12-16 15:43:20 +01:00
|
|
|
Transition current_transition;
|
|
|
|
bool force_auto_advance = false;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
|
|
|
StringName fading_from;
|
2021-02-07 22:29:31 +01:00
|
|
|
float fading_time = 0.0;
|
|
|
|
float fading_pos = 0.0;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
|
|
|
Vector<StringName> path;
|
2021-02-07 22:29:31 +01:00
|
|
|
bool playing = false;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
2018-08-20 18:38:18 +02:00
|
|
|
StringName start_request;
|
2021-02-07 22:29:31 +01:00
|
|
|
bool start_request_travel = false;
|
|
|
|
bool stop_request = false;
|
2018-08-20 18:38:18 +02:00
|
|
|
|
|
|
|
bool _travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel);
|
|
|
|
|
2022-05-04 20:53:48 +02:00
|
|
|
double process(AnimationNodeStateMachine *p_state_machine, double p_time, bool p_seek, bool p_seek_root);
|
2018-08-20 18:38:18 +02:00
|
|
|
|
2018-12-16 15:43:20 +01:00
|
|
|
bool _check_advance_condition(const Ref<AnimationNodeStateMachine> p_state_machine, const Ref<AnimationNodeStateMachineTransition> p_transition) const;
|
|
|
|
|
2018-08-20 18:38:18 +02:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void travel(const StringName &p_state);
|
|
|
|
void start(const StringName &p_state);
|
|
|
|
void stop();
|
|
|
|
bool is_playing() const;
|
|
|
|
StringName get_current_node() const;
|
|
|
|
StringName get_blend_from_node() const;
|
|
|
|
Vector<StringName> get_travel_path() const;
|
|
|
|
float get_current_play_pos() const;
|
|
|
|
float get_current_length() const;
|
|
|
|
|
|
|
|
AnimationNodeStateMachinePlayback();
|
|
|
|
};
|
|
|
|
|
|
|
|
class AnimationNodeStateMachine : public AnimationRootNode {
|
|
|
|
GDCLASS(AnimationNodeStateMachine, AnimationRootNode);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class AnimationNodeStateMachinePlayback;
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
Ref<AnimationRootNode> node;
|
|
|
|
Vector2 position;
|
|
|
|
};
|
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<StringName, State> states;
|
2018-08-20 18:38:18 +02:00
|
|
|
|
|
|
|
struct Transition {
|
|
|
|
StringName from;
|
|
|
|
StringName to;
|
2018-12-16 15:43:20 +01:00
|
|
|
StringName local_from;
|
|
|
|
StringName local_to;
|
2018-08-20 18:38:18 +02:00
|
|
|
Ref<AnimationNodeStateMachineTransition> transition;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Transition> transitions;
|
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
StringName playback = "playback";
|
2018-12-16 15:43:20 +01:00
|
|
|
StringName state_machine_name;
|
2022-06-08 04:06:45 +02:00
|
|
|
AnimationNodeStateMachine *prev_state_machine = nullptr;
|
2018-12-16 15:43:20 +01:00
|
|
|
bool updating_transitions = false;
|
2018-08-20 18:38:18 +02:00
|
|
|
|
|
|
|
Vector2 graph_offset;
|
|
|
|
|
|
|
|
void _tree_changed();
|
2018-12-16 15:43:20 +01:00
|
|
|
void _remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition);
|
|
|
|
void _rename_transition(const StringName &p_name, const StringName &p_new_name);
|
2022-06-08 04:06:45 +02:00
|
|
|
bool _can_connect(const StringName &p_name, Vector<AnimationNodeStateMachine *> p_parents = Vector<AnimationNodeStateMachine *>());
|
2018-12-16 15:43:20 +01:00
|
|
|
StringName _get_shortest_path(const StringName &p_path) const;
|
2018-08-20 18:38:18 +02:00
|
|
|
|
2018-06-25 21:21:57 +02:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
bool _set(const StringName &p_name, const Variant &p_value);
|
|
|
|
bool _get(const StringName &p_name, Variant &r_ret) const;
|
|
|
|
void _get_property_list(List<PropertyInfo> *p_list) const;
|
2018-12-16 15:43:20 +01:00
|
|
|
bool _check_advance_condition(const Ref<AnimationNodeStateMachine> p_state_machine, const Ref<AnimationNodeStateMachineTransition> p_transition) const;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
2021-02-11 18:18:45 +01:00
|
|
|
virtual void reset_state() override;
|
|
|
|
|
2018-06-25 21:21:57 +02:00
|
|
|
public:
|
2018-12-16 15:43:20 +01:00
|
|
|
StringName start_node = "Start";
|
|
|
|
StringName end_node = "End";
|
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
|
|
|
|
virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
|
2018-08-20 18:38:18 +02:00
|
|
|
|
|
|
|
void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2());
|
2020-03-16 17:42:29 +01:00
|
|
|
void replace_node(const StringName &p_name, Ref<AnimationNode> p_node);
|
2018-06-25 21:21:57 +02:00
|
|
|
Ref<AnimationNode> get_node(const StringName &p_name) const;
|
|
|
|
void remove_node(const StringName &p_name);
|
|
|
|
void rename_node(const StringName &p_name, const StringName &p_new_name);
|
|
|
|
bool has_node(const StringName &p_name) const;
|
|
|
|
StringName get_node_name(const Ref<AnimationNode> &p_node) const;
|
|
|
|
void get_node_list(List<StringName> *r_nodes) const;
|
|
|
|
|
2018-08-20 18:38:18 +02:00
|
|
|
void set_node_position(const StringName &p_name, const Vector2 &p_position);
|
|
|
|
Vector2 get_node_position(const StringName &p_name) const;
|
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual void get_child_nodes(List<ChildNode> *r_child_nodes) override;
|
2018-08-20 18:38:18 +02:00
|
|
|
|
2018-06-25 21:21:57 +02:00
|
|
|
bool has_transition(const StringName &p_from, const StringName &p_to) const;
|
2018-12-16 15:43:20 +01:00
|
|
|
bool has_local_transition(const StringName &p_from, const StringName &p_to) const;
|
2018-06-25 21:21:57 +02:00
|
|
|
int find_transition(const StringName &p_from, const StringName &p_to) const;
|
|
|
|
void add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition);
|
|
|
|
Ref<AnimationNodeStateMachineTransition> get_transition(int p_transition) const;
|
|
|
|
StringName get_transition_from(int p_transition) const;
|
|
|
|
StringName get_transition_to(int p_transition) const;
|
|
|
|
int get_transition_count() const;
|
2018-12-16 15:43:20 +01:00
|
|
|
void remove_transition_by_index(const int p_transition);
|
2018-06-25 21:21:57 +02:00
|
|
|
void remove_transition(const StringName &p_from, const StringName &p_to);
|
|
|
|
|
2018-12-16 15:43:20 +01:00
|
|
|
bool can_edit_node(const StringName &p_name) const;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
2022-06-08 04:06:45 +02:00
|
|
|
AnimationNodeStateMachine *get_prev_state_machine() const;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
|
|
|
void set_graph_offset(const Vector2 &p_offset);
|
|
|
|
Vector2 get_graph_offset() const;
|
|
|
|
|
2022-05-04 20:53:48 +02:00
|
|
|
virtual double process(double p_time, bool p_seek, bool p_seek_root) override;
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual String get_caption() const override;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) override;
|
2018-06-25 21:21:57 +02:00
|
|
|
|
|
|
|
AnimationNodeStateMachine();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ANIMATION_NODE_STATE_MACHINE_H
|