BehaviorTree.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. Object.defineProperty(i, "__esModule", {value: !0});
  5. const s = t("B3Functions"),
  6. l = t("constants"),
  7. r = t("Tick");
  8. i.default = class {
  9. constructor() {
  10. (this.id = s.createUUID()),
  11. (this.title = "The behavior tree"),
  12. (this.description = "Default description"),
  13. (this.properties = {}),
  14. (this.root = null),
  15. (this.debug = !1);
  16. }
  17. load(t, e = {}) {
  18. (e = e || {}),
  19. (this.title = t.title || this.title),
  20. (this.description = t.description || this.description),
  21. (this.properties = t.properties || this.properties);
  22. var i,
  23. s,
  24. o,
  25. a = {};
  26. for (i in t.nodes) {
  27. if (!((s = t.nodes[i]).name in e))
  28. throw new EvalError('BehaviorTree.load: Invalid node name + "' + s.name + '".');
  29. ((o = new e[s.name](s.properties)).id = s.id || o.id),
  30. (o.name = s.name),
  31. (o.title = s.title || o.title),
  32. (o.description = s.description || o.description),
  33. (o.properties = s.properties || o.properties),
  34. (a[i] = o);
  35. }
  36. for (i in t.nodes)
  37. if (((s = t.nodes[i]), (o = a[i]).category === l.COMPOSITE && s.children))
  38. for (var n = 0; n < s.children.length; n++) {
  39. var r = s.children[n];
  40. o.children.push(a[r]);
  41. }
  42. else o.category === l.DECORATOR && s.child && (o.child = a[s.child]);
  43. this.root = a[t.root];
  44. }
  45. dump() {}
  46. tick(t, e) {
  47. if (!e) throw "The blackboard parameter is obligatory and must be an instance of b3.Blackboard";
  48. const i = new r.default();
  49. (i.debug = this.debug), (i.target = t), (i.blackboard = e);
  50. const s = (i.tree = this).root._execute(i),
  51. o = e.get("openNodes", this.id),
  52. a = i._openNodes.slice(0);
  53. let n = Math.min(o.length, a.length);
  54. for (let t = 0; t < n; t++)
  55. if (o[t] !== a[t]) {
  56. n = t;
  57. break;
  58. }
  59. for (let t = o.length - 1; t >= n; t--)
  60. e.get("isOpen", this.id, o[t].id) && (o[t]._close(i), o[t].interrupt(i));
  61. return e.set("openNodes", a, this.id), e.set("nodeCount", i._nodeCount, this.id), s;
  62. }
  63. };