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