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.8KB

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