@urso/core 0.4.2 → 0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urso/core",
3
- "version": "0.4.2",
3
+ "version": "0.4.7",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
package/src/js/app.js CHANGED
@@ -44,6 +44,7 @@ class App {
44
44
 
45
45
  //Modules
46
46
  Urso.assets = Urso.getInstance('Modules.Assets.Controller');
47
+ Urso.i18n = Urso.getInstance('Modules.I18n.Controller');
47
48
  Urso.transport = Urso.getInstance('Modules.Transport.Controller');
48
49
  Urso.logic = Urso.getInstance('Modules.Logic.Controller');
49
50
  Urso.objects = Urso.getInstance('Modules.Objects.Controller');
@@ -1,5 +1,6 @@
1
1
  Urso.Core.Modules = {};
2
2
  require('./assets/_info.js');
3
+ require('./i18n/_info.js');
3
4
  require('./instances/_info.js');
4
5
  require('./logic/_info.js');
5
6
  require('./objects/_info.js');
@@ -38,7 +38,7 @@ class ModulesAssetsService {
38
38
  Urso.scenes.loadUpdate(Math.floor(param.progress));
39
39
  }
40
40
 
41
- loadGroup(group, callback) {
41
+ loadGroup(group, callback = () => { }) {
42
42
  if (!this.assets[group])
43
43
  return Urso.logger.error('ModulesAssetsService group error, no assets:' + group + ' Check ModulesAssetsConfig please');
44
44
 
@@ -0,0 +1,4 @@
1
+ Urso.Core.Modules.I18n = {
2
+ Config: require('./config.js'),
3
+ Controller: require('./controller.js')
4
+ };
@@ -0,0 +1,17 @@
1
+ class ModulesI18nConfig {
2
+ constructor() {
3
+ this.singleton = true;
4
+
5
+ /**
6
+ * example of locales. Set please your keys and paths
7
+ */
8
+ this.locales = {
9
+ 'de': 'i18n/de.json',
10
+ 'en': 'i18n/en.json',
11
+ 'ru': 'i18n/ru.json'
12
+ }
13
+ }
14
+ };
15
+
16
+ module.exports = ModulesI18nConfig;
17
+
@@ -0,0 +1,75 @@
1
+ class ModulesI18nController {
2
+
3
+ #vocabulary = null;
4
+
5
+ /**
6
+ * get text by localeId
7
+ * @param {String} localeId
8
+ * @param {Object} [localeVariables] - variables for locale string
9
+ */
10
+ get(localeId, localeVariables = {}) {
11
+ if (this.#vocabulary && this.#vocabulary[localeId]) {
12
+ return this._interpolate(this.#vocabulary[localeId], localeVariables);
13
+ }
14
+
15
+ return localeId;
16
+ }
17
+
18
+ /**
19
+ * set alredy loaded as asset locale
20
+ * @param {String} localeKey - loaded json asset key with locale
21
+ */
22
+ setLocale(localeKey) {
23
+ const jsonResource = Urso.cache.getJson(localeKey);
24
+
25
+ if (!jsonResource)
26
+ return Urso.logger.error('ModulesI18nController setLocale error, no loaded json:' + localeKey + '. Check assets please');
27
+
28
+ this.#vocabulary = jsonResource.data;
29
+ this.emit(Urso.events.MODULES_I18N_NEW_LOCALE_WAS_SET, localeKey);
30
+ }
31
+
32
+ /**
33
+ * load json with locale, store as asset witn localeKey as key and set locale
34
+ * @param {String} localeKey
35
+ * @param {String} [pathToLocaleJson] if this param will not used - we will check localeKey in config
36
+ */
37
+ loadAndSetLocale(localeKey, pathToLocaleJson) {
38
+ //check if localeKey was alredy loaded
39
+ const jsonResource = Urso.cache.getJson(localeKey);
40
+
41
+ if (jsonResource) {
42
+ this.setLocale(localeKey);
43
+ }
44
+
45
+ //load locale by path
46
+ if (pathToLocaleJson) {
47
+ this._loadLocaleByPath(localeKey, pathToLocaleJson);
48
+ } else { //we will check localeKey in config
49
+ this._loadLocaleByConfig(localeKey);
50
+ }
51
+ }
52
+
53
+ _loadLocaleByConfig(localeKey) {
54
+ const locales = this.getInstance('Config').locales;
55
+ const pathToLocaleJson = locales[localeKey];
56
+
57
+ if (!pathToLocaleJson)
58
+ return Urso.logger.error('ModulesI18nController _loadLocaleByConfig error, no locale data in config:' + localeKey + '. Check ModulesI18nConfig please');
59
+
60
+ this._loadLocaleByPath(localeKey, pathToLocaleJson);
61
+ }
62
+
63
+ _loadLocaleByPath(localeKey, pathToLocaleJson) {
64
+ const localeAsset = { type: Urso.types.assets.JSON, key: localeKey, path: pathToLocaleJson };
65
+ Urso.assets.preload(localeAsset, () => this.setLocale(localeKey));
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
+ }
73
+ }
74
+
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
  }
@@ -10,6 +10,10 @@ class ModulesObjectsModelsBitmapText extends Urso.Core.Modules.Objects.BaseModel
10
10
  super.setupParams(params);
11
11
 
12
12
  this.text = Urso.helper.recursiveGet('text', params, false);
13
+ this.localeId = Urso.helper.recursiveGet('localeId', params, false); //you can use this instead text for localization
14
+
15
+ this.localeVariables = Urso.helper.recursiveGet('localeVariables', params, {}); //optional variables for localization by localeId
16
+
13
17
  this.fontName = Urso.helper.recursiveGet('fontName', params, false);
14
18
  this.fontSize = Urso.helper.recursiveGet('fontSize', params, false);
15
19
 
@@ -17,8 +21,20 @@ class ModulesObjectsModelsBitmapText extends Urso.Core.Modules.Objects.BaseModel
17
21
  }
18
22
 
19
23
  _addBaseObject() {
24
+ if (this.localeId)
25
+ this._originalModel.text = this.text = Urso.i18n.get(this.localeId, this.localeVariables);
26
+
20
27
  this._baseObject = new PIXI.BitmapText(this.text, { fontName: this.fontName, fontSize: this.fontSize });
21
28
  };
29
+
30
+ _newLocaleHandler() {
31
+ this.text = this._baseObject.text = Urso.i18n.get(this.localeId, this.localeVariables);
32
+ }
33
+
34
+ _subscribeOnce() {
35
+ if (this.localeId)
36
+ this.addListener(Urso.events.MODULES_I18N_NEW_LOCALE_WAS_SET, this._newLocaleHandler.bind(this));
37
+ }
22
38
  }
23
39
 
24
40
  module.exports = ModulesObjectsModelsBitmapText;
@@ -10,6 +10,9 @@ class ModulesObjectsModelsText extends Urso.Core.Modules.Objects.BaseModel {
10
10
  super.setupParams(params);
11
11
 
12
12
  this.text = Urso.helper.recursiveGet('text', params, false);
13
+ this.localeId = Urso.helper.recursiveGet('localeId', params, false); //you can use this instead text for localization
14
+
15
+ this.localeVariables = Urso.helper.recursiveGet('localeVariables', params, {}); //optional variables for localization by localeId
13
16
 
14
17
  this.lineHeight = Urso.helper.recursiveGet('lineHeight', params, 0);
15
18
  this.fontFamily = Urso.helper.recursiveGet('fontFamily', params, 'Arial');
@@ -31,8 +34,20 @@ class ModulesObjectsModelsText extends Urso.Core.Modules.Objects.BaseModel {
31
34
  }
32
35
 
33
36
  _addBaseObject() {
37
+ if (this.localeId)
38
+ this._originalModel.text = this.text = Urso.i18n.get(this.localeId, this.localeVariables);
39
+
34
40
  this._baseObject = new PIXI.Text(this.text);
35
41
  };
42
+
43
+ _newLocaleHandler() {
44
+ this.text = this._baseObject.text = Urso.i18n.get(this.localeId, this.localeVariables);
45
+ }
46
+
47
+ _subscribeOnce() {
48
+ if (this.localeId)
49
+ this.addListener(Urso.events.MODULES_I18N_NEW_LOCALE_WAS_SET, this._newLocaleHandler.bind(this));
50
+ }
36
51
  }
37
52
 
38
53
  module.exports = ModulesObjectsModelsText;
@@ -6,6 +6,7 @@ class ModulesObserverConfig {
6
6
  MODULES_ASSETS_GROUP_LOADED: 'modules.assets.group.loaded',
7
7
  MODULES_ASSETS_LOAD_PROGRESS: 'modules.assets.load.progress',
8
8
  MODULES_ASSETS_LAZYLOAD_FINISHED: 'modules.assets.lazyLoad.finished',
9
+ MODULES_I18N_NEW_LOCALE_WAS_SET: 'modules.i18n.new.locale.was.set',
9
10
  MODULES_INSTANCES_MODES_CHANGED: 'modules.instances.modes.changed',
10
11
  MODULES_OBJECTS_BUTTON_PRESS: 'modules.objects.button.press',
11
12
  MODULES_OBJECTS_HIT_AREA_PRESS: 'modules.objects.hitArea.press',
@@ -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,31 @@ class ModulesScenesService {
21
21
  this._pixiWrapper.init();
22
22
  }
23
23
 
24
+ /**
25
+ * pause scene
26
+ */
27
+ pause() {
28
+ this.getInstance('PixiWrapper').pause();
29
+ }
30
+
31
+ /**
32
+ * resume scene
33
+ */
34
+ resume() {
35
+ this.getInstance('PixiWrapper').resume();
36
+ }
37
+
38
+ /**
39
+ * global timeScale getter
40
+ */
41
+ getTimeScale() {
42
+ const loopPaused = this.getInstance('PixiWrapper').isPaused();
43
+ return loopPaused ? 0 : this.timeScale;
44
+ }
45
+
46
+ /**
47
+ * global timeScale setter
48
+ */
24
49
  setTimeScale(value) {
25
50
  this.timeScale = value;
26
51
  gsap.globalTimeline.timeScale(this.timeScale);
@@ -28,15 +53,22 @@ class ModulesScenesService {
28
53
 
29
54
  addObject(objects, parent, doNotRefreshStylesFlag) {
30
55
  const newTemplatePart = Urso.template.parse({ objects: [objects] }, true);
31
- Urso.assets.preload(newTemplatePart.assets, () => this._newTemplateAssetsLoadedHandler(newTemplatePart, parent, doNotRefreshStylesFlag));
56
+
57
+ if (newTemplatePart.assets.length) {
58
+ Urso.assets.preload(newTemplatePart.assets, () => this._newTemplateAssetsLoadedHandler(newTemplatePart, parent, doNotRefreshStylesFlag));
59
+ return null; //objects will be created soon. Maybe we can return a promice
60
+ } else
61
+ return this._newTemplateAssetsLoadedHandler(newTemplatePart, parent, doNotRefreshStylesFlag);
32
62
  }
33
63
 
34
64
  _newTemplateAssetsLoadedHandler(newTemplatePart, parent, doNotRefreshStylesFlag) {
35
- Urso.objects.create(newTemplatePart.objects, parent, doNotRefreshStylesFlag);
65
+ const objectToCreate = newTemplatePart.objects[0];
66
+ const result = Urso.objects.create(objectToCreate, parent, doNotRefreshStylesFlag);
36
67
 
37
68
  //components create
38
69
  newTemplatePart.components.forEach(component => component.create());
39
70
  this._currentSceneTemplate.components = Urso.helper.mergeArrays(this._currentSceneTemplate.components, newTemplatePart.components);
71
+ return result;
40
72
  }
41
73
 
42
74
  display(name) {