@urso/core 0.4.1 → 0.4.5

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.1",
3
+ "version": "0.4.5",
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;
@@ -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;
@@ -36,49 +36,83 @@ class ModulesObjectsModelsSpine extends Urso.Core.Modules.Objects.BaseModel {
36
36
  }
37
37
 
38
38
  /**
39
- * stop track animation or all animations
40
- * @param {Number} [track] - you can define track number to stop
39
+ * play spine animation and execute function after animation completes
40
+ * @param {String} animation - name of the animation to be played
41
+ * @param {Function} func - function to be executed
41
42
  */
42
- stop(track) {
43
- if (typeof track !== 'undefined')
44
- this._baseObject.state.clearTrack(track);
45
- else
46
- this._baseObject.state.clearTracks();
43
+ playAndThen(animation, func) {
44
+ this.playInSequenceAndThen([animation], func);
47
45
  }
48
46
 
49
47
  /**
50
- * add object to spine slot
51
- * @param {Object} object - created by engine object
52
- * @param {String} slotName
53
- * @param {Boolean} replaceSlotContent - will replace other slot content
48
+ * play spine animations in sequence
49
+ * @param {String[]} animations - names of the animations to be played
54
50
  */
55
- addToSlot(object, slotName, replaceSlotContent) {
56
- const spine = this._baseObject;
57
- const slotIndex = spine.spineData.slots.findIndex(({ name }) => name === slotName);
58
- const currentSlot = spine.slotContainers[slotIndex];
51
+ playInSequence(animations) {
52
+ this.stop();
53
+ const defaultTrack = 0;
54
+ animations.forEach(e => this._baseObject.state.addAnimation(defaultTrack, e));
55
+ }
59
56
 
60
- if (currentSlot) {
61
- object._baseObject.scale.y = -1;
57
+ /**
58
+ * play spine animations in sequence and execute function after last animation completes
59
+ * @param {String[]} animations - names of the animations to be played
60
+ * @param {Function} func - function to be executed
61
+ */
62
+ playInSequenceAndThen(animations, func) {
63
+ let animationsLeft = animations.length;
64
+ let removeSelf = () => { };
65
+ let completer = {
66
+ complete: () => {
67
+ if (--animationsLeft === 0) {
68
+ func();
69
+ removeSelf();
70
+ }
71
+ }
72
+ };
73
+ removeSelf = () => this._baseObject.state.removeListener(completer);
74
+ this._baseObject.state.addListener(completer);
75
+ this.playInSequence(animations);
76
+ }
62
77
 
63
- Urso.objects.removeChild(object.parent, object);
78
+ /**
79
+ * stop track animation
80
+ * @param {Number} [track] - you can define track number to stop
81
+ */
82
+ stopTrack(track) {
83
+ this._baseObject.state.clearTrack(track);
84
+ }
64
85
 
65
- if (replaceSlotContent)
66
- currentSlot.removeChildren(); //todo check if its proxy and reset parent
86
+ /**
87
+ * stop all animations
88
+ */
89
+ stop() {
90
+ this._baseObject.state.clearTracks();
91
+ }
67
92
 
68
- //object.parent = this; //todo && make removeChild
69
- currentSlot.addChild(object._baseObject);
70
- } else {
71
- Urso.logger.warn('ModulesObjectsModelsSpine addToSlot error: no spine slot ' + slotName);
72
- }
93
+ /**
94
+ * reset all animations
95
+ */
96
+ reset() {
97
+ this._baseObject.state.setEmptyAnimations();
73
98
  }
74
99
 
75
100
  /**
76
- * replace spine slot with new object
101
+ * add object to spine slot
102
+ * @param {String} slotName
77
103
  * @param {Object} object - created by engine object
104
+ */
105
+ addToSlot(slotName, object) {
106
+ this._addToSlot(slotName, object, false);
107
+ }
108
+
109
+ /**
110
+ * replace spine slot with new object
78
111
  * @param {String} slotName
112
+ * @param {Object} object - created by engine object
79
113
  */
80
- replaceSlotWith(object, slotName) {
81
- this.addToSlot(object, slotName, true);
114
+ replaceSlotWith(slotName, object) {
115
+ this._addToSlot(slotName, object, true);
82
116
  }
83
117
 
84
118
  /**
@@ -100,6 +134,9 @@ class ModulesObjectsModelsSpine extends Urso.Core.Modules.Objects.BaseModel {
100
134
  this._baseObject.state.timeScale = config.timeScale;*/ //deprecated - now we use getTimeScale getter
101
135
 
102
136
  if (config.onComplete) {
137
+ if (this._baseObject.state.listeners.length !== 0) {
138
+ Urso.logger.warn('ModulesObjectsModelsSpine setAnimationConfig warning: animation state listeners will be cleared');
139
+ }
103
140
  this._baseObject.state.clearListeners();
104
141
  this._baseObject.state.addListener({ complete: this.animation.onComplete });
105
142
  }
@@ -126,6 +163,26 @@ class ModulesObjectsModelsSpine extends Urso.Core.Modules.Objects.BaseModel {
126
163
  this.play(this.animation.name, this.animation.loop);
127
164
  };
128
165
 
166
+ _addToSlot(slotName, object, replaceSlotContents) {
167
+ const spine = this._baseObject;
168
+ const slotIndex = spine.spineData.slots.findIndex(({ name }) => name === slotName);
169
+ const currentSlot = spine.slotContainers[slotIndex];
170
+
171
+ if (currentSlot) {
172
+ object._baseObject.scale.y = -1;
173
+
174
+ Urso.objects.removeChild(object.parent, object);
175
+
176
+ if (replaceSlotContents)
177
+ currentSlot.removeChildren(); //todo check if its proxy and reset parent
178
+
179
+ //object.parent = this; //todo && make removeChild
180
+ currentSlot.addChild(object._baseObject);
181
+ } else {
182
+ Urso.logger.warn('ModulesObjectsModelsSpine _addToSlot error: no spine slot ' + slotName);
183
+ }
184
+ }
185
+
129
186
  /**
130
187
  * get animation timeScale
131
188
  * @returns Number
@@ -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',