示例#1
0
 public void TakeOver()
 {
     currentProp = null;
     lastProp    = null;
     activeProp  = null;
     puppetMaster.DisconnectMuscleRecursive(muscle.index, MuscleDisconnectMode.Sever, true);
 }
示例#2
0
        protected void DrawScene(PuppetMasterProp script)
        {
            if (script == null)
            {
                return;
            }
            if (Application.isPlaying)
            {
                return;
            }

            GUIStyle sceneLabelStyle = new GUIStyle();

            sceneLabelStyle.wordWrap         = false;
            sceneLabelStyle.normal.textColor = sceneColor;

            Handles.color = sceneColor;
            float size = GetHandleSize(script.transform.position);

            if (script.additionalPinOffsetAdd != Vector3.zero)
            {
                SphereCapSafe(script.transform.position, Quaternion.identity, size);
                Vector3 aPos = script.transform.position + script.transform.rotation * script.additionalPinOffsetAdd;
                Handles.DrawLine(script.transform.position, aPos);
                SphereCapSafe(aPos, Quaternion.identity, size);

                if (Selection.activeGameObject == script.gameObject)
                {
                    Handles.Label(aPos + script.additionalPinOffsetAdd.normalized * size * 2f, new GUIContent("Additional Pin"), sceneLabelStyle);
                }
            }

            Handles.color = Color.white;
        }
示例#3
0
        /// <summary>
        /// Adds a PropMuscle to the puppet at runtime. If Vector3.zero passed for additionalPinOffset, additional pin will not be added.
        /// </summary>
        public bool AddPropMuscle(ConfigurableJoint addPropMuscleTo, Vector3 position, Quaternion rotation, Vector3 additionalPinOffset, Transform targetParent = null, PuppetMasterProp initiateWithProp = null)
        {
            if (!initiated)
            {
                Debug.LogError("Can not add Prop Muscle to PuppetMaster that has not been initiated! Please use Start() instead of Awake() or PuppetMaster.OnPostInitiate delegate to call AddPropMuscle.", transform);
                return(false);
            }

            if (addPropMuscleTo != null)
            {
                bool isFlat = HierarchyIsFlat();

                var addToMuscle = GetMuscle(addPropMuscleTo);
                if (addToMuscle != null)
                {
                    GameObject go = new GameObject("Prop Muscle " + addPropMuscleTo.name);
                    go.layer              = addPropMuscleTo.gameObject.layer;
                    go.transform.parent   = isFlat ? transform : addPropMuscleTo.transform;
                    go.transform.position = position;
                    go.transform.rotation = rotation;

                    go.AddComponent <Rigidbody>();

                    GameObject target = new GameObject("Prop Muscle Target " + addPropMuscleTo.name);
                    target.gameObject.layer   = addToMuscle.target.gameObject.layer;
                    target.transform.parent   = targetParent != null? targetParent: addToMuscle.target;
                    target.transform.position = go.transform.position;
                    target.transform.rotation = go.transform.rotation;

                    ConfigurableJoint joint = go.AddComponent <ConfigurableJoint>();
                    joint.xMotion        = ConfigurableJointMotion.Locked;
                    joint.yMotion        = ConfigurableJointMotion.Locked;
                    joint.zMotion        = ConfigurableJointMotion.Locked;
                    joint.angularXMotion = ConfigurableJointMotion.Locked;
                    joint.angularYMotion = ConfigurableJointMotion.Locked;
                    joint.angularZMotion = ConfigurableJointMotion.Locked;

                    Muscle.Props props = new Muscle.Props();
                    props.group = Muscle.Group.Prop;

                    AddMuscle(joint, target.transform, addPropMuscleTo.GetComponent <Rigidbody>(), targetParent != null ? targetParent : addToMuscle.target, props, false, true);

                    muscles[muscles.Length - 1].isPropMuscle = true;

                    var propMuscle = go.AddComponent <PropMuscle>();
                    propMuscle.puppetMaster        = this;
                    propMuscle.additionalPinOffset = additionalPinOffset;
                    propMuscle.currentProp         = initiateWithProp;
                    if (additionalPinOffset != Vector3.zero)
                    {
                        propMuscle.AddAdditionalPin();
                    }

                    Array.Resize(ref propMuscles, propMuscles.Length + 1);
                    propMuscles[propMuscles.Length - 1] = propMuscle;
                    propMuscle.OnInitiate();

                    return(true);
                }
                else
                {
                    Debug.LogError("Can't add Prop Muscle to a ConfigurableJoint that is not in the list of PuppetMaster.muscles.", transform);
                    return(false);
                }
            }
            else
            {
                Debug.LogError("Please assign the ConfigurableJoint of the muscle you wish to add the Prop Muscle to.", transform);
                return(false);
            }
        }
示例#4
0
        public void OnUpdate()
        {
            if (currentProp != lastProp && !puppetMaster.IsDisconnecting(muscle.index) && !puppetMaster.IsReconnecting(muscle.index))
            {
                // Drop any previously held props
                if (lastProp != null)
                {
                    lastProp.Drop(puppetMaster, muscle.index);

                    activeProp = null;

                    if (OnDropProp != null)
                    {
                        OnDropProp(lastProp);
                    }
                }

                if (currentProp != null)
                {
                    // Take over if currentProp is held by anoth prop muscle
                    foreach (PropMuscle otherPropMuscle in puppetMaster.propMuscles)
                    {
                        if (otherPropMuscle != this)
                        {
                            if (otherPropMuscle.currentProp == currentProp)
                            {
                                otherPropMuscle.TakeOver();
                            }
                        }
                    }

                    // Reconnect Prop Muscle
                    if (muscle.state.isDisconnected)
                    {
                        puppetMaster.ReconnectMuscleRecursive(muscle.index);
                    }

                    // Pick up the new prop
                    currentProp.PickUp(puppetMaster, muscle.index);
                    muscle.rigidbody.ResetInertiaTensor();
                    activeProp = currentProp;

                    if (OnPickUpProp != null)
                    {
                        OnPickUpProp(currentProp);
                    }
                }
                else
                {
                    // If no current prop, disconnect this prop muscle.
                    puppetMaster.DisconnectMuscleRecursive(muscle.index, MuscleDisconnectMode.Sever, true);
                }

                lastProp = currentProp;
            }

            if (currentProp != null)
            {
                muscle.rigidbody.mass = currentProp.mass;

                if (muscle.additionalPin != null)
                {
                    muscle.additionalPinWeight             = currentProp.additionalPinWeight;
                    muscle.additionalRigidbody.mass        = currentProp.additionalPinMass;// * Mathf.Max(muscle.additionalPinWeight, currentProp.mass * 0.1f);
                    muscle.additionalRigidbody.drag        = muscle.rigidbody.drag;
                    muscle.additionalRigidbody.angularDrag = muscle.rigidbody.angularDrag;
                    muscle.additionalRigidbody.useGravity  = muscle.rigidbody.useGravity;

                    muscle.additionalRigidbody.inertiaTensor = Vector3.one * 0.00001f;
                    //muscle.additionalRigidbody.ResetInertiaTensor();

                    Vector3 f = additionalPinOffset + currentProp.additionalPinOffsetAdd;
                    if (lastAdditionalPinOffset != f)
                    {
                        muscle.additionalPinTarget.localPosition = f;
                        muscle.additionalPin.connectedAnchor     = f;

                        lastAdditionalPinOffset = f;
                    }
                }
            }
        }