private static GameObject DeterminePiece(Node node, ref WoodMaterialObject boardToSplit)
    {
        GameObject objToReturn = null;

        if (node.ConnectedPieces.Count <= 0)
        {
            GameObject obj = node.gameObject;
            obj.transform.parent = null;
            if (obj.tag == "Piece")
            {
                PieceController controller = obj.GetComponent <PieceController>();
                if (controller == null)
                {
                    controller = obj.AddComponent <PieceController>();
                }
            }
            else if (obj.tag == "Leftover")
            {
                WoodLeftover leftoverScript = obj.GetComponent <WoodLeftover>();
                leftoverScript.BeginDisappearing();
            }
            else
            {
                Debug.LogError(obj.name + " is not tag as Piece or Leftover");
            }
            objToReturn = obj;
        }
        else
        {
            GameObject obj = WoodManagerHelper.CreateSeparateBoard(node, ref boardToSplit);
            objToReturn = obj;
        }
        return(objToReturn);
    }
    public static GameObject CreateSeparateBoard(Node baseNode, ref WoodMaterialObject boardToSplit)
    {
        List <Node> nodes = new List <Node>();

        WoodManagerHelper.RetrieveNodes(ref nodes, baseNode);

        GameObject board = new GameObject("WoodStrip");

        board.tag = "WoodStrip";
        WoodMaterialObject woodBoard   = board.AddComponent <WoodMaterialObject>();
        List <Node>        boardNodes  = new List <Node>();
        List <CutLine>     boardLines  = new List <CutLine>();
        Bounds             boardBounds = new Bounds();

        for (int index = 0; index < nodes.Count; index++)
        {
            Node n = nodes[index];
            boardNodes.Add(n);//n.gameObject.transform.parent = board.transform;
            woodBoard.WoodPieces.Add(n.gameObject);
            if (index == 0)
            {
                boardBounds = new Bounds(n.gameObject.transform.position, Vector3.zero);
            }
            else
            {
                boardBounds.Encapsulate(n.gameObject.transform.position);
            }
            if (n.gameObject.tag == "DadoBlock")
            {
                woodBoard.AddDado(n.gameObject.GetComponent <DadoBlock>());
            }
            for (int i = 0; i < boardToSplit.LinesToCut.Count; i++)
            {
                if (boardToSplit.LinesToCut[i].ContainsPiece(n))
                {
                    boardLines.Add(boardToSplit.LinesToCut[i]);//boardToSplit.LinesToCut[i].gameObject.transform.parent = board.transform;
                    boardBounds.Encapsulate(boardToSplit.LinesToCut[i].gameObject.transform.position);
                    woodBoard.AddLine(boardToSplit.LinesToCut[i]);
                    boardToSplit.RemoveLine(i--);
                }
            }
        }

        board.transform.position = boardBounds.center;

        foreach (Node n in boardNodes)
        {
            n.gameObject.transform.parent = board.transform;
        }

        foreach (CutLine line in boardLines)
        {
            line.gameObject.transform.parent = board.transform;
        }

        return(board);
    }
 //Recursive call to get all connected nodes
 private static void RetrieveNodes(ref List <Node> nodes, Node baseNode)
 {
     nodes.Add(baseNode);
     foreach (Node c in baseNode.ConnectedPieces)
     {
         if (!nodes.Contains(c))
         {
             WoodManagerHelper.RetrieveNodes(ref nodes, c);
         }
     }
 }
