LogicNode.hx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package arm.node;
  2. class LogicNode {
  3. var tree: LogicTree;
  4. var inputs: Array<LogicNodeInput> = [];
  5. var outputs: Array<Array<LogicNode>> = [];
  6. public function new(tree: LogicTree) {
  7. this.tree = tree;
  8. }
  9. public function addInput(node: LogicNode, from: Int) {
  10. inputs.push(new LogicNodeInput(node, from));
  11. }
  12. public function addOutputs(nodes: Array<LogicNode>) {
  13. outputs.push(nodes);
  14. }
  15. /**
  16. Called when this node is activated.
  17. @param from impulse index
  18. **/
  19. function run(from: Int) {}
  20. /**
  21. Call to activate node connected to the output.
  22. @param i output index
  23. **/
  24. function runOutput(i: Int) {
  25. if (i >= outputs.length) return;
  26. for (o in outputs[i]) {
  27. // Check which input activated the node
  28. for (j in 0...o.inputs.length) {
  29. if (o.inputs[j].node == this) {
  30. o.run(j);
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. @:allow(arm.node.LogicNodeInput)
  37. function get(from: Int): Dynamic {
  38. return this;
  39. }
  40. @:allow(arm.node.LogicNodeInput)
  41. function set(value: Dynamic) {}
  42. }
  43. class LogicNodeInput {
  44. @:allow(arm.node.LogicNode)
  45. var node: LogicNode;
  46. var from: Int; // Socket index
  47. public function new(node: LogicNode, from: Int) {
  48. this.node = node;
  49. this.from = from;
  50. }
  51. @:allow(arm.node.LogicNode)
  52. function get(): Dynamic {
  53. return node.get(from);
  54. }
  55. @:allow(arm.node.LogicNode)
  56. function set(value: Dynamic) {
  57. node.set(value);
  58. }
  59. }