Пример #1
0
    public void ShapesFall()
    {
        Debug.Log("Shapes Fall");
        EventManager.StopListening("pieces_disappear_after_match_success", ShapesFall);

        var   shapes  = shapesManager.shapes;
        int   botRow  = shapes.GetLength(0) - 1; // this is the bottom most row (number)
        bool  found   = false;
        float maxTime = 0f;                      // animation time (right now everything is set to a constant amount - all animations are the same length).

        // Checks for shapes to bring down. Starting in the bottom left corner (iterative)
        for (int col = 0; col < shapes.GetLength(1); col++)
        {
            for (int row = botRow; row >= 0; row--)
            {
                Shape s = shapes[row, col];
                if (s == null)
                {
                    found = true;
                    // grab next non null-non empty piece and bring it down.
                    if (row != 0)
                    {
                        Shape s1 = shapes[row - 1, col];
                        if (s1 == null || s1.Shape_Type == Shape.ShapeType.EMPTY)
                        {
                            Debug.Log("piece is empty or null ");
                        }
                        else
                        {
                            //move the piece down
                            shapes[row, col] = shapes[row - 1, col];
                            Vector3 v = shapesManager.GetPositionOfBackgroundPiece(row, col);
                            StartCoroutine(shapes[row, col].AnimatePosition(v, Constants.DEFAULT_SWAP_ANIMATION_DURATION, () => { }));
                            shapes[row - 1, col] = null;
                            float time = Constants.DEFAULT_SWAP_ANIMATION_DURATION;
                            maxTime = Math.Max(time, maxTime);
                        }
                    }

                    shapesManager.SpawnShapes(true);
                }
            }
        }
        StartCoroutine(Utility.instance.WaitForTime_Action(maxTime, (bool _found) =>
        {
            Debug.Log("in recurs");
            shapesManager.SpawnShapes();
            if (_found)
            {
                ShapesFall();
            }
            else
            {
                Debug.Log("Done do something else");
                CheckWholeBoard();
            }
        }, found));



        /*
         * float maxTime = 0f;
         * bool found = false;
         * int count = 0;
         * for (int row = shapes.GetLength(0) - 2; row >= 0; row--)
         * {
         *  for (int col = 0; col < shapes.GetLength(1); col++)
         *  {
         *      if (shapes[row + 1, col] == null)
         *      {
         *          count++;
         *          found = true;
         *          //shapesManager.MovePiecePosition(row, col, row + 1, col);
         *
         *          Shape s = shapes[row + 1, col] = shapes[row, col];
         *
         *          if (s != null)
         *          {
         *              float time = Constants.DEFAULT_SWAP_ANIMATION_DURATION;
         *              maxTime = Math.Max(time, maxTime);
         *              Vector3 v = shapesManager.GetPositionOfBackgroundPiece(row + 1, col);
         *              StartCoroutine(s.AnimatePosition(v, Constants.DEFAULT_SWAP_ANIMATION_DURATION, () => { }));
         *          }
         *          shapes[row, col] = null;
         *          shapesManager.SpawnShapes();
         *      }
         *  }
         * }
         * Debug.Log("Count: " + count);
         * StartCoroutine(WaitForTime_Action(maxTime, (bool _found) =>
         * {
         *  Debug.Log("in recurs");
         *  shapesManager.SpawnShapes();
         *  if (_found)
         *  {
         *      ShapesFall();
         *  }
         *  else
         *  {
         *      Debug.Log("Done do something else");
         *      CheckWholeBoard();
         *  }
         *
         * }, found));
         */
    }