PNet/js/PElems/Transition.ts
2023-06-27 09:19:06 +02:00

65 lines
No EOL
1.9 KiB
TypeScript

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));
}
#canFireC = false;
canFire() {
this.#canFireC = false;
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);
this.#canFireC = true;
return true;
}
fire() {
let canFireNow = this.canFire()
if(this.#canFireC && !canFireNow){
// TODO: koflikt.
}
if (!this.#canFireC || !canFireNow) 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();
}
}
}