Beispiel #1
0
    void Set(JigsawTile tile1, JigsawTile tile2)
    {
        //合成准备
        if (tile2.compound)
        {
            tile1.compound = true;
            tile1.transform.SetParent(tile2.parentTile.transform);
            tile1.parentTile = tile2.parentTile;
            if (!tile1.parentTile.curentJigsawTile.Contains(tile1))
            {
                parentTile.curentJigsawTile.Add(tile1);
            }
            tile1.SetPosition(tile2.GetPosition() + jg.GetTilePos(tile2.id, tile1.id));
            return;
        }

        //赋值相对位置
        tile1.compound = true;
        tile2.compound = true;
        GameObject tileParent = Instantiate(jg.tileParent);

        tile1.parentTile = tileParent.GetComponent <JigsawTileParent>();
        tile2.parentTile = tile1.parentTile;
        if (!tile1.parentTile.curentJigsawTile.Contains(tile1))
        {
            tile1.parentTile.curentJigsawTile.Add(tile1);
        }
        if (!tile1.parentTile.curentJigsawTile.Contains(tile2))
        {
            tile1.parentTile.curentJigsawTile.Add(tile2);
        }
        //Set
        tileParent.name = "tileParent";
        tileParent.transform.SetParent(jg.transform);
        tileParent.transform.localScale = Vector3.one;
        (tileParent.transform as RectTransform).anchoredPosition = tile1._thisRect.anchoredPosition;
        (tileParent.transform as RectTransform).sizeDelta        = tile1._thisRect.sizeDelta;
        //setParent
        tile1.transform.SetParent(tileParent.transform);
        tile2.transform.SetParent(tileParent.transform);
        tile2.SetPosition(tile1.GetPosition() + jg.GetTilePos(tile1.id, tile2.id));
    }
