コード例 #1
0
        public void Collapse()
        {
            Flow    = 0;
            Flowing = false;

            collapsing = true;

            Plug plugA = A.GetComponentInActor <Plug>();
            Plug plugB = B.GetComponentInActor <Plug>();

            collapseStart = 0;
            collapseEnd   = path.Count - 1;

            if (plugA != null)
            {
                CordFollower followerA = plugA.GetComponent <CordFollower>();
                followerA.enabled = true;
                followerA.TeleportToPoint(0);
                followerA.SetTargetPoint(path.Count - 1);
                followerA.NextPointReached += OnStartPointConsumed;
                plugA.GetComponent <VRTK_InteractableObject>().isGrabbable = false;
            }

            if (plugB != null)
            {
                CordFollower followerB = plugB.GetComponent <CordFollower>();
                followerB.enabled = true;
                followerB.TeleportToPoint(path.Count - 1);
                followerB.SetTargetPoint(0);
                followerB.NextPointReached += OnEndPointConsumed;
                plugB.GetComponent <VRTK_InteractableObject>().isGrabbable = false;
            }
        }
コード例 #2
0
        /// <summary>
        /// Stop listening for button presses if the plug stops touching the handle
        /// </summary>
        /// <param name="collision"></param>
        private void OnTriggerExit(Collider other)
        {
            Plug touchingPlug = other.transform.GetComponentInActor <Plug>();

            if (touchingPlug != null)
            {
                VRTK_InteractableObject plugInteractable = touchingPlug.GetComponent <VRTK_InteractableObject>();
                ObservePlugGrabbedState(plugInteractable, false);

                if (plugInteractable.IsGrabbed())
                {
                    GameObject grabbingController = touchingPlug.GetComponent <VRTK_InteractableObject>().GetGrabbingObject();
                    grabbingController.GetComponent <VRTK_ControllerEvents>().ButtonOnePressed -= OnMergeButtonPressed;
                }
            }
        }
コード例 #3
0
        private void OnDisconnectButtonPressed(object sender, ControllerInteractionEventArgs e)
        {
            grabber.ForceRelease();
            GetComponent <VRTK_InteractableObject>().enabled = false;
            GetComponent <MeshRenderer>().enabled            = false;
            GetComponent <Collider>().enabled = false;

            DisconnectBranchFromJunction();

            sourceCord = MergeJunction();
            sourceCord.AllowBranching(false);

            Plug disconnectedPlug = CreatePlug();

            if (branchNode.Cord.StartNode.Equals(branchNode.transform))
            {
                branchNode.Cord.Connect(disconnectedPlug.CordAttachPoint, branchNode.Cord.EndNode);
            }
            else
            {
                branchNode.Cord.Connect(branchNode.Cord.StartNode, disconnectedPlug.CordAttachPoint);
            }

            grabber.ForceGrab(disconnectedPlug.GetComponent <VRTK_InteractableObject>(), () =>
            {
                disconnectedPlug.EnableSnapping();
                sourceCord.AllowBranching(true);
                Destroy(this.gameObject);
            });
        }
コード例 #4
0
ファイル: CordDispenser.cs プロジェクト: lane-s/ComposeVR
        /// <summary>
        /// Creates a new cord consisting of two connected plugs and a actual cord object. This is the cord that the jack will deploy when the user's controller comes near
        /// </summary>
        private void CreateCord()
        {
            primaryPlugAttach   = CreatePlugAttach("PrimaryPlugAttach");
            secondaryPlugAttach = CreatePlugAttach("SecondaryPlugAttach");

            primaryPlug      = Instantiate(PlugPrefab, PlugStart.position, PlugStart.rotation);
            primaryPlug.name = "Primary";
            primaryPlug.GetComponent <VRTK_TransformFollow>().gameObjectToFollow = primaryPlugAttach;

            secondaryPlug      = Instantiate(PlugPrefab, PlugStart.position, PlugStart.rotation);
            secondaryPlug.name = "Secondary";
            secondaryPlug.GetComponent <VRTK_TransformFollow>().gameObjectToFollow = secondaryPlugAttach;

            cord = Instantiate(CordPrefab).GetComponent <Cord>();
            cord.Connect(secondaryPlug.CordAttachPoint, primaryPlug.CordAttachPoint);

            cord.Flow = 1;

            if (GetComponent <PhysicalDataInput>() != null)
            {
                cord.Flow = -cord.Flow;
            }
            cord.Flowing = true;

            cord.gameObject.SetActive(false);
            primaryPlug.gameObject.SetActive(false);
            secondaryPlug.gameObject.SetActive(false);
        }
