少年修仙传客户端代码仓库
client_Wu Xijin
2019-06-13 033958214c0b16d7e7b93cc821b018c295251867
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/******************************************************************************
 * Spine Runtimes Software License v2.5
 *
 * Copyright (c) 2013-2016, Esoteric Software
 * All rights reserved.
 *
 * You are granted a perpetual, non-exclusive, non-sublicensable, and
 * non-transferable license to use, install, execute, and perform the Spine
 * Runtimes software and derivative works solely for personal or internal
 * use. Without the written permission of Esoteric Software (see Section 2 of
 * the Spine Software License Agreement), you may not (a) modify, translate,
 * adapt, or develop new applications using the Spine Runtimes or otherwise
 * create derivative works or improvements of the Spine Runtimes or (b) remove,
 * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
 * or other intellectual property or proprietary rights notices on or in the
 * Software, including any copy thereof. Redistributions in binary or source
 * form must include this license and terms.
 *
 * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
 * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
 
using System;
using System.Collections.Generic;
 
namespace Spine {
    public class Skeleton {
        internal SkeletonData data;
        internal ExposedList<Bone> bones;
        internal ExposedList<Slot> slots;
        internal ExposedList<Slot> drawOrder;
        internal ExposedList<IkConstraint> ikConstraints;
        internal ExposedList<TransformConstraint> transformConstraints;
        internal ExposedList<PathConstraint> pathConstraints;
        internal ExposedList<IUpdatable> updateCache = new ExposedList<IUpdatable>();
        internal ExposedList<Bone> updateCacheReset = new ExposedList<Bone>();
        internal Skin skin;
        internal float r = 1, g = 1, b = 1, a = 1;
        internal float time;
        internal bool flipX, flipY;
        internal float x, y;
 
        public SkeletonData Data { get { return data; } }
        public ExposedList<Bone> Bones { get { return bones; } }
        public ExposedList<IUpdatable> UpdateCacheList { get { return updateCache; } }
        public ExposedList<Slot> Slots { get { return slots; } }
        public ExposedList<Slot> DrawOrder { get { return drawOrder; } }
        public ExposedList<IkConstraint> IkConstraints { get { return ikConstraints; } }
        public ExposedList<PathConstraint> PathConstraints { get { return pathConstraints; } }
        public ExposedList<TransformConstraint> TransformConstraints { get { return transformConstraints; } }
        public Skin Skin { get { return skin; } set { skin = value; } }
        public float R { get { return r; } set { r = value; } }
        public float G { get { return g; } set { g = value; } }
        public float B { get { return b; } set { b = value; } }
        public float A { get { return a; } set { a = value; } }
        public float Time { get { return time; } set { time = value; } }
        public float X { get { return x; } set { x = value; } }
        public float Y { get { return y; } set { y = value; } }
        public bool FlipX { get { return flipX; } set { flipX = value; } }
        public bool FlipY { get { return flipY; } set { flipY = value; } }
 
        public Bone RootBone {
            get { return bones.Count == 0 ? null : bones.Items[0]; }
        }
 
        public Skeleton (SkeletonData data) {
            if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
            this.data = data;
 
            bones = new ExposedList<Bone>(data.bones.Count);
            foreach (BoneData boneData in data.bones) {
                Bone bone;
                if (boneData.parent == null) {
                    bone = new Bone(boneData, this, null);                
                } else {
                    Bone parent = bones.Items[boneData.parent.index];
                    bone = new Bone(boneData, this, parent);
                    parent.children.Add(bone);
                }
                bones.Add(bone);
            }
 
            slots = new ExposedList<Slot>(data.slots.Count);
            drawOrder = new ExposedList<Slot>(data.slots.Count);
            foreach (SlotData slotData in data.slots) {
                Bone bone = bones.Items[slotData.boneData.index];
                Slot slot = new Slot(slotData, bone);
                slots.Add(slot);
                drawOrder.Add(slot);
            }
 
            ikConstraints = new ExposedList<IkConstraint>(data.ikConstraints.Count);
            foreach (IkConstraintData ikConstraintData in data.ikConstraints)
                ikConstraints.Add(new IkConstraint(ikConstraintData, this));
 
            transformConstraints = new ExposedList<TransformConstraint>(data.transformConstraints.Count);
            foreach (TransformConstraintData transformConstraintData in data.transformConstraints)
                transformConstraints.Add(new TransformConstraint(transformConstraintData, this));
 
            pathConstraints = new ExposedList<PathConstraint> (data.pathConstraints.Count);
            foreach (PathConstraintData pathConstraintData in data.pathConstraints)
                pathConstraints.Add(new PathConstraint(pathConstraintData, this));
 
            UpdateCache();
            UpdateWorldTransform();
        }
 
        /// <summary>Caches information about bones and constraints. Must be called if bones, constraints or weighted path attachments are added
        /// or removed.</summary>
        public void UpdateCache () {
            ExposedList<IUpdatable> updateCache = this.updateCache;
            updateCache.Clear();
            this.updateCacheReset.Clear();
 
            ExposedList<Bone> bones = this.bones;
            for (int i = 0, n = bones.Count; i < n; i++)
                bones.Items[i].sorted = false;
 
            ExposedList<IkConstraint> ikConstraints = this.ikConstraints;
            var transformConstraints = this.transformConstraints;
            var pathConstraints = this.pathConstraints;
            int ikCount = IkConstraints.Count, transformCount = transformConstraints.Count, pathCount = pathConstraints.Count;
            int constraintCount = ikCount + transformCount + pathCount;
            //outer:
            for (int i = 0; i < constraintCount; i++) {
                for (int ii = 0; ii < ikCount; ii++) {
                    IkConstraint constraint = ikConstraints.Items[ii];
                    if (constraint.data.order == i) {
                        SortIkConstraint(constraint);
                        goto outer; //continue outer;
                    }
                }
                for (int ii = 0; ii < transformCount; ii++) {
                    TransformConstraint constraint = transformConstraints.Items[ii];
                    if (constraint.data.order == i) {
                        SortTransformConstraint(constraint);
                        goto outer; //continue outer;
                    }
                }
                for (int ii = 0; ii < pathCount; ii++) {
                    PathConstraint constraint = pathConstraints.Items[ii];
                    if (constraint.data.order == i) {
                        SortPathConstraint(constraint);
                        goto outer; //continue outer;
                    }
                }
                outer: {}
            }
 
            for (int i = 0, n = bones.Count; i < n; i++)
                SortBone(bones.Items[i]);
        }
 
        private void SortIkConstraint (IkConstraint constraint) {
            Bone target = constraint.target;
            SortBone(target);
 
            var constrained = constraint.bones;
            Bone parent = constrained.Items[0];
            SortBone(parent);
 
            if (constrained.Count > 1) {
                Bone child = constrained.Items[constrained.Count - 1];
                if (!updateCache.Contains(child))
                    updateCacheReset.Add(child);
            }
 
            updateCache.Add(constraint);
 
            SortReset(parent.children);
            constrained.Items[constrained.Count - 1].sorted = true;
        }
 
        private void SortPathConstraint (PathConstraint constraint) {
            Slot slot = constraint.target;
            int slotIndex = slot.data.index;
            Bone slotBone = slot.bone;
            if (skin != null) SortPathConstraintAttachment(skin, slotIndex, slotBone);
            if (data.defaultSkin != null && data.defaultSkin != skin)
                SortPathConstraintAttachment(data.defaultSkin, slotIndex, slotBone);
            for (int ii = 0, nn = data.skins.Count; ii < nn; ii++)
                SortPathConstraintAttachment(data.skins.Items[ii], slotIndex, slotBone);
 
            Attachment attachment = slot.attachment;
            if (attachment is PathAttachment) SortPathConstraintAttachment(attachment, slotBone);
 
            var constrained = constraint.bones;
            int boneCount = constrained.Count;
            for (int ii = 0; ii < boneCount; ii++)
                SortBone(constrained.Items[ii]);
 
            updateCache.Add(constraint);
 
            for (int ii = 0; ii < boneCount; ii++)
                SortReset(constrained.Items[ii].children);
            for (int ii = 0; ii < boneCount; ii++)
                constrained.Items[ii].sorted = true;
        }
 
        private void SortTransformConstraint (TransformConstraint constraint) {
            SortBone(constraint.target);
 
            var constrained = constraint.bones;
            int boneCount = constrained.Count;
            for (int ii = 0; ii < boneCount; ii++)
                SortBone(constrained.Items[ii]);
 
            updateCache.Add(constraint);
 
            for (int ii = 0; ii < boneCount; ii++)
                SortReset(constrained.Items[ii].children);
            for (int ii = 0; ii < boneCount; ii++)
                constrained.Items[ii].sorted = true;
        }
 
        private void SortPathConstraintAttachment (Skin skin, int slotIndex, Bone slotBone) {
            foreach (var entry in skin.Attachments)
                if (entry.Key.slotIndex == slotIndex) SortPathConstraintAttachment(entry.Value, slotBone);
        }
 
        private void SortPathConstraintAttachment (Attachment attachment, Bone slotBone) {
            if (!(attachment is PathAttachment)) return;
            int[] pathBones = ((PathAttachment)attachment).bones;
            if (pathBones == null)
                SortBone(slotBone);
            else {
                var bones = this.bones;
                for (int i = 0, n = pathBones.Length; i < n;) {
                    int nn = pathBones[i++];
                    nn += i;
                    while (i < nn)
                        SortBone(bones.Items[pathBones[i++]]);
                }
            }
        }
 
        private void SortBone (Bone bone) {
            if (bone.sorted) return;
            Bone parent = bone.parent;
            if (parent != null) SortBone(parent);
            bone.sorted = true;
            updateCache.Add(bone);
        }
 
        private void SortReset (ExposedList<Bone> bones) {
            var bonesItems = bones.Items;
            for (int i = 0, n = bones.Count; i < n; i++) {
                Bone bone = bonesItems[i];
                if (bone.sorted) SortReset(bone.children);
                bone.sorted = false;
            }
        }
 
        /// <summary>Updates the world transform for each bone and applies constraints.</summary>
        public void UpdateWorldTransform () {
            var updateCacheReset = this.updateCacheReset;
            var updateCacheResetItems = updateCacheReset.Items;
            for (int i = 0, n = updateCacheReset.Count; i < n; i++) {
                Bone bone = updateCacheResetItems[i];
                bone.ax = bone.x;
                bone.ay = bone.y;
                bone.arotation = bone.rotation;
                bone.ascaleX = bone.scaleX;
                bone.ascaleY = bone.scaleY;
                bone.ashearX = bone.shearX;
                bone.ashearY = bone.shearY;
                bone.appliedValid = true;
            }
            var updateItems = this.updateCache.Items;
            for (int i = 0, n = updateCache.Count; i < n; i++)
                updateItems[i].Update();
        }
 
        /// <summary>Sets the bones, constraints, and slots to their setup pose values.</summary>
        public void SetToSetupPose () {
            SetBonesToSetupPose();
            SetSlotsToSetupPose();
        }
 
        /// <summary>Sets the bones and constraints to their setup pose values.</summary>
        public void SetBonesToSetupPose () {
            var bonesItems = this.bones.Items;
            for (int i = 0, n = bones.Count; i < n; i++)
                bonesItems[i].SetToSetupPose();
 
            var ikConstraintsItems = this.ikConstraints.Items;
            for (int i = 0, n = ikConstraints.Count; i < n; i++) {
                IkConstraint constraint = ikConstraintsItems[i];
                constraint.bendDirection = constraint.data.bendDirection;
                constraint.mix = constraint.data.mix;
            }
 
            var transformConstraintsItems = this.transformConstraints.Items;
            for (int i = 0, n = transformConstraints.Count; i < n; i++) {
                TransformConstraint constraint = transformConstraintsItems[i];
                TransformConstraintData data = constraint.data;
                constraint.rotateMix = data.rotateMix;
                constraint.translateMix = data.translateMix;
                constraint.scaleMix = data.scaleMix;
                constraint.shearMix = data.shearMix;
            }
 
            var pathConstraintItems = this.pathConstraints.Items;
            for (int i = 0, n = pathConstraints.Count; i < n; i++) {
                PathConstraint constraint = pathConstraintItems[i];
                PathConstraintData data = constraint.data;
                constraint.position = data.position;
                constraint.spacing = data.spacing;
                constraint.rotateMix = data.rotateMix;
                constraint.translateMix = data.translateMix;
            }
        }
 
        public void SetSlotsToSetupPose () {
            var slots = this.slots;
            var slotsItems = slots.Items;
            drawOrder.Clear();
            for (int i = 0, n = slots.Count; i < n; i++)
                drawOrder.Add(slotsItems[i]);
 
            for (int i = 0, n = slots.Count; i < n; i++)
                slotsItems[i].SetToSetupPose();
        }
 
        /// <returns>May be null.</returns>
        public Bone FindBone (String boneName) {
            if (boneName == null) throw new ArgumentNullException("boneName", "boneName cannot be null.");
            var bones = this.bones;
            var bonesItems = bones.Items;
            for (int i = 0, n = bones.Count; i < n; i++) {
                Bone bone = bonesItems[i];
                if (bone.data.name == boneName) return bone;
            }
            return null;
        }
 
        /// <returns>-1 if the bone was not found.</returns>
        public int FindBoneIndex (String boneName) {
            if (boneName == null) throw new ArgumentNullException("boneName", "boneName cannot be null.");
            var bones = this.bones;
            var bonesItems = bones.Items;
            for (int i = 0, n = bones.Count; i < n; i++)
                if (bonesItems[i].data.name == boneName) return i;
            return -1;
        }
 
        /// <returns>May be null.</returns>
        public Slot FindSlot (String slotName) {
            if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
            var slots = this.slots;
            var slotsItems = slots.Items;
            for (int i = 0, n = slots.Count; i < n; i++) {
                Slot slot = slotsItems[i];
                if (slot.data.name == slotName) return slot;
            }
            return null;
        }
 
        /// <returns>-1 if the bone was not found.</returns>
        public int FindSlotIndex (String slotName) {
            if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
            var slots = this.slots;
            var slotsItems = slots.Items;
            for (int i = 0, n = slots.Count; i < n; i++)
                if (slotsItems[i].data.name.Equals(slotName)) return i;
            return -1;
        }
 
        /// <summary>Sets a skin by name (see SetSkin).</summary>
        public void SetSkin (String skinName) {
            Skin skin = data.FindSkin(skinName);
            if (skin == null) throw new ArgumentException("Skin not found: " + skinName, "skinName");
            SetSkin(skin);
        }
 
        /// <summary>Sets the skin used to look up attachments before looking in the {@link SkeletonData#getDefaultSkin() default 
        /// skin}. Attachmentsfrom the new skin are attached if the corresponding attachment from the old skin was attached. If 
        /// there was no old skin, each slot's setup mode attachment is attached from the new skin.</summary>
        /// <param name="newSkin">May be null.</param>
        public void SetSkin (Skin newSkin) {
            if (newSkin != null) {
                if (skin != null)
                    newSkin.AttachAll(this, skin);
                else {
                    ExposedList<Slot> slots = this.slots;
                    for (int i = 0, n = slots.Count; i < n; i++) {
                        Slot slot = slots.Items[i];
                        String name = slot.data.attachmentName;
                        if (name != null) {
                            Attachment attachment = newSkin.GetAttachment(i, name);
                            if (attachment != null) slot.Attachment = attachment;
                        }
                    }
                }
            }
            skin = newSkin;
        }
 
        /// <returns>May be null.</returns>
        public Attachment GetAttachment (String slotName, String attachmentName) {
            return GetAttachment(data.FindSlotIndex(slotName), attachmentName);
        }
 
        /// <returns>May be null.</returns>
        public Attachment GetAttachment (int slotIndex, String attachmentName) {
            if (attachmentName == null) throw new ArgumentNullException("attachmentName", "attachmentName cannot be null.");
            if (skin != null) {
                Attachment attachment = skin.GetAttachment(slotIndex, attachmentName);
                if (attachment != null) return attachment;
            }
            if (data.defaultSkin != null) return data.defaultSkin.GetAttachment(slotIndex, attachmentName);
            return null;
        }
 
        /// <param name="attachmentName">May be null.</param>
        public void SetAttachment (String slotName, String attachmentName) {
            if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
            ExposedList<Slot> slots = this.slots;
            for (int i = 0, n = slots.Count; i < n; i++) {
                Slot slot = slots.Items[i];
                if (slot.data.name == slotName) {
                    Attachment attachment = null;
                    if (attachmentName != null) {
                        attachment = GetAttachment(i, attachmentName);
                        if (attachment == null) throw new Exception("Attachment not found: " + attachmentName + ", for slot: " + slotName);
                    }
                    slot.Attachment = attachment;
                    return;
                }
            }
            throw new Exception("Slot not found: " + slotName);
        }
            
        /// <returns>May be null.</returns>
        public IkConstraint FindIkConstraint (String constraintName) {
            if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            ExposedList<IkConstraint> ikConstraints = this.ikConstraints;
            for (int i = 0, n = ikConstraints.Count; i < n; i++) {
                IkConstraint ikConstraint = ikConstraints.Items[i];
                if (ikConstraint.data.name == constraintName) return ikConstraint;
            }
            return null;
        }
 
        /// <returns>May be null.</returns>
        public TransformConstraint FindTransformConstraint (String constraintName) {
            if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            ExposedList<TransformConstraint> transformConstraints = this.transformConstraints;
            for (int i = 0, n = transformConstraints.Count; i < n; i++) {
                TransformConstraint transformConstraint = transformConstraints.Items[i];
                if (transformConstraint.data.name == constraintName) return transformConstraint;
            }
            return null;
        }
 
        /// <returns>May be null.</returns>
        public PathConstraint FindPathConstraint (String constraintName) {
            if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            ExposedList<PathConstraint> pathConstraints = this.pathConstraints;
            for (int i = 0, n = pathConstraints.Count; i < n; i++) {
                PathConstraint constraint = pathConstraints.Items[i];
                if (constraint.data.name.Equals(constraintName)) return constraint;
            }
            return null;
        }
 
        public void Update (float delta) {
            time += delta;
        }
    }
}