@urso/core 0.4.33 → 0.4.36

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.33",
3
+ "version": "0.4.36",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
@@ -19,19 +19,26 @@ class ModulesLogicSounds {
19
19
  const cfg = this._cfg[configName];
20
20
 
21
21
  cfg.forEach(obj => {
22
- const params = {
23
- action: obj.action || 'play',
24
- relaunch: obj.relaunch || false,
25
- loop: obj.loop || false,
26
- volume: obj.volume || 1
27
- };
22
+ const {
23
+ action = 'play',
24
+ relaunch = false,
25
+ loop = false,
26
+ volume = 1,
27
+ ...otherParams
28
+ } = obj;
28
29
 
29
30
  if (!mappedCfg[obj.soundKey])
30
31
  mappedCfg[obj.soundKey] = {
31
32
  events: {}
32
33
  };
33
34
 
34
- mappedCfg[obj.soundKey].events[obj.event] = params;
35
+ mappedCfg[obj.soundKey].events[obj.event] = {
36
+ ...otherParams,
37
+ action,
38
+ relaunch,
39
+ loop,
40
+ volume,
41
+ };;
35
42
  });
36
43
  });
37
44
 
@@ -13,6 +13,7 @@ class ModulesObjectsBaseModel {
13
13
  this._uid = Urso.helper.recursiveGet('_uid', params, false); //will setup on create
14
14
  this._templatePath = false;
15
15
  this._parsed = false;
16
+ this._transitions = { tweens: {} };
16
17
  }
17
18
 
18
19
  /**
@@ -44,6 +45,11 @@ class ModulesObjectsBaseModel {
44
45
  this.visible = Urso.helper.recursiveGet('visible', params, true);
45
46
  this.alpha = Urso.helper.recursiveGet('alpha', params, 1);
46
47
  this.blendMode = Urso.helper.recursiveGet('blendMode', params, 1);
48
+
49
+ this.transitionDelay = Urso.helper.recursiveGet('transitionDelay', params, false); //time in ms
50
+ this.transitionDuration = Urso.helper.recursiveGet('transitionDuration', params, false); //time in ms
51
+ this.transitionProperty = Urso.helper.recursiveGet('transitionProperty', params, false); //props list ex: 'x', 'x y', 'x y alpha'
52
+
47
53
  this.append = Urso.helper.recursiveGet('append', params, true); //if false - object will not created //TODO
48
54
  this.custom = Urso.helper.recursiveGet('custom', params, {}); //custom params
49
55
  }
@@ -19,8 +19,9 @@ class ModulesObjectsProxy {
19
19
  },
20
20
 
21
21
  set(target, key, value, receiver) {
22
+ const oldValue = target._baseObject[key];
22
23
  const rv = Reflect.set(target, key, value, receiver);
23
- _this._customSetLogic(target, key, value, proxy);
24
+ _this._customSetLogic(target, key, value, proxy, oldValue);
24
25
  return rv;
25
26
  }
26
27
  });
@@ -32,13 +33,15 @@ class ModulesObjectsProxy {
32
33
  * propertyName - property to adapt for PIXI object
33
34
  * value - value to set
34
35
  */
