少年修仙传客户端代码仓库
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
/******************************************************************************
 * 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.
 *****************************************************************************/
 
// Contributed by: Mitch Thompson
 
//#define FLIPDEBUG
 
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
namespace Spine.Unity.Modules {
    [RequireComponent(typeof(SkeletonRenderer))]
    public class SkeletonRagdoll2D : MonoBehaviour {
        static Transform parentSpaceHelper;
 
        #region Inspector
        #if FLIPDEBUG
        [Header("DEBUG")]
        public bool flipXInitially;
        public bool flipYInitially;
        public bool spawnKinematic;
        public bool disableUpdateBones;
        #endif
 
        [Header("Hierarchy")]
        [SpineBone]
        public string startingBoneName = "";
        [SpineBone]
        public List<string> stopBoneNames = new List<string>();
 
        [Header("Parameters")]
        public bool applyOnStart;
        [Tooltip("Warning!  You will have to re-enable and tune mix values manually if attempting to remove the ragdoll system.")]
        public bool disableIK = true;
        public bool disableOtherConstraints = false;
        [Space]
        [Tooltip("Set RootRigidbody IsKinematic to true when Apply is called.")]
        public bool pinStartBone;
        public float gravityScale = 1;
        [Tooltip("If no BoundingBox Attachment is attached to a bone, this becomes the default Width or Radius of a Bone's ragdoll Rigidbody")]
        public float thickness = 0.125f;
        [Tooltip("Default rotational limit value.  Min is negative this value, Max is this value.")]
        public float rotationLimit = 20;
        public float rootMass = 20;
        [Tooltip("If your ragdoll seems unstable or uneffected by limits, try lowering this value.")]
        [Range(0.01f, 1f)]
        public float massFalloffFactor = 0.4f;
        [Tooltip("The layer assigned to all of the rigidbody parts.")]
        [SkeletonRagdoll.LayerField]
        public int colliderLayer = 0;
        [Range(0, 1)]
        public float mix = 1;
        #endregion
 
        ISkeletonAnimation targetSkeletonComponent;
        Skeleton skeleton;
        Dictionary<Bone, Transform> boneTable = new Dictionary<Bone, Transform>();
        Transform ragdollRoot;
        public Rigidbody2D RootRigidbody { get; private set; }
        public Bone StartingBone { get; private set; }
        Vector2 rootOffset;
        public Vector3 RootOffset { get { return this.rootOffset; } }
        bool isActive;
        public bool IsActive { get { return this.isActive; } }
 
        IEnumerator Start () {
            if (parentSpaceHelper == null) {
                parentSpaceHelper = (new GameObject("Parent Space Helper")).transform;
                #if !FLIPDEBUG
                parentSpaceHelper.hideFlags = HideFlags.HideInHierarchy;
                #endif
            }
 
            targetSkeletonComponent = GetComponent<SkeletonRenderer>() as ISkeletonAnimation;
            if (targetSkeletonComponent == null) Debug.LogError("Attached Spine component does not implement ISkeletonAnimation. This script is not compatible.");
            skeleton = targetSkeletonComponent.Skeleton;
 
            #if FLIPDEBUG
            skeleton.flipX = flipXInitially;
            skeleton.flipY = flipYInitially;
            #endif
 
            if (applyOnStart) {
                yield return null;
                Apply();
            }
        }
 
        #region API
        public Rigidbody2D[] RigidbodyArray {
            get {
                if (!isActive)
                    return new Rigidbody2D[0];
 
                var rigidBodies = new Rigidbody2D[boneTable.Count];
                int i = 0;
                foreach (Transform t in boneTable.Values) {
                    rigidBodies[i] = t.GetComponent<Rigidbody2D>();
                    i++;
                }
 
                return rigidBodies;
            }
        }
 
        public Vector3 EstimatedSkeletonPosition {
            get { return this.RootRigidbody.position - rootOffset; }
        }
 
