2
0

animation_node_state_machine.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_node_state_machine.h"
  31. /////////////////////////////////////////////////
  32. void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) {
  33. switch_mode = p_mode;
  34. }
  35. AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const {
  36. return switch_mode;
  37. }
  38. void AnimationNodeStateMachineTransition::set_auto_advance(bool p_enable) {
  39. auto_advance = p_enable;
  40. }
  41. bool AnimationNodeStateMachineTransition::has_auto_advance() const {
  42. return auto_advance;
  43. }
  44. void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {
  45. String cs = p_condition;
  46. ERR_FAIL_COND(cs.find("/") != -1 || cs.find(":") != -1);
  47. advance_condition = p_condition;
  48. if (cs != String()) {
  49. advance_condition_name = "conditions/" + cs;
  50. } else {
  51. advance_condition_name = StringName();
  52. }
  53. emit_signal("advance_condition_changed");
  54. }
  55. StringName AnimationNodeStateMachineTransition::get_advance_condition() const {
  56. return advance_condition;
  57. }
  58. StringName AnimationNodeStateMachineTransition::get_advance_condition_name() const {
  59. return advance_condition_name;
  60. }
  61. void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {
  62. ERR_FAIL_COND(p_xfade < 0);
  63. xfade = p_xfade;
  64. emit_changed();
  65. }
  66. float AnimationNodeStateMachineTransition::get_xfade_time() const {
  67. return xfade;
  68. }
  69. void AnimationNodeStateMachineTransition::set_disabled(bool p_disabled) {
  70. disabled = p_disabled;
  71. emit_changed();
  72. }
  73. bool AnimationNodeStateMachineTransition::is_disabled() const {
  74. return disabled;
  75. }
  76. void AnimationNodeStateMachineTransition::set_priority(int p_priority) {
  77. priority = p_priority;
  78. emit_changed();
  79. }
  80. int AnimationNodeStateMachineTransition::get_priority() const {
  81. return priority;
  82. }
  83. void AnimationNodeStateMachineTransition::_bind_methods() {
  84. ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);
  85. ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);
  86. ClassDB::bind_method(D_METHOD("set_auto_advance", "auto_advance"), &AnimationNodeStateMachineTransition::set_auto_advance);
  87. ClassDB::bind_method(D_METHOD("has_auto_advance"), &AnimationNodeStateMachineTransition::has_auto_advance);
  88. ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);
  89. ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);
  90. ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);
  91. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);
  92. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &AnimationNodeStateMachineTransition::set_disabled);
  93. ClassDB::bind_method(D_METHOD("is_disabled"), &AnimationNodeStateMachineTransition::is_disabled);
  94. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);
  95. ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);
  96. ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,AtEnd"), "set_switch_mode", "get_switch_mode");
  97. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_advance"), "set_auto_advance", "has_auto_advance");
  98. ADD_PROPERTY(PropertyInfo(Variant::STRING, "advance_condition"), "set_advance_condition", "get_advance_condition");
  99. ADD_PROPERTY(PropertyInfo(Variant::REAL, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01"), "set_xfade_time", "get_xfade_time");
  100. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");
  101. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  102. BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);
  103. BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);
  104. BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);
  105. ADD_SIGNAL(MethodInfo("advance_condition_changed"));
  106. }
  107. AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {
  108. switch_mode = SWITCH_MODE_IMMEDIATE;
  109. auto_advance = false;
  110. xfade = 0;
  111. disabled = false;
  112. priority = 1;
  113. }
  114. ////////////////////////////////////////////////////////
  115. void AnimationNodeStateMachinePlayback::travel(const StringName &p_state) {
  116. start_request_travel = true;
  117. start_request = p_state;
  118. stop_request = false;
  119. }
  120. void AnimationNodeStateMachinePlayback::start(const StringName &p_state) {
  121. start_request_travel = false;
  122. start_request = p_state;
  123. stop_request = false;
  124. }
  125. void AnimationNodeStateMachinePlayback::stop() {
  126. stop_request = true;
  127. }
  128. bool AnimationNodeStateMachinePlayback::is_playing() const {
  129. return playing;
  130. }
  131. StringName AnimationNodeStateMachinePlayback::get_current_node() const {
  132. return current;
  133. }
  134. StringName AnimationNodeStateMachinePlayback::get_blend_from_node() const {
  135. return fading_from;
  136. }
  137. Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {
  138. return path;
  139. }
  140. float AnimationNodeStateMachinePlayback::get_current_play_pos() const {
  141. return pos_current;
  142. }
  143. float AnimationNodeStateMachinePlayback::get_current_length() const {
  144. return len_current;
  145. }
  146. bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *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. loops_current = 0; // reset loops, so fade does not happen immediately
  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 = NULL;
  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.");
  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. path.clear();
  274. current = start_request;
  275. playing = true;
  276. play_start = true;
  277. start_request = StringName(); //clear start request
  278. }
  279. }
  280. bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
  281. if (do_start) {
  282. if (p_state_machine->start_node != StringName() && p_seek && p_time == 0) {
  283. current = p_state_machine->start_node;
  284. }
  285. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false);
  286. pos_current = 0;
  287. loops_current = 0;
  288. }
  289. if (!p_state_machine->states.has(current)) {
  290. playing = false; //current does not exist
  291. current = StringName();
  292. return 0;
  293. }
  294. float fade_blend = 1.0;
  295. if (fading_from != StringName()) {
  296. if (!p_state_machine->states.has(fading_from)) {
  297. fading_from = StringName();
  298. } else {
  299. if (!p_seek) {
  300. fading_pos += p_time;
  301. }
  302. fade_blend = MIN(1.0, fading_pos / fading_time);
  303. if (fade_blend >= 1.0) {
  304. fading_from = StringName();
  305. }
  306. }
  307. }
  308. float rem = p_state_machine->blend_node(current, p_state_machine->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false);
  309. if (fading_from != StringName()) {
  310. 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);
  311. }
  312. //guess playback position
  313. if (rem > len_current) { // weird but ok
  314. len_current = rem;
  315. }
  316. { //advance and loop check
  317. float next_pos = len_current - rem;
  318. if (next_pos < pos_current) {
  319. loops_current++;
  320. }
  321. pos_current = next_pos; //looped
  322. }
  323. //find next
  324. StringName next;
  325. float next_xfade = 0;
  326. AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
  327. if (path.size()) {
  328. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  329. if (p_state_machine->transitions[i].from == current && p_state_machine->transitions[i].to == path[0]) {
  330. next_xfade = p_state_machine->transitions[i].transition->get_xfade_time();
  331. switch_mode = p_state_machine->transitions[i].transition->get_switch_mode();
  332. next = path[0];
  333. }
  334. }
  335. } else {
  336. float priority_best = 1e20;
  337. int auto_advance_to = -1;
  338. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  339. bool auto_advance = false;
  340. if (p_state_machine->transitions[i].transition->has_auto_advance()) {
  341. auto_advance = true;
  342. }
  343. StringName advance_condition_name = p_state_machine->transitions[i].transition->get_advance_condition_name();
  344. if (advance_condition_name != StringName() && bool(p_state_machine->get_parameter(advance_condition_name))) {
  345. auto_advance = true;
  346. }
  347. if (p_state_machine->transitions[i].from == current && auto_advance) {
  348. if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  349. priority_best = p_state_machine->transitions[i].transition->get_priority();
  350. auto_advance_to = i;
  351. }
  352. }
  353. }
  354. if (auto_advance_to != -1) {
  355. next = p_state_machine->transitions[auto_advance_to].to;
  356. next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time();
  357. switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode();
  358. }
  359. }
  360. //if next, see when to transition
  361. if (next != StringName()) {
  362. bool goto_next = false;
  363. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
  364. goto_next = next_xfade >= (len_current - pos_current) || loops_current > 0;
  365. if (loops_current > 0) {
  366. next_xfade = 0;
  367. }
  368. } else {
  369. goto_next = fading_from == StringName();
  370. }
  371. if (goto_next) { //loops should be used because fade time may be too small or zero and animation may have looped
  372. if (next_xfade) {
  373. //time to fade, baby
  374. fading_from = current;
  375. fading_time = next_xfade;
  376. fading_pos = 0;
  377. } else {
  378. fading_from = StringName();
  379. fading_pos = 0;
  380. }
  381. if (path.size()) { //if it came from path, remove path
  382. path.remove(0);
  383. }
  384. current = next;
  385. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
  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 = MIN(pos_current, len_current);
  388. p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false);
  389. } else {
  390. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
  391. pos_current = 0;
  392. }
  393. rem = len_current; //so it does not show 0 on transition
  394. loops_current = 0;
  395. }
  396. }
  397. //compute time left for transitions by using the end node
  398. if (p_state_machine->end_node != StringName() && p_state_machine->end_node != current) {
  399. 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);
  400. }
  401. return rem;
  402. }
  403. void AnimationNodeStateMachinePlayback::_bind_methods() {
  404. ClassDB::bind_method(D_METHOD("travel", "to_node"), &AnimationNodeStateMachinePlayback::travel);
  405. ClassDB::bind_method(D_METHOD("start", "node"), &AnimationNodeStateMachinePlayback::start);
  406. ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
  407. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
  408. ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
  409. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
  410. }
  411. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  412. set_local_to_scene(true); //only one per instanced scene
  413. playing = false;
  414. len_current = 0;
  415. fading_time = 0;
  416. stop_request = false;
  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) == NULL) {
  425. advance_conditions.push_back(ac);
  426. }
  427. }
  428. advance_conditions.sort_custom<StringName::AlphCompare>();
  429. for (List<StringName>::Element *E = advance_conditions.front(); E; E = E->next()) {
  430. r_list->push_back(PropertyInfo(Variant::BOOL, E->get()));
  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.instance();
  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("tree_changed");
  452. p_node->connect("tree_changed", this, "_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", this, "_tree_changed");
  462. }
  463. }
  464. states[p_name].node = p_node;
  465. emit_changed();
  466. emit_signal("tree_changed");
  467. p_node->connect("tree_changed", this, "_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 (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  475. if (E->get().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 (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  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", this, "_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", this, "_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("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("tree_changed");
  549. }
  550. void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
  551. List<StringName> nodes;
  552. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  553. nodes.push_back(E->key());
  554. }
  555. nodes.sort_custom<StringName::AlphCompare>();
  556. for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) {
  557. r_nodes->push_back(E->get());
  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. return false;
  566. }
  567. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  568. for (int i = 0; i < transitions.size(); i++) {
  569. if (transitions[i].from == p_from && transitions[i].to == p_to)
  570. return i;
  571. }
  572. return -1;
  573. }
  574. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  575. ERR_FAIL_COND(p_from == p_to);
  576. ERR_FAIL_COND(!states.has(p_from));
  577. ERR_FAIL_COND(!states.has(p_to));
  578. ERR_FAIL_COND(p_transition.is_null());
  579. for (int i = 0; i < transitions.size(); i++) {
  580. ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);
  581. }
  582. Transition tr;
  583. tr.from = p_from;
  584. tr.to = p_to;
  585. tr.transition = p_transition;
  586. tr.transition->connect("advance_condition_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
  587. transitions.push_back(tr);
  588. }
  589. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  590. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  591. return transitions[p_transition].transition;
  592. }
  593. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  594. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  595. return transitions[p_transition].from;
  596. }
  597. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  598. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  599. return transitions[p_transition].to;
  600. }
  601. int AnimationNodeStateMachine::get_transition_count() const {
  602. return transitions.size();
  603. }
  604. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  605. for (int i = 0; i < transitions.size(); i++) {
  606. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  607. transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
  608. transitions.remove(i);
  609. return;
  610. }
  611. }
  612. /*if (playing) {
  613. path.clear();
  614. }*/
  615. }
  616. void AnimationNodeStateMachine::remove_transition_by_index(int p_transition) {
  617. ERR_FAIL_INDEX(p_transition, transitions.size());
  618. transitions.write[p_transition].transition->disconnect("advance_condition_changed", this, "_tree_changed");
  619. transitions.remove(p_transition);
  620. /*if (playing) {
  621. path.clear();
  622. }*/
  623. }
  624. void AnimationNodeStateMachine::set_start_node(const StringName &p_node) {
  625. ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
  626. start_node = p_node;
  627. }
  628. String AnimationNodeStateMachine::get_start_node() const {
  629. return start_node;
  630. }
  631. void AnimationNodeStateMachine::set_end_node(const StringName &p_node) {
  632. ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
  633. end_node = p_node;
  634. }
  635. String AnimationNodeStateMachine::get_end_node() const {
  636. return end_node;
  637. }
  638. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  639. graph_offset = p_offset;
  640. }
  641. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  642. return graph_offset;
  643. }
  644. float AnimationNodeStateMachine::process(float p_time, bool p_seek) {
  645. Ref<AnimationNodeStateMachinePlayback> playback = get_parameter(this->playback);
  646. ERR_FAIL_COND_V(playback.is_null(), 0.0);
  647. return playback->process(this, p_time, p_seek);
  648. }
  649. String AnimationNodeStateMachine::get_caption() const {
  650. return "StateMachine";
  651. }
  652. void AnimationNodeStateMachine::_notification(int p_what) {
  653. }
  654. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
  655. return get_node(p_name);
  656. }
  657. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  658. String name = p_name;
  659. if (name.begins_with("states/")) {
  660. String node_name = name.get_slicec('/', 1);
  661. String what = name.get_slicec('/', 2);
  662. if (what == "node") {
  663. Ref<AnimationNode> anode = p_value;
  664. if (anode.is_valid()) {
  665. add_node(node_name, p_value);
  666. }
  667. return true;
  668. }
  669. if (what == "position") {
  670. if (states.has(node_name)) {
  671. states[node_name].position = p_value;
  672. }
  673. return true;
  674. }
  675. } else if (name == "transitions") {
  676. Array trans = p_value;
  677. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  678. for (int i = 0; i < trans.size(); i += 3) {
  679. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  680. }
  681. return true;
  682. } else if (name == "start_node") {
  683. set_start_node(p_value);
  684. return true;
  685. } else if (name == "end_node") {
  686. set_end_node(p_value);
  687. return true;
  688. } else if (name == "graph_offset") {
  689. set_graph_offset(p_value);
  690. return true;
  691. }
  692. return false;
  693. }
  694. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  695. String name = p_name;
  696. if (name.begins_with("states/")) {
  697. String node_name = name.get_slicec('/', 1);
  698. String what = name.get_slicec('/', 2);
  699. if (what == "node") {
  700. if (states.has(node_name)) {
  701. r_ret = states[node_name].node;
  702. return true;
  703. }
  704. }
  705. if (what == "position") {
  706. if (states.has(node_name)) {
  707. r_ret = states[node_name].position;
  708. return true;
  709. }
  710. }
  711. } else if (name == "transitions") {
  712. Array trans;
  713. trans.resize(transitions.size() * 3);
  714. for (int i = 0; i < transitions.size(); i++) {
  715. trans[i * 3 + 0] = transitions[i].from;
  716. trans[i * 3 + 1] = transitions[i].to;
  717. trans[i * 3 + 2] = transitions[i].transition;
  718. }
  719. r_ret = trans;
  720. return true;
  721. } else if (name == "start_node") {
  722. r_ret = get_start_node();
  723. return true;
  724. } else if (name == "end_node") {
  725. r_ret = get_end_node();
  726. return true;
  727. } else if (name == "graph_offset") {
  728. r_ret = get_graph_offset();
  729. return true;
  730. }
  731. return false;
  732. }
  733. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  734. List<StringName> names;
  735. for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
  736. names.push_back(E->key());
  737. }
  738. names.sort_custom<StringName::AlphCompare>();
  739. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  740. String name = E->get();
  741. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NOEDITOR));
  742. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  743. }
  744. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  745. p_list->push_back(PropertyInfo(Variant::STRING, "start_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  746. p_list->push_back(PropertyInfo(Variant::STRING, "end_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  747. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  748. }
  749. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  750. ERR_FAIL_COND(!states.has(p_name));
  751. states[p_name].position = p_position;
  752. }
  753. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  754. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  755. return states[p_name].position;
  756. }
  757. void AnimationNodeStateMachine::_tree_changed() {
  758. emit_signal("tree_changed");
  759. }
  760. void AnimationNodeStateMachine::_bind_methods() {
  761. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  762. ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
  763. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  764. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  765. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  766. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  767. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  768. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  769. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  770. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  771. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  772. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  773. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  774. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  775. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  776. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  777. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  778. ClassDB::bind_method(D_METHOD("set_start_node", "name"), &AnimationNodeStateMachine::set_start_node);
  779. ClassDB::bind_method(D_METHOD("get_start_node"), &AnimationNodeStateMachine::get_start_node);
  780. ClassDB::bind_method(D_METHOD("set_end_node", "name"), &AnimationNodeStateMachine::set_end_node);
  781. ClassDB::bind_method(D_METHOD("get_end_node"), &AnimationNodeStateMachine::get_end_node);
  782. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  783. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  784. ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeStateMachine::_tree_changed);
  785. }
  786. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  787. playback = "playback";
  788. }