コード例 #5
0
        private void OnGrabbed(object sender, InteractableObjectEventArgs e)
        {
            grabber = e.interactingObject.GetComponent <VRTK_InteractGrab>();
            if (branchNode != null)
            {
                VRTK_ControllerEvents controllerEvents = e.interactingObject.GetComponent <VRTK_ControllerEvents>();
                controllerEvents.ButtonOnePressed += OnDisconnectButtonPressed;
            }
            else
            {
                //Force the controller to let go of the branch handle
                grabber.ForceRelease();
                GetComponent <VRTK_InteractableObject>().isGrabbable = false;

                Plug branchPlug = CreatePlug();
                Cord branchCord;

                //Create a cord between the plug and the branch handle
                branchCord       = Instantiate(CordPrefab).GetComponent <Cord>();
                branchCord.Color = sourceCord.Color;
                branchCord.Connect(transform, branchPlug.CordAttachPoint);
                branchCord.Flow = 0;
                branchNode      = new CordNode(branchCord, transform);

                cordJunction = SplitCord(sourceCord);
                StopMovementAlongCord();

                grabber.ForceGrab(branchPlug.GetComponent <VRTK_InteractableObject>(), () =>
                {
                    GetComponent <VRTK_InteractableObject>().isGrabbable = true;
                    branchPlug.EnableSnapping();
                });
            }
        }
コード例 #6
0
        private Plug CreatePlug()
        {
            Plug p = Instantiate(PlugPrefab).GetComponent <Plug>();

            p.transform.position = transform.position;
            p.transform.rotation = transform.rotation;
            p.DisableSnapping();
            p.GetComponent <VRTK_InteractableObject>().ForceStopInteracting();
            return(p);
        }
コード例 #7
0
        /// <summary>
        /// Disregard whether the plug is grabbed or not- lock it and connect it to the socket
        /// </summary
        /// <param name="p"></param>
        public void ForcePlugLockAndConnect(Plug p)
        {
            if (p.AttachLock(this))
            {
                if (p.GetComponent <VRTK_InteractableObject>().IsGrabbed())
                {
                    lockedPlugGrabber = p.GetComponent <VRTK_InteractableObject>().GetGrabbingObject().GetComponentInActor <VRTK_InteractGrab>();
                }
                else
                {
                    lockedPlugGrabber = null;
                }

                LockedPlug = p;
                LockedPlug.GetComponent <VRTK_InteractableObject>().InteractableObjectUngrabbed += OnLockedPlugUngrabbed;
                LockedPlug.GetComponent <VRTK_InteractableObject>().InteractableObjectGrabbed   += OnLockedPlugGrabbed;

                LockedPlug.ConnectToDataEndpoint(plugReceptacle);
            }
        }
コード例 #8
0
        protected virtual void TryLockPlug(Plug p)
        {
            if (p.GetComponent <VRTK_InteractableObject>().IsGrabbed())
            {
                float flow = p.ConnectedCord.Flow;
                if (p.CordAttachPoint.Equals(p.ConnectedCord.StartNode))
                {
                    flow = -flow;
                }

                bool validReceptacleType = (flow > 0 && GetComponent <PhysicalDataInput>() != null) || (flow < 0 && GetComponent <PhysicalDataOutput>() != null) || flow == 0;

                if (validReceptacleType)
                {
                    if (p.AttachLock(this))
                    {
                        lockedPlugGrabber = p.GetComponent <VRTK_InteractableObject>().GetGrabbingObject().GetComponentInActor <VRTK_InteractGrab>();
                        LockedPlug        = p;
                        LockedPlug.GetComponent <VRTK_InteractableObject>().InteractableObjectUngrabbed += OnLockedPlugUngrabbed;
                        LockedPlug.GetComponent <VRTK_InteractableObject>().InteractableObjectGrabbed   += OnLockedPlugGrabbed;
                    }
                }
            }
        }