Exemple #4
0
    public void SplitMaterial(CutLine lineToRemove)
    {
        WoodToCut[currentPieceIndex].transform.rotation = Quaternion.identity;
        WoodMaterialObject woodBoard = WoodToCut[currentPieceIndex].GetComponent <WoodMaterialObject>();

        LinesToCut.Remove(lineToRemove);
        List <GameObject> pieces = WoodManagerHelper.SplitBoard(lineToRemove.GetFirstBaseNode(),
                                                                lineToRemove.GetSecondBaseNode(),
                                                                woodBoard, lineToRemove);

        if (LinesToCut.Count > 0)
        {
            foreach (GameObject piece in pieces)
            {
                if (piece.tag == "Piece")
                {
                    WoodToCut.RemoveAt(currentPieceIndex);
                    Destroy(piece);
                    NextPiece();
                }
                else if (piece.tag == "Leftover")
                {
                }
                else
                {
                    Rigidbody physics = piece.GetComponent <Rigidbody>();
                    if (physics == null)
                    {
                        physics = piece.AddComponent <Rigidbody>();
                    }
                    physics.useGravity = true;
                    BandSawPieceController controller = piece.AddComponent <BandSawPieceController>();
                    controller.Moveable          = true;
                    WoodToCut[currentPieceIndex] = piece;
                    PlacePiece();
                }
            }
        }
        else
        {
            UI_Manager.InfoPanel.SetActive(true);
            UI_Manager.InfoText.text = "All of the lines are cut. \nOn to the next step.";
            UI_Manager.HideButton.gameObject.SetActive(false);
            UI_Manager.StartOverButton.gameObject.SetActive(false);
            UI_Manager.NextSceneButton.gameObject.SetActive(true);
            StillCutting = false;
            float percentage = cumulativeLineScore / numberOfCuts;
            Debug.Log(percentage);
            if (GameManager.instance != null)
            {
                GameManager.instance.scoreTracker.ApplyScore(percentage);
            }
        }
    }
    public static List <GameObject> SplitBoard(Node baseNode, Node baseNode2, WoodMaterialObject boardToSplit, CutLine detachedLine)
    {
        WoodManagerHelper.RemoveCutLine(boardToSplit, detachedLine);

        List <GameObject> splitPieces = new List <GameObject>();

        splitPieces.Add(WoodManagerHelper.DeterminePiece(baseNode, ref boardToSplit));
        splitPieces.Add(WoodManagerHelper.DeterminePiece(baseNode2, ref boardToSplit));
        Destroy(boardToSplit.gameObject);

        return(splitPieces);
    }
    /// <summary>
    /// Determines if the Node is a singular wood piece, a leftover wood piece, or a wood material gameobject that
    /// contains child pieces that still need to be cut out.
    /// </summary>
    /// <param name="node">A Node monobehavior object</param>
    /// <param name="boardToSplit">The wood material gameobject that contains the pieces that will be split</param>
    /// <returns>A gamobject that either a piece, a leftover piece, or a large wood board object</returns>
    private static GameObject DeterminePiece(Node node, ref WoodMaterialObject boardToSplit)
    {
        GameObject objToReturn = null;

        if (node.ConnectedPieces.Count <= 0)
        {
            GameObject obj = node.gameObject;
            obj.transform.parent = null;
            if (obj.tag == "Piece")
            {
                //The Node object is a wood piece
                PieceController controller = obj.GetComponent <PieceController>();
                if (controller == null)
                {
                    controller = obj.AddComponent <PieceController>();
                }
            }
            else if (obj.tag == "Leftover")
            {
                //The Node object is a leftover piece. Run the script that will delete it
                WoodLeftover leftoverScript = obj.GetComponent <WoodLeftover>();
                leftoverScript.BeginDisappearing();
            }
            else
            {
                Debug.LogError(obj.name + " is not tag as Piece or Leftover");
            }
            objToReturn = obj;
        }
        else
        {
            //The Node object is still a child of a wood material. Call the method that will split it from the wood material board.
            GameObject obj = WoodManagerHelper.CreateSeparateBoard(node, ref boardToSplit);
            objToReturn = obj;
        }
        return(objToReturn);
    }
    /// <summary>
    /// Handles what happens after a line is cut
    /// </summary>
    /// <param name="lineToRemove">The line that was cut</param>
    public void SplitMaterial(CutLine lineToRemove)
    {
        //Delete the cut line and get the split wood material
        WoodMaterialObject board = AvailableWoodMaterial[currentPieceIndex].GetComponent <WoodMaterialObject>();
        BoardController    previousBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();

        LinesToCut.Remove(lineToRemove);
        AvailableWoodMaterial.RemoveAt(currentPieceIndex);
        List <GameObject> pieces = WoodManagerHelper.SplitBoard(lineToRemove.GetFirstBaseNode(),
                                                                lineToRemove.GetSecondBaseNode(),
                                                                board, lineToRemove);

        bool pieceAdded = false;

        //This loop looks through the wood materials returned, assigns them a BoardController, and use the first piece found as the piece
        //to put on the table saw. All other pieces are hidden away until they are needed.
        foreach (GameObject piece in pieces)
        {
            WoodMaterialObject boardPiece = piece.GetComponent <WoodMaterialObject>();
            if (boardPiece != null)
            {
                bool lineFound = false;
                for (int i = 0; i < LinesToCut.Count && !lineFound; i++)
                {
                    lineFound = boardPiece.ContainsLine(LinesToCut[i]);
                }
                //If a line is found, then the piece is a wood material gameobject
                if (lineFound)
                {
                    BoardController controller = piece.AddComponent <BoardController>();
                    controller.Moveable   = true;
                    controller.WoodObject = boardPiece;
                    controller.MaxLimit_X = previousBoardController.MaxLimit_X;
                    controller.MaxLimit_Z = previousBoardController.MaxLimit_Z;
                    controller.MinLimit_X = previousBoardController.MinLimit_X;
                    controller.MinLimit_Z = previousBoardController.MinLimit_Z;
                    AvailableWoodMaterial.Add(piece);
                    if (!pieceAdded)
                    {
                        pieceAdded = true;
                        int index = AvailableWoodMaterial.IndexOf(piece);
                        currentPieceIndex = index;
                        AvailableWoodMaterial[currentPieceIndex].SetActive(true);
                        currentBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();
                    }
                    else
                    {
                        piece.SetActive(false);
                        piece.transform.position = Vector3.zero;
                        piece.transform.rotation = Quaternion.identity;
                    }
                }
                else
                {
                    Destroy(piece);
                }
            }
            else
            {
                //This was kept around for the prototype,
                //but we technically want to keep the piece around if it is a piece gameobject, or a wood material gameobject
                //There just needs to be a container that will store all of the pieces and wood materials in the project.
                Destroy(piece);
            }

            //If none of the pieces were wood material gameobjects, find one from the AvailableWoodMaterial list
            if (!pieceAdded && AvailableWoodMaterial.Count > 0)
            {
                currentPieceIndex = 0;
                AvailableWoodMaterial[currentPieceIndex].SetActive(true);
                currentBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();
                SetupForCutting();
                EnableCurrentBoardMovement(true);
                RestrictCurrentBoardMovement(false, false);
                AvailableWoodMaterial[currentPieceIndex].GetComponent <Rigidbody>().position = currentSpawnPoint.position + new Vector3(0.0f, 0.0f, -1.0f);
                Vector3    directionToPiece = (AvailableWoodMaterial[currentPieceIndex].GetComponent <Rigidbody>().position - currentSpawnPoint.position).normalized;
                Ray        ray = new Ray(currentSpawnPoint.position, directionToPiece);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    float distance = (hit.point - currentSpawnPoint.position).magnitude;
                    AvailableWoodMaterial[currentPieceIndex].GetComponent <Rigidbody>().position += (distance * -directionToPiece);
                }
            }
            SawBlade.TurnOff();
            UI_Manager.ChangeSawButtons(false);
        }

        //If all the lines are cut, the step is done
        if (LinesToCut.Count > 0)
        {
            UI_Manager.UpdateSelectionButtons(currentPieceIndex, AvailableWoodMaterial.Count);
        }
        else
        {
            UI_Manager.InfoPanel.SetActive(true);
            UI_Manager.InfoText.text = "All of the lines are cut. \nOn to the next step.";
            UI_Manager.HideButton.gameObject.SetActive(false);
            UI_Manager.StartOverButton.gameObject.SetActive(false);
            UI_Manager.NextSceneButton.gameObject.SetActive(true);
            StillCutting = false;
            float percentage = cumulativeLineScore / numberOfCuts;
            if (GameManager.instance != null)
            {
                GameManager.instance.scoreTracker.ApplyScore(percentage);
            }
            else
            {
                Debug.Log("Score Applied: " + percentage);
            }
        }
    }
