コード例 #1
0
    public void FlipPieces(Point one, Point two, bool main)
    {
        combo.combo_count = 0;

        if (getValueAtPoint(one) < 0)
        {
            return;
        }
        Node       nodeOne  = getNodeAtPoint(one);
        Node       nodeTwo  = getNodeAtPoint(two);
        NodePieces pieceOne = nodeOne.getPiece();
        NodePieces pieceTwo = nodeTwo.getPiece();

        if (getValueAtPoint(two) > 0)
        {
            nodeOne.SetPiece(pieceTwo);
            nodeTwo.SetPiece(pieceOne);
            if (main)
            {
                flipped.Add(new FlippedPieces(pieceOne, pieceTwo));
            }

            update.Add(pieceOne);
            update.Add(pieceTwo);
        }
        else
        {
            ResetPiece(pieceOne);
        }
    }
コード例 #2
0
 public void MovePiece(NodePieces piece)
 {
     if (moving != null)
     {
         return;
     }
     moving     = piece;
     mouseStart = Input.mousePosition;
 }
コード例 #3
0
 public void SetPiece(NodePieces p)
 {
     piece = p;
     value = (piece == null) ? 0 : piece.value;
     if (piece == null)
     {
         return;
     }
     piece.SetIndex(index);
 }
コード例 #4
0
    private void removeFlipped(NodePieces p)
    {
        FlippedPieces flip = null;

        for (int i = 0; i < flipped.Count; i++)
        {
            if (flipped[i].getOtherPiece(p) != null)
            {
                flip = flipped[i];
                break;
            }
        }
    }
コード例 #5
0
    private FlippedPieces getFlipped(NodePieces p)
    {
        FlippedPieces flip = null;

        for (int i = 0; i < flipped.Count; i++)
        {
            if (flipped[i].getOtherPiece(p) != null)
            {
                flip = flipped[i];
                break;
            }
        }
        return(flip);
    }
コード例 #6
0
 public NodePieces getOtherPiece(NodePieces p)
 {
     if (p == one)
     {
         return(two);
     }
     else if (p == two)
     {
         return(one);
     }
     else
     {
         return(null);
     }
 }
コード例 #7
0
    public void DropPiece()
    {
        if (moving == null)
        {
            return;
        }
        Debug.Log("Dropped");

        if (!newIndex.Equals(moving.index))
        {
            game.FlipPieces(moving.index, newIndex, true);
        }
        else
        {
            game.ResetPiece(moving);
        }
        moving = null;
    }
コード例 #8
0
 private void InstantiateBoard() //Instantiates board
 {
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             Node node = getNodeAtPoint(new Point(x, y));
             int  val  = board[x, y].value;
             if (val <= 0)
             {
                 continue;
             }
             GameObject    p    = Instantiate(nodePiece, gameBoard);
             RectTransform rect = p.GetComponent <RectTransform>();
             rect.anchoredPosition = new Vector2(32 + (78 * x), -32 - (80 * y));
             NodePieces piece = p.GetComponent <NodePieces>();
             piece.Initialize(val, new Point(x, y), pieces[val - 1]);
             node.SetPiece(piece);
         }
     }
 }
コード例 #9
0
 public FlippedPieces(NodePieces o, NodePieces t)
 {
     one = o;
     two = t;
 }
コード例 #10
0
    // update is called once per frame
    private void Update()
    {
        List <NodePieces> finishedUpdating = new List <NodePieces>();

        for (int i = 0; i < update.Count; i++)
        {
            NodePieces piece = update[i];
            if (!piece.UpdatePiece())
            {
                finishedUpdating.Add(piece);
            }
        }

        for (int i = 0; i < finishedUpdating.Count; i++)
        {
            NodePieces    piece        = finishedUpdating[i];
            FlippedPieces flip         = getFlipped(piece);
            NodePieces    flippedPiece = null;
            int           x            = (int)piece.index.x;
            fills[x] = Mathf.Clamp(fills[x] - 1, 0, width);
            List <Point> connected  = isConnected(piece.index, true);
            bool         wasFlipped = (flip != null);
            if (wasFlipped)
            {
                flippedPiece = flip.getOtherPiece(piece);
                AddPoints(ref connected, isConnected(flippedPiece.index, true));
            }
            if (connected.Count == 0)                                   //If we didn't make a match
            {
                if (wasFlipped)                                         // If we flipped
                {
                    FlipPieces(piece.index, flippedPiece.index, false); //Flip back
                }
            }
            else //Being called if we made a match
            {
                combo.combo_count += 1;

                foreach (Point pnt in connected) //Remove the node pieces when connected
                {
                    KillPiece(pnt);
                    Node       node      = getNodeAtPoint(pnt);
                    NodePieces nodePiece = node.getPiece();
                    if (node.value == 3)
                    {
                        enemy.TakeDamage(5);
                    }
                    if (nodePiece != null)
                    {
                        nodePiece.gameObject.SetActive(false);
                        deadPieces.Add(nodePiece);
                    }

                    node.SetPiece(null);
                }

                BoardGravity();
            }
            flipped.Remove(flip); //remove the flip after update

            update.Remove(piece);
        }
    }
コード例 #11
0
 public void ResetPiece(NodePieces piece) //resets a piece at old position if invalid move
 {
     piece.ResetPosition();
     update.Add(piece);
 }
コード例 #12
0
    private void BoardGravity()
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = (height - 1); y >= 0; y--)
            {
                Point p    = new Point(x, y);
                Node  node = getNodeAtPoint(p);
                int   val  = getValueAtPoint(p);
                if (val != 0)
                {
                    continue;           // if not a hole or 0, do nothing
                }
                for (int ny = (y - 1); ny >= -1; ny--)
                {
                    Point next    = new Point(x, ny);
                    int   nextVal = getValueAtPoint(next);
                    if (nextVal == 0)
                    {
                        continue;
                    }
                    if (nextVal != -1) // if we did not hit an end, but its not 0 use this to fill current hole
                    {
                        Node       got   = getNodeAtPoint(next);
                        NodePieces piece = got.getPiece();

                        //Set hole
                        node.SetPiece(piece);
                        update.Add(piece);

                        //replace hole
                        got.SetPiece(null);
                    }
                    else //hit an end
                    {
                        //fill hole
                        int        newVal = fillPiece();
                        NodePieces piece;
                        Point      fallPoint = new Point(x, (-1 - fills[x]));
                        if (deadPieces.Count > 0)
                        {
                            NodePieces revived = deadPieces[0];
                            revived.gameObject.SetActive(true);
                            piece = revived;
                            deadPieces.RemoveAt(0);
                        }
                        else
                        {
                            GameObject obj = Instantiate(nodePiece, gameBoard);
                            NodePieces n   = obj.GetComponent <NodePieces>();
                            piece = n;
                        }
                        piece.Initialize(newVal, p, pieces[newVal - 1]);
                        piece.rect.anchoredPosition = GetPositionFromPoint(fallPoint);
                        Node hole = getNodeAtPoint(p);
                        hole.SetPiece(piece);
                        ResetPiece(piece);
                        fills[x]++;
                    }
                    break;
                }
            }
        }
    }