Last active
May 24, 2018 11:16
-
-
Save jamesseanwright/4270ff9829725aefec7dea9f2896b4a5 to your computer and use it in GitHub Desktop.
Sect - functional System API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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