animation_node_state_machine.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. least_cost = cost;
  189. }
  190. }
  191. StringName transition_prev = sm->transitions[least_cost_transition->get()].from;
  192. StringName transition = sm->transitions[least_cost_transition->get()].to;
  193. for (int i = 0; i < sm->transitions.size(); i++) {
  194. if (sm->transitions[i].from != transition || sm->transitions[i].to == transition_prev) {
  195. continue; //not interested on those
  196. }
  197. float distance = sm->states[sm->transitions[i].from].position.distance_to(sm->states[sm->transitions[i].to].position);
  198. distance *= sm->transitions[i].transition->get_priority();
  199. distance += cost_map[sm->transitions[i].from].distance;
  200. if (cost_map.has(sm->transitions[i].to)) {
  201. //oh this was visited already, can we win the cost?
  202. if (distance < cost_map[sm->transitions[i].to].distance) {
  203. cost_map[sm->transitions[i].to].distance = distance;
  204. cost_map[sm->transitions[i].to].prev = sm->transitions[i].from;
  205. }
  206. } else {
  207. //add to open list
  208. AStarCost ac;
  209. ac.prev = sm->transitions[i].from;
  210. ac.distance = distance;
  211. cost_map[sm->transitions[i].to] = ac;
  212. open_list.push_back(i);
  213. if (sm->transitions[i].to == p_travel) {
  214. found_route = true;
  215. break;
  216. }
  217. }
  218. }
  219. if (found_route) {
  220. break;
  221. }
  222. open_list.erase(least_cost_transition);
  223. }
  224. //make path
  225. StringName at = p_travel;
  226. while (at != current) {
  227. path.push_back(at);
  228. at = cost_map[at].prev;
  229. }
  230. path.invert();
  231. return true;
  232. }
  233. float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, float p_time, bool p_seek) {
  234. //if not playing and it can restart, then restart
  235. if (!playing && start_request == StringName()) {
  236. if (!stop_request && sm->start_node) {
  237. start(sm->start_node);
  238. } else {
  239. return 0;
  240. }
  241. }
  242. if (playing && stop_request) {
  243. stop_request = false;
  244. playing = false;
  245. return 0;
  246. }
  247. bool play_start = false;
  248. if (start_request != StringName()) {
  249. if (start_request_travel) {
  250. if (!playing) {
  251. String node_name = start_request;
  252. start_request = StringName();
  253. ERR_EXPLAIN("Can't travel to '" + node_name + "' if state machine is not playing.");
  254. ERR_FAIL_V(0);
  255. }
  256. if (!_travel(sm, start_request)) {
  257. //can't travel, then teleport
  258. path.clear();
  259. current = start_request;
  260. }
  261. } else {
  262. path.clear();
  263. current = start_request;
  264. playing = true;
  265. play_start = true;
  266. }
  267. start_request = StringName(); //clear start request
  268. }
  269. bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
  270. if (do_start) {
  271. if (sm->start_node != StringName() && p_seek && p_time == 0) {
  272. current = sm->start_node;
  273. }
  274. len_current = sm->blend_node(current, sm->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false);
  275. pos_current = 0;
  276. loops_current = 0;
  277. }
  278. if (!sm->states.has(current)) {
  279. playing = false; //current does not exist
  280. current = StringName();
  281. return 0;
  282. }
  283. float fade_blend = 1.0;
  284. if (fading_from != StringName()) {
  285. if (!sm->states.has(fading_from)) {
  286. fading_from = StringName();
  287. } else {
  288. if (!p_seek) {
  289. fading_pos += p_time;
  290. }
  291. fade_blend = MIN(1.0, fading_pos / fading_time);
  292. if (fade_blend >= 1.0) {
  293. fading_from = StringName();
  294. }
  295. }
  296. }
  297. float rem = sm->blend_node(current, sm->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false);
  298. if (fading_from != StringName()) {
  299. sm->blend_node(fading_from, sm->states[fading_from].node, p_time, p_seek, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, false);
  300. }
  301. //guess playback position
  302. if (rem > len_current) { // weird but ok
  303. len_current = rem;
  304. }
  305. { //advance and loop check
  306. float next_pos = len_current - rem;
  307. if (next_pos < pos_current) {
  308. loops_current++;
  309. }
  310. pos_current = next_pos; //looped
  311. }
  312. //find next
  313. StringName next;
  314. float next_xfade = 0;
  315. AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
  316. if (path.size()) {
  317. for (int i = 0; i < sm->transitions.size(); i++) {
  318. if (sm->transitions[i].from == current && sm->transitions[i].to == path[0]) {
  319. next_xfade = sm->transitions[i].transition->get_xfade_time();
  320. switch_mode = sm->transitions[i].transition->get_switch_mode();
  321. next = path[0];
  322. }
  323. }
  324. } else {
  325. float priority_best = 1e20;
  326. int auto_advance_to = -1;
  327. for (int i = 0; i < sm->transitions.size(); i++) {
  328. bool auto_advance = false;
  329. if (sm->transitions[i].transition->has_auto_advance()) {
  330. auto_advance = true;
  331. }
  332. StringName advance_condition_name = sm->transitions[i].transition->get_advance_condition_name();
  333. if (advance_condition_name != StringName() && bool(sm->get_parameter(advance_condition_name))) {
  334. auto_advance = true;
  335. }
  336. if (sm->transitions[i].from == current && auto_advance) {
  337. if (sm->transitions[i].transition->get_priority() <= priority_best) {
  338. priority_best = sm->transitions[i].transition->get_priority();
  339. auto_advance_to = i;
  340. }
  341. }
  342. }
  343. if (auto_advance_to != -1) {
  344. next = sm->transitions[auto_advance_to].to;
  345. next_xfade = sm->transitions[auto_advance_to].transition->get_xfade_time();
  346. switch_mode = sm->transitions[auto_advance_to].transition->get_switch_mode();
  347. }
  348. }
  349. //if next, see when to transition
  350. if (next != StringName()) {
  351. bool goto_next = false;
  352. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
  353. goto_next = next_xfade >= (len_current - pos_current) || loops_current > 0;
  354. if (loops_current > 0) {
  355. next_xfade = 0;
  356. }
  357. } else {
  358. goto_next = fading_from == StringName();
  359. }
  360. if (goto_next) { //loops should be used because fade time may be too small or zero and animation may have looped
  361. if (next_xfade) {
  362. //time to fade, baby
  363. fading_from = current;
  364. fading_time = next_xfade;
  365. fading_pos = 0;
  366. } else {
  367. fading_from = StringName();
  368. fading_pos = 0;
  369. }
  370. if (path.size()) { //if it came from path, remove path
  371. path.remove(0);
  372. }
  373. current = next;
  374. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
  375. len_current = sm->blend_node(current, sm->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
  376. pos_current = MIN(pos_current, len_current);
  377. sm->blend_node(current, sm->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false);
  378. } else {
  379. len_current = sm->blend_node(current, sm->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
  380. pos_current = 0;
  381. }
  382. rem = len_current; //so it does not show 0 on transition
  383. loops_current = 0;
  384. }
  385. }
  386. //compute time left for transitions by using the end node
  387. if (sm->end_node != StringName() && sm->end_node != current) {
  388. rem = sm->blend_node(sm->end_node, sm->states[sm->end_node].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
  389. }
  390. return rem;
  391. }
  392. void AnimationNodeStateMachinePlayback::_bind_methods() {
  393. ClassDB::bind_method(D_METHOD("travel", "to_node"), &AnimationNodeStateMachinePlayback::travel);
  394. ClassDB::bind_method(D_METHOD("start", "node"), &AnimationNodeStateMachinePlayback::start);
  395. ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
  396. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
  397. ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
  398. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
  399. }
  400. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  401. set_local_to_scene(true); //only one per instanced scene
  402. playing = false;
  403. len_current = 0;
  404. fading_time = 0;
  405. stop_request = false;
  406. }
  407. ///////////////////////////////////////////////////////
  408. void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
  409. r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  410. List<StringName> advance_conditions;
  411. for (int i = 0; i < transitions.size(); i++) {
  412. StringName ac = transitions[i].transition->get_advance_condition_name();
  413. if (ac != StringName() && advance_conditions.find(ac) == NULL) {
  414. advance_conditions.push_back(ac);
  415. }
  416. }
  417. advance_conditions.sort_custom<StringName::AlphCompare>();
  418. for (List<StringName>::Element *E = advance_conditions.front(); E; E = E->next()) {
  419. r_list->push_back(PropertyInfo(Variant::BOOL, E->get()));
  420. }
  421. }
  422. Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
  423. if (p_parameter == playback) {
  424. Ref<AnimationNodeStateMachinePlayback> p;
  425. p.instance();
  426. return p;
  427. } else {
  428. return false; //advance condition
  429. }
  430. }
  431. void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  432. ERR_FAIL_COND(states.has(p_name));
  433. ERR_FAIL_COND(p_node.is_null());
  434. ERR_FAIL_COND(String(p_name).find("/") != -1);
  435. State state;
  436. state.node = p_node;
  437. state.position = p_position;
  438. states[p_name] = state;
  439. emit_changed();
  440. emit_signal("tree_changed");
  441. p_node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
  442. }
  443. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  444. ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
  445. return states[p_name].node;
  446. }
  447. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  448. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  449. if (E->get().node == p_node) {
  450. return E->key();
  451. }
  452. }
  453. ERR_FAIL_V(StringName());
  454. }
  455. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  456. Vector<StringName> nodes;
  457. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  458. nodes.push_back(E->key());
  459. }
  460. nodes.sort_custom<StringName::AlphCompare>();
  461. for (int i = 0; i < nodes.size(); i++) {
  462. ChildNode cn;
  463. cn.name = nodes[i];
  464. cn.node = states[cn.name].node;
  465. r_child_nodes->push_back(cn);
  466. }
  467. }
  468. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  469. return states.has(p_name);
  470. }
  471. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  472. ERR_FAIL_COND(!states.has(p_name));
  473. {
  474. Ref<AnimationNode> node = states[p_name].node;
  475. node->disconnect("tree_changed", this, "_tree_changed");
  476. }
  477. states.erase(p_name);
  478. //path.erase(p_name);
  479. for (int i = 0; i < transitions.size(); i++) {
  480. if (transitions[i].from == p_name || transitions[i].to == p_name) {
  481. transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
  482. transitions.remove(i);
  483. i--;
  484. }
  485. }
  486. if (start_node == p_name) {
  487. start_node = StringName();
  488. }
  489. if (end_node == p_name) {
  490. end_node = StringName();
  491. }
  492. /*if (playing && current == p_name) {
  493. stop();
  494. }*/
  495. emit_changed();
  496. emit_signal("tree_changed");
  497. }
  498. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  499. ERR_FAIL_COND(!states.has(p_name));
  500. ERR_FAIL_COND(states.has(p_new_name));
  501. states[p_new_name] = states[p_name];
  502. states.erase(p_name);
  503. for (int i = 0; i < transitions.size(); i++) {
  504. if (transitions[i].from == p_name) {
  505. transitions.write[i].from = p_new_name;
  506. }
  507. if (transitions[i].to == p_name) {
  508. transitions.write[i].to = p_new_name;
  509. }
  510. }
  511. if (start_node == p_name) {
  512. start_node = p_new_name;
  513. }
  514. if (end_node == p_name) {
  515. end_node = p_new_name;
  516. }
  517. /*if (playing && current == p_name) {
  518. current = p_new_name;
  519. }*/
  520. //path.clear(); //clear path
  521. emit_signal("tree_changed");
  522. }
  523. void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
  524. List<StringName> nodes;
  525. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  526. nodes.push_back(E->key());
  527. }
  528. nodes.sort_custom<StringName::AlphCompare>();
  529. for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) {
  530. r_nodes->push_back(E->get());
  531. }
  532. }
  533. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  534. for (int i = 0; i < transitions.size(); i++) {
  535. if (transitions[i].from == p_from && transitions[i].to == p_to)
  536. return true;
  537. }
  538. return false;
  539. }
  540. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  541. for (int i = 0; i < transitions.size(); i++) {
  542. if (transitions[i].from == p_from && transitions[i].to == p_to)
  543. return i;
  544. }
  545. return -1;
  546. }
  547. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  548. ERR_FAIL_COND(p_from == p_to);
  549. ERR_FAIL_COND(!states.has(p_from));
  550. ERR_FAIL_COND(!states.has(p_to));
  551. ERR_FAIL_COND(p_transition.is_null());
  552. for (int i = 0; i < transitions.size(); i++) {
  553. ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);
  554. }
  555. Transition tr;
  556. tr.from = p_from;
  557. tr.to = p_to;
  558. tr.transition = p_transition;
  559. tr.transition->connect("advance_condition_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
  560. transitions.push_back(tr);
  561. }
  562. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  563. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  564. return transitions[p_transition].transition;
  565. }
  566. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  567. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  568. return transitions[p_transition].from;
  569. }
  570. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  571. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  572. return transitions[p_transition].to;
  573. }
  574. int AnimationNodeStateMachine::get_transition_count() const {
  575. return transitions.size();
  576. }
  577. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  578. for (int i = 0; i < transitions.size(); i++) {
  579. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  580. transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
  581. transitions.remove(i);
  582. return;
  583. }
  584. }
  585. /*if (playing) {
  586. path.clear();
  587. }*/
  588. }
  589. void AnimationNodeStateMachine::remove_transition_by_index(int p_transition) {
  590. ERR_FAIL_INDEX(p_transition, transitions.size());
  591. transitions.write[p_transition].transition->disconnect("advance_condition_changed", this, "_tree_changed");
  592. transitions.remove(p_transition);
  593. /*if (playing) {
  594. path.clear();
  595. }*/
  596. }
  597. void AnimationNodeStateMachine::set_start_node(const StringName &p_node) {
  598. ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
  599. start_node = p_node;
  600. }
  601. String AnimationNodeStateMachine::get_start_node() const {
  602. return start_node;
  603. }
  604. void AnimationNodeStateMachine::set_end_node(const StringName &p_node) {
  605. ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
  606. end_node = p_node;
  607. }
  608. String AnimationNodeStateMachine::get_end_node() const {
  609. return end_node;
  610. }
  611. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  612. graph_offset = p_offset;
  613. }
  614. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  615. return graph_offset;
  616. }
  617. float AnimationNodeStateMachine::process(float p_time, bool p_seek) {
  618. Ref<AnimationNodeStateMachinePlayback> playback = get_parameter(this->playback);
  619. ERR_FAIL_COND_V(playback.is_null(), 0.0);
  620. return playback->process(this, p_time, p_seek);
  621. }
  622. String AnimationNodeStateMachine::get_caption() const {
  623. return "StateMachine";
  624. }
  625. void AnimationNodeStateMachine::_notification(int p_what) {
  626. }
  627. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
  628. return get_node(p_name);
  629. }
  630. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  631. String name = p_name;
  632. if (name.begins_with("states/")) {
  633. String node_name = name.get_slicec('/', 1);
  634. String what = name.get_slicec('/', 2);
  635. if (what == "node") {
  636. Ref<AnimationNode> anode = p_value;
  637. if (anode.is_valid()) {
  638. add_node(node_name, p_value);
  639. }
  640. return true;
  641. }
  642. if (what == "position") {
  643. if (states.has(node_name)) {
  644. states[node_name].position = p_value;
  645. }
  646. return true;
  647. }
  648. } else if (name == "transitions") {
  649. Array trans = p_value;
  650. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  651. for (int i = 0; i < trans.size(); i += 3) {
  652. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  653. }
  654. return true;
  655. } else if (name == "start_node") {
  656. set_start_node(p_value);
  657. return true;
  658. } else if (name == "end_node") {
  659. set_end_node(p_value);
  660. return true;
  661. } else if (name == "graph_offset") {
  662. set_graph_offset(p_value);
  663. return true;
  664. }
  665. return false;
  666. }
  667. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  668. String name = p_name;
  669. if (name.begins_with("states/")) {
  670. String node_name = name.get_slicec('/', 1);
  671. String what = name.get_slicec('/', 2);
  672. if (what == "node") {
  673. if (states.has(node_name)) {
  674. r_ret = states[node_name].node;
  675. return true;
  676. }
  677. }
  678. if (what == "position") {
  679. if (states.has(node_name)) {
  680. r_ret = states[node_name].position;
  681. return true;
  682. }
  683. }
  684. } else if (name == "transitions") {
  685. Array trans;
  686. trans.resize(transitions.size() * 3);
  687. for (int i = 0; i < transitions.size(); i++) {
  688. trans[i * 3 + 0] = transitions[i].from;
  689. trans[i * 3 + 1] = transitions[i].to;
  690. trans[i * 3 + 2] = transitions[i].transition;
  691. }
  692. r_ret = trans;
  693. return true;
  694. } else if (name == "start_node") {
  695. r_ret = get_start_node();
  696. return true;
  697. } else if (name == "end_node") {
  698. r_ret = get_end_node();
  699. return true;
  700. } else if (name == "graph_offset") {
  701. r_ret = get_graph_offset();
  702. return true;
  703. }
  704. return false;
  705. }
  706. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  707. List<StringName> names;
  708. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  709. names.push_back(E->key());
  710. }
  711. names.sort_custom<StringName::AlphCompare>();
  712. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  713. String name = E->get();
  714. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NOEDITOR));
  715. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  716. }
  717. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  718. p_list->push_back(PropertyInfo(Variant::STRING, "start_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  719. p_list->push_back(PropertyInfo(Variant::STRING, "end_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  720. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  721. }
  722. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  723. ERR_FAIL_COND(!states.has(p_name));
  724. states[p_name].position = p_position;
  725. }
  726. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  727. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  728. return states[p_name].position;
  729. }
  730. void AnimationNodeStateMachine::_tree_changed() {
  731. emit_signal("tree_changed");
  732. }
  733. void AnimationNodeStateMachine::_bind_methods() {
  734. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  735. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  736. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  737. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  738. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  739. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  740. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  741. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  742. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  743. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  744. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  745. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  746. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  747. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  748. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  749. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  750. ClassDB::bind_method(D_METHOD("set_start_node", "name"), &AnimationNodeStateMachine::set_start_node);
  751. ClassDB::bind_method(D_METHOD("get_start_node"), &AnimationNodeStateMachine::get_start_node);
  752. ClassDB::bind_method(D_METHOD("set_end_node", "name"), &AnimationNodeStateMachine::set_end_node);
  753. ClassDB::bind_method(D_METHOD("get_end_node"), &AnimationNodeStateMachine::get_end_node);
  754. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  755. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  756. ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeStateMachine::_tree_changed);
  757. }
  758. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  759. playback = "playback";
  760. }