Exemplo n.º 1
0
    private void DropDrag()
    {
        if (SelectedChuzzles.Any())
        {
            //drop shining
            foreach (var chuzzle in Gamefield.Level.ActiveChuzzles)
            {
                chuzzle.Shine = false;
                chuzzle.Teleportable.Hide();
            }

            //move all tiles to new real coordinates
            foreach (var chuzzle in SelectedChuzzles)
            {
                chuzzle.Real = Gamefield.Level.GetCellAt(GamefieldUtility.ToRealCoordinates(chuzzle), false);
            }

            foreach (var c in Gamefield.Level.Chuzzles)
            {
                c.MoveTo = c.Real;
            }

            foreach (var chuzzle in Gamefield.Level.Chuzzles)
            {
                chuzzle.AnimateMoveTo(chuzzle.MoveTo.Position, 0.1f);
            }

            CheckAnimationCompleted();
        }
    }
Exemplo n.º 2
0
    private static Cell GetLeftCell(List <Cell> activeCells, Cell targetCell, Chuzzle c)
    {
        if (targetCell == null)
        {
            targetCell = GamefieldUtility.CellAt(activeCells, activeCells.Max(x => x.x), c.Current.y);
            if (targetCell.Type == CellTypes.Block)
            {
                targetCell = targetCell.GetLeftWithType();
            }
        }
        else
        {
            //if block
            targetCell = targetCell.GetLeftWithType();

            if (targetCell == null)
            {
                targetCell = GamefieldUtility.CellAt(activeCells, activeCells.Max(x => x.x),
                                                     c.Current.y);
                if (targetCell.Type == CellTypes.Block)
                {
                    targetCell = targetCell.GetLeftWithType();
                }
            }
        }
        return(targetCell);
    }
Exemplo n.º 3
0
    public void OnAnimationFinished(Chuzzle chuzzle)
    {
        chuzzle.Real = chuzzle.Current = chuzzle.MoveTo;

        chuzzle.AnimationFinished -= OnAnimationFinished;
        AnimatedChuzzles.Remove(chuzzle);

        if (isAlreadyChangedState)
        {
            Debug.LogWarning("Finished in CRNC state ");
        }

        if (!AnimatedChuzzles.Any() && !isAlreadyChangedState)
        {
            Gamefield.Level.UpdateActive();

            var combinations = GamefieldUtility.FindCombinations(Gamefield.Level.ActiveChuzzles);
            if (combinations.Count > 0)
            {
                Gamefield.SwitchStateTo(Gamefield.CheckSpecialState);
            }
            else
            {
                if (!Gamefield.GameMode.IsWin && !Gamefield.GameMode.IsGameOver)
                {
                    Gamefield.SwitchStateTo(Gamefield.FieldState);
                }
                else
                {
                    Gamefield.GameMode.Check();
                }
            }
            isAlreadyChangedState = true;
        }
    }
Exemplo n.º 4
0
        public void Show()
        {
            gameObject.SetActive(true);

            IntVector2 targetPosition;
            Chuzzle    arrowChuzzle;

            GamefieldUtility.Tip(gamefield.Level.ActiveChuzzles, out targetPosition, out arrowChuzzle);

            fromFingerPosition = arrowChuzzle.Current.IntVector2Position;
            toFingerPosition   = targetPosition;

            var fromPosition = GamefieldUtility.ConvertXYToPosition((int)fromFingerPosition.x, (int)fromFingerPosition.y, Chuzzle.Scale);

            finger.transform.position = fromPosition;
            finger.Values["position"] = GamefieldUtility.ConvertXYToPosition((int)toFingerPosition.x, (int)toFingerPosition.y, Chuzzle.Scale);
            finger.Play();

            Tutorial.instance.targetCell      = gamefield.Level.GetCellAt(targetPosition);
            Tutorial.instance.takeableChuzzle = gamefield.Level.At(fromFingerPosition.x, fromFingerPosition.y);
            gamefield.TileDestroyed          += OnTileDestroyed;

            tutorialCloud.SetText("Drag to destroy");
            tutorialCloud.SetPosition(Camera.main.WorldToScreenPoint(fromPosition + Vector3.up * 0.5f));
            tutorialCloud.Show();
        }
Exemplo n.º 5
0
 public Cell(int x, int y, CellTypes type = CellTypes.Usual)
 {
     this.x    = x;
     this.y    = y;
     this.Type = type;
     Position  = GamefieldUtility.ConvertXYToPosition(x, y, Chuzzle.Scale);
 }
