-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathaudioEvents.ts
More file actions
101 lines (86 loc) · 3.13 KB
/
Copy pathaudioEvents.ts
File metadata and controls
101 lines (86 loc) · 3.13 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
import * as components from '../components'
import { DeepReadonlyObject, Entity, IEngine } from '../engine'
import { PBAudioEvent } from '../components'
import { EntityState } from '../engine/entity'
/**
* @public
*/
export type AudioEventsSystemCallback = (event: DeepReadonlyObject<PBAudioEvent>) => void
/**
* @public
*/
export interface AudioEventsSystem {
removeAudioEventsEntity(entity: Entity): void
registerAudioEventsEntity(entity: Entity, callback: AudioEventsSystemCallback): void
hasAudioEventsEntity(entity: Entity): boolean
/**
* Returns the latest state of the AudioEvent
* @param entity - Entity to retrieve the audio status
*/
getAudioState(entity: Entity): DeepReadonlyObject<PBAudioEvent> | undefined
}
/**
* @internal
*/
export function createAudioEventsSystem(engine: IEngine): AudioEventsSystem {
const audioSourceComponent = components.AudioSource(engine)
const audioStreamComponent = components.AudioStream(engine)
const audioEventComponent = components.AudioEvent(engine)
const entitiesCallbackAudioStateMap = new Map<
Entity,
{
callback: AudioEventsSystemCallback
lastAudioState?: number
}
>()
function registerAudioEventsEntity(entity: Entity, callback: AudioEventsSystemCallback) {
// audio event component is not added here because the renderer adds it
// to every entity with an AudioSource or AudioStream component
entitiesCallbackAudioStateMap.set(entity, { callback: callback })
}
function removeAudioEventsEntity(entity: Entity) {
entitiesCallbackAudioStateMap.delete(entity)
}
function hasAudioEventsEntity(entity: Entity) {
return entitiesCallbackAudioStateMap.has(entity)
}
// @internal
engine.addSystem(function EventSystem() {
for (const [entity, data] of entitiesCallbackAudioStateMap) {
const hasAudioSource = audioSourceComponent.has(entity)
const hasAudioStream = audioStreamComponent.has(entity)
if (engine.getEntityState(entity) === EntityState.Removed || (!hasAudioSource && !hasAudioStream)) {
removeAudioEventsEntity(entity)
continue
}
// Compare with last state
const audioEvent = audioEventComponent.get(entity)
const values = Array.from(audioEvent.values())
const lastValue = values[audioEvent.size - 1]
if (lastValue === undefined || (data.lastAudioState !== undefined && data.lastAudioState === lastValue.state))
continue
data.callback(lastValue)
entitiesCallbackAudioStateMap.set(entity, {
callback: data.callback,
lastAudioState: lastValue.state
})
}
})
return {
removeAudioEventsEntity(entity: Entity) {
removeAudioEventsEntity(entity)
},
registerAudioEventsEntity(entity: Entity, callback: AudioEventsSystemCallback) {
registerAudioEventsEntity(entity, callback)
},
hasAudioEventsEntity(entity: Entity) {
return hasAudioEventsEntity(entity)
},
getAudioState(entity: Entity) {
const audioEvent = audioEventComponent.get(entity)
const values = Array.from(audioEvent.values())
const lastValue = values[audioEvent.size - 1]
return lastValue
}
}
}