You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

circuit-components.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. export class Link {
  2. constructor(from, index) {
  3. this.from = from;
  4. this.index = index;
  5. this.current = false;
  6. this.next = false;
  7. this.connections = [];
  8. }
  9. connect(node, index) {
  10. node.inputs[index].links.push(this);
  11. this.connections.push({node, index});
  12. }
  13. disconnect(node, index) {
  14. for (let i = this.connections.length - 1; i >= 0; --i) {
  15. let conn = this.connections[i];
  16. if (conn.node == node && conn.index == index) {
  17. this.connections.splice(i, 1);
  18. }
  19. }
  20. }
  21. destroy() {
  22. for (let conn of this.connections) {
  23. for (let i = conn.node.inputs[conn.index].links.length - 1; i >= 0; --i) {
  24. if (conn.node.inputs[conn.index].links[i] == this) {
  25. conn.node.inputs[conn.index].links.splice(i, 1);
  26. }
  27. }
  28. }
  29. }
  30. commit() {
  31. this.current = this.next;
  32. }
  33. }
  34. export class Input {
  35. constructor(x, y) {
  36. this.name = "IN";
  37. this.x = x;
  38. this.y = y;
  39. this.inputs = [];
  40. this.outputs = [{name: "Out", link: new Link(this, 0)}];
  41. this.width = 3;
  42. this.height = 1;
  43. this.lit = false;
  44. }
  45. activate() {
  46. this.lit = !this.lit;
  47. }
  48. tick() {
  49. if (this.lit) {
  50. this.outputs[0].link.next = true;
  51. } else {
  52. this.outputs[0].link.next = false;
  53. }
  54. }
  55. commit() {
  56. this.outputs[0].link.commit();
  57. }
  58. }
  59. export class NotGate {
  60. constructor(x, y) {
  61. this.name = "NOT";
  62. this.x = x;
  63. this.y = y;
  64. this.inputs = [{name: "In", links: []}];
  65. this.outputs = [{name: "Out", link: new Link(this, 0)}];
  66. this.width = 4;
  67. this.height = 1;
  68. this.lit = false;
  69. }
  70. tick() {
  71. this.outputs[0].link.next = true;
  72. for (let link of this.inputs[0].links) {
  73. if (link.current) {
  74. this.outputs[0].link.next = false;
  75. break;
  76. }
  77. }
  78. }
  79. commit() {
  80. this.outputs[0].link.commit();
  81. this.lit = this.outputs[0].link.current;
  82. }
  83. }
  84. export class Diode {
  85. constructor(x, y) {
  86. this.name = "DIODE";
  87. this.x = x;
  88. this.y = y;
  89. this.inputs = [{name: "In", links: []}];
  90. this.outputs = [{name: "Out", link: new Link(this, 0)}];
  91. this.width = 4;
  92. this.height = 1;
  93. this.lit = false;
  94. }
  95. tick() {
  96. this.outputs[0].link.next = false;
  97. for (let link of this.inputs[0].links) {
  98. if (link.current) {
  99. this.outputs[0].link.next = true;
  100. break;
  101. }
  102. }
  103. }
  104. commit() {
  105. this.outputs[0].link.commit();
  106. this.lit = this.outputs[0].link.current;
  107. }
  108. }
  109. export class Lamp {
  110. constructor(x, y) {
  111. this.name = "LAMP";
  112. this.x = x;
  113. this.y = y;
  114. this.inputs = [{name: "In", links: []}];
  115. this.outputs = [];
  116. this.width = 4;
  117. this.height = 1;
  118. this.lit = false;
  119. this.nextLit = false;
  120. }
  121. tick() {
  122. this.nextLit = false;
  123. for (let link of this.inputs[0].links) {
  124. if (link.current) {
  125. this.nextLit = true;
  126. break;
  127. }
  128. }
  129. }
  130. commit() {
  131. this.lit = this.nextLit;
  132. }
  133. }