Skip to content

Commit

Permalink
Move RNG generation to controller, based on global seed
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixHenninger committed Apr 15, 2024
1 parent 2f2bd7b commit 411083b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/library/src/core/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export class Component extends BaseComponent {

// Hook up state from controller
this.state = this.internals.controller.global.datastore?.state
this.random = new Random(this.options.random)
this.random = this.internals.controller.createRNG(
this.id ?? '', // ID is defined only on base.Component ATM
this.options.random,
)
// Create timeline
this.internals.timeline = new Timeline(
this.internals.controller,
Expand Down
16 changes: 16 additions & 0 deletions packages/library/src/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Component } from './component'
import { Controller as BaseController } from '../base/controller'
import { Row, Store } from '../data/store'
import { ImageCache, AudioCache } from './cache'
import { RNGOptions, Random } from '../util/random'
import { autoSeed } from '../util/random/seed'

declare global {
interface Window {
Expand Down Expand Up @@ -38,6 +40,7 @@ export interface ControllerGlobal {
*/
export class Controller extends BaseController<Component> {
global!: ControllerGlobal
#seed: String

/**
* Create a new controller
Expand Down Expand Up @@ -71,5 +74,18 @@ export class Controller extends BaseController<Component> {
}

super({ root, global, initialContext })

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

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

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

0 comments on commit 411083b

Please sign in to comment.