コード例 #9
0
ファイル: CordDispenser.cs プロジェクト: lane-s/ComposeVR
        private IEnumerator ExtendPlug(Plug p, GameObject plugAttach, Vector3 target)
        {
            p.gameObject.SetActive(true);
            plugAttach.transform.position = PlugStart.position;
            plugAttach.transform.rotation = PlugStart.rotation;
            p.PlugTransform.position      = PlugStart.position;
            p.PlugTransform.rotation      = PlugStart.rotation;

            plugAttach.GetComponent <SnapToTargetPosition>().SnapToTarget(target, ExtendSpeed);

            p.GetComponent <VRTK_InteractableObject>().isGrabbable = true;

            while (plugAttach != null && !plugAttach.GetComponent <SnapToTargetPosition>().HasReachedTarget)
            {
                yield return(new WaitForEndOfFrame());
            }
        }
コード例 #10
0
ファイル: CordDispenser.cs プロジェクト: lane-s/ComposeVR
        private IEnumerator RetractPlug(Plug p, GameObject plugAttach)
        {
            plugAttach.GetComponent <SnapToTargetPosition>().SnapToTarget(PlugStart.position, ExtendSpeed);
            p.GetComponent <VRTK_InteractableObject>().isGrabbable = false;

            while (plugAttach != null && !plugAttach.GetComponent <SnapToTargetPosition>().HasReachedTarget)
            {
                yield return(new WaitForEndOfFrame());
            }

            p.gameObject.SetActive(false);

            if (primaryPlug.Equals(p))
            {
                cord.gameObject.SetActive(false);
            }

            state = State.Free;
        }
コード例 #11
0
ファイル: CordDispenser.cs プロジェクト: lane-s/ComposeVR
        /// <summary>
        /// When the dispenser is WaitingForGrab, it checks if the plug that it extended is grabbed. If it is, it extends the other plug on the same cord, releasing it into the world. It creates a new cord to replace that one.
        /// If there are no controllers in the area, then the plug retracts and the dispenser goes back to the Free state
        /// </summary>
        /// <returns></returns>
        private IEnumerator WaitingForGrab()
        {
            cord.gameObject.SetActive(true);
            StartCoroutine(ExtendPlug(primaryPlug, primaryPlugAttach, PlugStart.position + PlugStart.forward * ExtendDistance));

            while (state == State.WaitingForGrab)
            {
                if (primaryPlug.GetComponent <VRTK_InteractableObject>().IsGrabbed())
                {
                    Destroy(primaryPlugAttach);

                    InteractableObjectEventHandler destroyPlugAttach         = null;
                    VRTK_InteractableObject        secondaryPlugInteractable = secondaryPlug.GetComponent <VRTK_InteractableObject>();

                    destroyPlugAttach = (object sender, InteractableObjectEventArgs e) =>
                    {
                        Destroy(secondaryPlugAttach);
                        secondaryPlugInteractable.InteractableObjectGrabbed -= destroyPlugAttach;
                    };

                    secondaryPlugInteractable.InteractableObjectGrabbed += destroyPlugAttach;

                    StartCoroutine(ExtendPlug(secondaryPlug, secondaryPlugAttach, PlugConnectionPoint.position));
                    secondaryPlugAttach.transform.rotation *= Quaternion.AngleAxis(180.0f, Vector3.up);

                    GetComponent <SocketPlugReceptacle>().ForcePlugLockAndConnect(secondaryPlug);

                    CreateCord();
                    state = State.Blocked;
                }
                else if (nearbyControllers.Count == 0)
                {
                    StartCoroutine(RetractPlug(primaryPlug, primaryPlugAttach));
                    state = State.Free;
                }

                yield return(new WaitForEndOfFrame());
            }
        }