        /// <summary>Instantiates the ragdoll simulation and applies its transforms to the skeleton.</summary>
        public void Apply () {
            isActive = true;
            mix = 1;
 
            Bone startingBone = this.StartingBone = skeleton.FindBone(startingBoneName);
            RecursivelyCreateBoneProxies(startingBone);
 
            RootRigidbody = boneTable[startingBone].GetComponent<Rigidbody2D>();
            #if FLIPDEBUG
            if (!RootRigidbody.isKinematic)
            #endif
            RootRigidbody.isKinematic = pinStartBone;
            RootRigidbody.mass = rootMass;
            var boneColliders = new List<Collider2D>();
            foreach (var pair in boneTable) {
                var b = pair.Key;
                var t = pair.Value;
                Transform parentTransform;
                boneColliders.Add(t.GetComponent<Collider2D>());
                if (b == startingBone) {
                    ragdollRoot = new GameObject("RagdollRoot").transform;
                    ragdollRoot.SetParent(transform, false);
                    if (b == skeleton.RootBone) { // RagdollRoot is skeleton root.
                        ragdollRoot.localPosition = new Vector3(b.WorldX, b.WorldY, 0);
                        ragdollRoot.localRotation = Quaternion.Euler(0, 0, GetPropagatedRotation(b));
                    } else {
                        ragdollRoot.localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
                        ragdollRoot.localRotation = Quaternion.Euler(0, 0, GetPropagatedRotation(b.Parent));
                    }
                    parentTransform = ragdollRoot;
                    rootOffset = t.position - transform.position;
                } else {
                    parentTransform = boneTable[b.Parent];
                }
 
                // Add joint and attach to parent.
                var rbParent = parentTransform.GetComponent<Rigidbody2D>();
                if (rbParent != null) {
                    var joint = t.gameObject.AddComponent<HingeJoint2D>();
                    joint.connectedBody = rbParent;
                    Vector3 localPos = parentTransform.InverseTransformPoint(t.position);
                    joint.connectedAnchor = localPos;
 
                    joint.GetComponent<Rigidbody2D>().mass = joint.connectedBody.mass * massFalloffFactor;
                    joint.limits = new JointAngleLimits2D {
                        min = -rotationLimit,
                        max = rotationLimit
                    };
                    joint.useLimits = true;
                }
            }
 
            // Ignore collisions among bones.
            for (int x = 0; x < boneColliders.Count; x++) {
                for (int y = 0; y < boneColliders.Count; y++) {
                    if (x == y) continue;
                    Physics2D.IgnoreCollision(boneColliders[x], boneColliders[y]);
                }
            }
 
            // Destroy existing override-mode SkeletonUtility bones.
            var utilityBones = GetComponentsInChildren<SkeletonUtilityBone>();
            if (utilityBones.Length > 0) {
                var destroyedUtilityBoneNames = new List<string>();
                foreach (var ub in utilityBones) {
                    if (ub.mode == SkeletonUtilityBone.Mode.Override) {
                        destroyedUtilityBoneNames.Add(ub.gameObject.name);
                        Destroy(ub.gameObject);
                    }
                }
                if (destroyedUtilityBoneNames.Count > 0) {
                    string msg = "Destroyed Utility Bones: ";
                    for (int i = 0; i < destroyedUtilityBoneNames.Count; i++) {
                        msg += destroyedUtilityBoneNames[i];
                        if (i != destroyedUtilityBoneNames.Count - 1) {
                            msg += ",";
                        }
                    }
                    Debug.LogWarning(msg);
                }
            }
 
            // Disable skeleton constraints.
            if (disableIK) {
                var ikConstraints = skeleton.IkConstraints;
                for (int i = 0, n = ikConstraints.Count; i < n; i++)
                    ikConstraints.Items[i].mix = 0;
            }
 
            if (disableOtherConstraints) {
                var transformConstraints = skeleton.transformConstraints;
                for (int i = 0, n = transformConstraints.Count; i < n; i++) {
                    transformConstraints.Items[i].rotateMix = 0;
                    transformConstraints.Items[i].scaleMix = 0;
                    transformConstraints.Items[i].shearMix = 0;
                    transformConstraints.Items[i].translateMix = 0;
                }
 
                var pathConstraints = skeleton.pathConstraints;
                for (int i = 0, n = pathConstraints.Count; i < n; i++) {
                    pathConstraints.Items[i].rotateMix = 0;
                    pathConstraints.Items[i].translateMix = 0;
                }
            }
 
            targetSkeletonComponent.UpdateWorld += UpdateSpineSkeleton;
        }
 