35
- _setProperty(target, propertyName, value) {
36
+ _setProperty(target, propertyName, value, oldValue) {
36
37
  const isAdaptiveProperty = this._propertyAdapter.isAdaptiveProperty(propertyName);
37
38
 
38
39
  if (isAdaptiveProperty)
39
40
  this._propertyAdapter.propertyChangeHandler(target, propertyName);
40
41
  else
41
42
  Urso.helper.recursiveSet(propertyName, value, target._baseObject);
43
+
44
+ this._checkNeedTransitions(target, propertyName, value, oldValue);
42
45
  }
43
46
 
44
47
  safeSetValueToTarget(target, key, value) {
@@ -66,7 +69,7 @@ class ModulesObjectsProxy {
66
69
  return Urso.helper.recursiveGet(wrapKey, target._baseObject, reflectValue);
67
70
  }
68
71
 
69
- _customSetLogic(target, key, value, proxy) {
72
+ _customSetLogic(target, key, value, proxy, oldValue) {
70
73
  if (!this._getOriginalModelExceptions().includes(key))
71
74
  target._originalModel[key] = value;
72
75
 
@@ -83,7 +86,7 @@ class ModulesObjectsProxy {
83
86
 
84
87
  this._checkSelectorProperties(key);
85
88
 
86
- this._setProperty(target, propertyName, value);
89
+ this._setProperty(target, propertyName, value, oldValue);
87
90
 
88
91
  this._checkMaxSize(target);
89
92
 
@@ -131,6 +134,48 @@ class ModulesObjectsProxy {
131
134
  }
132
135
  }
133
136
 
137
+ /**
138
+ * transitions tweens creation
139
+ * @param {Object} target - ObjectsModel
140
+ * @param {String} propertyName
141
+ * @param {Number} value
142
+ * @param {Number} oldValue
143
+ */
144
+ _checkNeedTransitions(target, propertyName, value, oldValue) {
145
+ if (
146
+ typeof oldValue === 'undefined' ||
147
+ !target.transitionProperty ||
148
+ !target.transitionDuration ||
149
+ !target.transitionProperty.split(' ').includes(propertyName)
150
+ )
151
+ return;
152
+
153
+ const baseNewValue = target._baseObject[propertyName];
154
+
155
+ //remove old active tween
156
+ const currentTween = target._transitions.tweens[propertyName];
157
+
158
+ if (currentTween)
159
+ currentTween.kill();
160
+
161
+ //set base value
162
+ target._baseObject[propertyName] = oldValue;
163
+
164
+ //create new tween
165
+ const tweenParams = { duration: target.transitionDuration / 1000, ease: "none" };
166
+ tweenParams[propertyName] = baseNewValue;
167
+
168
+ if (target.transitionDelay)
169
+ tweenParams.delay = target.transitionDelay / 1000;
170
+
171
+ const newTween = gsap.to(target._baseObject, tweenParams);
172
+ target._transitions.tweens[propertyName] = newTween;
173
+
174
+ newTween.eventCallback("onComplete", () => {
175
+ target._transitions.tweens[propertyName] = null;
176
+ });
177
+ }
178
+
134
179
  _getSelectorProperties() {
135
180
  return [
136
181
  'id', 'name', 'class'
@@ -82,6 +82,7 @@ class SoundManagerController {
82
82
  this._systemVolume = ~~(state === 'visible');
83
83
  this._updateSoundVolume();
84
84
  }
85
+
85
86
  _updateSoundVolume() {
86
87
  const totalVolume = this._globalVolume * this._systemVolume;
87
88
  for (const key in this._sounds)
@@ -10,6 +10,7 @@ class SoundSprite {
10
10
  this._soundsState = this._initSoundsState();
11
11
 
12
12
  this._eventsCfg = {};
13
+ this._fadeTweens = {};
13
14
  this._eventsQueue = [];
14
15
  this._isAudioUnlocked = false;
15
16
 
@@ -97,10 +98,12 @@ class SoundSprite {
97
98
  this._player.loop(loop, this._soundsState[soundKey].id);
98
99
  };
99
100
 
100
- setVolume(soundKey, volume = 1) {
101
- this._soundsState[soundKey].volume = volume;
102
-
101
+ setVolume(soundKey, volume = 1, saveVolumeState = true) {
103
102
  this._player.volume(volume, this._soundsState[soundKey].id);
103
+
104
+ if(saveVolumeState) {
105
+ this._soundsState[soundKey].volume = volume;
106
+ }
104
107
  };
105
108
 
106
109
  setAllVolume(volume) {
@@ -113,9 +116,12 @@ class SoundSprite {
113
116
  }
114
117
 
115
118
  _updateVolume() {
116
- const keys = Object.keys(this._soundsState);
119
+ const soundKeys = Object.keys(this._soundsState);
117
120
  this._player._volume = this._totalVolume;
118
- keys.forEach(key => this.setVolume(key, this._totalVolume));
121
+ soundKeys.forEach(soundKey => {
122
+ const soundVolume = this._soundsState[soundKey].volume * this._totalVolume;
123
+ this.setVolume(soundKey, soundVolume, false);
124
+ });
119
125
  }
120
126
 
121
127
  setRelaunch(soundKey, needRelaunch = false) {
@@ -146,6 +152,38 @@ class SoundSprite {
146
152
  this._customSubscribe();
147
153
  };
148
154
 
155
+ _stopPrevFade(soundKey) {
156
+ if(this._fadeTweens[soundKey]) {
157
+ this._fadeTweens[soundKey].kill();
158
+ }
159
+
160
+ delete this._fadeTweens[soundKey];
161
+ }
162
+
163
+ _startFade({ fadeTo, fadeDuration, soundKey }) {
164
+ const fadeFrom = this._soundsState[soundKey].volume;
165
+ const delta = fadeTo - fadeFrom;
166
+
167
+ const onUpdate = () => {
168
+ const volume = fadeFrom + (delta * this._fadeTweens[soundKey].ratio);
169
+ this.setVolume(soundKey, volume);
170
+ };
171
+
172
+ this._fadeTweens[soundKey] = gsap.to({}, fadeDuration / 1000, { onUpdate });
173
+ }
174
+
175
+ fade({ fadeTo = 1, fadeDuration = 200, startSound = false, soundKey, ...others }) {
176
+ if(startSound) {
177
+ this.play({ ...others, soundKey });
178
+ }
179
+ if(this._soundsState[soundKey].id === null) {
180
+ return;
181
+ }
182
+
183
+ this._stopPrevFade(soundKey);
184
+ this._startFade({ fadeTo, fadeDuration, soundKey });
185
+ }
186
+
149
187
  _saveEvents(eventsCfg) {
150
188
  this._eventsCfg = eventsCfg;
151
189
  }
@@ -172,10 +210,10 @@ class SoundSprite {
172
210
  this._eventsQueue.push(data);
173
211
  };
174
212
 
175
- _reactToEvent(soundKey, { action, loop, relaunch, volume = 0 }) {
213
+ _reactToEvent(soundKey, { action, volume, ...otherParams }) {
176
214
  volume *= this._totalVolume;
177
215
  const self = this;
178
- const params = { loop, relaunch, volume, soundKey };
216
+ const params = { ...otherParams, action, soundKey, volume };
179
217
 
180
218
  if (!self[action])
181
219
  return Urso.logger.error(`SoundSprite error: Sound action '${action}' not found!`);