animation_node_state_machine.cpp 33 KB

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