@urso/core 0.4.1 → 0.4.2

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.2",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
@@ -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