/// <summary> /// Called when piece is colliding from its bottom side /// </summary> /// <param name="CollidingPieceId">Id of piece colliding with</param> /// <param name="ColP">Instance of piece colliding with</param> /// <param name="ColPPieceData">Metadata of piece colliding with</param> /// <param name="ColObjController">JPPieceController instance of piece colliding with</param> void OnBottomCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController) { //Check if this is child of current holding piece then apply collision logic bool IsChildOfHoldingPiece = false; JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren<JPPieceController>(); foreach (JPPieceController item in ChildrenControllers) { if (ThisPieceData.ID == item.ThisPieceData.ID) { IsChildOfHoldingPiece = true; break; } } if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece) { //Get colliding piece position in grid int CPElementRow = 0; int CPElementCol = 0; PPPuzzleController.ArrayPosToRC(CollidingPieceId, JpPuzzleControllerInstance.PiecesInCol, JpPuzzleControllerInstance.PiecesInRow, out CPElementRow, out CPElementCol); //Get this piece position in grid int PElementRow = 0; int PElementCol = 0; PPPuzzleController.ArrayPosToRC(ThisPieceData.ID, JpPuzzleControllerInstance.PiecesInCol, JpPuzzleControllerInstance.PiecesInRow, out PElementRow, out PElementCol); if (ThisPieceData.ID > CollidingPieceId && PElementCol == CPElementCol && PElementRow == CPElementRow + 1) { //If is child of holding piece make it parent //Make this piece parent of all Transform Temp = transform.root; transform.parent = null; Temp.parent = transform; JpPuzzleControllerInstance.UnholdPiece(); transform.position = new Vector3(ColP.transform.position.x, ColP.transform.position.y + JpPuzzleControllerInstance.PieceHeightInWorld, ColP.transform.position.z); ColP.transform.root.parent = transform; OnPieceJoined(); CheckForPuzzleComplete(transform); } } }
void Start() { if (!UseFilePath) { //random image selection PuzzleImage = new Texture2D(1, 1); PuzzleImage.LoadImage(File.ReadAllBytes(getPath() + "/dewa/" + Random.Range(0, 6) + ".jpg")); //Debug.Log(getPath()+ "/Puzzle Maker/PM Free Images/dewa/" + "1" + ".jpg"); //Random.Range(0, 3) //KIDS PUZZLE IMAGES //Random.Range(3,6) //ADULTS PUZZLE IMAGES _PuzzleMaker = new PuzzlePieceMaker(PuzzleImage, JointMaskImage, PiecesInRow, PiecesInCol); //Enable below code and provide a file path to save data created by _puzzlemaker class using provided image //_PuzzleMaker.SaveData("File Path here e.g c:\\Images\\Temp.pm"); } else { try { _PuzzleMaker = new PuzzlePieceMaker(PMFilePath); PiecesInCol = _PuzzleMaker.NoOfPiecesInCol; PiecesInRow = _PuzzleMaker.NoOfPiecesInRow; } catch (System.Exception e) { Debug.LogError("Please check if you have provided correct file path : " + e.Message); Destroy(gameObject); return; } } //Get main instance of puzzle piece _jpPuzzlePieceInst = gameObject.transform.FindChild("JPPiece").gameObject; //Seperate from cam for cam resizing to adjust whole puzzle in cam view Transform parentCamTransform = gameObject.transform.parent; Camera parentCam = parentCamTransform.GetComponent <Camera>(); gameObject.transform.parent = null; //Store current transform and translate to origin for everythings placement Vector3 OrigionalScale = transform.localScale; Vector3 OrigionalPosition = transform.position; Vector3 OrigionalRotation = transform.localRotation.eulerAngles; //Set background if (DisplayCompletedImage) { GetComponent <Renderer>().enabled = true; GetComponent <Renderer>().material.mainTexture = _PuzzleMaker.CreatedBackgroundImage; } //Resize gameobject to match size for pieces transform.localScale = new Vector3((float)PiecesInRow, (PiecesInCol * ((float)PuzzleImage.height / (float)PuzzleImage.width)), 1f); //Translate to zero transform.position = new Vector3(0, 0, 0); transform.rotation = Quaternion.Euler(Vector3.zero); #region "Pieces Initialization And Placement" //Initialize all pieces resize them and place them on screen randomly _PuzzlePieces = new GameObject[PiecesInRow * PiecesInCol]; Random.seed = System.DateTime.Now.Millisecond; for (int RowTrav = 0; RowTrav < PiecesInCol; RowTrav++) { for (int ColTrav = 0; ColTrav < PiecesInRow; ColTrav++) { GameObject Temp = Object.Instantiate(_jpPuzzlePieceInst) as GameObject; //AudioSettings name for this piece Temp.name = "Piece" + ((RowTrav * PiecesInRow) + ColTrav).ToString(); Texture2D Img = _PuzzleMaker._CreatedImagePiecesData[RowTrav, ColTrav].PieceImage; float PieceScaleX = (float)Img.width / (float)_PuzzleMaker.PieceWidthWithoutJoint; float PieceScaleY = this.PieceHeightInWorld * ((float)Img.height / (float)_PuzzleMaker.PieceHeightWithoutJoint); JPPieceController TempPieceControllerInst = Temp.GetComponent <JPPieceController>(); TempPieceControllerInst.JpPuzzleControllerInstance = this; //Get this piece information SPieceInfo ThisPieceData = _PuzzleMaker._CreatedImagePiecesData[RowTrav, ColTrav].PieceMetaData.MakeCopy(); TempPieceControllerInst.ThisPieceData = ThisPieceData; //Assign image to piece Temp.GetComponent <Renderer>().material.mainTexture = Img; //Resize piece in world Temp.transform.localScale = new Vector3(PieceScaleX, PieceScaleY, 1); //Position pieces randomly on screen Vector3 CalcPosition = Camera.main.ScreenToWorldPoint( new Vector3(Random.Range(_PuzzleMaker.PieceWidthWithoutJoint, Screen.width - _PuzzleMaker.PieceWidthWithoutJoint), Random.Range(_PuzzleMaker.PieceHeightWithoutJoint, Screen.height - _PuzzleMaker.PieceHeightWithoutJoint), 0)); CalcPosition.z = transform.position.z - 0.4f; Temp.transform.position = CalcPosition; //Enable collider for this piece Temp.GetComponent <BoxCollider>().enabled = true; TempPieceControllerInst.enabled = true; //Enable piece Temp.SetActive(true); _PuzzlePieces[(RowTrav * PiecesInRow) + ColTrav] = Temp; } } #endregion #region "Camera Resizing And Placement" //Resize camera to display whole puzzle in camera view float widthToBeSeen = PiecesInRow * PieceWidthInWorld; float heightToBeSeen = PiecesInCol * PieceHeightInWorld; parentCam.orthographicSize = widthToBeSeen > heightToBeSeen ? widthToBeSeen * 0.4f : heightToBeSeen * 0.4f; //Position camera in centre of puzzle //float CalcCameraX = ; //float CalcCameraY = ; //parentCamTransform.position = new Vector3(CalcCameraX, CalcCameraY, _pieceInstances[1][0].transform.position.z - 3); parentCamTransform.position = new Vector3(0, 0, transform.position.z - 10); #endregion }
/// <summary> /// Called when piece is colliding from its left side /// </summary> /// <param name="CollidingPieceId">Id of piece colliding with</param> /// <param name="ColP">Instance of piece colliding with</param> /// <param name="ColPPieceData">Metadata of piece colliding with</param> /// <param name="ColObjController">JPPieceController instance of piece colliding with</param> void OnLeftCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController) { //Check if this is child of current holding piece then apply collision logic bool IsChildOfHoldingPiece = false; JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren<JPPieceController>(); foreach (JPPieceController item in ChildrenControllers) { if (ThisPieceData.ID == item.ThisPieceData.ID) { IsChildOfHoldingPiece = true; break; } } if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece) { if (ThisPieceData.ID == CollidingPieceId + 1) { //If is child of holding piece make it parent //Make this piece parent of all Transform Temp = transform.root; transform.parent = null; Temp.parent = transform; JpPuzzleControllerInstance.UnholdPiece(); transform.position = new Vector3(ColP.transform.position.x + JpPuzzleControllerInstance.PieceWidthInWorld, ColP.transform.position.y, ColP.transform.position.z); ColP.transform.root.parent = transform; OnPieceJoined(); CheckForPuzzleComplete(transform); } } }
/// <summary> /// Called when piece is colliding from its bottom side /// </summary> /// <param name="CollidingPieceId">Id of piece colliding with</param> /// <param name="ColP">Instance of piece colliding with</param> /// <param name="ColPPieceData">Metadata of piece colliding with</param> /// <param name="ColObjController">JPPieceController instance of piece colliding with</param> void OnBottomCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController) { //Check if this is child of current holding piece then apply collision logic bool IsChildOfHoldingPiece = false; JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren <JPPieceController>(); foreach (JPPieceController item in ChildrenControllers) { if (ThisPieceData.ID == item.ThisPieceData.ID) { IsChildOfHoldingPiece = true; break; } } if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece) { //Get colliding piece position in grid int CPElementRow = 0; int CPElementCol = 0; PPPuzzleController.ArrayPosToRC(CollidingPieceId, JpPuzzleControllerInstance.PiecesInCol, JpPuzzleControllerInstance.PiecesInRow, out CPElementRow, out CPElementCol); //Get this piece position in grid int PElementRow = 0; int PElementCol = 0; PPPuzzleController.ArrayPosToRC(ThisPieceData.ID, JpPuzzleControllerInstance.PiecesInCol, JpPuzzleControllerInstance.PiecesInRow, out PElementRow, out PElementCol); if (ThisPieceData.ID > CollidingPieceId && PElementCol == CPElementCol && PElementRow == CPElementRow + 1) { //If is child of holding piece make it parent //Make this piece parent of all Transform Temp = transform.root; transform.parent = null; Temp.parent = transform; JpPuzzleControllerInstance.UnholdPiece(); transform.position = new Vector3(ColP.transform.position.x, ColP.transform.position.y + JpPuzzleControllerInstance.PieceHeightInWorld, ColP.transform.position.z); ColP.transform.root.parent = transform; OnPieceJoined(); CheckForPuzzleComplete(transform); } } }
/// <summary> /// Unity Event: Checks for and manages joining of pieces /// </summary> /// <param name="ColObj">Colliding piece</param> void OnTriggerEnter(Collider ColObj) { if (ColObj.name.Contains("Piece")) { if (JpPuzzleControllerInstance.IsHoldingPiece) { // Is it a side collision or top or bottom collision using collision range Vector3 CollisionVector = transform.position - ColObj.transform.position; CollisionVector.Normalize(); float CollisionThreshold = JpPuzzleControllerInstance.PieceJoinSensitivity; float DirectionCollisionThreshold = 0.3f; GameObject ColGameObject = ColObj.gameObject; JPPieceController ColObjController = ColGameObject.GetComponent <JPPieceController>(); //Check for correct or wrong piece collision for display of wrong piece animation if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID()) { JpPuzzleControllerInstance.WrongPieceCollision(ColGameObject); } if (CollisionVector.y < CollisionThreshold && CollisionVector.y > -CollisionThreshold) { if (CollisionVector.x < DirectionCollisionThreshold) //Right Collision { //Debug.Log("Right Collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized); if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Left)) { OnRightCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController); } } else if (CollisionVector.x > -DirectionCollisionThreshold) //Left Collision { //Debug.Log("Left collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized); if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Right)) { OnLeftCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController); } } } else if (CollisionVector.x < CollisionThreshold && CollisionVector.x > -CollisionThreshold) { if (CollisionVector.y < DirectionCollisionThreshold) //Top Collision { //Debug.Log("Top Collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized); if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Bottom)) { OnTopCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController); } } else if (CollisionVector.y > DirectionCollisionThreshold) //Bottom collision { //Debug.Log("Bottom collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized); if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Top)) { OnBottomCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController); } } } } } }
void OnBottomCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController) { //Check if this is child of current holding piece then apply collision logic bool IsChildOfHoldingPiece = false; JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren<JPPieceController>(); foreach (JPPieceController item in ChildrenControllers) { if (ThisPieceData.ID == item.ThisPieceData.ID) { IsChildOfHoldingPiece = true; break; } } if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece) { //Get colliding piece position in grid int CPElementRow = 0; int CPElementCol = 0; PPPuzzleController.ArrayPosToRC(CollidingPieceId, JpPuzzleControllerInstance.PiecesInCol, JpPuzzleControllerInstance.PiecesInRow, out CPElementRow, out CPElementCol); //Get this piece position in grid int PElementRow = 0; int PElementCol = 0; PPPuzzleController.ArrayPosToRC(ThisPieceData.ID, JpPuzzleControllerInstance.PiecesInCol, JpPuzzleControllerInstance.PiecesInRow, out PElementRow, out PElementCol); if (ThisPieceData.ID > CollidingPieceId && PElementCol == CPElementCol && PElementRow == CPElementRow + 1) { //If is child of holding piece make it parent //Make this piece parent of all Transform Temp = transform.root; transform.parent = null; Temp.parent = transform; JpPuzzleControllerInstance.UnholdPiece(); Vector3 CalculatedPos = new Vector3(); float LeftJointWorldScale = 0; float RightJointWorldScale = 0; float TopJointWorldScale = 0; float BotJointWorldScale = 0; float ColPLeftJointWorldScale = 0; float ColPRightJointWorldScale = 0; float ColPTopJointWorldScale = 0; float ColPBotJointWorldScale = 0; //Calculate required data CalculateDataForCollisionPlacement(ColPPieceData, out ColPLeftJointWorldScale, out ColPRightJointWorldScale, out ColPTopJointWorldScale, out ColPBotJointWorldScale); CalculateDataForCollisionPlacement(ThisPieceData, out LeftJointWorldScale, out RightJointWorldScale, out TopJointWorldScale, out BotJointWorldScale); //Calculate X CalculatedPos.x = ColP.transform.position.x - (ColPRightJointWorldScale / 2) + (ColPLeftJointWorldScale / 2) + (RightJointWorldScale / 2) - (LeftJointWorldScale / 2); //Calculate Y float CalcYWJ = ColP.transform.position.y + JpPuzzleControllerInstance.PieceHeightInWorld; CalculatedPos.y = CalcYWJ + (TopJointWorldScale / 2) - (BotJointWorldScale / 2) - (ColPTopJointWorldScale / 2) + (ColPBotJointWorldScale / 2); CalculatedPos.z = transform.position.z; transform.position = CalculatedPos; ColP.transform.root.parent = transform; OnPieceJoined(); CheckForPuzzleComplete(transform); } } }
/// <summary> /// Called when piece is colliding from its left side /// </summary> /// <param name="CollidingPieceId">Id of piece colliding with</param> /// <param name="ColP">Instance of piece colliding with</param> /// <param name="ColPPieceData">Metadata of piece colliding with</param> /// <param name="ColObjController">JPPieceController instance of piece colliding with</param> void OnLeftCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController) { //Check if this is child of current holding piece then apply collision logic bool IsChildOfHoldingPiece = false; JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren <JPPieceController>(); foreach (JPPieceController item in ChildrenControllers) { if (ThisPieceData.ID == item.ThisPieceData.ID) { IsChildOfHoldingPiece = true; break; } } if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece) { if (ThisPieceData.ID == CollidingPieceId + 1) { //If is child of holding piece make it parent //Make this piece parent of all Transform Temp = transform.root; transform.parent = null; Temp.parent = transform; JpPuzzleControllerInstance.UnholdPiece(); transform.position = new Vector3(ColP.transform.position.x + JpPuzzleControllerInstance.PieceWidthInWorld, ColP.transform.position.y, ColP.transform.position.z); ColP.transform.root.parent = transform; OnPieceJoined(); CheckForPuzzleComplete(transform); } } }
void OnRightCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController) { //Check if this is child of current holding piece then apply collision logic bool IsChildOfHoldingPiece = false; JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren<JPPieceController>(); foreach (JPPieceController item in ChildrenControllers) { if (ThisPieceData.ID == item.ThisPieceData.ID) { IsChildOfHoldingPiece = true; break; } } if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece) { if (ThisPieceData.ID == CollidingPieceId - 1) { //If is child of holding piece make it parent //Make this piece parent of all Transform Temp = transform.root; transform.parent = null; Temp.parent = transform; JpPuzzleControllerInstance.UnholdPiece(); Vector3 CalculatedPos = new Vector3(); float LeftJointWorldScale = 0; float RightJointWorldScale = 0; float TopJointWorldScale = 0; float BotJointWorldScale = 0; float ColPLeftJointWorldScale = 0; float ColPRightJointWorldScale = 0; float ColPTopJointWorldScale = 0; float ColPBotJointWorldScale = 0; //Calculate required data CalculateDataForCollisionPlacement(ColPPieceData, out ColPLeftJointWorldScale, out ColPRightJointWorldScale, out ColPTopJointWorldScale, out ColPBotJointWorldScale); CalculateDataForCollisionPlacement(ThisPieceData, out LeftJointWorldScale, out RightJointWorldScale, out TopJointWorldScale, out BotJointWorldScale); //Calculate X //Calculated X without joints float CalcXWJ = ColP.transform.position.x - JpPuzzleControllerInstance.PieceWidthInWorld; CalculatedPos.x = CalcXWJ - (ColPRightJointWorldScale / 2) + (ColPLeftJointWorldScale / 2) + (RightJointWorldScale / 2) - (LeftJointWorldScale / 2); //Calculate Y CalculatedPos.y = ColP.transform.position.y + (TopJointWorldScale / 2) - (BotJointWorldScale / 2) - (ColPTopJointWorldScale / 2) + (ColPBotJointWorldScale / 2); CalculatedPos.z = transform.position.z; transform.position = CalculatedPos; ColP.transform.root.parent = transform; OnPieceJoined(); CheckForPuzzleComplete(transform); } } else { //Wrong Piece } }
void OnRightCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController) { //Check if this is child of current holding piece then apply collision logic bool IsChildOfHoldingPiece = false; JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren <JPPieceController>(); foreach (JPPieceController item in ChildrenControllers) { if (ThisPieceData.ID == item.ThisPieceData.ID) { IsChildOfHoldingPiece = true; break; } } if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece) { if (ThisPieceData.ID == CollidingPieceId - 1) { //If is child of holding piece make it parent //Make this piece parent of all Transform Temp = transform.root; transform.parent = null; Temp.parent = transform; JpPuzzleControllerInstance.UnholdPiece(); Vector3 CalculatedPos = new Vector3(); float LeftJointWorldScale = 0; float RightJointWorldScale = 0; float TopJointWorldScale = 0; float BotJointWorldScale = 0; float ColPLeftJointWorldScale = 0; float ColPRightJointWorldScale = 0; float ColPTopJointWorldScale = 0; float ColPBotJointWorldScale = 0; //Calculate required data CalculateDataForCollisionPlacement(ColPPieceData, out ColPLeftJointWorldScale, out ColPRightJointWorldScale, out ColPTopJointWorldScale, out ColPBotJointWorldScale); CalculateDataForCollisionPlacement(ThisPieceData, out LeftJointWorldScale, out RightJointWorldScale, out TopJointWorldScale, out BotJointWorldScale); //Calculate X //Calculated X without joints float CalcXWJ = ColP.transform.position.x - JpPuzzleControllerInstance.PieceWidthInWorld; CalculatedPos.x = CalcXWJ - (ColPRightJointWorldScale / 2) + (ColPLeftJointWorldScale / 2) + (RightJointWorldScale / 2) - (LeftJointWorldScale / 2); //Calculate Y CalculatedPos.y = ColP.transform.position.y + (TopJointWorldScale / 2) - (BotJointWorldScale / 2) - (ColPTopJointWorldScale / 2) + (ColPBotJointWorldScale / 2); CalculatedPos.z = transform.position.z; transform.position = CalculatedPos; ColP.transform.root.parent = transform; CheckForPuzzleComplete(transform); } } }
void OnBottomCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController) { //Check if this is child of current holding piece then apply collision logic bool IsChildOfHoldingPiece = false; JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren <JPPieceController>(); foreach (JPPieceController item in ChildrenControllers) { if (ThisPieceData.ID == item.ThisPieceData.ID) { IsChildOfHoldingPiece = true; break; } } if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece) { //Get colliding piece position in grid int CPElementRow = 0; int CPElementCol = 0; PPPuzzleController.ArrayPosToRC(CollidingPieceId, JpPuzzleControllerInstance.PiecesInCol, JpPuzzleControllerInstance.PiecesInRow, out CPElementRow, out CPElementCol); //Get this piece position in grid int PElementRow = 0; int PElementCol = 0; PPPuzzleController.ArrayPosToRC(ThisPieceData.ID, JpPuzzleControllerInstance.PiecesInCol, JpPuzzleControllerInstance.PiecesInRow, out PElementRow, out PElementCol); if (ThisPieceData.ID > CollidingPieceId && PElementCol == CPElementCol && PElementRow == CPElementRow + 1) { //If is child of holding piece make it parent //Make this piece parent of all Transform Temp = transform.root; transform.parent = null; Temp.parent = transform; JpPuzzleControllerInstance.UnholdPiece(); Vector3 CalculatedPos = new Vector3(); float LeftJointWorldScale = 0; float RightJointWorldScale = 0; float TopJointWorldScale = 0; float BotJointWorldScale = 0; float ColPLeftJointWorldScale = 0; float ColPRightJointWorldScale = 0; float ColPTopJointWorldScale = 0; float ColPBotJointWorldScale = 0; //Calculate required data CalculateDataForCollisionPlacement(ColPPieceData, out ColPLeftJointWorldScale, out ColPRightJointWorldScale, out ColPTopJointWorldScale, out ColPBotJointWorldScale); CalculateDataForCollisionPlacement(ThisPieceData, out LeftJointWorldScale, out RightJointWorldScale, out TopJointWorldScale, out BotJointWorldScale); //Calculate X CalculatedPos.x = ColP.transform.position.x - (ColPRightJointWorldScale / 2) + (ColPLeftJointWorldScale / 2) + (RightJointWorldScale / 2) - (LeftJointWorldScale / 2); //Calculate Y float CalcYWJ = ColP.transform.position.y + JpPuzzleControllerInstance.PieceHeightInWorld; CalculatedPos.y = CalcYWJ + (TopJointWorldScale / 2) - (BotJointWorldScale / 2) - (ColPTopJointWorldScale / 2) + (ColPBotJointWorldScale / 2); CalculatedPos.z = transform.position.z; transform.position = CalculatedPos; ColP.transform.root.parent = transform; CheckForPuzzleComplete(transform); } } }
void OnTriggerEnter(Collider ColObj) { if (ColObj.name.Contains("Piece")) { if (JpPuzzleControllerInstance.IsHoldingPiece()) { // Is it a side collision or top or bottom collision using collision range Vector3 CollisionVector = transform.position - ColObj.transform.position; CollisionVector.Normalize(); float CollisionThreshold = 0.6f; GameObject ColGameObject = ColObj.gameObject; JPPieceController ColObjController = ColGameObject.GetComponent <JPPieceController>(); if (CollisionVector.y < 0.2f && CollisionVector.y > -0.2f) { if (CollisionVector.x < CollisionThreshold) //Right Collision { //Debug.Log("Right Collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized); if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Left)) { OnRightCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController); } } else if (CollisionVector.x > -CollisionThreshold) //Left Collision { //Debug.Log("Left collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized); if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Right)) { OnLeftCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController); } } } else if (CollisionVector.x < 0.2f && CollisionVector.x > -0.2f) { if (CollisionVector.y < CollisionThreshold) //Top Collision { //Debug.Log("Top Collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized); if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Bottom)) { OnTopCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController); } } else if (CollisionVector.y > CollisionThreshold) //Bottom collision { //Debug.Log("Bottom collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized); if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Top)) { OnBottomCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController); } } } } } }
/// <summary> /// UnityMethod: Initialize actual image mesh and handle camera resizing and placement /// </summary> /// <returns>Coroutine</returns> public override IEnumerator Start() { if (BackgroundMusic != null) { _audioManager.PlayMusic(BackgroundMusic); } yield return(StartCoroutine(base.Start())); #region "Pieces Initialization And Placement" Random.seed = System.DateTime.Now.Millisecond; //Place pieces randomly on screen and scale them accordingly for (int RowTrav = 0; RowTrav < PiecesInCol; RowTrav++) { for (int ColTrav = 0; ColTrav < PiecesInRow; ColTrav++) { GameObject Temp = _puzzlePieces[(RowTrav * PiecesInRow) + ColTrav]; JPPieceController TempPieceControllerInst = Temp.GetComponent <JPPieceController>(); TempPieceControllerInst.JpPuzzleControllerInstance = this; //Get this piece information SPieceInfo ThisPieceData = _puzzlePieceMaker._CreatedPiecesData[RowTrav, ColTrav].MakeCopy(); TempPieceControllerInst.ThisPieceData = ThisPieceData; //Position pieces randomly on screen Vector3 CalcPosition = Camera.main.ScreenToWorldPoint( new Vector3(Random.Range(_puzzlePieceMaker.PieceWidthWithoutJoint, Screen.width - _puzzlePieceMaker.PieceWidthWithoutJoint), Random.Range(_puzzlePieceMaker.PieceHeightWithoutJoint, Screen.height - _puzzlePieceMaker.PieceHeightWithoutJoint), 0)); CalcPosition.z = transform.position.z - 0.4f; Temp.transform.position = CalcPosition; //Enable collider for this piece Temp.GetComponent <BoxCollider>().enabled = true; TempPieceControllerInst.enabled = true; //Enable piece Temp.SetActive(true); } } #endregion //If have to display single mesh on puzzle completion set that mesh if (ActualImageOnPuzzleComplete) { GameObject ActualImagePrefab = Resources.Load("TestBackgroundImage", typeof(GameObject)) as GameObject; _actualImageGameObject = Instantiate(ActualImagePrefab); //_actualImageGameObject = GameObject.CreatePrimitive(PrimitiveType.Quad); _actualImageGameObject.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1); _actualImageGameObject.transform.localScale = transform.localScale; _actualImageGameObject.AddComponent <ActualImageObjController>(); _actualImageGameObject.name = "ActualPuzzleImage"; _actualImageGameObject.GetComponent <Renderer>().material.mainTexture = _puzzlePieceMaker.PuzzleOrigionalImage; _actualImageGameObject.AddComponent <BoxCollider>(); _actualImageGameObject.SetActive(false); } #region "Camera Resizing And Placement" //Resize camera to display whole puzzle in camera view float widthToBeSeen = PiecesInRow * PieceWidthInWorld; float heightToBeSeen = PiecesInCol * PieceHeightInWorld; parentCam.orthographicSize = widthToBeSeen > heightToBeSeen ? widthToBeSeen * 0.4f : heightToBeSeen * 0.4f; //Position camera in centre of puzzle //float CalcCameraX = ; //float CalcCameraY = ; //parentCamTransform.position = new Vector3(CalcCameraX, CalcCameraY, _pieceInstances[1][0].transform.position.z - 3); parentCamTransform.position = new Vector3(0, 0, transform.position.z - 10); #endregion }