Exemplo n.º 6
0
    public bool CheckForSpecial(List <List <Chuzzle> > combinations)
    {
        var isNewSpecial = false;

        foreach (var comb in combinations)
        {
            //if any tile is powerup - then don't check for new bonuses
            //or any tile has counter
            if (comb.Any(x => !(GamefieldUtility.IsUsual(x))))
            {
                continue;
            }

            if (comb.Count == 4)
            {
                isNewSpecial = CreateLine(comb);
            }
            else
            {
                if (comb.Count >= 5)
                {
                    isNewSpecial = CreateBomb(comb);
                }
            }
        }

        return(isNewSpecial);
    }
Exemplo n.º 7
0
    private static Cell GetTopCell(List <Cell> activeCells, Cell targetCell, Chuzzle c)
    {
        if (targetCell == null || targetCell.IsTemporary)
        {
            targetCell = GamefieldUtility.CellAt(activeCells, c.Current.x,
                                                 activeCells.Where(x => !x.IsTemporary).Min(x => x.y));
            if (targetCell.Type == CellTypes.Block)
            {
                targetCell = targetCell.GetTopWithType();
            }
        }
        else
        {
            targetCell = targetCell.GetTopWithType();

            if (targetCell == null)
            {
                targetCell = GamefieldUtility.CellAt(activeCells, c.Current.x,
                                                     activeCells.Where(x => !x.IsTemporary).Min(x => x.y));
                if (targetCell.Type == CellTypes.Block)
                {
                    targetCell = targetCell.GetTopWithType();
                }
            }
        }
        return(targetCell);
    }
Exemplo n.º 8
0
    public Cell GetCellAt(int x, int y, bool createIfNotFound = true)
    {
        var cell = GamefieldUtility.CellAt(Cells, x, y);

        if (cell == null && createIfNotFound)
        {
            var newCell = new Cell(x, y);
            AddCell(x, y, newCell);
            return(newCell);
        }
        return(cell);
    }
Exemplo n.º 9
0
    public override void OnEnter()
    {
        AnimatedChuzzles.Clear();
        Chuzzle.DropEventHandlers();
        Chuzzle.AnimationStarted += OnAnimationStarted;

        var combinations = GamefieldUtility.FindCombinations(Gamefield.Level.ActiveChuzzles);

        if (!CheckForSpecial(combinations))
        {
            Gamefield.SwitchStateTo(Gamefield.WinRemoveCombinationState);
        }
    }
Exemplo n.º 10
0
 private void RemoveColorFromPossible(Cell cell, List <GameObject> possiblePrefabs)
 {
     if (cell != null)
     {
         Chuzzle chuzzle = GamefieldUtility.GetChuzzleInCell(cell, Gamefield.Level.Chuzzles);
         if (chuzzle == null)
         {
             return;
         }
         GameObject possible = possiblePrefabs.FirstOrDefault(x => x.GetComponent <Chuzzle>().Color == chuzzle.Color);
         if (possible != null)
         {
             possiblePrefabs.Remove(possible);
         }
     }
 }
Exemplo n.º 11
0
    public override void OnEnter()
    {
        AnimatedChuzzles.Clear();
        Chuzzle.DropEventHandlers();
        Chuzzle.AnimationStarted += OnAnimationStarted;

        var anyCombination = GamefieldUtility.FindOnlyOneCombination(Gamefield.Level.ActiveChuzzles);

        if (anyCombination.Any())
        {
            StartCoroutine(RemoveCombinations());
        }
        else
        {
            Gamefield.SwitchStateTo(Gamefield.FieldState);
        }
    }
Exemplo n.º 12
0
    public void OnBomBomHided()
    {
        var newPowerUps   = new List <Chuzzle>();
        var usualChuzzles = Gamefield.Level.Chuzzles.Where(ch => !GamefieldUtility.IsPowerUp(ch)).ToList();

        for (var i = 0; i < Gamefield.GameMode.Turns; i++)
        {
            var newPowerUp = usualChuzzles[UnityEngine.Random.Range(0, usualChuzzles.Count())];
            newPowerUps.Add(newPowerUp);
            usualChuzzles.Remove(newPowerUp);
            if (!usualChuzzles.Any())
            {
                break;
            }
        }
        Gamefield.GameMode.Turns = 0;
        StartCoroutine(CreateNewPowerUps(newPowerUps.ToList()));
    }