Exemple #8
0
    public void SplitMaterial(CutLine lineToRemove)
    {
        WoodMaterialObject board = AvailableWoodMaterial[currentPieceIndex].GetComponent <WoodMaterialObject>();
        BoardController    previousBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();

        LinesToCut.Remove(lineToRemove);
        AvailableWoodMaterial.RemoveAt(currentPieceIndex);
        List <GameObject> pieces = WoodManagerHelper.SplitBoard(lineToRemove.GetFirstBaseNode(),
                                                                lineToRemove.GetSecondBaseNode(),
                                                                board, lineToRemove);

        bool pieceAdded = false;

        foreach (GameObject piece in pieces)
        {
            WoodMaterialObject boardPiece = piece.GetComponent <WoodMaterialObject>();
            if (boardPiece != null)
            {
                bool lineFound = false;
                for (int i = 0; i < LinesToCut.Count && !lineFound; i++)
                {
                    lineFound = boardPiece.ContainsLine(LinesToCut[i]);
                }
                if (lineFound)
                {
                    BoardController controller = piece.AddComponent <BoardController>();
                    controller.Moveable   = true;
                    controller.WoodObject = boardPiece;
                    controller.MaxLimit_X = previousBoardController.MaxLimit_X;
                    controller.MaxLimit_Z = previousBoardController.MaxLimit_Z;
                    controller.MinLimit_X = previousBoardController.MinLimit_X;
                    controller.MinLimit_Z = previousBoardController.MinLimit_Z;
                    AvailableWoodMaterial.Add(piece);
                    if (!pieceAdded)
                    {
                        pieceAdded = true;
                        int index = AvailableWoodMaterial.IndexOf(piece);
                        currentPieceIndex = index;
                        AvailableWoodMaterial[currentPieceIndex].SetActive(true);
                        currentBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();
                    }
                    else
                    {
                        piece.SetActive(false);
                        piece.transform.position = Vector3.zero;
                        piece.transform.rotation = Quaternion.identity;
                    }
                }
                else
                {
                    Destroy(piece);
                }
            }
            else
            {
                Destroy(piece);
            }

            if (!pieceAdded && AvailableWoodMaterial.Count > 0)
            {
                currentPieceIndex = 0;
                AvailableWoodMaterial[currentPieceIndex].SetActive(true);
                currentBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();
                SetupForCutting();
                EnableCurrentBoardMovement(true);
                RestrictCurrentBoardMovement(false, false);
                AvailableWoodMaterial[currentPieceIndex].transform.position = currentSpawnPoint.position + new Vector3(0.0f, 0.0f, -1.0f);
                Vector3    directionToPiece = (AvailableWoodMaterial[currentPieceIndex].transform.position - currentSpawnPoint.position).normalized;
                Ray        ray = new Ray(currentSpawnPoint.position, directionToPiece);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    float distance = (hit.point - currentSpawnPoint.position).magnitude;
                    AvailableWoodMaterial[currentPieceIndex].transform.position += (distance * -directionToPiece);
                }
            }
            SawBlade.TurnOff();
            UI_Manager.ChangeSawButtons(false);
        }

        if (LinesToCut.Count > 0)
        {
            UI_Manager.UpdateSelectionButtons(currentPieceIndex, AvailableWoodMaterial.Count);
        }
        else
        {
            UI_Manager.InfoPanel.SetActive(true);
            UI_Manager.InfoText.text = "All of the lines are cut. \nOn to the next step.";
            UI_Manager.HideButton.gameObject.SetActive(false);
            UI_Manager.StartOverButton.gameObject.SetActive(false);
            UI_Manager.NextSceneButton.gameObject.SetActive(true);
            StillCutting = false;
            float percentage = cumulativeLineScore / numberOfCuts;
            if (GameManager.instance != null)
            {
                GameManager.instance.ApplyScore(percentage);
            }
            else
            {
                Debug.Log("Score Applied: " + percentage);
            }
        }
    }
    /// <summary>
    /// Separates the pieces of the wood material based on the node passed in.
    /// Using a graph structure, this builds out a new wood material gameobject with the pieces that were
    /// connected to the base node and the base node's children
    /// </summary>
    /// <param name="baseNode">The node to start from</param>
    /// <param name="boardToSplit">The wood material that will be split</param>
    /// <returns>A gameobject representing the split wood material</returns>
    public static GameObject CreateSeparateBoard(Node baseNode, ref WoodMaterialObject boardToSplit)
    {
        //Finds all of the connected nodes that will be part of the new wood material gameobject
        List <Node> nodes = new List <Node>();

        WoodManagerHelper.RetrieveNodes(ref nodes, baseNode);

        GameObject board = new GameObject("WoodStrip");

        board.tag = "WoodStrip";
        WoodMaterialObject woodBoard   = board.AddComponent <WoodMaterialObject>();
        List <Node>        boardNodes  = new List <Node>();    //The pieces for the new wood material gameobject
        List <CutLine>     boardLines  = new List <CutLine>(); //The lines that belong to the new wood material gameobject
        Bounds             boardBounds = new Bounds();

        for (int index = 0; index < nodes.Count; index++)
        {
            Node n = nodes[index];
            boardNodes.Add(n);
            woodBoard.WoodPieces.Add(n.gameObject);
            if (index == 0)
            {
                boardBounds = new Bounds(n.gameObject.transform.position, Vector3.zero);
            }
            else
            {
                boardBounds.Encapsulate(n.gameObject.transform.position);
            }
            //Like the lines, make sure the dado cut areas go to the correct wood material
            if (n.gameObject.tag == "DadoBlock")
            {
                woodBoard.AddDado(n.gameObject.GetComponent <DadoBlock>());
            }
            for (int i = 0; i < boardToSplit.LinesToCut.Count; i++)
            {
                if (boardToSplit.LinesToCut[i] == null)
                {
                    Debug.Log(i + " is null");
                }
                else
                {
                    if (boardToSplit.LinesToCut[i].ContainsPiece(n))
                    {
                        boardLines.Add(boardToSplit.LinesToCut[i]);
                        boardBounds.Encapsulate(boardToSplit.LinesToCut[i].gameObject.transform.position);
                        woodBoard.AddLine(boardToSplit.LinesToCut[i]);
                        boardToSplit.RemoveLine(i--);
                    }
                }
            }
        }

        board.transform.position = boardBounds.center;

        foreach (Node n in boardNodes)
        {
            n.gameObject.transform.parent = board.transform;
        }

        foreach (CutLine line in boardLines)
        {
            line.gameObject.transform.parent = board.transform;
        }
        Rigidbody r = board.AddComponent <Rigidbody>();

        r.useGravity = true;
        return(board);
    }
    public void SplitMaterial(CutLine lineToRemove)
    {
        WoodMaterialObject board = AvailableWoodMaterial[currentPieceIndex].GetComponent <WoodMaterialObject>();
        BoardController    previousBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();

        LinesToCut.Remove(lineToRemove);
        AvailableWoodMaterial.RemoveAt(currentPieceIndex);
        List <GameObject> pieces = WoodManagerHelper.SplitBoard(lineToRemove.GetFirstBaseNode(),
                                                                lineToRemove.GetSecondBaseNode(),
                                                                board, lineToRemove);

        bool pieceAdded = false;

        foreach (GameObject piece in pieces)
        {
            WoodMaterialObject boardPiece = piece.GetComponent <WoodMaterialObject>();
            if (boardPiece != null)
            {
                bool lineFound = false;
                for (int i = 0; i < LinesToCut.Count && !lineFound; i++)
                {
                    lineFound = boardPiece.ContainsLine(LinesToCut[i]);
                }
                if (lineFound)
                {
                    BoardController controller = piece.AddComponent <BoardController>();
                    controller.Moveable   = true;
                    controller.WoodObject = boardPiece;
                    controller.MaxLimit_X = previousBoardController.MaxLimit_X;
                    controller.MaxLimit_Z = previousBoardController.MaxLimit_Z;
                    controller.MinLimit_X = previousBoardController.MinLimit_X;
                    controller.MinLimit_Z = previousBoardController.MinLimit_Z;
                    AvailableWoodMaterial.Add(piece);
                    if (!pieceAdded)
                    {
                        pieceAdded = true;
                        int index = AvailableWoodMaterial.IndexOf(piece);
                        currentPieceIndex = index;
                        AvailableWoodMaterial[currentPieceIndex].SetActive(true);
                        currentBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();
                    }
                    else
                    {
                        piece.SetActive(false);
                        piece.transform.position = Vector3.zero;
                        piece.transform.rotation = Quaternion.identity;
                    }
                }
                else
                {
                    Destroy(piece);
                }
            }
        }

        if (!pieceAdded && AvailableWoodMaterial.Count > 0)
        {
            currentPieceIndex = 0;
            AvailableWoodMaterial[currentPieceIndex].SetActive(true);
            currentBoardController = AvailableWoodMaterial[currentPieceIndex].GetComponent <BoardController>();
            SetupForCutting();
            PlacePiece();
        }
        SawBlade.TurnOff();
        UI_Manager.ChangeSawButtons(false);

        if (LinesToCut.Count > 0)
        {
            UI_Manager.UpdateSelectionButtons(currentPieceIndex, AvailableWoodMaterial.Count);
        }
        else
        {
            UI_Manager.InfoPanel.SetActive(true);
            UI_Manager.InfoText.text = "All of the lines are cut. \nOn to the next step.";
            UI_Manager.HideButton.gameObject.SetActive(false);
            UI_Manager.StartOverButton.gameObject.SetActive(false);
            UI_Manager.NextSceneButton.gameObject.SetActive(true);
            StillCutting = false;
            float percentage = cumulativeLineScore / numberOfCuts;
            if (GameManager.instance != null)
            {
                GameManager.instance.ApplyScore(percentage);
            }
            else
            {
                Debug.Log("No game manager");
            }
        }
    }