animation_node_state_machine.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #include "animation_node_state_machine.h"
  31. /////////////////////////////////////////////////
  32. void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) {
  33. switch_mode = p_mode;
  34. }
  35. AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const {
  36. return switch_mode;
  37. }
  38. void AnimationNodeStateMachineTransition::set_auto_advance(bool p_enable) {
  39. auto_advance = p_enable;
  40. }
  41. bool AnimationNodeStateMachineTransition::has_auto_advance() const {
  42. return auto_advance;
  43. }
  44. void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {
  45. String cs = p_condition;
  46. ERR_FAIL_COND(cs.find("/") != -1 || cs.find(":") != -1);
  47. advance_condition = p_condition;
  48. if (cs != String()) {
  49. advance_condition_name = "conditions/" + cs;
  50. } else {
  51. advance_condition_name = StringName();
  52. }
  53. emit_signal("advance_condition_changed");
  54. }
  55. StringName AnimationNodeStateMachineTransition::get_advance_condition() const {
  56. return advance_condition;
  57. }
  58. StringName AnimationNodeStateMachineTransition::get_advance_condition_name() const {
  59. return advance_condition_name;
  60. }
  61. void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {
  62. ERR_FAIL_COND(p_xfade < 0);
  63. xfade = p_xfade;
  64. emit_changed();
  65. }
  66. float AnimationNodeStateMachineTransition::get_xfade_time() const {
  67. return xfade;
  68. }
  69. void AnimationNodeStateMachineTransition::set_disabled(bool p_disabled) {
  70. disabled = p_disabled;
  71. emit_changed();
  72. }
  73. bool AnimationNodeStateMachineTransition::is_disabled() const {
  74. return disabled;
  75. }
  76. void AnimationNodeStateMachineTransition::set_priority(int p_priority) {
  77. priority = p_priority;
  78. emit_changed();
  79. }
  80. int AnimationNodeStateMachineTransition::get_priority() const {
  81. return priority;
  82. }
  83. void AnimationNodeStateMachineTransition::_bind_methods() {
  84. ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);
  85. ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);
  86. ClassDB::bind_method(D_METHOD("set_auto_advance", "auto_advance"), &AnimationNodeStateMachineTransition::set_auto_advance);
  87. ClassDB::bind_method(D_METHOD("has_auto_advance"), &AnimationNodeStateMachineTransition::has_auto_advance);
  88. ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);
  89. ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);
  90. ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);
  91. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);
  92. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &AnimationNodeStateMachineTransition::set_disabled);
  93. ClassDB::bind_method(D_METHOD("is_disabled"), &AnimationNodeStateMachineTransition::is_disabled);
  94. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);
  95. ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);
  96. ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,AtEnd"), "set_switch_mode", "get_switch_mode");
  97. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_advance"), "set_auto_advance", "has_auto_advance");
  98. ADD_PROPERTY(PropertyInfo(Variant::STRING, "advance_condition"), "set_advance_condition", "get_advance_condition");
  99. ADD_PROPERTY(PropertyInfo(Variant::REAL, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01"), "set_xfade_time", "get_xfade_time");
  100. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");
  101. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  102. BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);
  103. BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);
  104. BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);
  105. ADD_SIGNAL(MethodInfo("advance_condition_changed"));
  106. }
  107. AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {
  108. switch_mode = SWITCH_MODE_IMMEDIATE;
  109. auto_advance = false;
  110. xfade = 0;
  111. disabled = false;
  112. priority = 1;
  113. }
  114. ////////////////////////////////////////////////////////
  115. void AnimationNodeStateMachinePlayback::travel(const StringName &p_state) {
  116. start_request_travel = true;
  117. start_request = p_state;
  118. stop_request = false;
  119. }
  120. void AnimationNodeStateMachinePlayback::start(const StringName &p_state) {
  121. start_request_travel = false;
  122. start_request = p_state;
  123. stop_request = false;
  124. }
  125. void AnimationNodeStateMachinePlayback::stop() {
  126. stop_request = true;
  127. }
  128. bool AnimationNodeStateMachinePlayback::is_playing() const {
  129. return playing;
  130. }
  131. StringName AnimationNodeStateMachinePlayback::get_current_node() const {
  132. return current;
  133. }
  134. StringName AnimationNodeStateMachinePlayback::get_blend_from_node() const {
  135. return fading_from;
  136. }
  137. Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {
  138. return path;
  139. }
  140. float AnimationNodeStateMachinePlayback::get_current_play_pos() const {
  141. return pos_current;
  142. }
  143. float AnimationNodeStateMachinePlayback::get_current_length() const {
  144. return len_current;
  145. }
  146. bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, const StringName &p_travel) {
  147. ERR_FAIL_COND_V(!playing, false);
  148. ERR_FAIL_COND_V(!sm->states.has(p_travel), false);
  149. ERR_FAIL_COND_V(!sm->states.has(current), false);
  150. path.clear(); //a new one will be needed
  151. if (current == p_travel)
  152. return true; //nothing to do
  153. loops_current = 0; // reset loops, so fade does not happen immediately
  154. Vector2 current_pos = sm->states[current].position;
  155. Vector2 target_pos = sm->states[p_travel].position;
  156. Map<StringName, AStarCost> cost_map;
  157. List<int> open_list;
  158. //build open list
  159. for (int i = 0; i < sm->transitions.size(); i++) {
  160. if (sm->transitions[i].from == current) {
  161. open_list.push_back(i);
  162. float cost = sm->states[sm->transitions[i].to].position.distance_to(current_pos);
  163. cost *= sm->transitions[i].transition->get_priority();
  164. AStarCost ap;
  165. ap.prev = current;
  166. ap.distance = cost;
  167. cost_map[sm->transitions[i].to] = ap;
  168. if (sm->transitions[i].to == p_travel) { //prematurely found it! :D
  169. path.push_back(p_travel);
  170. return true;
  171. }
  172. }
  173. }
  174. //begin astar
  175. bool found_route = false;
  176. while (!found_route) {
  177. if (open_list.size() == 0) {
  178. return false; //no path found
  179. }
  180. //find the last cost transition
  181. List<int>::Element *least_cost_transition = NULL;
  182. float least_cost = 1e20;
  183. for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
  184. float cost = cost_map[sm->transitions[E->get()].to].distance;
  185. cost += sm->states[sm->transitions[E->get()].to].position.distance_to(target_pos);
  186. if (cost < least_cost) {
  187. least_cost_transition = E;
  188. }
  189. }
  190. StringName transition_prev = sm->transitions[least_cost_transition->get()].from;
  191. StringName transition = sm->transitions[least_cost_transition->get()].to;
  192. for (int i = 0; i < sm->transitions.size(); i++) {
  193. if (sm->transitions[i].from != transition || sm->transitions[i].to == transition_prev) {
  194. continue; //not interested on those
  195. }
  196. float distance = sm->states[sm->transitions[i].from].position.distance_to(sm->states[sm->transitions[i].to].position);
  197. distance *= sm->transitions[i].transition->get_priority();
  198. distance += cost_map[sm->transitions[i].from].distance;
  199. if (cost_map.has(sm->transitions[i].to)) {
  200. //oh this was visited already, can we win the cost?
  201. if (distance < cost_map[sm->transitions[i].to].distance) {
  202. cost_map[sm->transitions[i].to].distance = distance;
  203. cost_map[sm->transitions[i].to].prev = sm->transitions[i].from;
  204. }
  205. } else {
  206. //add to open list
  207. AStarCost ac;
  208. ac.prev = sm->transitions[i].from;
  209. ac.distance = distance;
  210. cost_map[sm->transitions[i].to] = ac;
  211. open_list.push_back(i);
  212. if (sm->transitions[i].to == p_travel) {
  213. found_route = true;
  214. break;
  215. }
  216. }
  217. }
  218. if (found_route) {
  219. break;
  220. }
  221. open_list.erase(least_cost_transition);
  222. }
  223. //make path
  224. StringName at = p_travel;
  225. while (at != current) {
  226. path.push_back(at);
  227. at = cost_map[at].prev;
  228. }
  229. path.invert();
  230. return true;
  231. }
  232. float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, float p_time, bool p_seek) {
  233. //if not playing and it can restart, then restart
  234. if (!playing && start_request == StringName()) {
  235. if (!stop_request && sm->start_node) {
  236. start(sm->start_node);
  237. } else {
  238. return 0;
  239. }
  240. }
  241. if (playing && stop_request) {
  242. stop_request = false;
  243. playing = false;
  244. return 0;
  245. }
  246. bool play_start = false;
  247. if (start_request != StringName()) {
  248. if (start_request_travel) {
  249. if (!playing) {
  250. start_request = StringName();
  251. ERR_EXPLAIN("Can't travel to '" + String(start_request) + "' if state machine is not active.");
  252. ERR_FAIL_V(0);
  253. }
  254. if (!_travel(sm, start_request)) {
  255. //can't travel, then teleport
  256. path.clear();
  257. current = start_request;
  258. }
  259. } else {
  260. path.clear();
  261. current = start_request;
  262. playing = true;
  263. play_start = true;
  264. }
  265. start_request = StringName(); //clear start request
  266. }
  267. bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
  268. if (do_start) {
  269. if (sm->start_node != StringName() && p_seek && p_time == 0) {
  270. current = sm->start_node;
  271. }
  272. len_current = sm->blend_node(current, sm->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false);
  273. pos_current = 0;
  274. loops_current = 0;
  275. }
  276. if (!sm->states.has(current)) {
  277. playing = false; //current does not exist
  278. current = StringName();
  279. return 0;
  280. }
  281. float fade_blend = 1.0;
  282. if (fading_from != StringName()) {
  283. if (!sm->states.has(fading_from)) {
  284. fading_from = StringName();
  285. } else {
  286. if (!p_seek) {
  287. fading_pos += p_time;
  288. }
  289. fade_blend = MIN(1.0, fading_pos / fading_time);
  290. if (fade_blend >= 1.0) {
  291. fading_from = StringName();
  292. }
  293. }
  294. }
  295. float rem = sm->blend_node(current, sm->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false);
  296. if (fading_from != StringName()) {
  297. sm->blend_node(current, sm->states[fading_from].node, p_time, p_seek, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, false);
  298. }
  299. //guess playback position
  300. if (rem > len_current) { // weird but ok
  301. len_current = rem;
  302. }
  303. { //advance and loop check
  304. float next_pos = len_current - rem;
  305. if (next_pos < pos_current) {
  306. loops_current++;
  307. }
  308. pos_current = next_pos; //looped
  309. }
  310. //find next
  311. StringName next;
  312. float next_xfade = 0;
  313. AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
  314. if (path.size()) {
  315. for (int i = 0; i < sm->transitions.size(); i++) {
  316. if (sm->transitions[i].from == current && sm->transitions[i].to == path[0]) {
  317. next_xfade = sm->transitions[i].transition->get_xfade_time();
  318. switch_mode = sm->transitions[i].transition->get_switch_mode();
  319. next = path[0];
  320. }
  321. }
  322. } else {
  323. float priority_best = 1e20;
  324. int auto_advance_to = -1;
  325. for (int i = 0; i < sm->transitions.size(); i++) {
  326. bool auto_advance = false;
  327. if (sm->transitions[i].transition->has_auto_advance()) {
  328. auto_advance = true;
  329. }
  330. StringName advance_condition_name = sm->transitions[i].transition->get_advance_condition_name();
  331. if (advance_condition_name != StringName() && bool(sm->get_parameter(advance_condition_name))) {
  332. auto_advance = true;
  333. }
  334. if (sm->transitions[i].from == current && auto_advance) {
  335. if (sm->transitions[i].transition->get_priority() < priority_best) {
  336. auto_advance_to = i;
  337. }
  338. }
  339. }
  340. if (auto_advance_to != -1) {
  341. next = sm->transitions[auto_advance_to].to;
  342. next_xfade = sm->transitions[auto_advance_to].transition->get_xfade_time();
  343. switch_mode = sm->transitions[auto_advance_to].transition->get_switch_mode();
  344. }
  345. }
  346. //if next, see when to transition
  347. if (next != StringName()) {
  348. bool goto_next = false;
  349. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE) {
  350. goto_next = fading_from == StringName();
  351. } else {
  352. goto_next = next_xfade >= (len_current - pos_current) || loops_current > 0;
  353. if (loops_current > 0) {
  354. next_xfade = 0;
  355. }
  356. }
  357. if (goto_next) { //loops should be used because fade time may be too small or zero and animation may have looped
  358. if (next_xfade) {
  359. //time to fade, baby
  360. fading_from = current;
  361. fading_time = next_xfade;
  362. fading_pos = 0;
  363. } else {
  364. fading_from = StringName();
  365. fading_pos = 0;
  366. }
  367. if (path.size()) { //if it came from path, remove path
  368. path.remove(0);
  369. }
  370. current = next;
  371. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
  372. len_current = sm->blend_node(current, sm->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
  373. pos_current = MIN(pos_current, len_current);
  374. sm->blend_node(current, sm->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false);
  375. } else {
  376. len_current = sm->blend_node(current, sm->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
  377. pos_current = 0;
  378. }
  379. rem = len_current; //so it does not show 0 on transition
  380. loops_current = 0;
  381. }
  382. }
  383. //compute time left for transitions by using the end node
  384. if (sm->end_node != StringName() && sm->end_node != current) {
  385. rem = sm->blend_node(sm->end_node, sm->states[sm->end_node].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
  386. }
  387. return rem;
  388. }
  389. void AnimationNodeStateMachinePlayback::_bind_methods() {
  390. ClassDB::bind_method(D_METHOD("travel", "to_node"), &AnimationNodeStateMachinePlayback::travel);
  391. ClassDB::bind_method(D_METHOD("start", "node"), &AnimationNodeStateMachinePlayback::start);
  392. ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
  393. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
  394. ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
  395. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
  396. }
  397. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  398. set_local_to_scene(true); //only one per instanced scene
  399. playing = false;
  400. len_current = 0;
  401. fading_time = 0;
  402. stop_request = false;
  403. }
  404. ///////////////////////////////////////////////////////
  405. void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
  406. r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  407. List<StringName> advance_conditions;
  408. for (int i = 0; i < transitions.size(); i++) {
  409. StringName ac = transitions[i].transition->get_advance_condition_name();
  410. if (ac != StringName() && advance_conditions.find(ac) == NULL) {
  411. advance_conditions.push_back(ac);
  412. }
  413. }
  414. advance_conditions.sort_custom<StringName::AlphCompare>();
  415. for (List<StringName>::Element *E = advance_conditions.front(); E; E = E->next()) {
  416. r_list->push_back(PropertyInfo(Variant::BOOL, E->get()));
  417. }
  418. }
  419. Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
  420. if (p_parameter == playback) {
  421. Ref<AnimationNodeStateMachinePlayback> p;
  422. p.instance();
  423. return p;
  424. } else {
  425. return false; //advance condition
  426. }
  427. }
  428. void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  429. ERR_FAIL_COND(states.has(p_name));
  430. ERR_FAIL_COND(p_node.is_null());
  431. ERR_FAIL_COND(String(p_name).find("/") != -1);
  432. State state;
  433. state.node = p_node;
  434. state.position = p_position;
  435. states[p_name] = state;
  436. emit_changed();
  437. emit_signal("tree_changed");
  438. p_node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
  439. }
  440. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  441. ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
  442. return states[p_name].node;
  443. }
  444. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  445. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  446. if (E->get().node == p_node) {
  447. return E->key();
  448. }
  449. }
  450. ERR_FAIL_V(StringName());
  451. }
  452. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  453. Vector<StringName> nodes;
  454. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  455. nodes.push_back(E->key());
  456. }
  457. nodes.sort_custom<StringName::AlphCompare>();
  458. for (int i = 0; i < nodes.size(); i++) {
  459. ChildNode cn;
  460. cn.name = nodes[i];
  461. cn.node = states[cn.name].node;
  462. r_child_nodes->push_back(cn);
  463. }
  464. }
  465. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  466. return states.has(p_name);
  467. }
  468. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  469. ERR_FAIL_COND(!states.has(p_name));
  470. {
  471. Ref<AnimationNode> node = states[p_name].node;
  472. node->disconnect("tree_changed", this, "_tree_changed");
  473. }
  474. states.erase(p_name);
  475. //path.erase(p_name);
  476. for (int i = 0; i < transitions.size(); i++) {
  477. if (transitions[i].from == p_name || transitions[i].to == p_name) {
  478. transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
  479. transitions.remove(i);
  480. i--;
  481. }
  482. }
  483. if (start_node == p_name) {
  484. start_node = StringName();
  485. }
  486. if (end_node == p_name) {
  487. end_node = StringName();
  488. }
  489. /*if (playing && current == p_name) {
  490. stop();
  491. }*/
  492. emit_changed();
  493. emit_signal("tree_changed");
  494. }
  495. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  496. ERR_FAIL_COND(!states.has(p_name));
  497. ERR_FAIL_COND(states.has(p_new_name));
  498. states[p_new_name] = states[p_name];
  499. states.erase(p_name);
  500. for (int i = 0; i < transitions.size(); i++) {
  501. if (transitions[i].from == p_name) {
  502. transitions.write[i].from = p_new_name;
  503. }
  504. if (transitions[i].to == p_name) {
  505. transitions.write[i].to = p_new_name;
  506. }
  507. }
  508. if (start_node == p_name) {
  509. start_node = p_new_name;
  510. }
  511. if (end_node == p_name) {
  512. end_node = p_new_name;
  513. }
  514. /*if (playing && current == p_name) {
  515. current = p_new_name;
  516. }*/
  517. //path.clear(); //clear path
  518. emit_signal("tree_changed");
  519. }
  520. void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
  521. List<StringName> nodes;
  522. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  523. nodes.push_back(E->key());
  524. }
  525. nodes.sort_custom<StringName::AlphCompare>();
  526. for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) {
  527. r_nodes->push_back(E->get());
  528. }
  529. }
  530. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  531. for (int i = 0; i < transitions.size(); i++) {
  532. if (transitions[i].from == p_from && transitions[i].to == p_to)
  533. return true;
  534. }
  535. return false;
  536. }
  537. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  538. for (int i = 0; i < transitions.size(); i++) {
  539. if (transitions[i].from == p_from && transitions[i].to == p_to)
  540. return i;
  541. }
  542. return -1;
  543. }
  544. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  545. ERR_FAIL_COND(p_from == p_to);
  546. ERR_FAIL_COND(!states.has(p_from));
  547. ERR_FAIL_COND(!states.has(p_to));
  548. ERR_FAIL_COND(p_transition.is_null());
  549. for (int i = 0; i < transitions.size(); i++) {
  550. ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);
  551. }
  552. Transition tr;
  553. tr.from = p_from;
  554. tr.to = p_to;
  555. tr.transition = p_transition;
  556. tr.transition->connect("advance_condition_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
  557. transitions.push_back(tr);
  558. }
  559. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  560. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  561. return transitions[p_transition].transition;
  562. }
  563. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  564. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  565. return transitions[p_transition].from;
  566. }
  567. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  568. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  569. return transitions[p_transition].to;
  570. }
  571. int AnimationNodeStateMachine::get_transition_count() const {
  572. return transitions.size();
  573. }
  574. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  575. for (int i = 0; i < transitions.size(); i++) {
  576. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  577. transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
  578. transitions.remove(i);
  579. return;
  580. }
  581. }
  582. /*if (playing) {
  583. path.clear();
  584. }*/
  585. }
  586. void AnimationNodeStateMachine::remove_transition_by_index(int p_transition) {
  587. ERR_FAIL_INDEX(p_transition, transitions.size());
  588. transitions.write[p_transition].transition->disconnect("advance_condition_changed", this, "_tree_changed");
  589. transitions.remove(p_transition);
  590. /*if (playing) {
  591. path.clear();
  592. }*/
  593. }
  594. void AnimationNodeStateMachine::set_start_node(const StringName &p_node) {
  595. ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
  596. start_node = p_node;
  597. }
  598. String AnimationNodeStateMachine::get_start_node() const {
  599. return start_node;
  600. }
  601. void AnimationNodeStateMachine::set_end_node(const StringName &p_node) {
  602. ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
  603. end_node = p_node;
  604. }
  605. String AnimationNodeStateMachine::get_end_node() const {
  606. return end_node;
  607. }
  608. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  609. graph_offset = p_offset;
  610. }
  611. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  612. return graph_offset;
  613. }
  614. float AnimationNodeStateMachine::process(float p_time, bool p_seek) {
  615. Ref<AnimationNodeStateMachinePlayback> playback = get_parameter(this->playback);
  616. ERR_FAIL_COND_V(playback.is_null(), 0.0);
  617. return playback->process(this, p_time, p_seek);
  618. }
  619. String AnimationNodeStateMachine::get_caption() const {
  620. return "StateMachine";
  621. }
  622. void AnimationNodeStateMachine::_notification(int p_what) {
  623. }
  624. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
  625. return get_node(p_name);
  626. }
  627. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  628. String name = p_name;
  629. if (name.begins_with("states/")) {
  630. String node_name = name.get_slicec('/', 1);
  631. String what = name.get_slicec('/', 2);
  632. if (what == "node") {
  633. Ref<AnimationNode> anode = p_value;
  634. if (anode.is_valid()) {
  635. add_node(node_name, p_value);
  636. }
  637. return true;
  638. }
  639. if (what == "position") {
  640. if (states.has(node_name)) {
  641. states[node_name].position = p_value;
  642. }
  643. return true;
  644. }
  645. } else if (name == "transitions") {
  646. Array trans = p_value;
  647. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  648. for (int i = 0; i < trans.size(); i += 3) {
  649. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  650. }
  651. return true;
  652. } else if (name == "start_node") {
  653. set_start_node(p_value);
  654. return true;
  655. } else if (name == "end_node") {
  656. set_end_node(p_value);
  657. return true;
  658. } else if (name == "graph_offset") {
  659. set_graph_offset(p_value);
  660. return true;
  661. }
  662. return false;
  663. }
  664. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  665. String name = p_name;
  666. if (name.begins_with("states/")) {
  667. String node_name = name.get_slicec('/', 1);
  668. String what = name.get_slicec('/', 2);
  669. if (what == "node") {
  670. if (states.has(node_name)) {
  671. r_ret = states[node_name].node;
  672. return true;
  673. }
  674. }
  675. if (what == "position") {
  676. if (states.has(node_name)) {
  677. r_ret = states[node_name].position;
  678. return true;
  679. }
  680. }
  681. } else if (name == "transitions") {
  682. Array trans;
  683. trans.resize(transitions.size() * 3);
  684. for (int i = 0; i < transitions.size(); i++) {
  685. trans[i * 3 + 0] = transitions[i].from;
  686. trans[i * 3 + 1] = transitions[i].to;
  687. trans[i * 3 + 2] = transitions[i].transition;
  688. }
  689. r_ret = trans;
  690. return true;
  691. } else if (name == "start_node") {
  692. r_ret = get_start_node();
  693. return true;
  694. } else if (name == "end_node") {
  695. r_ret = get_end_node();
  696. return true;
  697. } else if (name == "graph_offset") {
  698. r_ret = get_graph_offset();
  699. return true;
  700. }
  701. return false;
  702. }
  703. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  704. List<StringName> names;
  705. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  706. names.push_back(E->key());
  707. }
  708. names.sort_custom<StringName::AlphCompare>();
  709. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  710. String name = E->get();
  711. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NOEDITOR));
  712. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  713. }
  714. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  715. p_list->push_back(PropertyInfo(Variant::STRING, "start_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  716. p_list->push_back(PropertyInfo(Variant::STRING, "end_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  717. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  718. }
  719. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  720. ERR_FAIL_COND(!states.has(p_name));
  721. states[p_name].position = p_position;
  722. }
  723. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  724. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  725. return states[p_name].position;
  726. }
  727. void AnimationNodeStateMachine::_tree_changed() {
  728. emit_signal("tree_changed");
  729. }
  730. void AnimationNodeStateMachine::_bind_methods() {
  731. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  732. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  733. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  734. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  735. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  736. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  737. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  738. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  739. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  740. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  741. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  742. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  743. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  744. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  745. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  746. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  747. ClassDB::bind_method(D_METHOD("set_start_node", "name"), &AnimationNodeStateMachine::set_start_node);
  748. ClassDB::bind_method(D_METHOD("get_start_node"), &AnimationNodeStateMachine::get_start_node);
  749. ClassDB::bind_method(D_METHOD("set_end_node", "name"), &AnimationNodeStateMachine::set_end_node);
  750. ClassDB::bind_method(D_METHOD("get_end_node"), &AnimationNodeStateMachine::get_end_node);
  751. ClassDB::bind_method(D_METHOD("set_graph_offset", "name"), &AnimationNodeStateMachine::set_graph_offset);
  752. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  753. ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeStateMachine::_tree_changed);
  754. }
  755. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  756. playback = "playback";
  757. }