Ejemplo n.º 1
0
    bool connectingToAnotherShape(int x, int y, int origX, int origY)
    {
        MeshMan        shape;
        List <MeshMan> shapes = map.NodeInShapes(new Vector2(x, y));

        shape = map.GetBiggestPolygon(ref shapes);
        if (map.vertices.Count == 0) // first element, add to list of verices
        {
            map.vertices.Add(new Vector2(origX, origY));
        }
        map.endNode = new Vector2(x, y);

        // find index of end node
        var index = shape.nodes.FindIndex(a => a == map.endNode);
        // temporary List of nodes in shape wich we need to add to shape to conncet
        List <Vector2> connectingNodes  = new List <Vector2>();
        List <Vector2> connectingNodes1 = new List <Vector2>();

        List <Vector2> tempVertices_1 = new List <Vector2>(map.vertices);
        List <Vector2> tempVertices_2 = new List <Vector2>(map.vertices);

        Debug.Log("STAR AND END " + map.startNode + " " + map.endNode);
        if (!ReferenceEquals(map.isNodeInShape(map.startNode), map.isNodeInShape(map.endNode)))
        {
            return(false);
        }
        if (map.startNode == map.endNode)
        {
            return(false);
        }
        // Nedd to remember shape
        //if (map.startNode != map.endNode)
        //{
        //    Debug.Log("NOT sAME SHAPE");
        //}

        //Debug.Log("START NODE" + map.startNode);
        //Debug.Log("END NODE  " + map.endNode);

        // Find Path from start to end node
        //Debug.Log("Connecting nodes " );
        // when something goes wrong , dont crash whole PC!!! (fcking BSOD)
        // int fckUpCounter = 0;


        // get Last element from vertices
        Vector2 prevEle = map.vertices[map.vertices.Count - 1];

        // Make both paths
        map.YetDunno(shape, ref connectingNodes, prevEle, index, ref tempVertices_1, 1);
        map.YetDunno(shape, ref connectingNodes1, prevEle, index, ref tempVertices_2, -1);

        //Debug.Log("VERTICES +1 ");
        //foreach (var ele in tempVertices_1)
        //{
        //    Debug.Log(ele);
        //}
        //Debug.Log("VERTICES -1 ");
        //foreach (var ele in tempVertices_2)
        //{
        //    Debug.Log(ele);
        //}

        MeshMan meshData = null;

        Debug.Log("ID : S " + shape.id);


        //if (map.separateShapes)
        //Get helper shape to update , if no exiting , create new
        if (map.separateShapes)
        {
            if (!shape.isRendered)
            {
                Debug.Log("Updating");
                meshData = shape.GetComponent <MeshMan>();
            }
            else
            {
                Debug.Log("Creating");
                GameObject GO = Instantiate(map.trianglePref, map.trianglePref.transform.position, map.trianglePref.transform.rotation) as GameObject;
                meshData = GO.GetComponent <MeshMan>();

                meshData.id = map.ShapeId;
                map.ShapeId++;
            }
        }

        // bigger polygon is union of those two...
        if (map.separateShapes)
        {
            if (map.AreOfPolygon(tempVertices_1) > map.AreOfPolygon(tempVertices_2))
            {
                map.vertices    = new List <Vector2>(tempVertices_2);
                meshData.nodes  = new List <Vector2>(tempVertices_1);
                connectingNodes = connectingNodes1;
            }
            else
            {
                map.vertices   = new List <Vector2>(tempVertices_1);
                meshData.nodes = new List <Vector2>(tempVertices_2);
            }
        }
        else
        {
            // FEJKS !!!!!!!!!!
            // Vertices != Shape ... vertices define actual rendered shape
            //                       shape define just connecting part ... lot of rewrting to correct it.. IMA LAZY
            if (map.AreOfPolygon(tempVertices_1) < map.AreOfPolygon(tempVertices_2))
            {
                map.vertices = new List <Vector2>(tempVertices_2);
            }
            else
            {
                map.vertices    = new List <Vector2>(tempVertices_1);
                connectingNodes = connectingNodes1;
            }
        }
        // RESET Z POSITION OF ORIGINAL SHAPE
        foreach (var ele in shape.nodes)
        {
            Vector3 tempPos = map.graph[(int)ele.x, (int)ele.y].Go.transform.position;
            tempPos.z = 0f;
            map.graph[(int)ele.x, (int)ele.y].Go.transform.position = tempPos;
        }


        foreach (var ele in connectingNodes)
        {
            //  Filling shape
            int temp = map.DeltaMovement(prevEle, ele);
            if (map.currentDir != temp)
            {
                map.shape.Add(prevEle);
                map.currentDir = temp;
            }
            prevEle = ele;
            // Debug.Log("PREV ELE:" + prevEle);
        }

        /// Fix missing nodes in shape
        if (map.DeltaMovement(prevEle, map.startNode) != map.currentDir)
        {
            if (!map.shape.Contains(prevEle))
            {
                // Debug.Log("ADDED prevELE" + prevEle);
                map.shape.Add(prevEle);
            }
        }
        if (map.DeltaMovement(prevEle, map.startNode) != map.fistDirShape)
        {
            if (!map.shape.Contains(map.startNode))
            {
                Debug.Log("ADDED startELE" + map.startNode);
                map.shape.Add(map.startNode);
            }
        }

        // Debug.Log("AREA OF SHAPE :" + map.AreOfPolygon(map.vertices));
        // Draw mesh
        //Debug.Log("VERTICES");
        foreach (var ele in map.vertices)
        {
            Vector3 tempPos = map.graph[(int)ele.x, (int)ele.y].Go.transform.position;
            tempPos.z = -2f;
            map.graph[(int)ele.x, (int)ele.y].Go.transform.position = tempPos;
        }



        MeshMan tempMM = map.CreateMesh();

        tempMM.shapes.AddRange(shape.shapes);
        // Pass reference to helper shape
        if (map.separateShapes)
        {
            shape.helperShape   = meshData;
            tempMM.helperShape  = meshData;
            meshData.isRendered = false;

            Debug.Log("CONNECTED " + meshData.id + " ---> LEFT  S" + tempMM.id + "  RIGHT S" + shape.id);
        }
        else
        {
            Destroy(shape.gameObject);
        }
        map.ClearPath(false);
        return(true);
    }