예제 #1
0
    /// <summary>
    /// Performs game update and touch and mouse input.
    /// </summary>
    void Update()
    {
        // Back button.
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }


        // Wait for all animations to finish - TODO optimize.
        if (!ItemsIdle())
        {
            return;
        }

        // ------ Game Update -------
        switch (gameState)
        {
        // Check if move has been valid.
        case GameState.MoveCheck:
            if (match3.CheckMove(selObj1.X, selObj1.Y, selObj2.X, selObj2.Y))
            {
                // Move has been valid, swap objects (animation has only changed real positions).
                SwapObjects(selObj1, selObj2, false);
                possibleHighlightObj.renderer.enabled = false;
                gameState = GameState.MatchOrPossibleGet;
            }
            else
            {
                // Move hasn't been valid, animate items back.
                selObj1.AnimateMove(selObj2.transform.position, swapAnimSpeed);
                selObj2.AnimateMove(selObj1.transform.position, swapAnimSpeed);
                gameState = GameState.Idle;
            }

            // Clear selection.
            selObj1 = null;
            selObj2 = null;

            break;


        // Get matches or possible moves (and check for game over).
        case GameState.MatchOrPossibleGet:
            // Get matches.
            ArrayList matches = match3.GetMatches();
            if (matches.Count == 0)
            {
                // No matches found, check for possible moves.
                ArrayList possibleMoves = match3.GetPossibleMoves();
                if (possibleMoves.Count > 0)
                {
                    // There are some possible moves, highlight one of them (by random).
                    Match3.Point p = possibleMoves[Random.Range(0, possibleMoves.Count)] as Match3.Point;
                    ShowPossibleMove(p.x, p.y);
                    gameState = GameState.Idle;
                }
                else
                {
                    // No possible moves found, game is over.
                    gameState = GameState.GameOver;
                }
            }
            else
            {
                // Matches found.
                foreach (ArrayList match in matches)
                {
                    foreach (Match3.Point p in match)
                    {
                        // Animate destroy of matched items.
                        boardAll[p.x, p.y].AnimateDestroy(destroyAnimSpeed);
                    }
                }
                gameState = GameState.ItemGenerateAndDrop;
            }
            break;


        // Generate new items and perform drop.
        case GameState.ItemGenerateAndDrop:
            // Get drop swaps of existing items.
            ArrayList dropSwaps = match3.GetDropSwaps();

            // Begin drop animations and swap items data.
            foreach (Match3.Point[] p in dropSwaps)
            {
                BoardItem bi1 = boardAll[p[0].x, p[0].y];
                BoardItem bi2 = boardAll[p[1].x, p[1].y];

                bi1.AnimateMove(GetPosOnBoard(p[1].x, p[1].y), dropAnimSpeed);
                SwapObjects(bi1, bi2, false);     // Swap without positions.
            }

            // Get positions of destroyed items (already dropped) and generate new ones.
            ArrayList destroyed = match3.GetDestroyedItemsAndGenerateNew();
            int       lastX = -1, firstDestroyedY = -1;

            // Destroy old items, create new ones (above the board) and begin drop animations of them.
            foreach (Match3.Point p in destroyed)
            {
                Destroy(boardAll[p.x, p.y].gameObject);

                // Get y-coord where to put new items above the board (array is sorted,
                // going through every column from left to right, bottom to top.
                if (p.x != lastX)
                {
                    // New column.
                    firstDestroyedY = p.y;
                    lastX           = p.x;
                }

                // Create item with adjusted y-position.
                BoardItem clone = CreateBoardItem(p.x, p.y, 0, boardSizeY - firstDestroyedY);
                // Begin move animation to the right position.
                clone.AnimateMove(GetPosOnBoard(p.x, p.y), dropAnimSpeed);
            }

            // Check for the new matches.
            gameState = GameState.MatchOrPossibleGet;

            break;
        }


        // ----------- Input ----------
        // Touch input.
        if (Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch(0);

            switch (touch.phase)
            {
            case TouchPhase.Began:
                MyMouseDown(touch.position);
                break;

            case TouchPhase.Moved:
                MyMouseMove(touch.position);
                break;

            case TouchPhase.Ended:
            case TouchPhase.Canceled:
                MyMouseUp(touch.position);
                break;
            }
        }
        // Mouse input.
        else
        {
            if (Input.GetMouseButtonDown(0))
            {
                MyMouseDown(Input.mousePosition);
            }
            else if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
            {
                MyMouseMove(Input.mousePosition);
            }
            else if (Input.GetMouseButtonUp(0))
            {
                MyMouseUp(Input.mousePosition);
            }
        }
    }