Exemplo n.º 1
0
    void Update()
    {
        Debug.Log(_gameActive);
        if(_gameActive)
        {
                // Debug.Log("Match score = " + Constants.MatchScore * _multiplier + " + " + Constants.SubsequentMatchScore + "*" + _multiplier);
            if (_state == Enums.GameState.None)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
                    if (hit.collider != null)
                    {
                        _primaryHitGameObject = hit.collider.gameObject;
                        _state = Enums.GameState.SelectionStarted;
                    }
                }
            }
            else if (_state == Enums.GameState.SelectionStarted)
            {
                if (Input.GetMouseButton(0))
                {
                    // If user is dragging.
                    // Set hit to a raycast where your mouse pointer is.
                    RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
                    // if the collider hit by your raycast from the mousepointer is a thing, and it isn't equal to the original collider you hit,
                    if (hit.collider != null && _primaryHitGameObject != hit.collider.gameObject)
                    {
                        // No need to check for hints if user has found something.
                        StopCheckForPotentialMatches();

                        if (
                            !Utilities.AreVerticalOrHorizontalNeighbours(_primaryHitGameObject.GetComponent<Action>(),
                                hit.collider.gameObject.GetComponent<Action>()))
                        {
                            _state = Enums.GameState.None;
                        }
                        else
                        {
                            _state = Enums.GameState.Animating;
                            FixSortingLayer(_primaryHitGameObject, hit.collider.gameObject);
                            StartCoroutine(FindMatchesAndCollapse(hit));
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
    private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2)
    {
        float tempIconTypeValue;
        var hitGO2 = hit2.collider.gameObject;
        Actions.Swap(_primaryHitGameObject, hitGO2);

        // Move the swapped objects.
        _primaryHitGameObject.transform.DOMove(hitGO2.transform.position, Constants.AnimationDuration);
        hitGO2.transform.DOMove(_primaryHitGameObject.transform.position, Constants.AnimationDuration);
        yield return new WaitForSeconds(Constants.AnimationDuration);

        var hitGOMatchesInfo = Actions.GetMatches(_primaryHitGameObject);
        var hitGO2MatchesInfo = Actions.GetMatches(hitGO2);

        var totalMatches = hitGOMatchesInfo.MatchedActions.Union(hitGO2MatchesInfo.MatchedActions).Distinct();

        // If less than the required matches are found, return the tiles.
        if (totalMatches.Count() < Constants.MinMatches)
        {
            _primaryHitGameObject.transform.DOMove(hitGO2.transform.position, Constants.AnimationDuration);
            hitGO2.transform.DOMove(_primaryHitGameObject.transform.position, Constants.AnimationDuration);
            yield return new WaitForSeconds(Constants.AnimationDuration);

            Actions.UndoSwap();;
        }

        int timesRun = 1;
        while (totalMatches.Count() >= Constants.MinMatches)
        {
            IncreaseMultiplier(1);
            IncreaseScore((totalMatches.Count() - 2) * Constants.MatchScore * _multiplier);

            if (timesRun >= 2)
            {
                IncreaseScore(Constants.SubsequentMatchScore * _multiplier);
            }

            foreach (var item in totalMatches)
            {
                // ERROR HERE>...
                switch (hitGO2.GetComponent<Action>().Type)
                {
                    case "book":
                        tempIconTypeValue = PlayerPrefs.GetFloat("Book Value");
                        PlayerPrefs.SetFloat("Book Value", tempIconTypeValue += (float)(0.01 * totalMatches.Count()));
                        break;
                    case "hotchocolate":
                        tempIconTypeValue = PlayerPrefs.GetFloat("Choc Value");
                        PlayerPrefs.SetFloat("Choc Value", tempIconTypeValue += (float)(0.01 * totalMatches.Count()));
                        break;
                    case "dreamcatcher":
                        tempIconTypeValue = PlayerPrefs.GetFloat("Dream Value");
                        PlayerPrefs.SetFloat("Dream Value", tempIconTypeValue += (float)(0.01 * totalMatches.Count()));
                        break;
                    case "candle":
                        tempIconTypeValue = PlayerPrefs.GetFloat("Candle Value");
                        PlayerPrefs.SetFloat("Candle Value", tempIconTypeValue += (float)(0.01 * totalMatches.Count()));
                        break;
                    case "teddy":
                        tempIconTypeValue = PlayerPrefs.GetFloat("Teddy Value");
                        PlayerPrefs.SetFloat("Teddy Value", tempIconTypeValue += (float)(0.01 * totalMatches.Count()));
                        break;
                    case "pillow":
                        tempIconTypeValue = PlayerPrefs.GetFloat("Pillow Value");
                        PlayerPrefs.SetFloat("Pillow Value", tempIconTypeValue += (float)(0.01 * totalMatches.Count()));
                        break;
                    default:
                        tempIconTypeValue = 0.0f;
                        break;
                }
                Actions.Remove(item);
                RemoveFromScene(item);
            }

            var columns = totalMatches.Select(go => go.GetComponent<Action>().Column).Distinct();

            IEnumerable<int> columnsWithMissingActions = columns as int[] ?? columns.ToArray();
            var collapsedActionInfo = Actions.Collapse(columnsWithMissingActions);

            var newActionInfo = CreateNewActionInSpecificColumns(columnsWithMissingActions);

            int maxDistance = Mathf.Max(collapsedActionInfo.MaxDistance, newActionInfo.MaxDistance);

            MoveAndAnimate(newActionInfo.AlteredActions, maxDistance);
            MoveAndAnimate(collapsedActionInfo.AlteredActions, maxDistance);

            // Wait for the above animations to finish.
            yield return new WaitForSeconds(Constants.MoveAnimationMinDuration * maxDistance);

            totalMatches =
                Actions.GetMatches(collapsedActionInfo.AlteredActions)
                    .Union(Actions.GetMatches(newActionInfo.AlteredActions))
                    .Distinct();

            timesRun++;
        }

        _state = Enums.GameState.None;
        Debug.Log("Checking for matches");
        StartCheckForPotentialMatches();
    }
Exemplo n.º 3
0
 private void ChangeGameStateTo(Enums.GameState newState)
 {
     gameState = newState;
     SetGameState();
 }
Exemplo n.º 4
0
 private void Awake()
 {
     gameState = Enums.GameState.MainMenu;
 }
Exemplo n.º 5
0
 private void OnSetGameState(Enums.GameState state)
 {
     gameState = state;
 }