animation_node_state_machine.cpp 33 KB

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