@urso/core 0.4.0 → 0.4.4

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.0",
3
+ "version": "0.4.4",
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;
@@ -15,6 +15,10 @@ class ModulesObjectsBaseModel {
15
15
  this._parsed = false;
16
16
  }
17
17
 
18
+ /**
19
+ * setup params to object model
20
+ * @param {Object} params
21
+ */
18
22
  setupParams(params) {
19
23
  this.type = Urso.helper.recursiveGet('type', params, null);
20
24
 
@@ -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;
@@ -6,6 +6,10 @@ class ModulesObjectsModelsSpine extends Urso.Core.Modules.Objects.BaseModel {
6
6
  this._addBaseObject();
7
7
  }
8
8
 
9
+ /**
10
+ * setup params to object model
11
+ * @param {Object} params
12
+ */
9
13
  setupParams(params) {
10
14
  super.setupParams(params);
11
15
 
@@ -21,27 +25,98 @@ class ModulesObjectsModelsSpine extends Urso.Core.Modules.Objects.BaseModel {
21
25
  params.animation = this.animation; //we redefine original property here
22
26
  }
23
27
 
28
+ /**
29
+ * play spine animation
30
+ * @param {String} animationName
31
+ * @param {Boolean} [loopFlag]
32
+ * @param {Number} [track] - you can define track number for current animation
33
+ */
24
34
  play(animationName, loopFlag = false, track = 0) {
25
35
  this._baseObject.state.setAnimation(track, animationName, loopFlag);
26
36
  }
27
37
 
28
- addToSlot(object, slotName) {
29
- const spine = this._baseObject;
30
- const slotIndex = spine.spineData.slots.findIndex(({ name }) => name === slotName);
31
- const currentSlot = spine.slotContainers[slotIndex];
38
+ /**
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
42
+ */
43
+ playAndThen(animation, func) {
44
+ this.playInSequenceAndThen([animation], func);
45
+ }
32
46
 
33
- if (currentSlot) {
34
- object._baseObject.scale.y = -1;
47
+ /**
48
+ * play spine animations in sequence
49
+ * @param {String[]} animations - names of the animations to be played
50
+ */
51
+ playInSequence(animations) {
52
+ this.stop();
53
+ const defaultTrack = 0;
54
+ animations.forEach(e => this._baseObject.state.addAnimation(defaultTrack, e));
55
+ }
35
56
 
36
- Urso.objects.removeChild(object.parent, object)
37
- currentSlot.addChild(object._baseObject);
38
- } else {
39
- Urso.logger.warn('ModulesObjectsModelsSpine addToSlot error: no spine slot ' + slotName);
40
- }
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);
41
76
  }
42
77
 
43
78
  /**
44
- *
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
+ }
85
+
86
+ /**
87
+ * stop all animations
88
+ */
89
+ stop() {
90
+ this._baseObject.state.clearTracks();
91
+ }
92
+
93
+ /**
94
+ * reset all animations
95
+ */
96
+ reset() {
97
+ this._baseObject.state.setEmptyAnimations();
98
+ }
99
+
100
+ /**
101
+ * add object to spine slot
102
+ * @param {String} slotName
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
111
+ * @param {String} slotName
112
+ * @param {Object} object - created by engine object
113
+ */
114
+ replaceSlotWith(slotName, object) {
115
+ this._addToSlot(slotName, object, true);
116
+ }
117
+
118
+ /**
119
+ * set/update animation config
45
120
  * @param {*} config
46
121
  * @param {boolean} [noObjectCreate] dont use this flag out of core
47
122
  *
@@ -59,11 +134,18 @@ class ModulesObjectsModelsSpine extends Urso.Core.Modules.Objects.BaseModel {
59
134
  this._baseObject.state.timeScale = config.timeScale;*/ //deprecated - now we use getTimeScale getter
60
135
 
61
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
+ }
62
140
  this._baseObject.state.clearListeners();
63
141
  this._baseObject.state.addListener({ complete: this.animation.onComplete });
64
142
  }
65
143
  }
66
144
 
145
+ /**
146
+ * system function
147
+ * add object to pixi tree
148
+ */
67
149
  _addBaseObject() {
68
150
  let spineAsset = Urso.cache.getSpine(this.assetKey);
69
151
 
@@ -81,6 +163,30 @@ class ModulesObjectsModelsSpine extends Urso.Core.Modules.Objects.BaseModel {
81
163
  this.play(this.animation.name, this.animation.loop);
82
164
  };
83
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
+
186
+ /**
187
+ * get animation timeScale
188
+ * @returns Number
189
+ */
84
190
  getTimeScale() {
85
191
  return Urso.scenes.timeScale * this.animation.timeScale;
86
192
  }
@@ -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',