new-logic-gate-sim/gates/gate.js

68 lines
1.8 KiB
JavaScript

class Gate {
constructor(x, y, type, image) {
this.x = x;
this.y = y;
this.image = image;
this.types = new Types(type);
this.selected = false;
this.width = 50;
this.height = 50;
this.inputs = [];
this.outputs = [];
for (var i = 0; i < this.types.type.inputs; i++) {
//TODO calculate postions for ankors based on this.x and this.y
this.inputs.push(new Ankor(this.x - 10, this.y));
}
for (var i = 0; i < this.types.type.outputs; i++) {
//TODO calculate postions for ankors based on this.x and this.y
this.outputs.push(new Ankor(this.x + 10, this.y));
}
}
draw() {
//TODO draw ankors add exception for light and switch
//draw gate and load image
fill(255);
image(this.image, this.x, this.y);
noFill();
noStroke();
rect(this.x, this.y, this.width, this.height);
//draw ankors
}
calculate() {
//todo calculate outputs based on inputs and function and add exception for light and switch if type.state
}
onGate(x, y) {
if (
x > this.x &&
x < this.x + this.width &&
y > this.y &&
y < this.y + this.height
) {
return true;
}
return false;
}
returnOnAnkor(x, y) {
//todo add actual onankor that works with renderd circles
this.inputs.forEach(ankor => {
if (x == ankor.x && y == ankor.y) {
return ankor;
}
});
this.outputs.forEach(ankor => {
if (x == ankor.x && y == ankor.y) {
return ankor;
}
});
return false;
}
}