        /// <summary>Transitions the mix value from the current value to a target value.</summary>
        public Coroutine SmoothMix (float target, float duration) {
            return StartCoroutine(SmoothMixCoroutine(target, duration));
        }
 
        IEnumerator SmoothMixCoroutine (float target, float duration) {
            float startTime = Time.time;
            float startMix = mix;
            while (mix > 0) {
                skeleton.SetBonesToSetupPose();
                mix = Mathf.SmoothStep(startMix, target, (Time.time - startTime) / duration);
                yield return null;
            }
        }
 
        /// <summary>Set the transform world position while preserving the ragdoll parts world position.</summary>
        public void SetSkeletonPosition (Vector3 worldPosition) {
            if (!isActive) {
                Debug.LogWarning("Can't call SetSkeletonPosition while Ragdoll is not active!");
                return;
            }
 
            Vector3 offset = worldPosition - transform.position;
            transform.position = worldPosition;
            foreach (Transform t in boneTable.Values)
                t.position -= offset;
 
            UpdateSpineSkeleton(null);
            skeleton.UpdateWorldTransform();
        }
 
        /// <summary>Removes the ragdoll instance and effect from the animated skeleton.</summary>
        public void Remove () {
            isActive = false;
            foreach (var t in boneTable.Values)
                Destroy(t.gameObject);
 
            Destroy(ragdollRoot.gameObject);
            boneTable.Clear();
            targetSkeletonComponent.UpdateWorld -= UpdateSpineSkeleton;
        }
 
        public Rigidbody2D GetRigidbody (string boneName) {
            var bone = skeleton.FindBone(boneName);
            return (bone != null && boneTable.ContainsKey(bone)) ? boneTable[bone].GetComponent<Rigidbody2D>() : null;
        }
        #endregion
 
        /// <summary>Generates the ragdoll simulation's Transform and joint setup.</summary>
        void RecursivelyCreateBoneProxies (Bone b) {
            string boneName = b.data.name;
            if (stopBoneNames.Contains(boneName))
                return;
 
            var boneGameObject = new GameObject(boneName);
            boneGameObject.layer = this.colliderLayer;
            Transform t = boneGameObject.transform;
            boneTable.Add(b, t);
 
            t.parent = transform;
            t.localPosition = new Vector3(b.WorldX, b.WorldY, 0);
            t.localRotation = Quaternion.Euler(0, 0, b.WorldRotationX - b.shearX);
            t.localScale = new Vector3(b.WorldScaleX, b.WorldScaleY, 0);
 
            // MITCH: You left "todo: proper ragdoll branching"
            var colliders = AttachBoundingBoxRagdollColliders(b, boneGameObject, skeleton);
            if (colliders.Count == 0) {
                float length = b.data.length;
                if (length == 0) {
                    var circle = boneGameObject.AddComponent<CircleCollider2D>();
                    circle.radius = thickness * 0.5f;
                } else {                
                    var box = boneGameObject.AddComponent<BoxCollider2D>();
                    box.size = new Vector2(length, thickness);
                    box.offset = new Vector2(length * 0.5f, 0); // box.center in UNITY_4
                }
            }
            var rb = boneGameObject.AddComponent<Rigidbody2D>();
            rb.gravityScale = this.gravityScale;
 
            #if FLIPDEBUG
            rb.isKinematic = spawnKinematic;
            #endif
 
            foreach (Bone child in b.Children)
                RecursivelyCreateBoneProxies(child);
        }
 