Exemplo n.º 13
0
    private void CheckPossibleCombinations()
    {
        _targetPosition = null;
        _arrowChuzzle   = null;
        var numberOfTries = 0;

        do
        {
            if (GamefieldUtility.Repaint(numberOfTries))
            {
                break;
            }

            _possibleCombination = GamefieldUtility.Tip(Gamefield.Level.ActiveChuzzles, out _targetPosition,
                                                        out _arrowChuzzle);
            Debug.Log(string.Format("Tip. From: {0} To: {1}", _arrowChuzzle, _targetPosition));
            numberOfTries++;
        }while (!_possibleCombination.Any());
    }
Exemplo n.º 14
0
    public void CheckCombinations()
    {
        var combinations = GamefieldUtility.FindCombinations(Gamefield.Level.ActiveChuzzles);

        if (combinations.Any() && (!Tutorial.isActive || (Tutorial.isActive && CurrentChuzzle != null && Tutorial.instance.IsTargetCell(CurrentChuzzle.Real))))
        {
            foreach (var c in Gamefield.Level.Chuzzles)
            {
                c.MoveTo = c.Current = c.Real;
            }
            Gamefield.SwitchStateTo(Gamefield.CheckSpecialState);
            Reset();
        }
        else
        {
            if (CurrentChuzzle != null)
            {
                StartReturn();
            }
        }
    }
    public void OnAnimationFinished(Chuzzle chuzzle)
    {
        chuzzle.Real = chuzzle.Current = chuzzle.MoveTo;

        chuzzle.AnimationFinished -= OnAnimationFinished;
        AnimatedChuzzles.Remove(chuzzle);

        if (!AnimatedChuzzles.Any())
        {
            Gamefield.Level.UpdateActive();

            var combinations = GamefieldUtility.FindCombinations(Gamefield.Level.ActiveChuzzles);
            if (combinations.Count > 0)
            {
                Gamefield.SwitchStateTo(Gamefield.WinCheckSpecialState);
            }
            else
            {
                Gamefield.SwitchStateTo(Gamefield.WinRemoveCombinationState);
            }
        }
    }
Exemplo n.º 16
0
    private IEnumerator RemoveCombinations()
    {
        var powerUpCombination = GamefieldUtility.FindOnlyOneCombinationWithCondition(Gamefield.Chuzzles,
                                                                                      GamefieldUtility.IsPowerUp);

        //if has any powerups
        if (powerUpCombination.Any())
        {
            //destroy step by step
            PowerUpDestroyManager.Instance.Destroy(powerUpCombination);

            if (!AnimatedChuzzles.Any())
            {
                Gamefield.SwitchStateTo(Gamefield.CreateNewChuzzlesState);
            }
        }
        else
        {
            var combinations = GamefieldUtility.FindCombinations(Gamefield.Chuzzles);
            //remove combinations
            foreach (var combination in combinations)
            {
                Gamefield.InvokeCombinationDestroyed(combination);

                foreach (var chuzzle in combination)
                {
                    chuzzle.Destroy(true);
                }

                if (!AnimatedChuzzles.Any())
                {
                    Gamefield.SwitchStateTo(Gamefield.CreateNewChuzzlesState);
                }
                yield return(new WaitForSeconds(0.05f));
            }
        }

        yield return(new WaitForEndOfFrame());
    }
Exemplo n.º 17
0
    public override void OnEnter()
    {
        AnimatedChuzzles.Clear();
        Chuzzle.DropEventHandlers();
        Chuzzle.AnimationStarted += OnAnimationStarted;

        var powerUpChuzzles = Gamefield.Level.Chuzzles.Where(GamefieldUtility.IsPowerUp).ToArray();

        foreach (var ch in powerUpChuzzles)
        {
            ch.Destroy(true);
        }

        var combinations = GamefieldUtility.FindCombinations(Gamefield.Level.ActiveChuzzles);

        if (combinations.Any())
        {
            RemoveCombinations(combinations);
        }
        else if (!powerUpChuzzles.Any())
        {
            StartCoroutine(GameModeCheck());
        }
    }
Exemplo n.º 18
0
    private void OnTileDestroyed(Chuzzle destroyedChuzzle)
    {
        if (PlaceCoordinates.Count == 0 || destroyedChuzzle.IsReplacingOnDeath ||
            !GamefieldUtility.IsOrdinaryDestroyable(destroyedChuzzle))
        {
            return;
        }


        IntVector2 place =
            CurrentPlaceCoordinates.FirstOrDefault(
                x => x.x == destroyedChuzzle.Current.x && x.y == destroyedChuzzle.Current.y);

        if (place != null)
        {
            NGUITools.Destroy(destroyedChuzzle.Current.PlaceSprite);
            CurrentPlaceCoordinates.Remove(place);
        }

        if (CurrentPlaceCoordinates.Count == 0)
        {
            IsWin = true;
        }
    }
