silphius/app/ecs/components/emitter.js
2024-09-05 16:52:22 -05:00

50 lines
1.3 KiB
JavaScript

import Component from '@/ecs/component.js';
import Emitter from '@/particles/emitter.js';
import {Ticker as TickerPromise} from '@/util/promise.js';
export default class EmitterComponent extends Component {
instanceFromSchema() {
const Component = this;
const {ecs} = this;
return class EmitterInstance extends super.instanceFromSchema() {
emitting = {};
id = 0;
emit(specification) {
if (specification.server) {
const {Ticker} = ecs.get(1);
if (Ticker) {
const emitter = new Emitter(ecs);
const promise = new Promise((resolve) => {
emitter.emit().onEnd(resolve);
});
Ticker.add(
new TickerPromise(
(resolve) => {
promise.then(resolve);
},
(elapsed) => {
this.emitter.tick(elapsed);
}
)
);
}
}
else {
Component.markChange(this.entity, 'emit', {[this.id++]: specification});
}
}
};
}
mergeDiff(original, update) {
const merged = {};
if (original.emit || update.emit) {
merged.emit = {
...original.emit,
...update.emit,
}
}
return merged;
}
}