public void DeattachFromBody(bool reset, float cooldown = 0)
 {
     foreach (Joint joint in AttachJoints)
     {
         GameMain.World.Remove(joint);
     }
     AttachJoints.Clear();
     if (cooldown > 0)
     {
         attachCooldown = cooldown;
     }
     if (reset)
     {
         Reset();
     }
 }
예제 #2
0
 public void DeattachFromBody(bool reset, float cooldown = 0)
 {
     foreach (Joint joint in AttachJoints)
     {
         GameMain.World.Remove(joint);
     }
     AttachJoints.Clear();
     if (cooldown > 0)
     {
         attachCooldown = cooldown;
     }
     if (reset)
     {
         Reset();
     }
     if (targetCharacter != null)
     {
         targetCharacter.Latchers.Remove(this);
     }
 }
        private void AttachToBody(Vector2 attachPos)
        {
            if (attachLimb == null)
            {
                return;
            }
            if (targetBody == null)
            {
                return;
            }
            if (attachCooldown > 0)
            {
                return;
            }
            var collider = character.AnimController.Collider;

            //already attached to something
            if (AttachJoints.Count > 0)
            {
                //already attached to the target body, no need to do anything
                if (AttachJoints[0].BodyB == targetBody)
                {
                    return;
                }
                DeattachFromBody(reset: false);
            }

            jointDir = attachLimb.Dir;

            Vector2 transformedLocalAttachPos = localAttachPos * attachLimb.Scale * attachLimb.Params.Ragdoll.LimbScale;

            if (jointDir < 0.0f)
            {
                transformedLocalAttachPos.X = -transformedLocalAttachPos.X;
            }

            float angle = MathUtils.VectorToAngle(-attachSurfaceNormal) - MathHelper.PiOver2 + attachLimbRotation * attachLimb.Dir;

            attachLimb.body.SetTransform(attachPos + attachSurfaceNormal * transformedLocalAttachPos.Length(), angle);

            var limbJoint = new WeldJoint(attachLimb.body.FarseerBody, targetBody,
                                          transformedLocalAttachPos, targetBody.GetLocalPoint(attachPos), false)
            {
                FrequencyHz      = 10.0f,
                DampingRatio     = 0.5f,
                KinematicBodyB   = true,
                CollideConnected = false,
            };

            GameMain.World.Add(limbJoint);
            AttachJoints.Add(limbJoint);

            // Limb scale is already taken into account when creating the collider.
            Vector2 colliderFront = collider.GetLocalFront();

            if (jointDir < 0.0f)
            {
                colliderFront.X = -colliderFront.X;
            }
            collider.SetTransform(attachPos + attachSurfaceNormal * colliderFront.Length(), MathUtils.VectorToAngle(-attachSurfaceNormal) - MathHelper.PiOver2);

            var colliderJoint = new WeldJoint(collider.FarseerBody, targetBody, colliderFront, targetBody.GetLocalPoint(attachPos), false)
            {
                FrequencyHz      = 10.0f,
                DampingRatio     = 0.5f,
                KinematicBodyB   = true,
                CollideConnected = false,
                //Length = 0.1f
            };

            GameMain.World.Add(colliderJoint);
            AttachJoints.Add(colliderJoint);
        }