// This coroutine has the connector line follow the mouse position. private IEnumerator MouseDragLineRenderer() { // Keep track of the line being active. lineActive = true; // The end of the line will follow the mouse position while the mouse left click is held down. while (Input.GetMouseButton(0)) { Vector3 screenPoint = Input.mousePosition; screenPoint.z = 9; Vector3 worldPoint = Camera.main.ScreenToWorldPoint(screenPoint); DotConnector.SetPosition(DotConnector.positionCount - 1, worldPoint); yield return(null); } // Once the click has been released, we turn off the score graphic and check if any dots have been scored. dotScoreGraphicXPos = 0.0f; DotScoreGraphicTop.SetPosition(0, new Vector3(dotScoreGraphicXPos, 0, 0)); DotScoreGraphicTop.SetPosition(1, new Vector3(dotScoreGraphicXPos, 0, 0)); DotScoreGraphicBot.SetPosition(0, new Vector3(dotScoreGraphicXPos, 0, 0)); DotScoreGraphicBot.SetPosition(1, new Vector3(dotScoreGraphicXPos, 0, 0)); DotConnector.positionCount = 1; DotScoreGraphicLeft.gameObject.SetActive(false); DotScoreGraphicRight.gameObject.SetActive(false); StartCoroutine(GameBoardManager.GameBoardManagerInstance.DotsScored()); lineActive = false; }
// This is called when we confirm that a dot should be added to the chain. private void DotConnected(Transform Dot) { // Update the previous dot. PreviousDot = Dot; // Add the dot to the connected dots list. GameBoardManager.GameBoardManagerInstance.ConnectedDots.Add(Dot); // The position of the new dot. Vector3 Pos = new Vector3(Dot.position.x, Dot.position.y, -1); // Update the dot connector line and increase the score graphic. DotConnector.SetPosition(DotConnector.positionCount - 1, Pos); DotConnector.positionCount++; dotScoreGraphicXPos *= dotScoreGraphicGrowAmount; DotScoreGraphicTop.SetPosition(0, new Vector3(-dotScoreGraphicXPos, 0, 0)); DotScoreGraphicTop.SetPosition(1, new Vector3(dotScoreGraphicXPos, 0, 0)); DotScoreGraphicBot.SetPosition(0, new Vector3(-dotScoreGraphicXPos, 0, 0)); DotScoreGraphicBot.SetPosition(1, new Vector3(dotScoreGraphicXPos, 0, 0)); }
// This is called when we connect the dots in a square. private void MadeASquare() { // Add all dots of the chains color to the chain of connected dots. foreach (Transform Dot in GameBoardManager.GameBoardManagerInstance.GameBoardParent) { if ((int)Dot.GetComponent <IndividualDot>().ThisDotsColor == (int)CurrentlySelectedDotColor) { GameBoardManager.GameBoardManagerInstance.ConnectedDots.Add(Dot); } } // Set the flag to avoid the chosen color when respawning dots. // Since there is a none color option, we want to subtract 1 from the enum int to account for only colors. GameBoardManager.GameBoardManagerInstance.DotReassignmentAvoidColor((int)CurrentlySelectedDotColor - 1); // Trigger the square shapped score graphics in celebration. DotScoreGraphicTop.SetPosition(0, new Vector3(-4.5f, 0, 0)); DotScoreGraphicTop.SetPosition(1, new Vector3(4.5f, 0, 0)); DotScoreGraphicBot.SetPosition(0, new Vector3(-4.5f, 0, 0)); DotScoreGraphicBot.SetPosition(1, new Vector3(4.5f, 0, 0)); DotScoreGraphicLeft.gameObject.SetActive(true); DotScoreGraphicLeft.material = StartingDot.GetComponent <Renderer>().material; DotScoreGraphicRight.gameObject.SetActive(true); DotScoreGraphicRight.material = StartingDot.GetComponent <Renderer>().material; }
// This is called when we want to disconnect a dot (the undo line option). private void DotDisconnected(Transform Dot) { // Update the previous dot. PreviousDot = Dot; // This is basically the reverse of adding a dot. We shrink the score graphic and remove the dot from the connected list. Vector3 Pos = new Vector3(Dot.position.x, Dot.position.y, -1); DotConnector.SetPosition(DotConnector.positionCount - 1, Pos); DotConnector.positionCount--; dotScoreGraphicXPos /= dotScoreGraphicGrowAmount; DotScoreGraphicTop.SetPosition(0, new Vector3(-dotScoreGraphicXPos, 0, 0)); DotScoreGraphicTop.SetPosition(1, new Vector3(dotScoreGraphicXPos, 0, 0)); DotScoreGraphicBot.SetPosition(0, new Vector3(-dotScoreGraphicXPos, 0, 0)); DotScoreGraphicBot.SetPosition(1, new Vector3(dotScoreGraphicXPos, 0, 0)); GameBoardManager.GameBoardManagerInstance.ConnectedDots.RemoveAt(GameBoardManager.GameBoardManagerInstance.ConnectedDots.Count - 1); }
// This is called when a dot is clicked to begin a line. public void StartLine(Transform ClickedDot) { // Keep track of the starting dot of the line as well as the previous dot for the next dot in the line. StartingDot = ClickedDot; PreviousDot = StartingDot; // Add the starting dot to the list of connected dots. GameBoardManager.GameBoardManagerInstance.ConnectedDots.Add(ClickedDot); // This sets the connection lines position and color equal to the starting dot. Vector3 Pos = new Vector3(ClickedDot.position.x, ClickedDot.position.y, -1); DotConnector.SetPosition(0, Pos); DotConnector.material = ClickedDot.GetComponent <Renderer>().material; // When no dot is selected, the line renderer has only 1 position so that it disappears. Here we increase the position count to be able to see the line. DotConnector.positionCount++; // We set the score graphic equal to it's starting value. I found that 0.1 felt right for this. dotScoreGraphicXPos = dotScoreGraphicStartingValue; DotScoreGraphicTop.SetPosition(0, new Vector3(-dotScoreGraphicXPos, 0, 0)); DotScoreGraphicTop.SetPosition(1, new Vector3(dotScoreGraphicXPos, 0, 0)); DotScoreGraphicBot.SetPosition(0, new Vector3(-dotScoreGraphicXPos, 0, 0)); DotScoreGraphicBot.SetPosition(1, new Vector3(dotScoreGraphicXPos, 0, 0)); DotScoreGraphicTop.material = ClickedDot.GetComponent <Renderer>().material; DotScoreGraphicBot.material = ClickedDot.GetComponent <Renderer>().material; // Start the coroutine which has the connecting line follow the mouse. StartCoroutine(MouseDragLineRenderer()); }