コード例 #1
0
    void Update()
    {
        List <NodePiece> finishedUpdating = new List <NodePiece>();

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

        for (int i = 0; i < finishedUpdating.Count; i++)
        {
            NodePiece     piece        = finishedUpdating[i];
            FlippedPieces flip         = getFlipped(piece);
            NodePiece     flippedPiece = null;

            int x = 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 (wasFlipped)
                {
                    FlipPieces(piece.index, flippedPiece.index, false);
                }
            }
            else
            {
                if (connected.Count != 0 && connected.Count != 4)
                {
                    List <Point> killlist  = new List <Point>();
                    List <Point> bonuslist = new List <Point>();
                    foreach (Point p in connected)
                    {
                        killlist.Add(p);
                        Node      node      = GetNodeAtPoint(p);
                        NodePiece nodePiece = node.GetPiece();

                        if (nodePiece.isBonus())
                        {
                            Bonus.AddBonusPoints(height, width, nodePiece.BonusValue(), p, killlist, bonuslist);
                        }
                    }

                    foreach (Point p in killlist)
                    {
                        KillPiece(p);
                        Node      node      = GetNodeAtPoint(p);
                        NodePiece nodePiece = node.GetPiece();


                        if (nodePiece != null)
                        {
                            nodePiece.gameObject.SetActive(false);
                            dead.Add(nodePiece);
                        }
                        node.SetPiece(null);
                    }
                }

                if (connected.Count == 4)
                {
                    Node      bnode  = GetNodeAtPoint(connected[0]);
                    NodePiece bpiece = bnode.GetPiece();
                    bnode.SetPiece(bpiece);
                    bpiece.InitializeBonus(Random.Range(1, 4), bonuses);

                    for (int j = 1; j < connected.Count; j++)
                    {
                        KillPiece(connected[j]);
                        Node      node      = GetNodeAtPoint(connected[j]);
                        NodePiece nodePiece = node.GetPiece();
                        node.SetPiece(nodePiece);

                        if (nodePiece != null)
                        {
                            nodePiece.gameObject.SetActive(false);
                            dead.Add(nodePiece);
                        }
                        node.SetPiece(null);
                    }
                }
                ApplyGravityToBoard();
            }

            flipped.Remove(flip);
            update.Remove(piece);
        }
    }