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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, name) {
  36. this.name = name || "Input";
  37. this.x = x;
  38. this.y = y;
  39. this.inputs = [];
  40. this.outputs = [{name: "Input", link: new Link(this, 0)}];
  41. this.width = 4;
  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 Output {
  60. constructor(x, y, name) {
  61. this.name = name || "Output";
  62. this.x = x;
  63. this.y = y;
  64. this.inputs = [{name: "Output", links: []}];
  65. this.outputs = [];
  66. this.width = 4;
  67. this.height = 1;
  68. this.lit = false;
  69. this.nextLit = false;
  70. }
  71. tick() {
  72. this.nextLit = false;
  73. for (let link of this.inputs[0].links) {
  74. if (link.current) {
  75. this.nextLit = true;
  76. break;
  77. }
  78. }
  79. }
  80. commit() {
  81. this.lit = this.nextLit;
  82. }
  83. }
  84. export class Switch {
  85. constructor(x, y) {
  86. this.name = "OFF";
  87. this.x = x;
  88. this.y = y;
  89. this.inputs = [];
  90. this.outputs = [{name: "Out", link: new Link(this, 0)}];
  91. this.width = 3;
  92. this.height = 1;
  93. this.lit = false;
  94. }
  95. activate() {
  96. this.lit = !this.lit;
  97. this.name = this.lit ? "ON" : "OFF";
  98. }
  99. tick() {
  100. if (this.lit) {
  101. this.outputs[0].link.next = true;
  102. } else {
  103. this.outputs[0].link.next = false;
  104. }
  105. }
  106. commit() {
  107. this.outputs[0].link.commit();
  108. }
  109. }
  110. export class NotGate {
  111. constructor(x, y) {
  112. this.name = "NOT";
  113. this.x = x;
  114. this.y = y;
  115. this.inputs = [{name: "In", links: []}];
  116. this.outputs = [{name: "Out", link: new Link(this, 0)}];
  117. this.width = 4;
  118. this.height = 1;
  119. this.lit = false;
  120. }
  121. tick() {
  122. this.outputs[0].link.next = true;
  123. for (let link of this.inputs[0].links) {
  124. if (link.current) {
  125. this.outputs[0].link.next = false;
  126. break;
  127. }
  128. }
  129. }
  130. commit() {
  131. this.outputs[0].link.commit();
  132. this.lit = this.outputs[0].link.current;
  133. }
  134. }
  135. export class Diode {
  136. constructor(x, y) {
  137. this.name = "DIODE";
  138. this.x = x;
  139. this.y = y;
  140. this.inputs = [{name: "In", links: []}];
  141. this.outputs = [{name: "Out", link: new Link(this, 0)}];
  142. this.width = 4;
  143. this.height = 1;
  144. this.lit = false;
  145. }
  146. tick() {
  147. this.outputs[0].link.next = false;
  148. for (let link of this.inputs[0].links) {
  149. if (link.current) {
  150. this.outputs[0].link.next = true;
  151. break;
  152. }
  153. }
  154. }
  155. commit() {
  156. this.outputs[0].link.commit();
  157. this.lit = this.outputs[0].link.current;
  158. }
  159. }
  160. export class Lamp {
  161. constructor(x, y) {
  162. this.name = "LAMP";
  163. this.x = x;
  164. this.y = y;
  165. this.inputs = [{name: "In", links: []}];
  166. this.outputs = [];
  167. this.width = 4;
  168. this.height = 1;
  169. this.lit = false;
  170. this.nextLit = false;
  171. }
  172. tick() {
  173. this.nextLit = false;
  174. for (let link of this.inputs[0].links) {
  175. if (link.current) {
  176. this.nextLit = true;
  177. break;
  178. }
  179. }
  180. }
  181. commit() {
  182. this.lit = this.nextLit;
  183. }
  184. }