示例#1
0
    public IEnumerator handleMatches(bool playerTurn = true)
    {
        // wait until other stuff finishes
        while (BattleInfo.gameState != GameState.HandlingMatches)
        {
            ;
        }

        List <Vector2>[,] matchGroupLists = getAllMatches();
        Dictionary <GamePieceType, int> matchedPieces = new Dictionary <GamePieceType, int>();

        // start with 0 pieces of each type matched
        foreach (GamePieceType gptype in Enum.GetValues(typeof(GamePieceType)))
        {
            matchedPieces.Add(gptype, 0);
        }

        bool matchedFervor = false;

        bool extraTurn = false;


        foreach (List <Vector2> matchGrp in matchGroupLists)
        {
            if (matchGrp.Count >= 3)
            {
                // record pieces matched so they can have their match effect
                GamePieceType matchType = triangles [(int)matchGrp [0].x, (int)matchGrp [0].y].GetComponent <TriangleHandler> ().type;

                matchedPieces [matchType] += matchGrp.Count;

                if (matchType == GamePieceType.Fervor)
                {
                    matchedFervor = true;
                }


                // if you match enough pieces, you get an extra turn!
                if (matchGrp.Count >= 4)
                {
                    Vector2 positionOfFirstPiece = RectTransformUtility.WorldToScreenPoint(
                        Camera.main, incenters [(int)matchGrp [0].x, (int)matchGrp [0].y]);

                    extraTurn = true;
                    UIHandler.Instance().spawnRisingText(positionOfFirstPiece, "Extra Turn!", Color.red, floatingDistance: 15,
                                                         lifespan: 1.5f, fontSize: 18, fontStyle: FontStyle.Normal);
                }

                yield return(explodePieces(matchGrp));

                yield return(applyMatchedPieces(matchType, matchedPieces[matchType]));
            }
        }

        // if you got an extra turn, let that register briefly before giving next turn.
        // TODO: probably remove this when game pieces slide into place instead of appearing instantly


        if (!matchedFervor)
        {
            if (BattleInfo.playerWentLast)
            {
                BattleInfo.doPlayerFervorDecay();
            }
            else
            {
                BattleInfo.currentEnemy.doFervorDecay();
            }
        }

        fillBoard();

        // if the board is left with no matches, reset it.
        while (getLegalSwaps().Count == 0)
        {
            reset();
        }

        if (extraTurn)
        {
            yield return(new WaitForSeconds(1.5f));
        }

        // TODO: instead of always switching turns after handling matches, wait for falling pieces
        //       to finish falling, handle their matches, and only switch turns once this process yields
        //       no more matches



        // if the game should be over now (someone's health is 0), do end-game popup
        if (BattleInfo.playerHealth == 0)
        {
            UIHandler.Instance().youLostDialog();
            yield break;
        }
        else if (BattleInfo.currentEnemy.health == 0)
        {
            UIHandler.Instance().youWonDialog();
            yield break;
        }


        // Display "Your turn" or "enemy's turn" for some amount of time first,
        float timeToShowTurnSwitch = 1.5f;

        if (!extraTurn)           // only show if it's the beginning of the turn, not an extra turn
        {
            if (BattleInfo.playerWentLast)
            {
                Vector2 middleScreen = RectTransformUtility.WorldToScreenPoint(Camera.main, Camera.main.transform.position);
                UIHandler.Instance().spawnRisingText(middleScreen, "Enemy Turn", Color.red, floatingDistance: 0,
                                                     lifespan: timeToShowTurnSwitch, fontSize: 70, fontStyle: FontStyle.Normal);
            }
            else                 //if ((BattleInfo.playerWentLast && extraTurn) || (!BattleInfo.playerWentLast && !extraTurn))
            {
                Vector2 middleScreen = RectTransformUtility.WorldToScreenPoint(Camera.main, Camera.main.transform.position);
                UIHandler.Instance().spawnRisingText(middleScreen, "Your Turn", Color.cyan, floatingDistance: 0,
                                                     lifespan: timeToShowTurnSwitch, fontSize: 70, fontStyle: FontStyle.Normal);
            }
        }
        yield return(new WaitForSeconds(timeToShowTurnSwitch));

        // then actually change turns
        if ((BattleInfo.playerWentLast && !extraTurn) || (!BattleInfo.playerWentLast && extraTurn))
        {
            // wait a little longer, to feel like the enemy is "thinking"...
            yield return(new WaitForSeconds(timeToShowTurnSwitch));

            BattleInfo.gameState = GameState.EnemyTurn;
        }
        else             // if ((BattleInfo.playerWentLast && extraTurn) || (!BattleInfo.playerWentLast && !extraTurn))
        {
            BattleInfo.gameState = GameState.PlayerTurn;
        }



        yield return(null);
    }