コード例 #12
0
        // Update is called once per frame
        void Update()
        {
            if (A != null && B != null)
            {
                if (!collapsing)
                {
                    //Start a path from the origin
                    if (path.Count == 0)
                    {
                        path.Add(A.position);
                    }

                    if (!lastPos.Equals(B.position))
                    {
                        updateLine = false;

                        //Extend path if the end moves
                        if (path.Count > 0)
                        {
                            if (Vector3.Distance(path.Last(), B.position) > SegmentLength)
                            {
                                path.Add(B.position);
                                updateLine  = true;
                                timeRelaxed = 0;
                            }
                        }
                    }

                    //Add on to the beginning of the cord if the start point is not at the beginning of the cord path
                    if (A.gameObject.activeSelf && path[0] != A.position)
                    {
                        if (Vector3.Distance(path[0], A.position) > SegmentLength)
                        {
                            path.Insert(0, A.position);
                            updateLine  = true;
                            timeRelaxed = 0;
                        }
                    }

                    if (timeRelaxed < RelaxTime)
                    {
                        timeRelaxed += Time.deltaTime;

                        for (int i = 0; i < RelaxIterationsPerFrame; i++)
                        {
                            RelaxPath();
                        }

                        updateLine = true;
                    }

                    if (updateLine)
                    {
                        UpdateLine();
                        UpdateBoundingBox();
                    }

                    if (nearbyControllers.Count > 0)
                    {
                        UpdateBranchHandles();
                    }

                    lastPos = B.position;

                    TranslateFlowTexture();
                }
                else if (collapsing)
                {
                    int lineVertCount = collapseEnd - collapseStart;

                    if (lineVertCount <= 1)
                    {
                        OnCollapseFinished();
                    }
                    else
                    {
                        //Only render the portion of the path that has not yet been collapsed
                        lineRenderer.positionCount = lineVertCount + 1;
                        Vector3[] collapsingPath = new Vector3[lineRenderer.positionCount];

                        int j = 0;
                        for (int i = collapseStart; i <= collapseEnd; i++)
                        {
                            collapsingPath[j] = path[i];
                            j += 1;
                        }

                        lineRenderer.SetPositions(collapsingPath);

                        //Rotate plugs to point away from their movement direction
                        Plug plugA = A.GetComponentInActor <Plug>();
                        if (plugA != null)
                        {
                            if (plugALookVector != Vector3.zero)
                            {
                                plugA.PlugTransform.transform.rotation = Quaternion.Slerp(plugA.PlugTransform.transform.rotation, Quaternion.LookRotation(plugALookVector), COLLAPSE_ROTATION_SPEED * Time.deltaTime);
                            }
                            plugA.GetComponent <CordFollower>().Speed += COLLAPSE_ACCELERATION;
                        }

                        Plug plugB = B.GetComponentInActor <Plug>();
                        if (plugB != null)
                        {
                            if (plugBLookVector != Vector3.zero)
                            {
                                plugB.PlugTransform.transform.rotation = Quaternion.Slerp(plugB.PlugTransform.transform.rotation, Quaternion.LookRotation(plugBLookVector), COLLAPSE_ROTATION_SPEED * Time.deltaTime);
                            }
                            plugB.GetComponent <CordFollower>().Speed += COLLAPSE_ACCELERATION;
                        }

                        //Play the collapse particle effect when the cord is almost completely collapsed
                        if (lineRenderer.positionCount < COLLAPSE_PARTICLE_FIRE_COUNT && collapseParticles != null && !collapseParticles.gameObject.activeSelf)
                        {
                            collapseParticles.gameObject.SetActive(true);
                            if (plugA != null && plugB != null)
                            {
                                collapseParticles.transform.position = GetPathPointAtIndex((collapseEnd - collapseStart) / 2 + collapseStart);
                            }
                            else if (plugA != null)
                            {
                                collapseParticles.transform.position = GetPathPointAtIndex(path.Count - 1);
                            }
                            else
                            {
                                collapseParticles.transform.position = GetPathPointAtIndex(0);
                            }

                            collapseParticles.Play();
                        }
                    }
                }
            }
        }