        /// <summary>Performed every skeleton animation update to translate Unity Transforms positions into Spine bone transforms.</summary>
        void UpdateSpineSkeleton (ISkeletonAnimation animatedSkeleton) {
            #if FLIPDEBUG
            if (disableUpdateBones) return;
            #endif
 
            bool flipX = skeleton.flipX;
            bool flipY = skeleton.flipY;
            bool flipXOR = flipX ^ flipY;
            bool flipOR = flipX || flipY;
            var startingBone = this.StartingBone;
 
            foreach (var pair in boneTable) {
                var b = pair.Key;
                var t = pair.Value;
                bool isStartingBone = (b == startingBone);
                Transform parentTransform = isStartingBone ? ragdollRoot : boneTable[b.Parent];
                Vector3 parentTransformWorldPosition = parentTransform.position;
                Quaternion parentTransformWorldRotation = parentTransform.rotation;
 
                parentSpaceHelper.position = parentTransformWorldPosition;
                parentSpaceHelper.rotation = parentTransformWorldRotation;
                parentSpaceHelper.localScale = parentTransform.localScale;
 
                Vector3 boneWorldPosition = t.position;
                Vector3 right = parentSpaceHelper.InverseTransformDirection(t.right);
 
                Vector3 boneLocalPosition = parentSpaceHelper.InverseTransformPoint(boneWorldPosition);
                float boneLocalRotation = Mathf.Atan2(right.y, right.x) * Mathf.Rad2Deg;
                if (flipOR) {
                    if (isStartingBone) {
                        if (flipX) boneLocalPosition.x *= -1f;
                        if (flipY) boneLocalPosition.y *= -1f;
 
                        boneLocalRotation = boneLocalRotation * (flipXOR ? -1f : 1f);
                        if (flipX) boneLocalRotation += 180;
                    } else {
                        if (flipXOR) {
                            boneLocalRotation *= -1f;
                            boneLocalPosition.y *= -1f; // wtf??
                        }
                    }
                }
 
                b.x = Mathf.Lerp(b.x, boneLocalPosition.x, mix);
                b.y = Mathf.Lerp(b.y, boneLocalPosition.y, mix);
                b.rotation = Mathf.Lerp(b.rotation, boneLocalRotation, mix);
                //b.AppliedRotation = Mathf.Lerp(b.AppliedRotation, boneLocalRotation, mix);
            }
        }
 
        static List<Collider2D> AttachBoundingBoxRagdollColliders (Bone b, GameObject go, Skeleton skeleton) {
            const string AttachmentNameMarker = "ragdoll";
            var colliders = new List<Collider2D>();
            var skin = skeleton.Skin ?? skeleton.Data.DefaultSkin;
 
            var attachments = new List<Attachment>();
            foreach (Slot s in skeleton.Slots) {
                if (s.bone == b) {
                    skin.FindAttachmentsForSlot(skeleton.Slots.IndexOf(s), attachments);
                    foreach (var a in attachments) {
                        var bbAttachment = a as BoundingBoxAttachment;
                        if (bbAttachment != null) {
                            if (!a.Name.ToLower().Contains(AttachmentNameMarker))
                                continue;
 
                            var bbCollider = SkeletonUtility.AddBoundingBoxAsComponent(bbAttachment, s, go, false);
                            colliders.Add(bbCollider);
                        }
                    }
                }
            }
 
            return colliders;
        }
            
        static float GetPropagatedRotation (Bone b) {
            Bone parent = b.Parent;
            float a = b.AppliedRotation;
            while (parent != null) {
                a += parent.AppliedRotation;
                parent = parent.parent;
            }
            return a;
        }
 
        static Vector3 FlipScale (bool flipX, bool flipY) {
            return new Vector3(flipX ? -1f : 1f, flipY ? -1f : 1f, 1f);
        }
 
        #if UNITY_EDITOR
        void OnDrawGizmosSelected () {
            if (isActive) {
                Gizmos.DrawWireSphere(transform.position, thickness * 1.2f);
                Vector3 newTransformPos = RootRigidbody.position - rootOffset;
                Gizmos.DrawLine(transform.position, newTransformPos);
                Gizmos.DrawWireSphere(newTransformPos, thickness * 1.2f);
            }
        }
        #endif
    }
 
}