LogicNode.hx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { return this; }
  38. @:allow(arm.node.LogicNodeInput)
  39. function set(value: Dynamic) {}
  40. }
  41. class LogicNodeInput {
  42. @:allow(arm.node.LogicNode)
  43. var node: LogicNode;
  44. var from: Int; // Socket index
  45. public function new(node: LogicNode, from: Int) {
  46. this.node = node;
  47. this.from = from;
  48. }
  49. @:allow(arm.node.LogicNode)
  50. function get(): Dynamic {
  51. return node.get(from);
  52. }
  53. @:allow(arm.node.LogicNode)
  54. function set(value: Dynamic) {
  55. node.set(value);
  56. }
  57. }