Exemplo n.º 19
0
    public void UpdateState(IEnumerable <Chuzzle> draggableChuzzles)
    {
        if (_isReturning)
        {
            return;
        }

        TimeFromTip += Time.deltaTime;
        if (TimeFromTip > 1)
        {
            if (_possibleCombination.Any() && _arrowChuzzle)
            {
                foreach (var chuzzle in _possibleCombination)
                {
                    chuzzle.Shine = true;
                }
                GamefieldUtility.ShowArrow(_arrowChuzzle, _targetPosition, tipArrow);
            }

            TimeFromTip = 0;
        }

        #region Drag

        if (CurrentChuzzle == null && (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)))
        {
            _dragOrigin = Input.mousePosition;
            // Debug.Log("Position: " + _dragOrigin);

            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                //   Debug.Log("is touch drag started");
                _dragOrigin = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y);
            }

            var ray = Camera.main.ScreenPointToRay(_dragOrigin);

            //    Debug.Log("Ray: " + ray);
            Debug.DrawRay(ray.origin, ray.direction * Single.MaxValue);
            var hit = Physics2D.Raycast(ray.origin, ray.direction, Single.MaxValue, Gamefield.ChuzzleMask);
            if (hit.transform != null)
            {
                //  Debug.Log("hit: " + hit.transform.gameObject);
                var wasNull = CurrentChuzzle == null;
                CurrentChuzzle = hit.transform.gameObject.transform.parent.GetComponent <Chuzzle>();
                if (wasNull)
                {
                    _minY = _minX = float.MinValue;
                    _maxX = _maxY = float.MaxValue;
                }
            }

            return;
        }

        // CHECK DRAG STATE (Mouse or Touch)
        if ((!Input.GetMouseButton(0) || Input.GetMouseButtonUp(0)) && 0 == Input.touchCount)
        {
            DropDrag();
            return;
        }

        if (CurrentChuzzle == null)
        {
            return;
        }

        if (CurrentChuzzle && Tutorial.isActive && !Tutorial.instance.CanTakeOnlyThisChuzzle(CurrentChuzzle))
        {
            Reset();
            return;
        }


        if (Input.GetMouseButton(0)) // Get Position Difference between Last and Current Touches
        {
            // MOUSE
            _delta = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.ScreenToWorldPoint(_dragOrigin);
        }
        else
        {
            if (Input.touchCount > 0)
            {
                // TOUCH
                _deltaTouch =
                    Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x,
                                                               Input.GetTouch(0).position.y, 0));
                _delta = _deltaTouch - Camera.main.ScreenToWorldPoint(_dragOrigin);
            }
        }

        //Debug.Log("Delta: " + _delta);
        _delta = Vector3.ClampMagnitude(_delta, 0.45f * Chuzzle.Scale.x);

        if (!_axisChozen)
        {
            //chooze drag direction
            if (Mathf.Abs(_delta.x) < 1.5 * Mathf.Abs(_delta.y) || Mathf.Abs(_delta.x) > 1.5 * Mathf.Abs(_delta.y))
            {
                if (Mathf.Abs(_delta.x) < Mathf.Abs(_delta.y))
                {
                    SelectedChuzzles = draggableChuzzles.Where(x => x.Current.x == CurrentChuzzle.Current.x).ToList();
                    _isVerticalDrag  = true;
                }
                else
                {
                    SelectedChuzzles = draggableChuzzles.Where(x => x.Current.y == CurrentChuzzle.Current.y).ToList();
                    _isVerticalDrag  = false;
                }

                _hasLockedChuzzles = HasLockChuzzles;
                if (_hasLockedChuzzles)
                {
                    _minX = CurrentChuzzle.Current.Position.x - Chuzzle.Scale.x * 0.4f;
                    _maxX = CurrentChuzzle.Current.Position.x + Chuzzle.Scale.x * 0.4f;
                    _minY = CurrentChuzzle.Current.Position.y - Chuzzle.Scale.y * 0.4f;
                    _maxY = CurrentChuzzle.Current.Position.y + Chuzzle.Scale.y * 0.4f;
                }

                _axisChozen = true;
                //Debug.Log("Direction chozen. Vertical: " + _isVerticalDrag);
            }
        }

        if (_axisChozen)
        {
            if (_isVerticalDrag)
            {
                CurrentDirection = _delta.y > 0 ? Direction.Top : Direction.Bottom;
                _delta.z         = _delta.x = 0;
            }
            else
            {
                CurrentDirection = _delta.x > 0 ? Direction.Right : Direction.Left;
                _delta.y         = _delta.z = 0;
            }
        }

        // RESET START POINT
        _dragOrigin = Input.mousePosition;

        #endregion
    }
