-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmodule.js
129 lines (102 loc) · 2.38 KB
/
module.js
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
import * as THREE from 'three'
// @TODO:
// - Do more in lifecycles, like register/remove data, events, animations
// - Freeze common managers, can use Object.defineProperty
export default class Module {
constructor(attributes) {
// States
this.enabled = true
this.priority = 0
if (attributes && attributes.priority) {
this.priority = attributes.priority
}
}
play() {
this.enabled = true
}
pause() {
this.enabled = false
}
stop() {
this.pause()
if (this.instance) {
this.scene.remove(this.instance)
}
this.audio.stopAll()
// @TODO: Stop all running animations
}
resize() {}
update(delta, elapsed, timestamp) {}
}
Module.isModule = true
export class ModuleManager {
constructor(sketch) {
this.sketch = sketch
this.modules = []
}
sortModules() {
this.modules.sort((a, b) => {
return a.priority - b.priority || a.order - b.order
})
}
get(Module) {
return this.modules.find((s) => s instanceof Module)
}
add(Module, attributes) {
if (!Module.isModule) {
throw new Error(
`Module '${Module.name}' does not extend 'Module' class`
)
}
if (this.get(Module) !== undefined) {
console.warn(`Module '${Module.name}' already registered.`)
return
}
const module = new Module(this.sketch, attributes)
module.order = this.modules.length
this.modules.push(module)
this.sortModules()
return module
}
remove(Module) {
const module = this.get(Module)
if (module === undefined) {
console.warn(
`Can unregister module '${Module.name}'. It doesn't exist.`
)
return
}
module.stop()
this.modules.splice(this.modules.indexOf(module), 1)
}
play() {
this.modules.map((module) => module.play())
}
pause() {
this.modules.map((module) => module.pause())
}
stop() {
this.modules.map((module) => module.stop())
}
resize() {
this.modules.map((module) => module.resize())
}
set(object) {
this.modules.map((module) => {
Object.assign(module, object)
})
}
update(delta, elapsed, timestamp) {
this.modules.map((module) => {
if (module.enabled) {
module.update(delta, elapsed, timestamp)
}
})
}
// @TODO: Add destory method if necessary
destroy() {
this.modules.map((module) => {
this.remove(module)
})
}
}