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.
- package/core/Collection.js +3 -2
- package/core/model/value/IntValue.js +1 -1
- package/game/GameModel.js +14 -1
- package/game/GameRenderer.js +11 -6
- package/game/assets/AssetCache.js +52 -53
- package/game/resources/ResourcesModel.js +20 -0
- package/game/resources/image/ImageModel.js +2 -7
- package/package.json +1 -1
package/core/Collection.js
CHANGED
|
@@ -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) {
|
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.
|
|
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
|
}
|
package/game/GameRenderer.js
CHANGED
|
@@ -33,9 +33,14 @@ export default class GameRenderer extends DomRenderer {
|
|
|
33
33
|
)
|
|
34
34
|
);
|
|
35
35
|
|
|
36
|
-
this.
|
|
37
|
-
|
|
38
|
-
|
|
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.
|
|
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
|
-
|
|
9
|
-
|
|
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
|
|
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
|
|
32
|
+
* @type IntValue
|
|
35
33
|
*/
|
|
36
|
-
totalLoaders
|
|
34
|
+
totalLoaders;
|
|
37
35
|
|
|
38
36
|
/**
|
|
39
|
-
* @type
|
|
37
|
+
* @type IntValue
|
|
40
38
|
*/
|
|
41
|
-
|
|
39
|
+
preloadingLoaders;
|
|
42
40
|
|
|
43
41
|
/**
|
|
44
|
-
* @type
|
|
42
|
+
* @type IntValue
|
|
45
43
|
*/
|
|
46
|
-
sessionTotalLoaders
|
|
44
|
+
sessionTotalLoaders;
|
|
47
45
|
|
|
48
46
|
/**
|
|
49
|
-
* @type
|
|
47
|
+
* @type IntValue
|
|
50
48
|
*/
|
|
51
|
-
sessionFinishedLoaders
|
|
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
|
-
|
|
64
|
-
this.
|
|
65
|
-
|
|
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
|
-
|
|
73
|
-
this.
|
|
74
|
-
|
|
75
|
-
this.
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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(
|
|
98
|
-
if (
|
|
99
|
-
this.cache.remove(
|
|
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,
|
|
111
|
+
loaderSucceeded(loader, asset, onLoaded) {
|
|
112
112
|
if (this.cache.exists(loader.uri)) {
|
|
113
|
-
this.cache.set(loader.uri,
|
|
114
|
-
console.warn(`
|
|
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,
|
|
116
|
+
this.cache.add(loader.uri, asset);
|
|
117
117
|
}
|
|
118
|
-
if (onLoaded) onLoaded(
|
|
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
|
-
|
|
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
|
|
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
|
}
|