public void OnPointerDown(PointerEventData data) { if (raycastingScript.startStopButtonScript.physicsOn) { return; } if (raycastingScript.activePiece != null) { PiecePrefabBehaviour activePieceBehaviour = raycastingScript.activePiece.GetComponent <PiecePrefabBehaviour>(); if (activePieceBehaviour.isMoving() || activePieceBehaviour.isPlacementCorrecting()) { return; } } if (camLeftButtonScript.rotating() || camRightButtonScript.rotating() || pieceLeftButtonScript.rotating() || pieceRightButtonScript.rotating()) { return; } raycastingScript.SetTopButtonsVisible(true); // set resettable to false since the new piece doesn't have a place in the previous saved state raycastingScript.resetButtonScript.setResettable(false); GameObject newPiece = Instantiate(piecePrefab); PiecePrefabBehaviour newPieceScript = newPiece.GetComponent <PiecePrefabBehaviour>(); newPieceScript.prefab = piecePrefab; newPieceScript.OnPieceTouchBegin(); }
public void OnPointerDown(PointerEventData data){ if(raycastingScript.activePiece != null){ PiecePrefabBehaviour activePieceBehaviour = raycastingScript.activePiece.GetComponent<PiecePrefabBehaviour>(); if(activePieceBehaviour.isMoving() || activePieceBehaviour.isPlacementCorrecting()){ return; } } pieceRotationDirection = -1; }
private void OnTriggerExit(Collider other) { Transform parent = other.gameObject.transform.parent; if (parent == null || parent.gameObject.name != "Workspace Boundaries") // presumably, therefore, it's a machine piece that had caused the collision { PiecePrefabBehaviour collidingPieceScript = getCompletePiece(other.gameObject).GetComponent <PiecePrefabBehaviour>(); collidingPieceScript.collidersInContact.Remove(gameObject.GetComponent <Collider>()); } }
private void OnTriggerEnter(Collider other) { Transform otherParent = other.gameObject.transform.parent; if (otherParent == null || otherParent.gameObject.name != "Workspace Boundaries") // presumably, therefore, it's a machine piece that had caused the collision { GameObject collidingPiece = getCompletePiece(other.gameObject); GameObject thisPiece = getCompletePiece(gameObject); if (collidingPiece != thisPiece) // make sure we don't add colliders that are actually part of this piece--this happens with flat levers, for example { PiecePrefabBehaviour collidingPieceScript = collidingPiece.GetComponent <PiecePrefabBehaviour>(); collidingPieceScript.collidersInContact.Add(gameObject.GetComponent <Collider>()); } } }
public void removeActivePiece() { GameObject activePiece = raycastingScript.activePiece; PiecePrefabBehaviour activePieceScript = activePiece.GetComponent <PiecePrefabBehaviour>(); // mark the snap-to target GameObject that the piece is currrently occupying, if there is one, as unoccupied GameObject target = activePieceScript.currOccupiedSnapToTarget; if (target != null) { target.GetComponent <SnapToTargetBehaviour>().occupied = false; } // remove activePiece's colliders from the collidersInContact lists of any other GameObjects in contact with activePiece foreach (Collider colliderInContact in activePieceScript.collidersInContact) // for each collider in contact with activePiece { Transform parent = colliderInContact.gameObject.transform.parent; if (parent == null || parent.gameObject.name != "Workspace Boundaries") // presumably, therefore, the collider in question belongs to a machine piece that's in contact with activePiece { PiecePrefabBehaviour pieceInContactScript = getCompletePiece(colliderInContact.gameObject).GetComponent <PiecePrefabBehaviour>(); // get that collider's piece and its script int lastIndex = pieceInContactScript.collidersInContact.Count - 1; for (int i = lastIndex; i >= 0; i--) // for each collider in contact with the piece in question { Collider colliderInContactWithOtherPiece = pieceInContactScript.collidersInContact[i]; if (isDescendent(colliderInContactWithOtherPiece.gameObject.transform, activePiece.transform)) // if the collider belongs to activePiece // remove the collider from the collidersInContact list of the piece in question { pieceInContactScript.collidersInContact.RemoveAt(i); } } } } // finally, actually take care of removing the piece if (resetButtonScript.getResettable()) { raycastingScript.piecesRemovedWhileResettable.Add(activePiece); activePiece.SetActive(false); } else { Destroy(activePiece); } raycastingScript.pieces.Remove(activePiece); // needs to be before the call to clearActivePiece raycastingScript.ClearActivePiece(); }
public void OnResetButtonPress() { // reenable the start/stop button, in the event that it had been disabled temporarily through the temporary removal of the last piece startStopObject.GetComponent <Button>().interactable = true; // turn off physics, if it's on if (startStopButtonScript.physicsOn) { startStopButtonScript.OnStartStopPress(); } piecesScrollView.SetActive(true); // needs to be after the OnStartStopPress call // Reactivate the temporarily removed pieces and move them to the normal pieces list foreach (GameObject piece in raycastingScript.piecesRemovedWhileResettable) { piece.SetActive(true); raycastingScript.pieces.Add(piece); } raycastingScript.piecesRemovedWhileResettable.Clear(); // Before resetting *any* positions (which will cause OnTriggerEnter to fire for the pieces), // clear *all* of the collidersInContact lists so that we don't have unwanted duplicates // (so don't merge this loop with the next one!) foreach (GameObject piece in raycastingScript.pieces) { PiecePrefabBehaviour pieceScript = piece.GetComponent <PiecePrefabBehaviour>(); pieceScript.collidersInContact.Clear(); } // Finally, actually do the resetting foreach (GameObject piece in raycastingScript.pieces) { PiecePrefabBehaviour pieceScript = piece.GetComponent <PiecePrefabBehaviour>(); pieceScript.resetTransforms(); pieceScript.clearVelocities(); // needs to be after the OnStartStopPress call } setResettable(false); // needs to be after the OnStartStopPress call }
// remove piece's colliders from the collidersInContact lists of any other GameObjects in contact with obj, then destroy piece private void DisconnectAndDestroy(GameObject piece) { foreach (Collider colliderInContact in piece.GetComponent <PiecePrefabBehaviour>().collidersInContact) // for each collider in contact with piece { Transform parent = colliderInContact.gameObject.transform.parent; if (parent == null || parent.gameObject.name != "Workspace Boundaries") // presumably, therefore, the collider in question belongs to another machine piece that's in contact with piece { PiecePrefabBehaviour pieceInContactScript = getCompletePiece(colliderInContact.gameObject).GetComponent <PiecePrefabBehaviour>(); // get that other collider's piece and its script int lastIndex = pieceInContactScript.collidersInContact.Count - 1; for (int i = lastIndex; i >= 0; i--) // for each collider in contact with the other piece in question { Collider colliderInContactWithOtherPiece = pieceInContactScript.collidersInContact[i]; if (isDescendent(colliderInContactWithOtherPiece.gameObject.transform, piece.transform)) // if the collider belongs to piece // remove the collider from the collidersInContact list of the other piece in question { pieceInContactScript.collidersInContact.RemoveAt(i); } } } } Destroy(piece); }
public void OnStartStopPress() { physicsOn = !physicsOn; foreach (GameObject piece in raycastingScript.pieces) { PiecePrefabBehaviour pieceScript = piece.GetComponent <PiecePrefabBehaviour>(); if (physicsOn && !raycastingScript.resetButtonScript.getResettable()) { // If physics is on, save positions, if positions haven't already been saved since the last reset (or since the Contraption Designer loaded, if there hasn't been a reset yet) // i.e. this code runs when the user pressed a button that said, "start" pieceScript.saveTransforms(); } else if (physicsOn) { // Here, physics is being turned on and has already been turned on before since the last reset, so resume those velocities // i.e. this code runs when the user pressed a button that said, "resume" pieceScript.resumeVelocities(); } else { // Here, physics is being turned off, so save the velocities in case the user wants to resume subsequently // i.e. this code runs when the user pressed a button that said, "pause" pieceScript.saveVelocities(); } } raycastingScript.resetButtonScript.setResettable(true); if (physicsOn) { raycastingScript.ClearActivePiece(); // probably not necessary since pressing the button should clear active piece anyway, but might as well play it safe setButtonState("pause"); // change the text and color of the button } else { setButtonState("resume"); // change the text and color of the button } // disable piece control buttons (even when turning physics off, since we still don't have an active piece yet at that point. But it's all irrelevant anyway if we don't allow paused-machine editing) raycastingScript.SetAllPieceControlsButtonsInteractable(false); // toggle the visibility of elements that are only visible when physics is off clearAllObject.SetActive(!physicsOn); // turn off visibility of pieces scroll view and piece controls panel, since we don't want to allow user to add or edit pieces before resetting piecesScrollView.SetActive(false); pieceControlsPanel.SetActive(false); // toggle isKinematic and isTrigger for all pieces foreach (GameObject piece in raycastingScript.pieces) { PiecePrefabBehaviour pieceScript = piece.GetComponent <PiecePrefabBehaviour>(); pieceScript.setKinematic(!physicsOn); pieceScript.setTriggers(!physicsOn); // needs to be after the ClearActivePiece call } // toggle isTrigger for all workspace boundary colliders foreach (Transform bound_trans in workspaceBoundariesObject.transform) { GameObject boundary = bound_trans.gameObject; boundary.GetComponent <Collider>().isTrigger = !physicsOn; } }