コード例 #1
0
ファイル: TileGroup.cs プロジェクト: GlowPuff/your-journey
    /// <summary>
    /// Randomly attaches one group to another
    /// </summary>
    public bool AttachTo(TileGroup tgToAttachTo)
    {
        //get all open connectors in THIS tilegroup
        Vector3[] openConnectors = GlowEngine.RandomizeArray(GetOpenConnectors());
        //get all open anchors on group we're connecting TO
        Vector3[] tgOpenConnectors = GlowEngine.RandomizeArray(tgToAttachTo.GetOpenAnchors());
        //dummy
        GameObject dummy = new GameObject();

        Vector3[] orTiles = new Vector3[tileList.Count];

        //record original CONTAINER position
        Vector3 or = containerObject.position;

        //record original TILE positions
        for (int i = 0; i < tileList.Count; i++)
        {
            orTiles[i] = tileList[i].transform.position;
        }

        bool safe = false;

        foreach (Vector3 c in openConnectors)
        {
            safe = false;
            //parent each TILE to dummy
            foreach (Tile tile in tileList)
            {
                tile.transform.parent.transform.parent = dummy.transform;
            }
            //move containerObject to each connector in THIS group
            containerObject.position = c;
            //parent TILES back to containerObject
            foreach (Tile tile in tileList)
            {
                tile.transform.parent.transform.parent = containerObject.transform;
            }

            //move containerObject to each anchor trying to connect to
            foreach (Vector3 a in tgOpenConnectors)
            {
                containerObject.position = a;
                //rotate 360 for different orientations?

                //check collision
                if (!CheckCollisionsWithinGroup(GetAllOpenConnectorsOnBoard()))
                {
                    safe = true;
                    break;
                }
            }

            if (safe)
            {
                break;
            }
            else
            {
                //Debug.Log( "RESETTING" );
                //reset tilegroup to original position
                containerObject.position = or;
                for (int i = 0; i < tileList.Count; i++)
                {
                    tileList[i].transform.position = orTiles[i];
                }
            }
        }

        Object.Destroy(dummy);
        if (!safe)
        {
            Debug.Log("AttachTo*********NOT FOUND");
            return(false);
        }

        GenerateGroupCenter();
        return(true);
    }