Beispiel #2
0
    /// <summary>
    /// 检测吸附
    /// </summary>
    public void DetectionAdsorb()
    {
        FuzhiUsable();
        for (int i = 0; i < usableJigsawTile.Count; i++)
        {
            for (int j = 0; j < curentJigsawTile.Count; j++)
            {
                int upid    = curentJigsawTile[j].adjacentTile.upTileid;
                int rightid = curentJigsawTile[j].adjacentTile.rightTileid;
                int downid  = curentJigsawTile[j].adjacentTile.downTileid;
                int leftid  = curentJigsawTile[j].adjacentTile.leftTileid;

                if (upid != 0 && upid == usableJigsawTile[i].id)
                {
                    Vector3 targetPos = _thisRect.InverseTransformPoint(usableJigsawTile[i].downPoint.position);
                    Vector3 localPos  = _thisRect.InverseTransformPoint(curentJigsawTile[j].upPoint.position);
                    if ((localPos - targetPos).sqrMagnitude <= 225f)
                    {
                        if (usableJigsawTile[i].parentTile != null && usableJigsawTile[i].parentTile != this)
                        {//如果时合成后的图块
                            usableJigsawTile[i].parentTile.transform.SetParent(this.transform);
                            JigsawTileParent destory = usableJigsawTile[i].parentTile;
                            foreach (var item in usableJigsawTile[i].parentTile.curentJigsawTile)
                            {
                                item.transform.SetParent(this.transform);
                                if (!this.curentJigsawTile.Contains(item))
                                {
                                    this.curentJigsawTile.Add(item);
                                }
                                item.parentTile = this;
                                //setPos
                                item._thisRect.anchoredPosition = curentJigsawTile[j]._thisRect.anchoredPosition + jg.GetTilePos(curentJigsawTile[j].id, item.id);
                            }
                            Destroy(destory.gameObject);
                        }
                        else
                        {
                            usableJigsawTile[i].transform.SetParent(this.transform);
                            usableJigsawTile[i].parentTile = this;
                            usableJigsawTile[i].compound   = true;
                            if (!this.curentJigsawTile.Contains(usableJigsawTile[i]))
                            {
                                this.curentJigsawTile.Add(usableJigsawTile[i]);
                            }
                            usableJigsawTile[i]._thisRect.anchoredPosition = new Vector2(curentJigsawTile[j]._thisRect.anchoredPosition.x, curentJigsawTile[j]._thisRect.anchoredPosition.y + curentJigsawTile[j]._thisRect.sizeDelta.y);
                        }
                        //set
                        curentJigsawTile[j].upPoint                 = null;
                        usableJigsawTile[i].downPoint               = null;
                        curentJigsawTile[j].adjacentTile.upTileid   = 0;
                        usableJigsawTile[i].adjacentTile.downTileid = 0;
                        curentJigsawTile[j].tileTree.upTileid       = usableJigsawTile[i].id;
                        usableJigsawTile[i].tileTree.downTileid     = curentJigsawTile[j].id;
                        if (!usableJigsawTile[i].tilesTree.Contains(curentJigsawTile[j]))
                        {
                            usableJigsawTile[i].tilesTree.Add(curentJigsawTile[j]);
                        }
                        if (!usableJigsawTile[i].tilesTree.Contains(usableJigsawTile[i]))
                        {
                            curentJigsawTile[j].tilesTree.Add(usableJigsawTile[i]);
                        }
                    }
                }
                else if (rightid != 0 && rightid == usableJigsawTile[i].id)
                {
                    Vector3 targetPos = _thisRect.InverseTransformPoint(usableJigsawTile[i].leftPoint.position);
                    Vector3 localPos  = _thisRect.InverseTransformPoint(curentJigsawTile[j].rightPoint.position);
                    if ((localPos - targetPos).sqrMagnitude <= 225f)
                    {
                        if (usableJigsawTile[i].parentTile != null && usableJigsawTile[i].parentTile != this)
                        {//如果时合成后的图块
                            usableJigsawTile[i].parentTile.transform.SetParent(this.transform);
                            JigsawTileParent destory = usableJigsawTile[i].parentTile;
                            foreach (var item in usableJigsawTile[i].parentTile.curentJigsawTile)
                            {
                                item.transform.SetParent(this.transform);
                                if (!this.curentJigsawTile.Contains(item))
                                {
                                    this.curentJigsawTile.Add(item);
                                }
                                item.parentTile = this;
                                //setPos
                                item._thisRect.anchoredPosition = curentJigsawTile[j]._thisRect.anchoredPosition + jg.GetTilePos(curentJigsawTile[j].id, item.id);
                            }
                            Destroy(destory.gameObject);
                        }
                        else
                        {
                            usableJigsawTile[i].transform.SetParent(this.transform);
                            usableJigsawTile[i].parentTile = this;
                            usableJigsawTile[i].compound   = true;
                            if (!this.curentJigsawTile.Contains(usableJigsawTile[i]))
                            {
                                this.curentJigsawTile.Add(usableJigsawTile[i]);
                            }
                            usableJigsawTile[i]._thisRect.anchoredPosition = new Vector2(curentJigsawTile[j]._thisRect.anchoredPosition.x + curentJigsawTile[j]._thisRect.sizeDelta.x, curentJigsawTile[j]._thisRect.anchoredPosition.y);
                        }
                        //set
                        curentJigsawTile[j].rightPoint = null;
                        usableJigsawTile[i].leftPoint  = null;
                        curentJigsawTile[j].adjacentTile.rightTileid = 0;
                        usableJigsawTile[i].adjacentTile.leftTileid  = 0;
                        curentJigsawTile[j].tileTree.rightTileid     = usableJigsawTile[i].id;
                        usableJigsawTile[i].tileTree.leftTileid      = curentJigsawTile[j].id;
                        if (!usableJigsawTile[i].tilesTree.Contains(curentJigsawTile[j]))
                        {
                            usableJigsawTile[i].tilesTree.Add(curentJigsawTile[j]);
                        }
                        if (!usableJigsawTile[i].tilesTree.Contains(usableJigsawTile[i]))
                        {
                            curentJigsawTile[j].tilesTree.Add(usableJigsawTile[i]);
                        }
                    }
                }
                else if (downid != 0 && downid == usableJigsawTile[i].id)
                {
                    Vector3 targetPos = _thisRect.InverseTransformPoint(usableJigsawTile[i].upPoint.position);
                    Vector3 localPos  = _thisRect.InverseTransformPoint(curentJigsawTile[j].downPoint.position);
                    if ((localPos - targetPos).sqrMagnitude <= 225f)
                    {
                        if (usableJigsawTile[i].parentTile != null && usableJigsawTile[i].parentTile != this)
                        {//如果时合成后的图块
                            usableJigsawTile[i].parentTile.transform.SetParent(this.transform);
                            JigsawTileParent destory = usableJigsawTile[i].parentTile;
                            foreach (var item in usableJigsawTile[i].parentTile.curentJigsawTile)
                            {
                                item.transform.SetParent(this.transform);
                                if (!this.curentJigsawTile.Contains(item))
                                {
                                    this.curentJigsawTile.Add(item);
                                }
                                item.parentTile = this;
                                //setPos
                                item._thisRect.anchoredPosition = curentJigsawTile[j]._thisRect.anchoredPosition + jg.GetTilePos(curentJigsawTile[j].id, item.id);
                            }
                            Destroy(destory.gameObject);
                        }
                        else
                        {
                            usableJigsawTile[i].transform.SetParent(this.transform);
                            usableJigsawTile[i].parentTile = this;
                            usableJigsawTile[i].compound   = true;
                            if (!this.curentJigsawTile.Contains(usableJigsawTile[i]))
                            {
                                this.curentJigsawTile.Add(usableJigsawTile[i]);
                            }
                            usableJigsawTile[i]._thisRect.anchoredPosition = new Vector2(curentJigsawTile[j]._thisRect.anchoredPosition.x, curentJigsawTile[j]._thisRect.anchoredPosition.y - curentJigsawTile[j]._thisRect.sizeDelta.y);
                        }
                        //set
                        curentJigsawTile[j].downPoint = null;
                        usableJigsawTile[i].upPoint   = null;
                        curentJigsawTile[j].adjacentTile.downTileid = 0;
                        usableJigsawTile[i].adjacentTile.upTileid   = 0;
                        curentJigsawTile[j].tileTree.downTileid     = usableJigsawTile[i].id;
                        usableJigsawTile[i].tileTree.upTileid       = curentJigsawTile[j].id;
                        if (!usableJigsawTile[i].tilesTree.Contains(curentJigsawTile[j]))
                        {
                            usableJigsawTile[i].tilesTree.Add(curentJigsawTile[j]);
                        }
                        if (!usableJigsawTile[i].tilesTree.Contains(usableJigsawTile[i]))
                        {
                            curentJigsawTile[j].tilesTree.Add(usableJigsawTile[i]);
                        }
                    }
                }
                else if (leftid != 0 && leftid == usableJigsawTile[i].id)
                {
                    Vector3 targetPos = _thisRect.InverseTransformPoint(usableJigsawTile[i].rightPoint.position);
                    Vector3 localPos  = _thisRect.InverseTransformPoint(curentJigsawTile[j].leftPoint.position);
                    if ((localPos - targetPos).sqrMagnitude <= 225f)
                    {
                        if (usableJigsawTile[i].parentTile != null && usableJigsawTile[i].parentTile != this)
                        {//如果时合成后的图块
                            usableJigsawTile[i].parentTile.transform.SetParent(this.transform);
                            JigsawTileParent destory = usableJigsawTile[i].parentTile;
                            foreach (var item in usableJigsawTile[i].parentTile.curentJigsawTile)
                            {
                                item.transform.SetParent(this.transform);
                                if (!this.curentJigsawTile.Contains(item))
                                {
                                    this.curentJigsawTile.Add(item);
                                }
                                item.parentTile = this;
                                //setPos
                                item._thisRect.anchoredPosition = curentJigsawTile[j]._thisRect.anchoredPosition + jg.GetTilePos(curentJigsawTile[j].id, item.id);
                            }
                            Destroy(destory.gameObject);
                        }
                        else
                        {
                            usableJigsawTile[i].transform.SetParent(this.transform);
                            usableJigsawTile[i].parentTile = this;
                            usableJigsawTile[i].compound   = true;
                            if (!this.curentJigsawTile.Contains(usableJigsawTile[i]))
                            {
                                this.curentJigsawTile.Add(usableJigsawTile[i]);
                            }
                            usableJigsawTile[i]._thisRect.anchoredPosition = new Vector2(curentJigsawTile[j]._thisRect.anchoredPosition.x - curentJigsawTile[j]._thisRect.sizeDelta.x, curentJigsawTile[j]._thisRect.anchoredPosition.y);
                        }
                        //set
                        curentJigsawTile[j].leftPoint  = null;
                        usableJigsawTile[i].rightPoint = null;
                        curentJigsawTile[j].adjacentTile.leftTileid  = 0;
                        usableJigsawTile[i].adjacentTile.rightTileid = 0;
                        curentJigsawTile[j].tileTree.leftTileid      = usableJigsawTile[i].id;
                        usableJigsawTile[i].tileTree.rightTileid     = curentJigsawTile[j].id;
                        if (!usableJigsawTile[i].tilesTree.Contains(curentJigsawTile[j]))
                        {
                            usableJigsawTile[i].tilesTree.Add(curentJigsawTile[j]);
                        }
                        if (!usableJigsawTile[i].tilesTree.Contains(usableJigsawTile[i]))
                        {
                            curentJigsawTile[j].tilesTree.Add(usableJigsawTile[i]);
                        }
                    }
                }
            }
        }
    }