@vyr/engine 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/package.json +1 -19
  2. package/src/Category.ts +7 -7
  3. package/src/Engine.ts +29 -15
  4. package/src/actor/AnimationUnitActor.ts +3 -3
  5. package/src/actor/DivActor.ts +6 -6
  6. package/src/actor/FragmentActor.ts +3 -3
  7. package/src/actor/HTMActor.ts +19 -16
  8. package/src/actor/HTMServiceActor.ts +4 -4
  9. package/src/actor/HTMTransformControllerActor.ts +15 -10
  10. package/src/asset/Asset.ts +25 -27
  11. package/src/asset/AssetGraph.ts +4 -4
  12. package/src/descriptor/DatasetDescriptor.ts +20 -22
  13. package/src/descriptor/Descriptor.ts +1 -28
  14. package/src/descriptor/HTMLTransformControllerDescriptor.ts +2 -2
  15. package/src/descriptor/index.ts +0 -1
  16. package/src/graphics/Compilation.ts +37 -45
  17. package/src/graphics/Graphics.ts +80 -68
  18. package/src/graphics/VariableProxy.ts +8 -8
  19. package/src/index.ts +2 -1
  20. package/src/interaction/InteractionDescriptor.ts +96 -0
  21. package/src/interaction/InteractionExecutor.ts +84 -0
  22. package/src/interaction/Scriptable.ts +40 -0
  23. package/src/interaction/index.ts +3 -0
  24. package/src/interpreter/AnimationUnitInterpreter.ts +5 -5
  25. package/src/interpreter/DivInterpreter.ts +15 -15
  26. package/src/interpreter/DynamicInterpreter.ts +9 -9
  27. package/src/interpreter/FragmentInterpreter.ts +10 -10
  28. package/src/interpreter/HTMLServiceInterpreter.ts +15 -15
  29. package/src/interpreter/HTMLTransformControllerInterpreter.ts +15 -15
  30. package/src/interpreter/Interpreter.ts +9 -9
  31. package/src/interpreter/ServiceInterpreter.ts +6 -6
  32. package/src/interpreter/ServiceSchedulerInterpreter.ts +6 -6
  33. package/src/interpreter/StyleInterpreter.ts +7 -7
  34. package/src/interpreter/index.ts +0 -1
  35. package/src/locale/LanguageProvider.ts +4 -2
  36. package/src/preset/execute/dataset/compile.ts +99 -0
  37. package/src/preset/execute/dataset/index.ts +2 -1
  38. package/src/preset/execute/dataset/update.ts +7 -8
  39. package/src/preset/execute/graphics/invoke.ts +5 -6
  40. package/src/preset/execute/net/{request.ts → http.ts} +9 -17
  41. package/src/preset/execute/net/index.ts +1 -1
  42. package/src/preset/execute/scheduler/switch.ts +8 -8
  43. package/src/preset/index.ts +2 -2
  44. package/src/preset/interaction/dataset/compile.ts +27 -0
  45. package/src/preset/interaction/dataset/index.ts +1 -0
  46. package/src/preset/interaction/graphics/invoke.ts +27 -0
  47. package/src/preset/interaction/index.ts +3 -0
  48. package/src/preset/interaction/scheduler/switch.ts +27 -0
  49. package/src/utils/AssetProvider.ts +7 -6
  50. package/src/utils/constants.ts +10 -0
  51. package/src/utils/index.ts +1 -0
  52. package/src/Scriptable.ts +0 -27
  53. package/src/descriptor/RoutineDescriptor.ts +0 -54
  54. package/src/interpreter/RoutineInterpreter.ts +0 -88
  55. package/src/preset/routine/graphics/invoke.ts +0 -27
  56. package/src/preset/routine/index.ts +0 -2
  57. package/src/preset/routine/scheduler/switch.ts +0 -27
  58. /package/src/preset/{routine → interaction}/graphics/index.ts +0 -0
  59. /package/src/preset/{routine → interaction}/scheduler/index.ts +0 -0
package/package.json CHANGED
@@ -1,19 +1 @@
1
- {
2
- "name": "@vyr/engine",
3
- "version": "0.0.1",
4
- "description": "",
5
- "main": "./src/index.ts",
6
- "author": "",
7
- "license": "MIT",
8
- "dependencies": {
9
- "@vyr/locale": "0.0.1",
10
- "tinycolor2": "1.6.0"
11
- },
12
- "devDependencies": {
13
- "@types/tinycolor2": "1.4.6"
14
- },
15
- "files": [
16
- "package.json",
17
- "src/"
18
- ]
19
- }
1
+ {"name":"@vyr/engine","version":"0.0.3","description":"","main":"./src/index.ts","author":"","license":"MIT","dependencies":{"@vyr/locale":"0.0.3","tinycolor2":"1.6.0"},"devDependencies":{"@types/tinycolor2":"1.4.6"},"files":["package.json","src/"]}
package/src/Category.ts CHANGED
@@ -5,7 +5,7 @@ class Category {
5
5
  static dataset = 'dataset'
6
6
  static style = 'style'
7
7
  static font = 'font'
8
- static routine = 'routine'
8
+ static interaction = 'interaction'
9
9
  static texture = 'texture'
10
10
  static material = 'material'
11
11
  static geometry = 'geometry'
@@ -22,7 +22,7 @@ class Category {
22
22
  static datasetSuffix = `.${this.dataset}.json`
23
23
  static styleSuffix = `.${this.style}.json`
24
24
  static fontSuffix = `.${this.font}.json`
25
- static routineSuffix = `.${this.routine}.json`
25
+ static interactionSuffix = `.${this.interaction}.json`
26
26
  static textureSuffix = `.${this.texture}.json`
27
27
  static materialSuffix = `.${this.material}.json`
28
28
  static geometrySuffix = `.${this.geometry}.json`
@@ -39,7 +39,7 @@ class Category {
39
39
  this.prefabSuffix,
40
40
  this.datasetSuffix,
41
41
  this.styleSuffix,
42
- this.routineSuffix,
42
+ this.interactionSuffix,
43
43
  this.textureSuffix,
44
44
  this.materialSuffix,
45
45
  this.geometrySuffix,
@@ -49,7 +49,7 @@ class Category {
49
49
  this.prefab,
50
50
  this.dataset,
51
51
  this.style,
52
- this.routine,
52
+ this.interaction,
53
53
  this.texture,
54
54
  this.material,
55
55
  this.geometry,
@@ -68,8 +68,8 @@ class Category {
68
68
  return this.style
69
69
  } else if (suffix === this.fontSuffix) {
70
70
  return this.font
71
- } else if (suffix === this.routineSuffix) {
72
- return this.routine
71
+ } else if (suffix === this.interactionSuffix) {
72
+ return this.interaction
73
73
  } else if (suffix === this.textureSuffix) {
74
74
  return this.texture
75
75
  } else if (suffix === this.materialSuffix) {
@@ -109,7 +109,7 @@ class Category {
109
109
  return result
110
110
  }
111
111
 
112
- /**根据后缀判断是否为描述器类资产(场景,组合,材质,纹理,几何体) */
112
+ /**根据后缀判断是否为描述器类资产 */
113
113
  static isDescriptor(suffix: string, type: 'suffix' | 'category' = 'suffix') {
114
114
  const tests = type === 'suffix' ? Category.descriptorSuffixs : Category.descriptorCategorys
115
115
  return tests.includes(suffix)
package/src/Engine.ts CHANGED
@@ -1,26 +1,22 @@
1
1
  import { language } from './locale'
2
- import { Descriptor, RoutineDescriptor, ServiceSchedulerDescriptor, StyleDescriptor, UpdateArgs } from './descriptor'
2
+ import { Asset } from './asset'
3
+ import { ServiceSchedulerDescriptor, StyleDescriptor } from './descriptor'
3
4
  import { Generate } from './Generate'
4
5
  import { InputSystem } from './InputSystem'
5
6
  import { Listener } from './Listener'
6
- import { Compilation, Graphics } from './graphics'
7
+ import { Compilation, TickArgs } from './graphics'
7
8
  import { setupStyle } from './setup';
8
- import { Asset } from './asset'
9
- import { RoutineInterpreter } from './interpreter'
10
9
 
11
10
  interface EngineData { }
12
11
 
13
12
  interface EngineListener {
14
- beforeRender: (args: UpdateArgs) => void
15
- afterRender: (args: UpdateArgs) => void
16
- }
17
-
18
- interface PickupUpdateArgs extends UpdateArgs {
19
- mouse: { x: number; y: number }
13
+ beforeRender: (args: TickArgs) => void
14
+ afterRender: (args: TickArgs) => void
20
15
  }
21
16
 
22
17
  class Engine extends Listener<EngineListener> {
23
18
  private compilation = new Compilation()
19
+ private active = ''
24
20
  private prevTimestamp = 0
25
21
  private frame = 0
26
22
 
@@ -52,6 +48,8 @@ class Engine extends Listener<EngineListener> {
52
48
 
53
49
  const args = { delta }
54
50
 
51
+ this.compilation.foreach(graphics => graphics.args = args)
52
+
55
53
  this.inputSystem.process()
56
54
 
57
55
  this.compilation.process(this, args)
@@ -80,22 +78,38 @@ class Engine extends Listener<EngineListener> {
80
78
  }
81
79
 
82
80
  switch(scheduler: ServiceSchedulerDescriptor) {
83
- this.compilation.clear(this)
81
+ const url = Asset.getUrlByUuid(scheduler.uuid)
82
+
83
+ if (!url) throw '切换失败,无法获资产地址!'
84
+
85
+ const current = Asset.get<ServiceSchedulerDescriptor>(this.active)
86
+ if (current) this.compilation.unlisten(current)
84
87
 
85
- this.compilation.listen(scheduler, this)
88
+ const graphics = this.compilation.ensureExists(url, scheduler, this)
89
+
90
+ this.compilation.listen(graphics)
91
+ this.active = url
86
92
 
87
93
  window.dispatchEvent(new Event('resize'))
88
94
 
89
- this.compilation.process(this, { delta: 0 })
95
+ const args = { delta: 0 }
96
+
97
+ this.compilation.foreach(graphics => graphics.args = args)
98
+
99
+ this.compilation.process(this, args)
90
100
  }
91
101
 
92
102
  clear() {
93
- this.compilation.clear(this)
103
+ this.compilation.clear()
94
104
  }
95
105
 
96
106
  getGraphics(scheduler: ServiceSchedulerDescriptor) {
97
107
  return this.compilation.get(scheduler)
98
108
  }
109
+
110
+ ensureGraphicsExists(url: string, scheduler: ServiceSchedulerDescriptor) {
111
+ return this.compilation.ensureExists(url, scheduler, this)
112
+ }
99
113
  }
100
114
 
101
- export { PickupUpdateArgs, Engine }
115
+ export { Engine }
@@ -1,7 +1,7 @@
1
1
  import { Euler, Quaternion, Vector2, Vector3 } from "../math"
2
- import { AnimationUnitDescriptor, Descriptor, UpdateArgs } from "../descriptor"
2
+ import { AnimationUnitDescriptor, Descriptor } from "../descriptor"
3
3
  import { AnimationUnitInterpreter } from "../interpreter"
4
- import { Graphics } from "../graphics"
4
+ import { Graphics, TickArgs } from "../graphics"
5
5
  import { ObjectUtils } from '../ObjectUtils'
6
6
  import { Color } from '../Color'
7
7
  import { Actor } from "./Actor"
@@ -91,7 +91,7 @@ abstract class AnimationUnitActor extends Actor {
91
91
  }
92
92
  }
93
93
 
94
- update = (args: UpdateArgs) => {
94
+ update = (args: TickArgs) => {
95
95
  if (AnimationUnitInterpreter.enabled === false) return
96
96
  if (this.play === false) return
97
97
  const uptime = this.uptime + args.delta
@@ -1,4 +1,4 @@
1
- import { DivDescriptor, StyleDescriptor, UpdateArgs } from "../descriptor"
1
+ import { DivDescriptor, StyleDescriptor } from "../descriptor"
2
2
  import { Actor } from "./Actor"
3
3
  import { HTMLActor } from "./HTMActor"
4
4
 
@@ -52,13 +52,13 @@ class DivActor extends HTMLActor {
52
52
  }
53
53
  }
54
54
 
55
- update(descriptor: DivDescriptor, args: UpdateArgs) {
55
+ update(descriptor: DivDescriptor) {
56
56
  const wrapper = this.getWrapper()
57
- const style = this.getWrapperStyle(descriptor, args)
57
+ const style = this.getWrapperStyle(descriptor)
58
58
  this.setHTMLStyle(wrapper, style)
59
- this.setHTMLInteraction(wrapper, descriptor, args)
60
- this.setHTMLStyle(this.DOM, this.getLayoutStyle(descriptor, args))
61
- const styleClass = [...this.getStyleClass(descriptor, args)]
59
+ this.setHTMLInteraction(wrapper, descriptor)
60
+ this.setHTMLStyle(this.DOM, this.getLayoutStyle(descriptor))
61
+ const styleClass = [...this.getStyleClass(descriptor)]
62
62
  if (descriptor.active) styleClass.push(StyleDescriptor.activeName)
63
63
  this.setStyleClass(this.DOM, styleClass)
64
64
  this.DOM.style.backgroundImage = descriptor.backroundIamge ? `url(${descriptor.backroundIamge})` : ''
@@ -15,7 +15,7 @@ class FragmentActor extends Actor {
15
15
  const graphics = Actor.getGraphics(this)
16
16
  const parent = Descriptor.get(this._parent)
17
17
  if (parent instanceof Descriptor) {
18
- const parentActor = graphics.getActor(parent, { delta: 0 })
18
+ const parentActor = graphics.getActor(parent)
19
19
  if (parentActor !== null) {
20
20
  for (const actor of this.collection) parentActor.remove(actor)
21
21
  }
@@ -31,7 +31,7 @@ class FragmentActor extends Actor {
31
31
  const parent = Descriptor.get<Descriptor>(this._parent)
32
32
 
33
33
  if (parent instanceof Descriptor) {
34
- const parentActor = graphics.getActor(parent, { delta: 0 })
34
+ const parentActor = graphics.getActor(parent)
35
35
  if (parentActor !== null) parentActor.add(actor)
36
36
  }
37
37
 
@@ -44,7 +44,7 @@ class FragmentActor extends Actor {
44
44
  const graphics = Actor.getGraphics(this)
45
45
  const parent = Descriptor.get<Descriptor>(this._parent)
46
46
  if (parent instanceof Descriptor) {
47
- const parentActor = graphics.getActor(parent, { delta: 0 })
47
+ const parentActor = graphics.getActor(parent)
48
48
  if (parentActor !== null) parentActor.remove(actor)
49
49
  }
50
50
  ArrayUtils.remove(this.collection, actor)
@@ -1,6 +1,6 @@
1
+ import { InteractionDescriptor, InteractionProperty } from "../interaction";
1
2
  import { Asset } from "../asset"
2
- import { Descriptor, InteractionProperty, HTMLDescriptor, HTMLStyle, RoutineDescriptor, StyleDescriptor, UpdateArgs } from "../descriptor"
3
- import { RoutineInterpreter } from "../interpreter";
3
+ import { Descriptor, HTMLDescriptor, HTMLStyle, StyleDescriptor } from "../descriptor"
4
4
  import { Graphics } from "../graphics";
5
5
  import { Actor } from "./Actor"
6
6
  import { StyleActor } from "./StyleActor"
@@ -8,6 +8,9 @@ import { StyleActor } from "./StyleActor"
8
8
  abstract class HTMLActor extends Actor {
9
9
  static className = 'vyr-html-wrapper'
10
10
  static uuidKey = 'data-vyr-uuid'
11
+ static isTrigger(target: any, uuid: string): target is HTMLElement {
12
+ return target.getAttribute(HTMLActor.uuidKey) === uuid
13
+ }
11
14
  readonly uuid
12
15
  readonly unlisteners: Array<() => void> = []
13
16
  className?: string[]
@@ -29,7 +32,7 @@ abstract class HTMLActor extends Actor {
29
32
  return DOM
30
33
  }
31
34
 
32
- getWrapperStyle(descriptor: HTMLDescriptor, args: UpdateArgs) {
35
+ getWrapperStyle(descriptor: HTMLDescriptor) {
33
36
  const style: HTMLStyle = {}
34
37
  style.pointerEvents = 'auto'
35
38
  style.flexGrow = descriptor.flexGrow + ''
@@ -87,7 +90,7 @@ abstract class HTMLActor extends Actor {
87
90
  return style
88
91
  }
89
92
 
90
- getLayoutStyle(descriptor: HTMLDescriptor, args: UpdateArgs) {
93
+ getLayoutStyle(descriptor: HTMLDescriptor) {
91
94
  const style: HTMLStyle = {}
92
95
 
93
96
  style.display = descriptor.display
@@ -106,11 +109,11 @@ abstract class HTMLActor extends Actor {
106
109
  Object.assign(DOM.style, style)
107
110
  }
108
111
 
109
- getStyleClass(descriptor: HTMLDescriptor, args: UpdateArgs) {
112
+ getStyleClass(descriptor: HTMLDescriptor) {
110
113
  const styleDescriptor = Asset.get<StyleDescriptor>(descriptor.style)
111
114
  if (styleDescriptor === null) return []
112
115
  const graphics = Actor.getGraphics(this)
113
- const styleActor = graphics.getInterpreter(styleDescriptor).getActor<StyleActor>(descriptor, args)
116
+ const styleActor = graphics.getInterpreter(styleDescriptor).getActor<StyleActor>(descriptor)
114
117
  return styleActor.getClassName()
115
118
  }
116
119
 
@@ -126,16 +129,16 @@ abstract class HTMLActor extends Actor {
126
129
  delete this.className
127
130
  }
128
131
 
129
- bindInteraction(DOM: HTMLElement, interaction: InteractionProperty, trigger: Descriptor, graphics: Graphics, args: UpdateArgs) {
130
- const routine = Asset.get<RoutineDescriptor>(interaction.url)
131
- const interpreter = graphics.getInterpreter<RoutineInterpreter>(routine)
132
+ bindInteraction(DOM: HTMLElement, property: InteractionProperty, trigger: Descriptor, graphics: Graphics,) {
133
+ const interaction = Asset.get<InteractionDescriptor>(property.url)
132
134
 
133
- const eventArgs = { ...args, trigger }
134
135
  const listener = (e: Event) => {
135
- e.stopPropagation()
136
- interpreter.do(interaction.inputs, routine, eventArgs)
136
+ if (HTMLActor.isTrigger(e.target, this.uuid)) {
137
+ e.stopPropagation()
138
+ interaction.execute(property.inputs, graphics, trigger)
139
+ }
137
140
  }
138
- const eid = graphics.engine.inputSystem.listen(interaction.type as any, listener, { target: DOM })
141
+ const eid = graphics.engine.inputSystem.listen(property.type as any, listener, { target: DOM })
139
142
  this.unlisteners.push(() => graphics.engine.inputSystem.unlisten(eid))
140
143
  }
141
144
 
@@ -144,11 +147,11 @@ abstract class HTMLActor extends Actor {
144
147
  this.unlisteners.length = 0
145
148
  }
146
149
 
147
- setHTMLInteraction(DOM: HTMLElement, trigger: Descriptor, args: UpdateArgs) {
150
+ setHTMLInteraction(DOM: HTMLElement, trigger: Descriptor) {
148
151
  this.cleanInteraction()
149
152
  const graphics = HTMLActor.getGraphics(this)
150
153
  for (const interaction of trigger.interactions) {
151
- if (interaction.url) this.bindInteraction(DOM, interaction, trigger, graphics, args)
154
+ if (interaction.url) this.bindInteraction(DOM, interaction, trigger, graphics)
152
155
  }
153
156
  }
154
157
 
@@ -158,7 +161,7 @@ abstract class HTMLActor extends Actor {
158
161
 
159
162
  abstract getWrapper(): HTMLElement | null
160
163
 
161
- abstract update(descriptor: HTMLDescriptor, args: UpdateArgs): void
164
+ abstract update(descriptor: HTMLDescriptor): void
162
165
  }
163
166
 
164
167
  export {
@@ -1,4 +1,4 @@
1
- import { HTMLServiceDescriptor, StyleDescriptor, UpdateArgs } from "../descriptor"
1
+ import { HTMLServiceDescriptor, StyleDescriptor } from "../descriptor"
2
2
  import { Actor } from "./Actor"
3
3
  import { HTMLActor } from "./HTMActor"
4
4
 
@@ -45,10 +45,10 @@ class HTMLServiceActor extends HTMLActor {
45
45
  }
46
46
  }
47
47
 
48
- update(descriptor: HTMLServiceDescriptor, args: UpdateArgs) {
48
+ update(descriptor: HTMLServiceDescriptor) {
49
49
  const wrapper = this.getWrapper()
50
- this.setHTMLStyle(wrapper, this.getWrapperStyle(descriptor, args))
51
- const styleClass = [...this.getStyleClass(descriptor, args)]
50
+ this.setHTMLStyle(wrapper, this.getWrapperStyle(descriptor))
51
+ const styleClass = [...this.getStyleClass(descriptor)]
52
52
  if (descriptor.active) styleClass.push(StyleDescriptor.activeName)
53
53
  this.setStyleClass(this.DOM, styleClass)
54
54
  }
@@ -1,7 +1,7 @@
1
1
  import { Vector2 } from "../math"
2
2
  import { Asset } from "../asset"
3
- import { Descriptor, HTMLDescriptor, HTMLTransformControllerChangeArgs, HTMLTransformControllerDescriptor, UpdateArgs } from "../descriptor"
4
- import { Scriptable } from "../Scriptable"
3
+ import { Descriptor, HTMLDescriptor, HTMLTransformControllerChangeArgs, HTMLTransformControllerDescriptor } from "../descriptor"
4
+ import { Scriptable } from "../interaction/Scriptable"
5
5
  import { Actor } from "./Actor"
6
6
  import { HTMLActor } from "./HTMActor"
7
7
 
@@ -98,13 +98,13 @@ class HTMLTransformControllerActor extends Actor {
98
98
  circle.style.cursor = [1, 3].includes(index) ? 'nesw-resize' : 'nwse-resize'
99
99
  }
100
100
 
101
- listen(descriptor: HTMLTransformControllerDescriptor, args: UpdateArgs) {
101
+ listen(descriptor: HTMLTransformControllerDescriptor) {
102
102
  this.reset()
103
103
  const graphics = Actor.getGraphics(this)
104
- const target = graphics.variableProxy.get<Descriptor>(descriptor.target, graphics, args)
104
+ const target = graphics.variableProxy.get<Descriptor>(descriptor.target, graphics)
105
105
  if (target instanceof HTMLDescriptor) {
106
106
 
107
- const targetActor = graphics.getActor<HTMLActor>(target, args)
107
+ const targetActor = graphics.getActor<HTMLActor>(target)
108
108
  if (targetActor === null) return
109
109
 
110
110
  const wrapper = targetActor.getWrapper()
@@ -128,16 +128,21 @@ class HTMLTransformControllerActor extends Actor {
128
128
  this.reset()
129
129
  }
130
130
 
131
- update(descriptor: HTMLTransformControllerDescriptor, args: UpdateArgs) {
132
- this.listen(descriptor, args)
131
+ update(descriptor: HTMLTransformControllerDescriptor) {
132
+ this.listen(descriptor)
133
133
  }
134
134
 
135
135
  change = (type = 'change') => {
136
- const scriptable = Asset.get<Scriptable>(this.descriptor.event)
136
+ const scriptable = Asset.get<Scriptable<HTMLTransformControllerChangeArgs>>(this.descriptor.event)
137
137
  if (scriptable === null) return
138
138
  const graphics = Actor.getGraphics(this)
139
- const _args: HTMLTransformControllerChangeArgs = { delta: 0, event: { type, target: this.descriptor.target } }
140
- scriptable.execute(this.descriptor, graphics, _args)
139
+ const _args: HTMLTransformControllerChangeArgs = {
140
+ event: { type, target: this.descriptor.target },
141
+ trigger: this.descriptor,
142
+ input: undefined,
143
+ result: undefined,
144
+ }
145
+ scriptable.execute(graphics, _args)
141
146
  }
142
147
 
143
148
  reset() {
@@ -1,15 +1,16 @@
1
1
  import { language } from "../locale"
2
2
  import { Category } from "../Category"
3
- import { DatasetDescriptor, Descriptor } from "../descriptor"
4
3
  import { AsyncTask } from "../AsyncTask"
5
- import { Scriptable } from '../Scriptable'
4
+ import { DatasetDescriptor, Descriptor, ServiceSchedulerDescriptor } from "../descriptor"
5
+ import { InteractionDescriptor, Scriptable, ScriptableArgs } from "../interaction"
6
6
  import { AssetGraph } from "./AssetGraph"
7
+ import { Engine } from "../Engine"
7
8
 
8
9
  type JsonAsset = { [k: string]: any }
9
10
  type RawAsset = HTMLImageElement | ImageBitmap | HTMLAudioElement | AudioBuffer | HTMLVideoElement | string | JsonAsset
10
11
  type StaticFactory = (url: string, forced?: boolean) => Promise<any>
11
12
  type DescriptorProvider = () => Promise<Descriptor>
12
- type ScriptableProvider = () => Promise<{ default: typeof Scriptable }>
13
+ type ScriptableProvider = () => Promise<{ default: typeof Scriptable<ScriptableArgs<any, any, any>> }>
13
14
  type AssetProperty = { url: string }
14
15
  type AssetPropertyCollection<T extends AssetProperty = AssetProperty> = T[]
15
16
 
@@ -31,7 +32,7 @@ const privateState = {
31
32
  }
32
33
 
33
34
  class Asset {
34
- static baseUrl = ''
35
+ static baseUrl = location.origin
35
36
  static graph = new AssetGraph()
36
37
 
37
38
  static fetch(url: string, params?: any) {
@@ -139,8 +140,8 @@ class Asset {
139
140
 
140
141
  static get<U extends keyof SnowAssets>(url: U): SnowAssets[U]
141
142
  static get<T extends SnowAssets[keyof SnowAssets] = SnowAssets[keyof SnowAssets]>(url: keyof SnowAssets): T
142
- static get<T extends RawAsset | Descriptor | null = Descriptor | null>(url: string): T
143
- static get<T extends RawAsset | Descriptor | null = Descriptor | null>(url: string) {
143
+ static get<T extends RawAsset | InteractionDescriptor | Descriptor | null = Descriptor | null>(url: string): T
144
+ static get<T extends RawAsset | InteractionDescriptor | Descriptor | null = Descriptor | null>(url: string) {
144
145
  return (privateState.assetCache.get(url) ?? null) as T
145
146
  }
146
147
 
@@ -169,6 +170,20 @@ class Asset {
169
170
  privateState.activeAssets.length = 0
170
171
  }
171
172
 
173
+ static async compileDataset(url: string, engine: Engine) {
174
+ const descriptor = this.get<ServiceSchedulerDescriptor>(url)
175
+ const graphics = engine.ensureGraphicsExists(url, descriptor)
176
+ const dependencide = Asset.graph.getDependencide(url)
177
+
178
+ const queue: Promise<void>[] = []
179
+ for (const dataset of dependencide.dataset) {
180
+ const trigger = Asset.get<Descriptor>(dataset)
181
+ queue.push(graphics.invoke('compile', trigger))
182
+ }
183
+
184
+ await Promise.all(queue)
185
+ }
186
+
172
187
  static async loadAll(url: string, forced = false, record: string[] = []) {
173
188
  await Asset.load(url, forced)
174
189
  Asset.graph.create(url)
@@ -208,14 +223,15 @@ Asset.register(Category.material, factoryDescriptor)
208
223
  Asset.register(Category.geometry, factoryDescriptor)
209
224
  Asset.register(Category.texture, factoryDescriptor)
210
225
  Asset.register(Category.prefab, factoryDescriptor)
211
- Asset.register(Category.routine, factoryDescriptor)
226
+ Asset.register(Category.interaction, factoryDescriptor)
227
+ Asset.register(Category.scene, factoryDescriptor)
212
228
 
213
229
  const factoryDataset = async (url: string) => {
214
230
  const asset = await factoryDescriptor(url) as DatasetDescriptor
215
- if (asset.auto) await asset.fetch()
231
+ // if (asset.auto) await asset.fetch()
232
+
216
233
  return asset
217
234
  }
218
-
219
235
  Asset.register(Category.dataset, factoryDataset)
220
236
 
221
237
  const factoryJson = async (url: string) => {
@@ -225,24 +241,6 @@ const factoryJson = async (url: string) => {
225
241
  }
226
242
  Asset.register(Category.json, factoryJson)
227
243
 
228
- const factoryScene = (url: string, forced?: boolean) => {
229
- return new Promise<Descriptor>(async (resolve, reject) => {
230
- const sceneUrl = Asset.joinUrl(url)
231
- try {
232
- const res = await Asset.fetch(sceneUrl)
233
-
234
- const scene = await res.text()
235
-
236
- const sceneDes = Descriptor.create(Descriptor.deserialization(scene))
237
-
238
- resolve(sceneDes)
239
- } catch (error) {
240
- reject(error)
241
- }
242
- })
243
- }
244
- Asset.register(Category.scene, factoryScene)
245
-
246
244
  const factoryScript = (url: string) => {
247
245
  return new Promise(async (resolve, reject) => {
248
246
  try {
@@ -35,10 +35,10 @@ const privateState = {
35
35
  class Dependencide {
36
36
  all: string[] = []
37
37
  descriptor: string[] = []
38
+ interaction: string[] = []
38
39
  prefab: string[] = []
39
40
  dataset: string[] = []
40
41
  style: string[] = []
41
- routine: string[] = []
42
42
  texture: string[] = []
43
43
  material: string[] = []
44
44
  geometry: string[] = []
@@ -50,10 +50,10 @@ class Dependencide {
50
50
  this.descriptor.push(url)
51
51
  }
52
52
 
53
- foreach(cb: (url: string) => void) {
53
+ forEachForGraphics(cb: (url: string) => void) {
54
+ //Graphics遍历资产时,跳过 interaction prefab
54
55
  for (const url of this.dataset) cb(url)
55
56
  for (const url of this.style) cb(url)
56
- for (const url of this.routine) cb(url)
57
57
  for (const url of this.texture) cb(url)
58
58
  for (const url of this.material) cb(url)
59
59
  for (const url of this.geometry) cb(url)
@@ -65,7 +65,7 @@ class Dependencide {
65
65
  this.prefab.length = 0
66
66
  this.dataset.length = 0
67
67
  this.style.length = 0
68
- this.routine.length = 0
68
+ this.interaction.length = 0
69
69
  this.texture.length = 0
70
70
  this.material.length = 0
71
71
  this.geometry.length = 0
@@ -1,3 +1,4 @@
1
+ import { HttpType } from "../utils/constants";
1
2
  import { Asset } from "../asset";
2
3
  import { observer } from "../graphics/Observer";
3
4
  import { DeserializationObject } from "../Serialization";
@@ -7,7 +8,18 @@ interface Data {
7
8
  [k: string]: any
8
9
  }
9
10
 
10
- class DatasetDescriptor extends Descriptor {
11
+ interface DataConfig {
12
+ mode: string
13
+ key: string
14
+ }
15
+
16
+ interface HttpDataConfig extends DataConfig {
17
+ mode: 'http',
18
+ url: string
19
+ type: HttpType
20
+ }
21
+
22
+ class DatasetDescriptor<C extends DataConfig = HttpDataConfig> extends Descriptor {
11
23
  static type = 'Dataset'
12
24
 
13
25
  static getData<T = Data>(dataset: string) {
@@ -26,36 +38,18 @@ class DatasetDescriptor extends Descriptor {
26
38
  return Array.isArray(data) ? data : [data]
27
39
  }
28
40
 
29
- private _extraData: Data = {}
30
41
  private _data!: Data
42
+ private _extraData: Data = {}
43
+ readonly dataConfigs: C[]
31
44
  defaultData: Data
32
- url: string
33
- auto: boolean
34
45
 
35
46
  constructor(descriptor: Partial<DeserializationObject<DatasetDescriptor>> = {}) {
36
47
  super(descriptor)
48
+ this.dataConfigs = descriptor.dataConfigs === undefined ? [] : Descriptor.deepClone(descriptor.dataConfigs)
37
49
  this.defaultData = descriptor.defaultData === undefined ? {} : Descriptor.deepClone(descriptor.defaultData)
38
- this.url = descriptor.url ?? ''
39
- this.auto = descriptor.auto ?? true
40
50
  this._updateData(this.defaultData)
41
51
  }
42
52
 
43
- async fetch() {
44
- if (!this.url) return
45
- try {
46
- const res = await Asset.fetch(Asset.joinUrl(this.url))
47
- const data = await res.json()
48
-
49
- if (Object.hasOwn(data, 'data')) {
50
- if (data.data !== null && typeof data.data === 'object') this.setData(data.data)
51
- } else {
52
- this.setData(data)
53
- }
54
- } catch (error) {
55
- console.warn(error)
56
- }
57
- }
58
-
59
53
  private _updateData(data: Data) {
60
54
  this._data = data
61
55
  }
@@ -82,11 +76,15 @@ class DatasetDescriptor extends Descriptor {
82
76
  const { _data, _extraData, ...rest } = this;
83
77
  return rest;
84
78
  }
79
+
80
+ setNeedsUpdate() { }
85
81
  }
86
82
 
87
83
  Descriptor.register(DatasetDescriptor)
88
84
 
89
85
  export {
90
86
  Data,
87
+ DataConfig,
88
+ HttpDataConfig,
91
89
  DatasetDescriptor
92
90
  }