PNet/js/PElems/Transition.ts

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-06-22 17:47:04 +02:00
import { svgNodes } from "..";
import { SVGTRansition } from "../graphics/SVGTransition";
import { Positionable } from "../types";
import { Edge } from "./Edge";
export class Transition implements Positionable {
renderedElement: SVGTRansition;
name = "noname";
inEdges: Edge[] = [];
outEdges: Edge[] = [];
x: number;
y: number;
constructor(name: string, x: number, y: number) {
this.name = name;
this.x = x;
this.y = y;
this.renderedElement = new SVGTRansition(this);
this.renderedElement.appendIt(svgNodes);
this.renderedElement.onMove((event) => this.updatePosition(event.x, event.y));
}
canFire() {
for (const inE of this.inEdges) {
if (!inE.canSuck()) {
this.renderedElement.setCanFire(false);
return false;
}
}
for (const outE of this.outEdges) {
if (!outE.canPush()) {
this.renderedElement.setCanFire(false);
return false;
}
}
this.renderedElement.setCanFire(true);
return true;
}
fire() {
if (!this.canFire()) return false;
for (const inE of this.inEdges) {
inE.suck();
}
for (const outE of this.outEdges) {
outE.push();
}
return true;
}
updatePosition(x: number, y: number) {
this.x = x;
this.y = y;
this.renderedElement.translate(x, y);
for (const inE of this.inEdges) {
inE.updatePosition();
}
for (const outE of this.outEdges) {
outE.updatePosition();
}
}
}