Skip to content

Commit c8d61d9

Browse files
committed
Lots of new updates include alpha and tint support, bone angles, render overrides and more
1 parent 76eb713 commit c8d61d9

1 file changed

Lines changed: 260 additions & 8 deletions

File tree

plugins/spine/src/gameobject/SpineGameObject.js

Lines changed: 260 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66

77
var Class = require('../../../../src/utils/Class');
8-
var ComponentsAlpha = require('../../../../src/gameobjects/components/Alpha');
9-
var ComponentsBlendMode = require('../../../../src/gameobjects/components/BlendMode');
8+
var Clamp = require('../../../../src/math/Clamp');
109
var ComponentsComputedSize = require('../../../../src/gameobjects/components/ComputedSize');
1110
var ComponentsDepth = require('../../../../src/gameobjects/components/Depth');
1211
var ComponentsFlip = require('../../../../src/gameobjects/components/Flip');
@@ -15,7 +14,9 @@ var ComponentsTransform = require('../../../../src/gameobjects/components/Transf
1514
var ComponentsVisible = require('../../../../src/gameobjects/components/Visible');
1615
var GameObject = require('../../../../src/gameobjects/GameObject');
1716
var SpineGameObjectRender = require('./SpineGameObjectRender');
17+
var AngleBetween = require('../../../../src/math/angle/Between');
1818
var CounterClockwise = require('../../../../src/math/angle/CounterClockwise');
19+
var DegToRad = require('../../../../src/math/DegToRad');
1920
var RadToDeg = require('../../../../src/math/RadToDeg');
2021

2122
/**
@@ -34,8 +35,6 @@ var SpineGameObject = new Class({
3435
Extends: GameObject,
3536

3637
Mixins: [
37-
ComponentsAlpha,
38-
ComponentsBlendMode,
3938
ComponentsComputedSize,
4039
ComponentsDepth,
4140
ComponentsFlip,
@@ -71,6 +70,9 @@ var SpineGameObject = new Class({
7170

7271
this.preMultipliedAlpha = false;
7372

73+
// BlendMode Normal
74+
this.blendMode = 0;
75+
7476
this.setPosition(x, y);
7577

7678
if (key)
@@ -79,6 +81,219 @@ var SpineGameObject = new Class({
7981
}
8082
},
8183

84+
willRender: function ()
85+
{
86+
return true;
87+
},
88+
89+
/**
90+
* Set the Alpha level of this Game Object. The alpha controls the opacity of the Game Object as it renders.
91+
* Alpha values are provided as a float between 0, fully transparent, and 1, fully opaque.
92+
*
93+
* If your game is running under WebGL you can optionally specify four different alpha values, each of which
94+
* correspond to the four corners of the Game Object. Under Canvas only the `topLeft` value given is used.
95+
*
96+
* @method SpineGameObject#setAlpha
97+
* @since 3.19.0
98+
*
99+
* @param {number} [value=1] - The alpha value used for the whole Skeleton.
100+
*
101+
* @return {this} This Game Object instance.
102+
*/
103+
setAlpha: function (value, slotName)
104+
{
105+
if (value === undefined) { value = 1; }
106+
107+
if (slotName)
108+
{
109+
var slot = this.findSlot(slotName);
110+
111+
if (slot)
112+
{
113+
slot.color.a = Clamp(value, 0, 1);
114+
}
115+
}
116+
else
117+
{
118+
this.alpha = value;
119+
}
120+
121+
return this;
122+
},
123+
124+
/**
125+
* The alpha value of the Skeleton.
126+
*
127+
* A value between 0 and 1.
128+
*
129+
* This is a global value, impacting the entire Skeleton, not just a region of it.
130+
*
131+
* @name SpineGameObject#alpha
132+
* @type {number}
133+
* @since 3.0.0
134+
*/
135+
alpha: {
136+
137+
get: function ()
138+
{
139+
return this.skeleton.color.a;
140+
},
141+
142+
set: function (value)
143+
{
144+
var v = Clamp(value, 0, 1);
145+
146+
if (this.skeleton)
147+
{
148+
this.skeleton.color.a = v;
149+
}
150+
151+
if (v === 0)
152+
{
153+
this.renderFlags &= ~2;
154+
}
155+
else
156+
{
157+
this.renderFlags |= 2;
158+
}
159+
}
160+
161+
},
162+
163+
/**
164+
* The amount of red used when rendering the Skeletons.
165+
*
166+
* A value between 0 and 1.
167+
*
168+
* This is a global value, impacting the entire Skeleton, not just a region of it.
169+
*
170+
* @name SpineGameObject#red
171+
* @type {number}
172+
* @since 3.0.0
173+
*/
174+
red: {
175+
176+
get: function ()
177+
{
178+
return this.skeleton.color.r;
179+
},
180+
181+
set: function (value)
182+
{
183+
var v = Clamp(value, 0, 1);
184+
185+
if (this.skeleton)
186+
{
187+
this.skeleton.color.r = v;
188+
}
189+
}
190+
191+
},
192+
193+
/**
194+
* The amount of green used when rendering the Skeletons.
195+
*
196+
* A value between 0 and 1.
197+
*
198+
* This is a global value, impacting the entire Skeleton, not just a region of it.
199+
*
200+
* @name SpineGameObject#green
201+
* @type {number}
202+
* @since 3.0.0
203+
*/
204+
green: {
205+
206+
get: function ()
207+
{
208+
return this.skeleton.color.g;
209+
},
210+
211+
set: function (value)
212+
{
213+
var v = Clamp(value, 0, 1);
214+
215+
if (this.skeleton)
216+
{
217+
this.skeleton.color.g = v;
218+
}
219+
}
220+
221+
},
222+
223+
/**
224+
* The amount of blue used when rendering the Skeletons.
225+
*
226+
* A value between 0 and 1.
227+
*
228+
* This is a global value, impacting the entire Skeleton, not just a region of it.
229+
*
230+
* @name SpineGameObject#blue
231+
* @type {number}
232+
* @since 3.0.0
233+
*/
234+
blue: {
235+
236+
get: function ()
237+
{
238+
return this.skeleton.color.b;
239+
},
240+
241+
set: function (value)
242+
{
243+
var v = Clamp(value, 0, 1);
244+
245+
if (this.skeleton)
246+
{
247+
this.skeleton.color.b = v;
248+
}
249+
}
250+
251+
},
252+
253+
/**
254+
* Sets an additive tint on this Game Object.
255+
*
256+
* @method Phaser.GameObjects.Components.Tint#setColor
257+
* @webglOnly
258+
* @since 3.19.0
259+
*
260+
* @param {integer} [color=0xffffff] - The tint being applied to the top-left of the Game Object. If no other values are given this value is applied evenly, tinting the whole Game Object.
261+
*
262+
* @return {this} This Game Object instance.
263+
*/
264+
setColor: function (color, slotName)
265+
{
266+
if (color === undefined) { color = 0xffffff; }
267+
268+
var red = (color >> 16 & 0xFF) / 255;
269+
var green = (color >> 8 & 0xFF) / 255;
270+
var blue = (color & 0xFF) / 255;
271+
var alpha = (color > 16777215) ? (color >>> 24) / 255 : null;
272+
273+
var target = this.skeleton;
274+
275+
if (slotName)
276+
{
277+
var slot = this.findSlot(slotName);
278+
279+
if (slot)
280+
{
281+
target = slot;
282+
}
283+
}
284+
285+
target.color.r = red;
286+
target.color.g = green;
287+
target.color.b = blue;
288+
289+
if (alpha !== null)
290+
{
291+
target.color.a = alpha;
292+
}
293+
294+
return this;
295+
},
296+
82297
setSkeletonFromJSON: function (atlasDataKey, skeletonJSON, animationName, loop)
83298
{
84299
return this.setSkeleton(atlasDataKey, skeletonJSON, animationName, loop);
@@ -282,13 +497,22 @@ var SpineGameObject = new Class({
282497
return output;
283498
},
284499

285-
play: function (animationName, loop)
500+
getCurrentAnimation: function (trackIndex)
286501
{
287-
if (loop === undefined)
502+
if (trackIndex === undefined) { trackIndex = 0; }
503+
504+
var current = this.state.getCurrent(trackIndex);
505+
506+
if (current)
288507
{
289-
loop = false;
508+
return current.animation;
290509
}
510+
},
291511

512+
play: function (animationName, loop)
513+
{
514+
if (loop === undefined) { loop = false; }
515+
0
292516
return this.setAnimation(0, animationName, loop);
293517
},
294518

@@ -373,7 +597,19 @@ var SpineGameObject = new Class({
373597

374598
setAttachment: function (slotName, attachmentName)
375599
{
376-
return this.skeleton.setAttachment(slotName, attachmentName);
600+
if (Array.isArray(slotName) && Array.isArray(attachmentName) && slotName.length === attachmentName.length)
601+
{
602+
for (var i = 0; i < slotName.length; i++)
603+
{
604+
this.skeleton.setAttachment(slotName[i], attachmentName[i]);
605+
}
606+
}
607+
else
608+
{
609+
this.skeleton.setAttachment(slotName, attachmentName);
610+
}
611+
612+
return this;
377613
},
378614

379615
setToSetupPose: function ()
@@ -402,6 +638,22 @@ var SpineGameObject = new Class({
402638
return this.skeleton.getRootBone();
403639
},
404640

641+
angleBoneToXY: function (bone, worldX, worldY, offset, minAngle, maxAngle)
642+
{
643+
if (offset === undefined) { offset = 0; }
644+
if (minAngle === undefined) { minAngle = 0; }
645+
if (maxAngle === undefined) { maxAngle = 360; }
646+
647+
var renderer = this.scene.sys.renderer;
648+
var height = renderer.height;
649+
650+
var angle = CounterClockwise(AngleBetween(bone.worldX, height - bone.worldY, worldX, worldY) + DegToRad(offset));
651+
652+
bone.rotation = Clamp(RadToDeg(angle), minAngle, maxAngle);
653+
654+
return this;
655+
},
656+
405657
findBone: function (boneName)
406658
{
407659
return this.skeleton.findBone(boneName);

0 commit comments

Comments
 (0)