Beispiel #1
0
    Vector2 getMovementDirection(Vector2 from, Vector2 to)
    {
        int     direction         = map.DeltaMovement(from, to);
        Vector2 movementDirection = new Vector2();

        switch (direction)
        {
        case 1:
            movementDirection = new Vector2(1, 0);
            break;

        case 2:
            movementDirection = new Vector2(1, 1);
            break;

        case 3:
            movementDirection = new Vector2(0, 1);
            break;

        case 4:
            movementDirection = new Vector2(-1, 1);
            break;

        case 5:
            movementDirection = new Vector2(-1, 0);
            break;

        case 6:
            movementDirection = new Vector2(1, -1);
            break;

        case 7:
            movementDirection = new Vector2(0, -1);
            break;

        case 8:
            movementDirection = new Vector2(-1, -1);
            break;
        }

        //Debug.Log( from + " " + to + " " + movementDirection);
        return(movementDirection);
    }
Beispiel #2
0
    public void setConnection(int fromTileX, int fromTileY, GameObject toGo)
    {
        //Debug.Log("SET CONNECTION " + fromTileX + " " + fromTileY + " " + map.graph[fromTileX, fromTileY].isConnected);
        int newTileX = toGo.GetComponent <Cell>().tileX;
        int newTileY = toGo.GetComponent <Cell>().tileY;


        if ((!map.graph[fromTileX, fromTileY].isConnected || !map.graph[newTileX, newTileY].isConnected) ||
            ReferenceEquals(map.isNodeInShape(new Vector2(fromTileX, fromTileY)), map.isNodeInShape(new Vector2(newTileX, newTileY))))
        {
            // Draw line between object and lock it
            if (toGo != null && toGo.GetComponent <Cell>() != null)
            {
                isNeighbour = false;
                bool crossConnect = false;

                // FIX WHEN THERE IS NO NODE IN GIVEN DIRECTION
                // Chceck for cross paths
                Vector2 from    = new Vector2(tileX, tileY);
                Vector2 to      = new Vector2(newTileX, newTileY);
                int     tempDir = map.DeltaMovement(from, to);
                Vector2 dirVec  = to - from;
                if (tempDir % 2 == 0)
                {
                    if (map.tiles[tileX + (int)dirVec.x, tileY] != 0 && map.tiles[tileX, tileY + (int)dirVec.y] != 0)
                    {
                        if (map.graph[tileX + (int)dirVec.x, tileY].isConnected &&
                            map.graph[tileX, tileY + (int)dirVec.y].isConnected)
                        {
                            crossConnect = true;
                        }
                    }
                }

                if (!crossConnect)
                {
                    foreach (var tile in map.graph[fromTileX, fromTileY].edges)
                    {
                        // Check if node is neighbour and if node can be connected (not already in path)


                        if (tile.node.x == newTileX && tile.node.y == newTileY)
                        {
                            // TO FIX (so f*****g discusting !!!) ... but working :)
                            if (!map.vertices.Contains(new Vector2(newTileX, newTileY)))
                            {
                                isNeighbour = true;
                                map.graph[fromTileX, fromTileY].isConnected = true;
                                makeConnection(newTileX, newTileY, fromTileX, fromTileY, toGo);
                                // Debug.Log(" LOCKED: " + fromTileX + " " + fromTileY);
                                break;
                            }
                            else if (map.vertices.Count > 0 && map.vertices[0] == new Vector2(newTileX, newTileY))
                            {
                                isNeighbour = true;
                                map.graph[fromTileX, fromTileY].isConnected = true;
                                makeConnection(newTileX, newTileY, fromTileX, fromTileY, toGo);
                                //  Debug.Log(" LOCKED: " + fromTileX + " " + fromTileY);
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                myLine.SetPosition(0, transform.position);
                myLine.SetPosition(1, transform.position);
                // Set Line to origin
                // myLine.SetPosition(0, v3);
                Debug.Log("ALREADY CONNECTED");

                /*
                 * if (prevNodeGO != null)
                 * {
                 *  prevNodeGO.GetComponent<SpriteRenderer>().color = Color.white;
                 * }
                 */

                //go.GetComponent<Cell>().isConnected = false;
            }

            if (!isNeighbour)
            {
                // Set Line to origin
                myLine.SetPosition(0, transform.position);
                myLine.SetPosition(1, transform.position);
                //Debug.Log("NOT NEIGHBOUR!");

                /*
                 * if (prevNodeGO != null)
                 * {
                 *  prevNodeGO.GetComponent<SpriteRenderer>().color = Color.white;
                 * }
                 * //go.GetComponent<Cell>().isConnected = false;
                 */
            }
        }
        else
        {
            Debug.Log("WBABABABABA");
            myLine.SetPosition(0, transform.position);
            myLine.SetPosition(1, transform.position);
        }
    }
    public Shape ValidShape()
    {
        //Debug.Log("SHAPE IN RECOGNITION");
        //Debug.Log(map.shape.Count);
        //foreach (var ele in map.shape)
        //{
        //    Debug.Log(ele.x + " "  + ele.y);
        //}
        if (map.shape.Count == 3) // Triangle
        {
            return(new Shape.Triangle());
        }
        else if (map.shape.Count == 4) // Square
        {
            Debug.Log("4 GON");
            // direction between first 2 nodes
            int dirType = map.DeltaMovement(map.shape[0], map.shape[1]);
            //Debug.Log("INIT: " + dirType + " " + map.shape[0] +" " + map.shape[1]);
            //lenght , in case of square , all sides will be equal
            float length = Vector2.Distance(map.shape[0], map.shape[1]);
            //determine if shape have diffrent side lenghts
            bool rectangleFlag = false;

            /*                   Direction types
             *
             *                      - only vertical and horzintal directions between nodes
             *
             * * *
             * * *
             * * *
             */

            /*                    -only diagonal direcitions
             *
             *
             * * *
             * * * * *
             * * *
             *
             *
             *
             */

            if (dirType % 2 != 0)
            {
                if (CheckAllNodesDirection(1, ref rectangleFlag, length))
                {
                    if (!rectangleFlag)
                    {
                        Debug.Log("SQUARE");
                        return(new Shape.Rectangle(true));
                    }
                    else
                    {
                        Debug.Log("RECTANGLE");
                        return(new Shape.Rectangle(false));
                    }
                }
            }
            else
            {
                if (CheckAllNodesDirection(0, ref rectangleFlag, length))
                {
                    if (!rectangleFlag)
                    {
                        return(new Shape.Diamond(true));
                    }
                    else
                    {
                        Debug.Log("DIAMOND RECT");
                        return(new Shape.Diamond(false));
                    }
                }
            }

            return(null);
        }
        else
        {
            Debug.Log("NOT SUPPRTED SHAPE , NUM OF CRONERS " + map.shape.Count);
        }

        return(null);
    }