-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulatorEngine.ts
More file actions
132 lines (113 loc) · 3.41 KB
/
Copy pathSimulatorEngine.ts
File metadata and controls
132 lines (113 loc) · 3.41 KB
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
import type { CanonicalAircraftDefinition } from './aircraft'
import {
SimCommandBus,
type SimCommand,
type SimCommandDispatchResult,
} from './commands'
import { SimStateStore } from './state'
import type {
SimDiagnostic,
SimSchedulerPhase,
SimSubsystem,
SimSubsystemContext,
SimSubsystemTickContext,
} from './subsystem'
const SCHEDULER_PHASES: readonly SimSchedulerPhase[] = [
'input',
'systems',
'physics',
'output',
]
export class SimulatorEngine {
readonly state = new SimStateStore()
readonly commands = new SimCommandBus()
readonly diagnostics: SimDiagnostic[] = []
private readonly subsystems: SimSubsystem[] = []
private readonly commandUnsubscribers = new Map<string, () => void>()
private aircraft: CanonicalAircraftDefinition | undefined
private elapsedSeconds = 0
constructor(aircraft?: CanonicalAircraftDefinition) {
if (aircraft != null) {
this.loadAircraft(aircraft)
}
}
loadAircraft(aircraft: CanonicalAircraftDefinition): void {
this.aircraft = aircraft
for (const seed of aircraft.initialState ?? []) {
this.state.define({
key: seed.key,
unit: seed.unit,
valueType: seed.valueType,
description: seed.description,
})
this.state.set(seed.key, seed.value, {
source: seed.source ?? 'loaded',
unit: seed.unit,
metadata: { aircraftId: aircraft.identity.id },
})
}
}
getAircraft(): CanonicalAircraftDefinition | undefined {
return this.aircraft
}
registerSubsystem(subsystem: SimSubsystem): void {
if (this.subsystems.some((candidate) => candidate.id === subsystem.id)) {
throw new Error(`Subsystem already registered: ${subsystem.id}`)
}
this.subsystems.push(subsystem)
subsystem.initialize?.(this.createSubsystemContext())
if (subsystem.handleCommand != null) {
const unsubscribe = this.commands.subscribe('*', (command) => {
return subsystem.handleCommand?.(command, this.createSubsystemContext())
})
this.commandUnsubscribers.set(subsystem.id, unsubscribe)
}
}
dispatch(command: SimCommand): SimCommandDispatchResult {
return this.commands.dispatch({
...command,
timestampSeconds: command.timestampSeconds ?? this.elapsedSeconds,
})
}
tick(dtSeconds: number): void {
for (const phase of SCHEDULER_PHASES) {
for (const subsystem of this.subsystems) {
if ((subsystem.phase ?? 'systems') !== phase || subsystem.tick == null) {
continue
}
subsystem.tick(this.createTickContext(dtSeconds, phase))
}
}
this.elapsedSeconds += dtSeconds
}
dispose(): void {
const context = this.createSubsystemContext()
for (const subsystem of [...this.subsystems].reverse()) {
subsystem.shutdown?.(context)
this.commandUnsubscribers.get(subsystem.id)?.()
this.commandUnsubscribers.delete(subsystem.id)
}
this.subsystems.length = 0
}
private createSubsystemContext(): SimSubsystemContext {
return {
state: this.state,
commands: this.commands,
aircraft: this.aircraft,
elapsedSeconds: this.elapsedSeconds,
diagnose: (diagnostic) => {
this.diagnostics.push(diagnostic)
},
}
}
private createTickContext(
dtSeconds: number,
phase: SimSchedulerPhase
): SimSubsystemTickContext {
return {
...this.createSubsystemContext(),
dtSeconds,
phase,
}
}
}