Ejemplo n.º 1
0
    //Method used to add a new line renderer vertex and a new vertex sphere.
    public GameObject addVertex(Vector3 pos, int vertexID, GameObject selectedVertex)
    {
        pos = rotateVertex(pos, -getLocalRotation());

        //Get the vertex spheres that are children of this object
        List <GameObject> children = getChildrenVertices();

        //Add the selected vertex to that list (as it won't be a child of the object if it's selected), and sort the list.
        if (selectedVertex)
        {
            children.Add(selectedVertex);
        }

        children.Sort(sortByVertexID);


        //for loop to go through each vertex and update it's vertexID if it's PAST the selected vertex
        foreach (GameObject child in children)
        {
            VertexManager childsVM = child.GetComponent <VertexManager>();
            if (childsVM.getVertexID() >= vertexID)
            {
                childsVM.setVertexID(childsVM.getVertexID() + 1);
            }
        }

        //create a new vertex sphere
        GameObject    newVert    = drawVert(pos, vertexID, false);
        VertexManager newVertsVM = newVert.GetComponent <VertexManager>();

        //make the new vertex's size and "length" field equal to the one currently selected
        if (selectedVertex)
        {
            newVert.transform.localScale = selectedVertex.transform.lossyScale;
            newVertsVM.setVertexLength(selectedVertex.GetComponent <VertexManager>().getVertexLength());
        }
        //add this new vertex to the dictionary of vertexmanagers with its current timing
        timingDict[newVertsVM.getVertexTiming()].Add(newVertsVM);
        //add this new vertex to the list of children, and sort the list again.
        children.Add(newVert);
        children.Sort(sortByVertexID);
        //create a Vector3[] to hold the positions that the line renderer will be set to.
        Vector3[] finalPositions = new Vector3[children.Count];
        //translate the position of every child and add it to finalPositions.
        for (int i = 0; i < children.Count; i++)
        {
            Vector3 tempPos = children[i].transform.position;
            tempPos = rotateVertex(tempPos, -getLocalRotation());
            //tempPos = vertexRotationTwo(tempPos, localRotation+270, i);
            finalPositions[i] = tempPos;
        }

        //create a new vertex on the line renderer and set it's positions to finalPositions.
        attachedLR.positionCount += 1;
        attachedLR.SetPositions(finalPositions);

        boxColliderManager.addBoxCollider(vertexID);

        return(newVert);
    }
Ejemplo n.º 2
0
    //Method used to remove a vertex from a line and return it back to the pool
    public void removeVertex(Vector3 pos, int vertexID, GameObject selectedVertex)
    {
        boxColliderManager.removeBoxCollider(vertexID);

        //if statement to detect whether the vertex being removed is the last in the line.
        if (vertexID == attachedLR.positionCount - 1)
        {
            //Lower the position count by 1 and return the object to the pool.
            attachedLR.positionCount -= 1;
            timingDict[selectedVertex.GetComponent <VertexManager>().getVertexTiming()].Remove(selectedVertex.GetComponent <VertexManager>());
            objectPooler.returnToPool("Vertex", selectedVertex);
        }
        else
        {
            //removing a vertex from the middle of a list
            //get the children of the lineBase and sort them
            List <GameObject> children = getChildrenVertices();
            children.Sort(sortByVertexID);

            //go through each vertex sphere that is a child of lineBase and lower it's vertexID by 1 if it is past the vertex being removed.
            foreach (GameObject child in children)
            {
                VertexManager childsVM = child.GetComponent <VertexManager>();
                if (childsVM.getVertexID() >= vertexID)
                {
                    childsVM.setVertexID(childsVM.getVertexID() - 1);
                }
            }

            //Translate the vector3 pos to take into account the position of the baseline object.
            pos = rotateVertex(pos, -getLocalRotation());

            //create a list of final positions that the line renderer vertices will be set too
            Vector3[] finalPositions = new Vector3[children.Count];
            for (int i = 0; i < children.Count; i++)
            {
                Vector3 tempPos = children[i].transform.position;
                tempPos           = rotateVertex(tempPos, -getLocalRotation());
                finalPositions[i] = tempPos;
            }

            //lower the number of vertices in the line renderer by 1, and set the remaining vertices to finalPositions
            attachedLR.positionCount -= 1;
            timingDict[selectedVertex.GetComponent <VertexManager>().getVertexTiming()].Remove(selectedVertex.GetComponent <VertexManager>());
            attachedLR.SetPositions(finalPositions);
            //return the removed vertex sphere back to the pool
            objectPooler.returnToPool("Vertex", selectedVertex);
        }
    }