Esempio n. 1
0
    public void AfterMoving()
    {
        transform.parent = PuzzlePieceGroup.Instance.transform;

        // 取得所在Cell
        int x, y;
        var group = PuzzlePieceGroup.Instance;

        group.getAlignCell(transform.localPosition, out x, out y);

        // (1)pos重新對齊Cell
        group.snapPieceToCell(this, x, y);

        // (2)更新位在那一個Bucket
        group.injectToBucket(this, x, y);

        // (3)設為Layer
        if (GetLayerIndex() == Tool.NullIndex)
        {
            LayerMananger.GetInstance().add(this);
        }

        // (4)找出可以相連的Layer
        findConnectLayerAndMerge(x, y);
    }
Esempio n. 2
0
    /* 合併相關 */
    void findConnectLayerAndMerge(int x, int y)
    {
        var group = PuzzlePieceGroup.Instance;
        var set   = new HashSet <IPuzzleLayer>();

        foreach (var p in pieces)
        {
            if (p != pieceForAlign)
            {
                var targetX = p.xIndexInFull + bucketOffsetX;
                var targetY = p.yIndexInFull + bucketOffsetY;

                if (group.IsValidIndex(targetX, targetY))
                {
                    p.findConnectLayer(targetX, targetY, set);
                }
            }
            else
            {
                p.findConnectLayer(x, y, set);
            }
        }

        // 沒有找到任何相鄰Layer
        if (set.Count == 0)
        {
            LayerMananger.GetInstance().refreshLayerDepth();
            return;
        }

        //Merge Layer
        set.Add(this);//把自己也加進去
        LayerMananger.GetInstance().merge(set, group);
    }
Esempio n. 3
0
    public static LayerMananger GetInstance()
    {
        if (instance == null)
        {
            instance = new LayerMananger();
        }

        return(instance);
    }
Esempio n. 4
0
    public void BeforeMoving()
    {
        // 從Bucket裡清除
        PuzzlePieceGroup.Instance.removeFromBucket(this);

        // 放到最上面,並從Layer移除
        if (layerIndex != Tool.NullIndex)
        {
            var instance = LayerMananger.GetInstance();
            instance.moveToTop(this);
            instance.remove(this);
        }
    }
Esempio n. 5
0
    /* 合併相關 */
    void findConnectLayerAndMerge(int x, int y)
    {
        var set = new HashSet <IPuzzleLayer>();

        findConnectLayer(x, y, set);

        // 沒有找到任何相鄰Layer
        if (set.Count == 0)
        {
            LayerMananger.GetInstance().refreshLayerDepth();
            return;
        }

        // Merge Layer
        set.Add(this); // 把自己也加進去
        LayerMananger.GetInstance().merge(set, PuzzlePieceGroup.Instance);
    }
Esempio n. 6
0
    public void merge(HashSet <IPuzzleLayer> set, PuzzlePieceGroup group)
    {
        var layerManager = LayerMananger.GetInstance();
        var layers       = new List <IPuzzleLayer>(set);

        // print("before sort");
        // foreach (var e in layers)
        //     print(e.GetLayerIndex());

        // 從depth小排到大
        layers.Sort((a, b) =>
        {
            return(a.GetLayerIndex() - b.GetLayerIndex());
        });

        // print("after sort");
        // foreach (var e in layers)
        //     print(e.GetLayerIndex());

        // 取出含有最多piece的Layer,把所有piece都給它
        var theChosenOne = layers[0];

        // 全部set都只有1塊piece
        if (theChosenOne.GetPiecesCount() == 1)
        {
            var p = theChosenOne as PuzzlePiece;

            // 建立connectedSet,全部piece都加進來
            var cs = group.createConnectedSet(p);
            for (var i = layers.Count - 1; i >= 0; --i) //從最上層開始
            {
                var L = layers[i];
                cs.add(L as PuzzlePiece);
                layerManager.remove(L);
            }

            layerManager.add(cs);
            return;
        }

        var nowCS = theChosenOne as ConnectedSet;

        // 把其他piece都加進來
        for (var i = layers.Count - 1; i >= 1; --i) //從最上層開始
        {
            var L = layers[i];

            if (L.GetPiecesCount() == 1)
            {
                nowCS.add(L as PuzzlePiece);
                layerManager.remove(L);
            }
            else
            {
                var cs = L as ConnectedSet;
                nowCS.add(cs);
                layerManager.remove(L);
                Object.Destroy(cs.gameObject);//刪除connectedSet
            }
        }
        layerManager.update(theChosenOne);
    }