Skip to content

Instantly share code, notes, and snippets.

@jamesseanwright
Last active May 24, 2018 11:16
Show Gist options
  • Save jamesseanwright/4270ff9829725aefec7dea9f2896b4a5 to your computer and use it in GitHub Desktop.
Save jamesseanwright/4270ff9829725aefec7dea9f2896b4a5 to your computer and use it in GitHub Desktop.
Sect - functional System API
'use strict';
class System {
constructor(name, next) {
this.name = name;
this.components = [];
this.next = next;
}
getOtherComponents(component) {
return this.components.filter(c => c !== component); // TODO: memoize à la LinearCollisionSystem
}
register(component) {
this.components.push(component);
}
update(timestamp) {
for (const component of this.components) {
try {
this.next(timestamp, component, this.getOtherComponents(component));
} catch (e) {
throw new Error(`Failed to update ${this.name}: ${error.message}`);
}
}
}
}
const createSystem = (name, next) => new System(name, next);
const mySystem = createSystem('dummySystem', (timestamp, component, ...otherComponents) => {
console.log('System logic here!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment