wgge 2.0.5 → 2.0.7

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.
@@ -63,8 +63,9 @@ export default class Collection extends NodeWithEvents {
63
63
  return this.count() <= 0;
64
64
  }
65
65
 
66
- count() {
67
- return this.items.length;
66
+ count(filter = null) {
67
+ if (!filter) return this.items.length;
68
+ return this.items.reduce((prev, current) => filter(current) ? prev + 1 : prev, 0);
68
69
  }
69
70
 
70
71
  contains(item) {
@@ -22,7 +22,7 @@ export default class IntValue extends DirtyValue {
22
22
  return super.isEmpty() || Number.isNaN(this.value);
23
23
  }
24
24
 
25
- increase(num) {
25
+ increase(num = 1) {
26
26
  this.set(this.get() + num);
27
27
  }
28
28
 
package/game/GameModel.js CHANGED
@@ -5,6 +5,7 @@ import BoolValue from "../core/model/value/BoolValue";
5
5
  import GlobalAudioModel from "../core/audio/global/GlobalAudioModel";
6
6
  import AssetCache from "./assets/AssetCache";
7
7
  import NullableNode from "../core/model/value/NullableNode";
8
+ import ResourcesModel from "./resources/ResourcesModel";
8
9
 
9
10
  export default class GameModel extends ObjectModel {
10
11
 
@@ -23,6 +24,16 @@ export default class GameModel extends ObjectModel {
23
24
  */
24
25
  viewBoxSize;
25
26
 
27
+ /**
28
+ * @type NullableNode<SaveGameModel>
29
+ */
30
+ saveGame;
31
+
32
+ /**
33
+ * @type ResourcesModel
34
+ */
35
+ resources;
36
+
26
37
  /**
27
38
  * @type AssetCache
28
39
  */
@@ -44,7 +55,9 @@ export default class GameModel extends ObjectModel {
44
55
  this.isInDebugMode = this.addProperty('isInDebugMode', new BoolValue(debugModeEnabled));
45
56
  this.menu = this.addProperty('menu', new NullableNode());
46
57
  this.viewBoxSize = this.addProperty('viewBoxSize', new Vector2());
47
- this.assets = new AssetCache(this.resources);
58
+ this.saveGame = this.addProperty('saveGame', new NullableNode());
59
+ this.resources = this.addProperty('resources', new ResourcesModel());
60
+ this.assets = this.addProperty('assets', new AssetCache(this.resources));
48
61
  this.audio = this.addProperty('audio', new GlobalAudioModel());
49
62
  this.controls = this.addProperty('controls', new ControlsModel());
50
63
  }
@@ -33,9 +33,14 @@ export default class GameRenderer extends DomRenderer {
33
33
  )
34
34
  );
35
35
 
36
- this.addAutoEvents(
37
- this.model.assets,
38
- ['session-finished-loaders-changed', 'session-total-loaders-changed'],
36
+ this.addAutoEventMultiple(
37
+ [
38
+ this.model.assets.totalLoaders,
39
+ this.model.assets.blockingLoaders,
40
+ this.model.assets.sessionTotalLoaders,
41
+ this.model.assets.sessionFinishedLoaders
42
+ ],
43
+ 'changed',
39
44
  () => this.updateLoading(),
40
45
  true
41
46
  );
@@ -70,7 +75,7 @@ export default class GameRenderer extends DomRenderer {
70
75
  }
71
76
 
72
77
  updateLoading() {
73
- const isLoading = this.model.assets.blockingLoaders > 0;
78
+ const isLoading = this.model.assets.totalLoaders.get() > 0;
74
79
  if (this.loading && !isLoading) {
75
80
  this.removeElement(this.loading);
76
81
  this.loading = null;
@@ -86,9 +91,9 @@ export default class GameRenderer extends DomRenderer {
86
91
  const progressWrapper = DOMHelper.createElement(content, 'div', 'progress-wrapper mt-2');
87
92
  this.loadingProgress = DOMHelper.createElement(progressWrapper, 'div', 'stretch');
88
93
  }
89
- const portion = this.game.assets.sessionFinishedLoaders / this.game.assets.sessionTotalLoaders;
94
+ const portion = this.game.assets.sessionFinishedLoaders.get() / this.game.assets.sessionTotalLoaders.get();
90
95
  this.loadingProgress.style.width = `${Math.round(portion * 100)}%`;
91
- this.label2.innerText = `${this.game.assets.sessionFinishedLoaders}/${this.game.assets.sessionTotalLoaders}`;
96
+ this.label2.innerText = `${this.game.assets.sessionFinishedLoaders.get()}/${this.game.assets.sessionTotalLoaders.get()}`;
92
97
  }
93
98
  }
94
99
 
@@ -4,16 +4,14 @@ import ImageLoader from "./loader/ImageLoader";
4
4
  import AudioLoader from "./loader/AudioLoader";
5
5
  import NodeWithEvents from "../../core/model/event/NodeWithEvents";
6
6
  import StringHelper from "../../core/helper/StringHelper";
7
-
8
- const ASSET_TYPE_LOADERS = {
9
- 'aud': AudioLoader,
10
- 'img': ImageLoader
11
- }
7
+ import ObjectModel from "../../core/model/ObjectModel";
8
+ import ResourcesModel from "../resources/ResourcesModel";
9
+ import IntValue from "../../core/model/value/IntValue";
12
10
 
13
11
  /**
14
12
  * Keeps cached raw resources like images, sounds and 3D models
15
13
  */
16
- export default class AssetCache extends NodeWithEvents {
14
+ export default class AssetCache extends ObjectModel {
17
15
 
18
16
  /**
19
17
  * @type ResourcesModel
@@ -31,72 +29,74 @@ export default class AssetCache extends NodeWithEvents {
31
29
  loaders;
32
30
 
33
31
  /**
34
- * @type int
32
+ * @type IntValue
35
33
  */
36
- totalLoaders = 0;
34
+ totalLoaders;
37
35
 
38
36
  /**
39
- * @type int
37
+ * @type IntValue
40
38
  */
41
- blockingLoaders = 0;
39
+ preloadingLoaders;
42
40
 
43
41
  /**
44
- * @type int
42
+ * @type IntValue
45
43
  */
46
- sessionTotalLoaders = 0;
44
+ sessionTotalLoaders;
47
45
 
48
46
  /**
49
- * @type int
47
+ * @type IntValue
50
48
  */
51
- sessionFinishedLoaders = 0;
49
+ sessionFinishedLoaders;
52
50
 
53
51
  constructor(resources) {
54
- super();
52
+ super(false);
53
+
54
+ this.loaderClasses = new Dictionary(
55
+ {
56
+ 'aud': AudioLoader,
57
+ 'img': ImageLoader
58
+ }
59
+ );
55
60
  this.resources = resources;
56
61
  this.cache = new Dictionary();
57
62
  this.loaders = new Collection();
63
+
64
+ this.totalLoaders = this.addProperty('totalLoaders', new IntValue(0));
65
+ this.preloadingLoaders = this.addProperty('preloadingLoaders', new IntValue(0));
66
+ this.sessionTotalLoaders = this.addProperty('sessionTotalLoaders', new IntValue(0));
67
+ this.sessionFinishedLoaders = this.addProperty('sessionFinishedLoaders', new IntValue(0));
68
+
58
69
  this.loaders.addOnAddListener((loader) => this.loaderAdded(loader));
59
70
  this.loaders.addOnRemoveListener((loader) => this.loaderRemoved(loader));
60
71
 
61
72
  }
62
73
 
63
- loaderAdded(loader) {
64
- this.updateLoadingState();
65
- if (!loader.isPreloading) {
66
- this.sessionTotalLoaders++;
67
- this.triggerEvent('session-total-loaders-changed', this.sessionTotalLoaders);
68
- }
74
+ registerLoaderClass(id, cls) {
75
+ this.loaderClasses.set(id, cls);
76
+ }
69
77
 
78
+ updateLoadingState() {
79
+ this.totalLoaders.set(this.loaders.count());
80
+ this.preloadingLoaders.set(this.loaders.count((l) => l.isPreloading));
70
81
  }
71
82
 
72
- loaderRemoved(loader) {
73
- this.updateLoadingState();
74
- if (!loader.isPreloading) {
75
- this.sessionFinishedLoaders++;
76
- if (this.blockingLoaders === 0) {
77
- this.sessionFinishedLoaders = 0;
78
- this.sessionTotalLoaders = 0;
79
- }
80
- this.triggerEvent('session-finished-loaders-changed', this.sessionFinishedLoaders);
83
+ loaderAdded(loader) {
84
+ if (this.totalLoaders.equalsTo(0)) {
85
+ this.sessionFinishedLoaders.set(0);
86
+ this.sessionTotalLoaders.set(0);
81
87
  }
88
+ this.updateLoadingState();
89
+ this.sessionTotalLoaders.increase();
82
90
  }
83
91
 
84
- updateLoadingState() {
85
- const total = this.loaders.count();
86
- if (this.totalLoaders !== total) {
87
- this.totalLoaders = total;
88
- this.triggerEvent('total-loaders-changed', total);
89
- }
90
- const blocking = this.loaders.filter((l) => l.isPreloading === false).length;
91
- if (this.blockingLoaders !== blocking) {
92
- this.blockingLoaders = blocking;
93
- this.triggerEvent('blocking-loaders-changed', blocking);
94
- }
92
+ loaderRemoved(loader) {
93
+ this.updateLoadingState();
94
+ this.sessionFinishedLoaders.increase();
95
95
  }
96
96
 
97
- resetCache(name = null) {
98
- if (name) {
99
- this.cache.remove(name);
97
+ resetCache(uri = null) {
98
+ if (uri) {
99
+ this.cache.remove(uri);
100
100
  } else {
101
101
  this.cache.reset();
102
102
  }
@@ -108,14 +108,14 @@ export default class AssetCache extends NodeWithEvents {
108
108
  this.loaders.remove(loader);
109
109
  }
110
110
 
111
- loaderSucceeded(loader, resource, onLoaded) {
111
+ loaderSucceeded(loader, asset, onLoaded) {
112
112
  if (this.cache.exists(loader.uri)) {
113
- this.cache.set(loader.uri, resource);
114
- console.warn(`Resource ${loader.uri} was already present then loaded and replaced.`);
113
+ this.cache.set(loader.uri, asset);
114
+ console.warn(`Asset ${loader.uri} was already present then loaded and replaced.`);
115
115
  } else {
116
- this.cache.add(loader.uri, resource);
116
+ this.cache.add(loader.uri, asset);
117
117
  }
118
- if (onLoaded) onLoaded(resource);
118
+ if (onLoaded) onLoaded(asset);
119
119
  this.loaders.remove(loader);
120
120
  }
121
121
 
@@ -139,12 +139,11 @@ export default class AssetCache extends NodeWithEvents {
139
139
  }
140
140
 
141
141
  const assetType = StringHelper.extractId(uri, 0);
142
- if (ASSET_TYPE_LOADERS[assetType] === undefined) {
142
+ const loaderClass = this.loaderClasses.get(assetType);
143
+ if (!loaderClass) {
143
144
  console.error(`Valid resource type could not be inferred from URI '${uri}'!`);
144
145
  }
145
-
146
- const loaderType = ASSET_TYPE_LOADERS[assetType];
147
- const loader = new loaderType(this, uri, onLoaded === null);
146
+ const loader = new loaderClass(this, uri, onLoaded === null);
148
147
  this.loaders.add(loader);
149
148
  loader.load(
150
149
  (resource) => this.loaderSucceeded(loader, resource, onLoaded),
@@ -0,0 +1,20 @@
1
+ import ObjectModel from "../../core/model/ObjectModel";
2
+ import ModelNodeTable from "../../core/model/collection/table/ModelNodeTable";
3
+ import ImageModel from "./image/ImageModel";
4
+
5
+ export default class ResourcesModel extends ObjectModel {
6
+
7
+ /**
8
+ * @type ModelNodeTable
9
+ */
10
+ images;
11
+
12
+ constructor() {
13
+ super();
14
+
15
+ this.images = this.addProperty('images', new ModelNodeTable((id) => new ImageModel(id)));
16
+ //this.sounds = this.addProperty('sounds', new ModelNodeTable((id) => new Model3dModel(id)));
17
+
18
+ }
19
+
20
+ }
@@ -1,24 +1,19 @@
1
1
  import ObjectModel from "../../../core/model/ObjectModel";
2
2
  import StringValue from "../../../core/model/value/StringValue";
3
3
  import Vector2 from "../../../core/model/vector/Vector2";
4
+ import IdentifiedModelNode from "../../../core/model/collection/table/IdentifiedModelNode";
4
5
 
5
- export default class ImageModel extends ObjectModel {
6
+ export default class ImageModel extends IdentifiedModelNode {
6
7
 
7
8
  /**
8
9
  * @type StringValue
9
10
  */
10
11
  uri;
11
12
 
12
- /**
13
- * @type Vector2
14
- */
15
- size;
16
-
17
13
  constructor(persistent = true) {
18
14
  super(persistent);
19
15
 
20
16
  this.uri = this.addProperty('uri', new StringValue());
21
- this.size = this.addProperty('size', new Vector2(0, 0, false));
22
17
  }
23
18
 
24
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wgge",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "description": "Web GL Game Engine",
5
5
  "main": "index.js",
6
6
  "devDependencies": {