@urso/core 0.4.3 → 0.4.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urso/core",
3
- "version": "0.4.3",
3
+ "version": "0.4.8",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
@@ -5,9 +5,14 @@ class ModulesI18nController {
5
5
  /**
6
6
  * get text by localeId
7
7
  * @param {String} localeId
8
+ * @param {Object} [localeVariables] - variables for locale string
8
9
  */
9
- get(localeId) {
10
- return this.#vocabulary && this.#vocabulary[localeId] || localeId;
10
+ get(localeId, localeVariables = {}) {
11
+ if (this.#vocabulary && this.#vocabulary[localeId]) {
12
+ return this._interpolate(this.#vocabulary[localeId], localeVariables);
13
+ }
14
+
15
+ return localeId;
11
16
  }
12
17
 
13
18
  /**
@@ -59,6 +64,12 @@ class ModulesI18nController {
59
64
  const localeAsset = { type: Urso.types.assets.JSON, key: localeKey, path: pathToLocaleJson };
60
65
  Urso.assets.preload(localeAsset, () => this.setLocale(localeKey));
61
66
  }
67
+
68
+ _interpolate(string, params) {
69
+ const names = Object.keys(params);
70
+ const vals = Object.values(params);
71
+ return new Function(...names, `return \`${string}\`;`)(...vals);
72
+ }
62
73
  }
63
74
 
64
75
  module.exports = ModulesI18nController;
@@ -30,7 +30,7 @@ class ModulesObjectsController {
30
30
  }
31
31
 
32
32
  _createSingleObject(object, parent, doNotRefreshStylesFlag) {
33
- //parse template for assets and objects (groups, components)
33
+ //parse template for assets and objects (groups, components) Urso.types.objects.COMPONENT Urso.types.objects.GROUP
34
34
  if (!object._parsed) { //todo only for objects, contains components && groups in future
35
35
  return Urso.scenes.addObject(object, parent, doNotRefreshStylesFlag);
36
36
  }
@@ -12,6 +12,8 @@ class ModulesObjectsModelsBitmapText extends Urso.Core.Modules.Objects.BaseModel
12
12
  this.text = Urso.helper.recursiveGet('text', params, false);
13
13
  this.localeId = Urso.helper.recursiveGet('localeId', params, false); //you can use this instead text for localization
14
14
 
15
+ this.localeVariables = Urso.helper.recursiveGet('localeVariables', params, {}); //optional variables for localization by localeId
16
+
15
17
  this.fontName = Urso.helper.recursiveGet('fontName', params, false);
16
18
  this.fontSize = Urso.helper.recursiveGet('fontSize', params, false);
17
19
 
@@ -20,13 +22,13 @@ class ModulesObjectsModelsBitmapText extends Urso.Core.Modules.Objects.BaseModel
20
22
 
21
23
  _addBaseObject() {
22
24
  if (this.localeId)
23
- this._originalModel.text = this.text = Urso.i18n.get(this.localeId);
25
+ this._originalModel.text = this.text = Urso.i18n.get(this.localeId, this.localeVariables);
24
26
 
25
27
  this._baseObject = new PIXI.BitmapText(this.text, { fontName: this.fontName, fontSize: this.fontSize });
26
28
  };
27
29
 
28
30
  _newLocaleHandler() {
29
- this.text = this._baseObject.text = Urso.i18n.get(this.localeId);
31
+ this.text = this._baseObject.text = Urso.i18n.get(this.localeId, this.localeVariables);
30
32
  }
31
33
 
32
34
  _subscribeOnce() {
@@ -12,6 +12,8 @@ class ModulesObjectsModelsText extends Urso.Core.Modules.Objects.BaseModel {
12
12
  this.text = Urso.helper.recursiveGet('text', params, false);
13
13
  this.localeId = Urso.helper.recursiveGet('localeId', params, false); //you can use this instead text for localization
14
14
 
15
+ this.localeVariables = Urso.helper.recursiveGet('localeVariables', params, {}); //optional variables for localization by localeId
16
+
15
17
  this.lineHeight = Urso.helper.recursiveGet('lineHeight', params, 0);
16
18
  this.fontFamily = Urso.helper.recursiveGet('fontFamily', params, 'Arial');
17
19
  this.fontSize = Urso.helper.recursiveGet('fontSize', params, false);
@@ -33,13 +35,13 @@ class ModulesObjectsModelsText extends Urso.Core.Modules.Objects.BaseModel {
33
35
 
34
36
  _addBaseObject() {
35
37
  if (this.localeId)
36
- this._originalModel.text = this.text = Urso.i18n.get(this.localeId);
38
+ this._originalModel.text = this.text = Urso.i18n.get(this.localeId, this.localeVariables);
37
39
 
38
40
  this._baseObject = new PIXI.Text(this.text);
39
41
  };
40
42
 
41
43
  _newLocaleHandler() {
42
- this.text = this._baseObject.text = Urso.i18n.get(this.localeId);
44
+ this.text = this._baseObject.text = Urso.i18n.get(this.localeId, this.localeVariables);
43
45
  }
44
46
 
45
47
  _subscribeOnce() {
@@ -28,6 +28,8 @@ class ModulesObserverConfig {
28
28
  MODULES_SCENES_DISPLAY_START: 'modules.scenes.display.start',
29
29
  MODULES_SCENES_DISPLAY_FINISHED: 'modules.scenes.display.finished',
30
30
  MODULES_SCENES_MOUSE_NEW_POSITION: 'modules.scenes.mouse.newPosition',
31
+ MODULES_SCENES_PAUSE: 'modules.scenes.pause',
32
+ MODULES_SCENES_RESUME: 'modules.scenes.resume',
31
33
  MODULES_SCENES_UPDATE: 'modules.scenes.update',
32
34
  EXTRA_BROWSEREVENTS_WINDOW_PRE_RESIZE: 'extra.browserEvents.window.pre.resize',
33
35
  EXTRA_BROWSEREVENTS_WINDOW_RESIZE: 'extra.browserEvents.window.resize',
@@ -6,38 +6,86 @@ class ModulesScenesController {
6
6
  this.init();
7
7
  }
8
8
 
9
+ /**
10
+ * init scenes mahager
11
+ */
9
12
  init() {
10
13
  this._service = this.getInstance('Service');
11
14
  }
12
15
 
16
+ /**
17
+ * display scene with name
18
+ * @param {String} name
19
+ */
13
20
  display(name) {
14
21
  this._service.display(name);
15
22
  }
16
23
 
24
+ /**
25
+ * pause scene
26
+ */
27
+ pause() {
28
+ this._service.pause();
29
+ }
30
+
31
+ /**
32
+ * resume scene
33
+ */
34
+ resume() {
35
+ this._service.resume();
36
+ }
37
+
38
+ /**
39
+ * system function for transfer loadUpdate progress to components
40
+ * @param {Number} loadProgress
41
+ */
17
42
  loadUpdate(loadProgress) {
18
43
  this._service.loadUpdate(loadProgress);
19
44
  }
20
45
 
46
+ /**
47
+ * system function, returns pixi world container
48
+ */
21
49
  getPixiWorld() {
22
50
  return this.getInstance('PixiWrapper').getPixiWorld();
23
51
  }
24
52
 
53
+ /**
54
+ * get template size
55
+ * @returns {Object}
56
+ */
25
57
  getTemplateSize() {
26
58
  return this.getInstance('Resolutions').getTemplateSize();
27
59
  }
28
60
 
61
+ /**
62
+ * return mouse coords related to scene
63
+ * @returns {Object}
64
+ */
29
65
  getMouseCoords() {
30
66
  return this.getInstance('PixiWrapper').getCachedMouseCoords();
31
67
  }
32
68
 
69
+ /**
70
+ * system function to add custom objects to scene like groups or components
71
+ * @param {Object|Array} objects
72
+ * @param {Object} parent
73
+ * @param {Boolean} doNotRefreshStylesFlag
74
+ */
33
75
  addObject(objects, parent, doNotRefreshStylesFlag) {
34
- this._service.addObject(objects, parent, doNotRefreshStylesFlag);
76
+ return this._service.addObject(objects, parent, doNotRefreshStylesFlag);
35
77
  }
36
78
 
79
+ /**
80
+ * global timeScale getter
81
+ */
37
82
  get timeScale() {
38
- return this._service.timeScale;
83
+ return this._service.getTimeScale();
39
84
  }
40
85
 
86
+ /**
87
+ * global timeScale setter
88
+ */
41
89
  set timeScale(value) {
42
90
  return this._service.setTimeScale(value);
43
91
  }
@@ -1,7 +1,4 @@
1
1
  class ModulesScenesModel {
2
- constructor() {
3
- this._isPaused = false;
4
- }
5
2
 
6
3
  loadUpdate() {
7
4
  //just blank
@@ -23,10 +20,6 @@ class ModulesScenesModel {
23
20
  //just blank
24
21
  }
25
22
 
26
- isPaused() {
27
- return this._isPaused;
28
- }
29
-
30
23
  destroy() {
31
24
  //just blank
32
25
  }
@@ -40,6 +40,32 @@ class ModulesScenesPixiWrapper {
40
40
  this.getInstance('Resolutions');
41
41
  }
42
42
 
43
+ /**
44
+ * returns is scene paused
45
+ * @returns {Boolean}
46
+ */
47
+ isPaused() {
48
+ return this._loopPaused;
49
+ }
50
+
51
+ /**
52
+ * pause scene
53
+ */
54
+ pause() {
55
+ this._loopPaused = true;
56
+ PIXI.spine.settings.GLOBAL_AUTO_UPDATE = false;
57
+ }
58
+
59
+ /**
60
+ * resume scene
61
+ */
62
+ resume() {
63
+ this._loopLastCall = Date.now();
64
+ this._loopPaused = false;
65
+ this.update();
66
+ PIXI.spine.settings.GLOBAL_AUTO_UPDATE = true;
67
+ }
68
+
43
69
  _setPixiSettings() {
44
70
  PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.LINEAR;
45
71
  PIXI.settings.TEXT_RESOLUTION = 1;
@@ -94,7 +120,7 @@ class ModulesScenesPixiWrapper {
94
120
  };
95
121
 
96
122
  update() {
97
- if (!this.currentScene || this.currentScene.isPaused())
123
+ if (!this.currentScene)
98
124
  return;
99
125
 
100
126
  let deltaTime = this._getDeltaTime();
@@ -21,6 +21,33 @@ class ModulesScenesService {
21
21
  this._pixiWrapper.init();
22
22
  }
23
23
 
24
+ /**
25
+ * pause scene
26
+ */
27
+ pause() {
28
+ this.getInstance('PixiWrapper').pause();
29
+ this.emit(Urso.events.MODULES_SCENES_PAUSE);
30
+ }
31
+
32
+ /**
33
+ * resume scene
34
+ */
35
+ resume() {
36
+ this.getInstance('PixiWrapper').resume();
37
+ this.emit(Urso.events.MODULES_SCENES_RESUME);
38
+ }
39
+
40
+ /**
41
+ * global timeScale getter
42
+ */
43
+ getTimeScale() {
44
+ const loopPaused = this.getInstance('PixiWrapper').isPaused();
45
+ return loopPaused ? 0 : this.timeScale;
46
+ }
47
+
48
+ /**
49
+ * global timeScale setter
50
+ */
24
51
  setTimeScale(value) {
25
52
  this.timeScale = value;
26
53
  gsap.globalTimeline.timeScale(this.timeScale);
@@ -28,15 +55,22 @@ class ModulesScenesService {
28
55
 
29
56
  addObject(objects, parent, doNotRefreshStylesFlag) {
30
57
  const newTemplatePart = Urso.template.parse({ objects: [objects] }, true);
31
- Urso.assets.preload(newTemplatePart.assets, () => this._newTemplateAssetsLoadedHandler(newTemplatePart, parent, doNotRefreshStylesFlag));
58
+
59
+ if (newTemplatePart.assets.length) {
60
+ Urso.assets.preload(newTemplatePart.assets, () => this._newTemplateAssetsLoadedHandler(newTemplatePart, parent, doNotRefreshStylesFlag));
61
+ return null; //objects will be created soon. Maybe we can return a promice
62
+ } else
63
+ return this._newTemplateAssetsLoadedHandler(newTemplatePart, parent, doNotRefreshStylesFlag);
32
64
  }
33
65
 
34
66
  _newTemplateAssetsLoadedHandler(newTemplatePart, parent, doNotRefreshStylesFlag) {
35
- Urso.objects.create(newTemplatePart.objects, parent, doNotRefreshStylesFlag);
67
+ const objectToCreate = newTemplatePart.objects[0];
68
+ const result = Urso.objects.create(objectToCreate, parent, doNotRefreshStylesFlag);
36
69
 
37
70
  //components create
38
71
  newTemplatePart.components.forEach(component => component.create());
39
72
  this._currentSceneTemplate.components = Urso.helper.mergeArrays(this._currentSceneTemplate.components, newTemplatePart.components);
73
+ return result;
40
74
  }
41
75
 
42
76
  display(name) {