Exemplo n.º 20
0
 public static bool IsPowerUp(this Chuzzle chuzzle)
 {
     return(GamefieldUtility.IsPowerUp(chuzzle));
 }
Exemplo n.º 21
0
    private void MoveChuzzles(List <Cell> activeCells)
    {
        foreach (var c in SelectedChuzzles)
        {
            var copyPosition = c.transform.position;

            var real       = GamefieldUtility.ToRealCoordinates(c);
            var targetCell = GamefieldUtility.CellAt(activeCells, real.x, real.y);

            var difference = c.transform.position - GamefieldUtility.ConvertXYToPosition(real.x, real.y, Chuzzle.Scale);

            var isNeedCopy = false;

            if (targetCell != null && !targetCell.IsTemporary)
            {
                if (!_isVerticalDrag)
                {
                    if (difference.x > 0)
                    {
                        isNeedCopy = targetCell.Right == null ||
                                     (targetCell.Right != null && targetCell.Right.Type != CellTypes.Usual);
                        if (isNeedCopy)
                        {
                            var rightCell = GetRightCell(activeCells, targetCell.Right, c);
                            copyPosition = rightCell.Position + difference - new Vector3(Chuzzle.Scale.x, 0, 0);
                        }
                    }
                    else
                    {
                        isNeedCopy = targetCell.Left == null ||
                                     (targetCell.Left != null && targetCell.Left.Type != CellTypes.Usual);
                        if (isNeedCopy)
                        {
                            var leftCell = GetLeftCell(activeCells, targetCell.Left, c);
                            copyPosition = leftCell.Position + difference + new Vector3(Chuzzle.Scale.x, 0, 0);
                        }
                    }
                }
                else
                {
                    if (difference.y > 0)
                    {
                        isNeedCopy = targetCell.Top == null ||
                                     (targetCell.Top != null &&
                                      (targetCell.Top.Type == CellTypes.Block || targetCell.Top.IsTemporary));
                        if (isNeedCopy)
                        {
                            var topCell = GetTopCell(activeCells, targetCell.Top, c);
                            copyPosition = topCell.Position + difference - new Vector3(0, Chuzzle.Scale.y, 0);
                        }
                    }
                    else
                    {
                        isNeedCopy = targetCell.Bottom == null ||
                                     (targetCell.Bottom != null && targetCell.Bottom.Type == CellTypes.Block);
                        if (isNeedCopy)
                        {
                            var bottomCell = GetBottomCell(activeCells, targetCell.Bottom, c);
                            copyPosition = bottomCell.Position + difference + new Vector3(0, Chuzzle.Scale.y, 0);
                        }
                    }
                }
            }
            else
            {
                isNeedCopy = true;
            }

            if (targetCell == null || targetCell.Type == CellTypes.Block || targetCell.IsTemporary)
            {
                switch (CurrentDirection)
                {
                case Direction.Left:
                    //if border
                    targetCell = GetLeftCell(activeCells, targetCell, c);
                    break;

                case Direction.Right:
                    targetCell = GetRightCell(activeCells, targetCell, c);
                    break;

                case Direction.Top:
                    //if border
                    targetCell = GetTopCell(activeCells, targetCell, c);
                    break;

                case Direction.Bottom:
                    targetCell = GetBottomCell(activeCells, targetCell, c);
                    break;

                default:
                    throw new ArgumentOutOfRangeException("Current direction can not be shit");
                }

                c.transform.position = targetCell.Position + difference;

                // Debug.Log("New coord: "+GamefieldUtility.ToRealCoordinates(c)+" for "+c.gameObject.name + " pos: "+c.transform.position);
            }

            if (difference.magnitude < (Chuzzle.Scale.x / 25))
            {
                isNeedCopy = false;
            }

            if (isNeedCopy)
            {
                c.Teleportable.Show();
                c.Teleportable.Copy.transform.position = copyPosition;
            }
            else
            {
                c.Teleportable.Hide();
            }
        }
    }