-
Notifications
You must be signed in to change notification settings - Fork 11
/
Automaton.ts
206 lines (174 loc) · 5.56 KB
/
Automaton.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { Channel } from './Channel';
import { Curve } from './Curve';
import type { AutomatonOptions } from './types/AutomatonOptions';
import type { ChannelUpdateEvent } from './types/ChannelUpdateEvent';
import type { FxDefinition } from './types/FxDefinition';
import type { SerializedAutomaton } from './types/SerializedAutomaton';
/**
* IT'S AUTOMATON!
* @param data Serialized data of the automaton
* @param options Options for this Automaton instance
*/
export class Automaton {
/**
* It returns the current value of the [[Channel]] called `name`.
* If the `name` is an array, it returns a set of name : channel as an object instead.
* You can also give a listener which will be executed when the channel changes its value (optional).
* @param name The name of the channel
* @param listener A function that will be executed when the channel changes its value
* @returns Current value of the channel
*/
public readonly auto: Automaton[ '__auto' ] = this.__auto.bind( this );
/**
* Curves of the automaton.
*/
public readonly curves: Curve[] = [];
/**
* Channels of the timeline.
*/
public readonly channels: Channel[] = [];
/**
* Map of channels, name vs. channel itself.
*/
public readonly mapNameToChannel = new Map<string, Channel>();
/**
* Current time of the automaton.
* Can be set by [[update]], be retrieved by [[get time]], be used by [[auto]]
*/
protected __time: number = 0.0;
/**
* Version of the automaton.
*/
protected __version: string = process.env.VERSION!;
/**
* Resolution of the timeline.
*/
protected __resolution: number = 1000;
/**
* A map of fx definitions.
*/
protected __fxDefinitions: { [ name: string ]: FxDefinition } = {};
public constructor(
data: SerializedAutomaton,
options: AutomatonOptions = {}
) {
options.fxDefinitions && this.addFxDefinitions( options.fxDefinitions );
this.deserialize( data );
}
/**
* Current time of the automaton, that is set via [[update]].
*/
public get time(): number { return this.__time; }
/**
* Version of the automaton.
*/
public get version(): string { return this.__version; }
/**
* Resolution = Sampling point per second.
*/
public get resolution(): number { return this.__resolution; }
/**
* Load serialized automaton data.
* @param data Serialized object contains automaton data.
*/
public deserialize( data: SerializedAutomaton ): void {
this.__resolution = data.resolution;
this.curves.splice( 0 );
this.curves.push(
...data.curves.map( ( data ) => new Curve( this, data ) )
);
this.mapNameToChannel.clear();
this.channels.splice( 0 );
this.channels.push(
...data.channels.map( ( [ name, data ] ) => {
const channel = new Channel( this, data );
if ( process.env.NODE_ENV === 'development' ) {
if ( this.mapNameToChannel.has( name ) ) {
console.warn( `Duplicated channel: ${ name }` );
}
}
this.mapNameToChannel.set( name, channel );
return channel;
} )
);
}
/**
* Add fx definitions.
* @param fxDefinitions A map of id - fx definition
*/
public addFxDefinitions( fxDefinitions: { [ id: string ]: FxDefinition } ): void {
Object.entries( fxDefinitions ).forEach( ( [ id, fxDef ] ) => {
if ( typeof fxDef.func === 'function' ) { // ignore unrelated entries
if ( process.env.NODE_ENV === 'development' ) {
if ( this.__fxDefinitions[ id ] != null ) {
console.warn( `Overwriting the existing fx definition: ${ id }` );
}
}
this.__fxDefinitions[ id ] = fxDef;
}
} );
this.precalcAll();
}
/**
* Get a fx definition.
* If it can't find the definition, it returns `null` instead.
* @param id Unique id for the Fx definition
*/
public getFxDefinition( id: string ): FxDefinition | null {
return this.__fxDefinitions[ id ] || null;
}
/**
* Get a curve.
* @param index An index of the curve
*/
public getCurve( index: number ): Curve | null {
return this.curves[ index ] || null;
}
/**
* Precalculate all curves.
*/
public precalcAll(): void {
Object.values( this.curves ).forEach( ( curve ) => curve.precalc() );
}
/**
* Reset the internal states of channels.
* **Call this method when you seek the time.**
*/
public reset(): void {
Object.values( this.channels ).forEach( ( channel ) => channel.reset() );
}
/**
* Update the entire automaton.
* **You may want to call this in your update loop.**
* @param time Current time
*/
public update( time: number ): void {
const t = Math.max( time, 0.0 );
// cache the time
this.__time = t;
// update channels
const array = this.channels.map( ( channel ) => channel.consume( this.__time ) ).flat( 1 );
array.sort( ( [ a ], [ b ] ) => a - b ).forEach( ( [ _, func ] ) => func() );
}
/**
* Assigned to {@link Automaton#auto} on its initialize phase.
* @param name The name of the channel
* @param listener A function that will be executed when the channel changes its value
* @returns Current value of the channel
*/
protected __auto(
name: string,
listener?: ( event: ChannelUpdateEvent ) => void
): number {
const channel = this.mapNameToChannel.get( name );
if ( process.env.NODE_ENV === 'development' ) {
if ( !channel ) {
throw new Error( `No such channel: ${ name }` );
}
}
if ( listener ) {
channel!.subscribe( listener );
}
return channel!.currentValue;
}
}