Skip to content

Commit

Permalink
Apply root component RNG options to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixHenninger committed May 7, 2024
1 parent 94c74f3 commit ab0b816
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/library/src/base/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ export class Component {
this.options.parameters,
)
this.internals.armOptions = arm

// Setup plugins
//@ts-ignore Use not-for-public-consumption method
plugins.init()
}

log(message: string) {
Expand Down
2 changes: 2 additions & 0 deletions packages/library/src/base/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export class PluginAPI<C extends Component = Component, E = string> {
constructor(context: C, plugins: Array<Plugin<C, E>> = []) {
this.#context = context
this.plugins = plugins
}

private init() {
// Initialize existing plugins
this.plugins.forEach(p => p.handle(this.#context, 'plugin:add'))

Expand Down
1 change: 1 addition & 0 deletions packages/library/src/core/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class Component extends BaseComponent {
this.internals.controller = new Controller({
root: this,
el: this.options.el,
random: this.options.random,
})
}

Expand Down
23 changes: 17 additions & 6 deletions packages/library/src/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ export interface ControllerGlobal {
*/
export class Controller extends BaseController<Component> {
global!: ControllerGlobal
#seed: String
#random?: RNGOptions

/**
* Create a new controller
*/
constructor({ root, el }: { root: Component; el?: Element }) {
constructor({
root,
el,
random,
}: {
root: Component
el?: Element
random?: RNGOptions
}) {
const audioContext = new (window.AudioContext ??
window.webkitAudioContext)()

Expand Down Expand Up @@ -75,17 +83,20 @@ export class Controller extends BaseController<Component> {

super({ root, global, initialContext })

// Generate random seed
this.#seed = autoSeed()
// Setup RNG options
this.#random = random
}

createRNG(seedFragment: String, options: RNGOptions = {}) {
// Auto-generate seed by combining controller seed + component ID
const seed = `${this.#seed}-${seedFragment}`
const seed = this.#random?.seed
? `${this.#random?.seed}-${seedFragment}`
: undefined

return new Random({
seed,
...options, // Allow manual override of component seed
...this.#random,
...options, // Allow manual override of global options
})
}
}

0 comments on commit ab0b816

Please sign in to comment.