assassin-bug/framework/resonator/effect-chain.js

46 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2022-11-26 01:22:02 +00:00
// A chain of effects that connect to the effect bus
export default class EffectChain {
constructor(context, graph, input, output) {
this.effects = [];
this.context = context;
this.graph = graph;
this.inputNode = input;
this.outputNode = output;
this.updateConnections();
}
applyEffect(effect) {
this.effects.push(effect);
this.updateConnections();
}
removeEffect(effect) {
this.effects.forEach((currEffect) => {
if (effect === currEffect) {
currEffect.disconnect();
}
});
this.effects = this.effects.filter((currEffect) => effect !== currEffect);
this.updateConnections();
}
updateConnections() {
if (this.effects.length == 0) {
this.inputNode.connect(this.outputNode);
return;
}
let current = null;
let previous = null;
this.effects.forEach((effect) => {
current = effect;
if (previous) {
current.connectInput(previous.getOutput());
}
else {
current.connectInput(this.inputNode);
}
previous = current;
});
if (current) {
current.connectOutput(this.outputNode);
}
}
}