private void OnCombinationDestroyed(List<Chuzzle> destroyedChuzzles)
    {
        //TODO find chuzzle (it's special type)
        if (TargetChuzzle == null)
        {
            TargetChuzzle = Gamefield.Level.ActiveChuzzles.FirstOrDefault(x => x.Counter > 0);
            if (TargetChuzzle == null)
            {
                Debug.Log("No target chuzzle");
                return;
            }
        }

           // Debug.Log("destroy");
        if (destroyedChuzzles.Contains(TargetChuzzle))
        {
            Amount -= destroyedChuzzles.Count-1;
            if (Amount < 0)
            {
                Amount = 0;
            }
            TargetChuzzle.GetComponentInChildren<tk2dTextMesh>().text = Amount.ToString(CultureInfo.InvariantCulture);
        }

        if (Amount <= 0)
        {
            IsWin = true;
        }
    }
 public override void OnEnter()
 {
     AnimatedChuzzles.Clear();
     Chuzzle.DropEventHandlers();
     Chuzzle.AnimationStarted += OnAnimationStarted;
     CreateNew();
 }
Example #3
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);
    }
Example #4
0
    private void OnAnimationFinished(Chuzzle chuzzle)
    {
        chuzzle.AnimationFinished -= OnAnimationFinished;
        AnimatedChuzzles.Remove(chuzzle);

        CheckAnimationCompleted();
    }
Example #5
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);
    }
Example #6
0
    //TODO check for if start chuzzle contains in tilesToKill
    public static void ApplyPowerUp(this Gamefield gamefield, List<Chuzzle> tilesToKill,  Chuzzle chuzzle)
    {
        if (chuzzle.PowerType == PowerType.Usual)
        {
            Debug.LogError("Try apply power up for non power type chuzzle: "+chuzzle.ToString());
            return;
        }

        if (chuzzle.PowerType == PowerType.HorizontalLine)
        {
            var horizontalChuzzles = gamefield.Level.ActiveChuzzles.Where(x => x.Current.y == chuzzle.Current.y && x.PowerType == PowerType.Usual);
            tilesToKill.AddUniqRange(horizontalChuzzles);
        }

        if (chuzzle.PowerType == PowerType.VerticalLine)
        {
            var vertical = gamefield.Level.ActiveChuzzles.Where(x => x.Current.x == chuzzle.Current.x && x.PowerType == PowerType.Usual);
            tilesToKill.AddUniqRange(vertical);
        }

        if (chuzzle.PowerType == PowerType.Bomb)
        {
            var square = gamefield.Level.ActiveChuzzles.Where(x => (x.Current.x == chuzzle.Current.x - 1 || x.Current.x == chuzzle.Current.x + 1 || x.Current.x == chuzzle.Current.x) && (x.Current.y == chuzzle.Current.y - 1 || x.Current.y == chuzzle.Current.y || x.Current.y == chuzzle.Current.y + 1) && x.PowerType == PowerType.Usual);
            tilesToKill.AddUniqRange(square);
        }
    }
Example #7
0
    public void OnChuzzleDeath(Chuzzle chuzzle)
    {
        chuzzle.Died -= OnChuzzleDeath;

        //remove chuzzle from game logic
        Gamefield.RemoveChuzzle(chuzzle);
    }
Example #8
0
 public void InvokeTileDestroyed(Chuzzle destroyedChuzzle)
 {
     if (TileDestroyed != null)
     {
         TileDestroyed(destroyedChuzzle);
     }
 }
Example #9
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;
        }
    }
Example #10
0
 private void OnAnimationStarted(Chuzzle chuzzle)
 {
     if (!AnimatedChuzzles.Contains(chuzzle))
     {
         AnimatedChuzzles.Add(chuzzle);
         chuzzle.AnimationFinished += OnAnimationFinished;
     }
 }
Example #11
0
    public Chuzzle CreateBomb(Cell cell)
    {
        int        colorsNumber = NumberOfColors == -1 ? ChuzzlePrefabs.Length : NumberOfColors;
        GameObject prefab       = BombChuzzlePrefabs[Random.Range(0, colorsNumber)];
        Chuzzle    ch           = CreateChuzzle(cell, prefab);

        return(ch);
    }
Example #12
0
 public override void OnEnter()
 {
     isAlreadyChangedState = false;
     AnimatedChuzzles.Clear();
     Chuzzle.DropEventHandlers();
     Chuzzle.AnimationStarted += OnAnimationStarted;
     CreateNew();
 }
Example #13
0
    public static int CompareByY(Chuzzle first, Chuzzle second)
    {
        if (first.Current.y == second.Current.y)
        {
            return(0);
        }

        return(first.Current.y > second.Current.y ? 1 : -1);
    }
Example #14
0
    public void ReplaceWithOtherColor(Chuzzle toReplace)
    {
        ChuzzleColor        exceptColor    = toReplace.Color;
        List <ChuzzleColor> possibleColors = ((ChuzzleColor[])Enum.GetValues(typeof(ChuzzleColor))).ToList();

        possibleColors.Remove(exceptColor);

        ReplaceWithColor(toReplace, possibleColors[Random.Range(0, possibleColors.Count)]);
    }
Example #15
0
 public void OnAnimationFinished(Chuzzle chuzzle)
 {
     chuzzle.AnimationFinished -= OnAnimationFinished;
     AnimatedChuzzles.Remove(chuzzle);
     if (!AnimatedChuzzles.Any())
     {
         Gamefield.SwitchStateTo(Gamefield.WinCreateNewChuzzlesState);
     }
 }
Example #16
0
    public static int CompareByX(Chuzzle first, Chuzzle second)
    {
        if (first.Current.x == second.Current.x)
        {
            return(0);
        }

        return(first.Current.x > second.Current.x ? 1 : -1);
    }
Example #17
0
    public Chuzzle CreateTwoTimeChuzzle(Cell cell, bool isUniq)
    {
        int        colorsNumber = NumberOfColors == -1 ? ChuzzlePrefabs.Length : NumberOfColors;
        GameObject prefab       = isUniq
            ? GetUniqRandomPrefabForCell(cell, new List <GameObject>(ChuzzleTwoTimesPrefabs))
            : ChuzzleTwoTimesPrefabs[Random.Range(0, colorsNumber)];
        Chuzzle c = CreateChuzzle(cell, prefab);

        return(c);
    }
Example #18
0
    private void OnAnimationFinished(Chuzzle chuzzle)
    {
        chuzzle.AnimationFinished -= OnAnimationFinished;
        AnimatedChuzzles.Remove(chuzzle);

        chuzzle.Destroy(true, false);

        if (!AnimatedChuzzles.Any())
        {
            Gamefield.SwitchStateTo(Gamefield.WinCreateNewChuzzlesState);
        }
    }
Example #19
0
 public void Reset()
 {
     foreach (var selectedChuzzle in SelectedChuzzles)
     {
         selectedChuzzle.Teleportable.Hide();
     }
     SelectedChuzzles.Clear();
     CurrentChuzzle  = null;
     _axisChozen     = false;
     _isVerticalDrag = false;
     _isReturning    = false;
 }
Example #20
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);
        }
    }
Example #21
0
 /// <summary>
 ///     Remove chuzzle from game logic and add new tiles in column
 /// </summary>
 /// <param name="chuzzle">Chuzzle to remove</param>
 /// <param name="invokeEvent">Need to invoke event or not</param>
 public void RemoveChuzzle(Chuzzle chuzzle, bool invokeEvent = true)
 {
     Level.Chuzzles.Remove(chuzzle);
     if (Level.ActiveChuzzles.Contains(chuzzle))
     {
         Level.ActiveChuzzles.Remove(chuzzle);
     }
     NewTilesInColumns[chuzzle.Current.x]++;
     if (invokeEvent)
     {
         InvokeTileDestroyed(chuzzle);
     }
 }
Example #22
0
    public static bool BetweenYCheck(Chuzzle chuzzle, List<Chuzzle> allChuzzles)
    {
        var firstChuzzle = chuzzle;
        var secondChuzzle =
            allChuzzles.FirstOrDefault(
                ch =>
                    ch.Current.x == firstChuzzle.Current.x && ch.Current.y == firstChuzzle.Current.y + 2 &&
                    ch.Type == firstChuzzle.Type);

        if (secondChuzzle == null)
            return false;

        return allChuzzles.Any(x => x.Current.y == firstChuzzle.Current.y + 1 && x.Type == firstChuzzle.Type);
    }
Example #23
0
    public void Explode(Chuzzle chuzzle)
    {
        var ps = GetExplosion();

        iTween.ScaleTo(chuzzle.gameObject,
                       iTween.Hash(
                           "x", 0,
                           "y", 0,
                           "z", 0,
                           "time", ps.particleSystem.duration));

        ps.transform.position = EffectsCamera.ScreenToWorldPoint(Camera.main.WorldToScreenPoint(chuzzle.transform.position));
        ps.Init(chuzzle.Color);
    }
Example #24
0
        public void Explode(Chuzzle chuzzle)
        {
            var ps = GetExplosion();

            iTween.ScaleTo(chuzzle.gameObject,
                           iTween.Hash(
                                       "x", 0,
                                       "y", 0,
                                       "z", 0,
                                       "time", ps.GetComponent<ParticleSystem>().duration));

            ps.transform.position = EffectsCamera.ScreenToWorldPoint(Camera.main.WorldToScreenPoint(chuzzle.transform.position));
            ps.Init(chuzzle.Color);
        }
Example #25
0
    private void OnAnimationStarted(Chuzzle chuzzle)
    {
        if (isAlreadyChangedState)
        {
            Debug.LogWarning("Already changed state create new chuzzle");
            return;
        }

        if (!AnimatedChuzzles.Contains(chuzzle))
        {
            AnimatedChuzzles.Add(chuzzle);
            chuzzle.AnimationFinished += OnAnimationFinished;
        }
    }
Example #26
0
    public static bool IsSameColor(Chuzzle a, Chuzzle b)
    {
        if (a == null || b == null)
        {
            Debug.LogError(String.Format("A or b is NULL. a: {0} b: {1}", a, b));
            return(false);
        }

        if (a is InvaderChuzzle || b is InvaderChuzzle)
        {
            return(false);
        }

        return(a.Color == b.Color);
    }
Example #27
0
    public static bool BetweenYCheck(Chuzzle chuzzle, List <Chuzzle> allChuzzles)
    {
        var firstChuzzle  = chuzzle;
        var secondChuzzle = allChuzzles.FirstOrDefault(ch =>
                                                       ch.Current.x == firstChuzzle.Current.x &&
                                                       ch.Current.y == firstChuzzle.Current.y + 2 &&
                                                       IsSameColor(ch, firstChuzzle));

        if (secondChuzzle == null || allChuzzles.Any(x => x.Current.y == firstChuzzle.Current.y + 1 && IsLock(x)))
        {
            return(false);
        }

        return(allChuzzles.Any(x => x.Current.y == firstChuzzle.Current.y + 1 && IsSameColor(x, firstChuzzle)));
    }
Example #28
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);
         }
     }
 }
Example #29
0
    public override void OnEnter()
    {
        AnimatedChuzzles.Clear();
        Chuzzle.DropEventHandlers();
        Chuzzle.AnimationStarted += OnAnimationStarted;
        if (!Tutorial.isActive)
        {
            CheckPossibleCombinations();
        }


        if (!Gamefield.InvaderWasDestroyed)
        {
            InvaderChuzzle.Populate(Gamefield);
        }
        Gamefield.InvaderWasDestroyed = false;
    }
Example #30
0
    // вертикальная и горизонтальная проверка для второго случая
    public static bool AnotherVerticalCheck(Chuzzle chuzzle, List<Chuzzle> allChuzzles)
    {
        var firstChuzzle = chuzzle;
        var secondChuzzle =
            allChuzzles.FirstOrDefault(
                ch =>
                    ch.Current.x == firstChuzzle.Current.x && ch.Current.y == firstChuzzle.Current.y + 1 &&
                    ch.Type == firstChuzzle.Type);

        if (secondChuzzle == null) return false;

        return
            allChuzzles.Where(
                ch =>
                    Math.Abs(ch.Current.x - firstChuzzle.Current.x) == 1 || ch.Current.y == firstChuzzle.Current.y - 1 ||
                    ch.Current.y == firstChuzzle.Current.y + 2).Any(ch => ch.Type == firstChuzzle.Type);
    }
Example #31
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);
        }
    }
Example #32
0
    /// <summary>
    ///     Remove chuzzle from game logic and add new tiles in column
    /// </summary>
    /// <param name="chuzzle">Chuzzle to remove</param>
    /// <param name="invokeEvent">Need to invoke event or not</param>
    public void RemoveChuzzle(Chuzzle chuzzle, bool invokeEvent = true)
    {
        Level.Chuzzles.Remove(chuzzle);
        Level.ActiveChuzzles.Remove(chuzzle);

        if (chuzzle.NeedCreateNew)
        {
            if (chuzzle is TwoTimeChuzzle)
            {
                Debug.LogError("Error: Two time chuzzle creation!!");
            }
            NewTilesInColumns[chuzzle.Current.x]++;
        }
        if (invokeEvent)
        {
            InvokeTileDestroyed(chuzzle);
        }
    }
Example #33
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());
    }
Example #34
0
    // вертикальная и горизонтальная проверка для второго случая
    public static bool AnotherVerticalCheck(Chuzzle chuzzle, List <Chuzzle> allChuzzles)
    {
        var firstChuzzle  = chuzzle;
        var secondChuzzle =
            allChuzzles.FirstOrDefault(
                ch =>
                ch.Current.x == firstChuzzle.Current.x && ch.Current.y == firstChuzzle.Current.y + 1 &&
                IsSameColor(ch, firstChuzzle));

        if (secondChuzzle == null)
        {
            return(false);
        }

        return
            (allChuzzles.Where(
                 ch =>
                 Math.Abs(ch.Current.x - firstChuzzle.Current.x) == 1 || ch.Current.y == firstChuzzle.Current.y - 1 ||
                 ch.Current.y == firstChuzzle.Current.y + 2).Any(ch => IsSameColor(ch, firstChuzzle)));
    }
Example #35
0
    private void OnTileDestroyed(Chuzzle destroyedChuzzle)
    {
        if (PlaceCoordinates.Count == 0)
        {
            return;
        }

        var place =
            CurrentPlaceCoordinates.FirstOrDefault(
                x => x.x == destroyedChuzzle.Current.x && x.y == destroyedChuzzle.Current.y);
        if (place != null)
        {
            NGUITools.ClearChildren(destroyedChuzzle.Current.GameObject);
            CurrentPlaceCoordinates.Remove(place);
        }

        if (CurrentPlaceCoordinates.Count == 0)
        {
            IsWin = true;
        }
    }
    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);
            }
        }
    }
Example #37
0
    public Chuzzle CreateCounterChuzzle(Cell cell, bool isUniq)
    {
        int colorsNumber = NumberOfColors == -1 ? ChuzzlePrefabs.Length : NumberOfColors;

        GameObject prefab = isUniq
            ? GetUniqRandomPrefabForCell(cell, new List <GameObject>(ChuzzleCounterPrefabs))
            : ChuzzleCounterPrefabs[Random.Range(0, colorsNumber)];
        Chuzzle c = CreateChuzzle(cell, prefab);

        var chuzzle = c as CounterChuzzle;

        if (chuzzle == null)
        {
            Debug.LogError("Incorrect prefabs for counters");
        }
        else
        {
            ((TargetChuzzleGameMode)Gamefield.GameMode).UpdateCounter();
        }
        cell.CreationType = CreationType.Usual;

        return(c);
    }
Example #38
0
 private void OnDied(Chuzzle deadChuzzle)
 {
     Chuzzle = null;
 }
Example #39
0
 public static Chuzzle GetTopFor(Chuzzle c, IEnumerable<Chuzzle> chuzzles)
 {
     return chuzzles.FirstOrDefault(x => x.Real == c.Real.Top);
 }
Example #40
0
 public void Reset()
 {
     SelectedChuzzles.Clear();
     CurrentChuzzle = null;
     _directionChozen = false;
     _isVerticalDrag = false;
 }
Example #41
0
    public static List<Chuzzle> RecursiveFind(Chuzzle chuzzle, List<Chuzzle> combination, IEnumerable<Chuzzle> chuzzles)
    {
        if (chuzzle == null || combination.Contains(chuzzle) || chuzzle.IsCheckedForSearch)
        {
            return new List<Chuzzle>();
        }
        combination.Add(chuzzle);
        chuzzle.IsCheckedForSearch = true;

        var left = GetLeftFor(chuzzle, chuzzles);
        if (left != null && left.Type == chuzzle.Type)
        {
            var answer = RecursiveFind(left, combination, chuzzles);
            foreach (var a in answer)
            {
                if (combination.Contains(a) == false)
                {
                    combination.Add(a);
                }
            }
        }

        var right = GetRightFor(chuzzle, chuzzles);
        if (right != null && chuzzle.Type == right.Type)
        {
            var answer = RecursiveFind(right, combination, chuzzles);
            foreach (var a in answer)
            {
                if (combination.Contains(a) == false)
                {
                    combination.Add(a);
                }
            }
        }

        var top = GetTopFor(chuzzle, chuzzles);
        if (top != null && chuzzle.Type == top.Type)
        {
            var answer = RecursiveFind(top, combination, chuzzles);
            foreach (var a in answer)
            {
                if (combination.Contains(a) == false)
                {
                    combination.Add(a);
                }
            }
        }

        var bottom = GetBottomFor(chuzzle, chuzzles);
        if (bottom != null && chuzzle.Type == bottom.Type)
        {
            var answer = RecursiveFind(bottom, combination, chuzzles);
            foreach (var a in answer)
            {
                if (combination.Contains(a) == false)
                {
                    combination.Add(a);
                }
            }
        }

        return combination;
    }
Example #42
0
 private void InvokeTileDestroyed(Chuzzle destroyedChuzzle)
 {
     if (TileDestroyed != null)
     {
         TileDestroyed(destroyedChuzzle);
     }
 }
Example #43
0
    // Update is called once per frame
    public void Update(IEnumerable<Chuzzle> draggableChuzzles)
    {
        Gamefield.TimeFromTip += Time.deltaTime;
        if (Gamefield.TimeFromTip > 1 && !Gamefield.Level.ActiveChuzzles.Any(x=>x.Shine))
        {
            IntVector2 targetPosition = null;
            Chuzzle arrowChuzzle = null;
            var possibleCombination = GamefieldUtility.Tip(Gamefield.Level.ActiveChuzzles,out targetPosition, out arrowChuzzle);
            if (possibleCombination.Any())
            {
                foreach (var chuzzle in possibleCombination)
                {
                    chuzzle.Shine = true;
                }
                GamefieldUtility.ShowArrow(arrowChuzzle, targetPosition, Gamefield.DownArrow);
            }
            else
            {
                RepaintRandom();
                return;
            }

            Gamefield.TimeFromTip = 0;
        }

        #region Drag

        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
            _dragOrigin = Input.mousePosition;

            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);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Single.MaxValue, Gamefield.ChuzzleMask))
            {
                CurrentChuzzle = hit.transform.gameObject.GetComponent<Chuzzle>();
            }

            return;
        }

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

        if (CurrentChuzzle == null)
        {
            return;
        }

        if (Input.GetMouseButton(0)) // Get Position Difference between Last and Current Touches
        {
            // MOUSE
            _delta = Input.mousePosition - _dragOrigin;

            //   Debug.Log("Drag delta");
        }
        else
        {
            if (Input.touchCount > 0)
            {
                // TOUCH
                _deltaTouch = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0);
                _delta = _deltaTouch - _dragOrigin;
                // Debug.Log("Drag delta TOUCH");
            }
        }

        _delta = Vector3.ClampMagnitude(_delta, CurrentChuzzle.Scale.x);

        if (!_directionChozen)
        {
            //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))
                {
                    //TODO: choose row
                    SelectedChuzzles = draggableChuzzles.Where(x => x.Current.x == CurrentChuzzle.Current.x).ToList();
                    _isVerticalDrag = true;
                }
                else
                {
                    //TODO: choose column
                    SelectedChuzzles = draggableChuzzles.Where(x => x.Current.y == CurrentChuzzle.Current.y).ToList();
                    _isVerticalDrag = false;
                }

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

        if (_directionChozen)
        {
            if (_isVerticalDrag)
            {
                CurrentDirection = _delta.y > 0 ? Direction.ToTop : Direction.ToBottom;
            }
            else
            {
                CurrentDirection = _delta.x > 0 ? Direction.ToLeft : Direction.ToRight;
            }
        }

        // RESET START POINT
        _dragOrigin = Input.mousePosition;

        #endregion
    }
Example #44
0
    public static void ShowArrow(Chuzzle from, IntVector2 to, GameObject downArrowPrefab)
    {
        var down = Object.Instantiate(downArrowPrefab) as GameObject;
        ScaleSprite(down.GetComponent<tk2dSprite>(), from.Scale);

        if (from.Current.x == to.x)
        {
            //vertical
            if (from.Current.y >= to.y)
            {
                //to down
                //do nothing
            }
            else
            {
                //to up
                //mirror vertical
                down.GetComponent<tk2dSprite>().FlipY = true;
            }
        }
        else
        {
            //horizontal
            if (from.Current.x < to.x)
            {
                //to right
                down.transform.rotation = Quaternion.Euler(0, 0, 90);
            }
            else
            {
                //to left
                //to right
                down.transform.rotation = Quaternion.Euler(0, 0, -90);
            }
        }

        down.transform.parent = from.transform;
        down.transform.localPosition = from.Scale/2f;

        from.Arrow = down;
    }
Example #45
0
    /// <summary>
    ///     Находит любую возможную комбинацию
    /// </summary>
    /// <param name="chuzzles">Список элементов в котором надо найти комбинацию</param>
    /// <returns>Список элементов которые составляют эту комбинацию</returns>
    public static List<Chuzzle> Tip(List<Chuzzle> chuzzles, out IntVector2 isHorizontalMove, out Chuzzle chuzzleToMove)
    {
        var bottom =
            chuzzles.FirstOrDefault(
                x => BetweenYCheck(x, chuzzles));

        if (bottom != null && bottom.Current.Top != null && bottom.Current.Top.Type != CellTypes.Block)
        {
            var top = chuzzles.First(ch => ch.Current == bottom.Current.Top.Top);

            var bottomPart = RecursiveFind(bottom, new List<Chuzzle>(), chuzzles);
            var middlePart = GetHorizontalLineChuzzles(bottom.Current.y + 1, bottom.Type, chuzzles);
            var topPart = RecursiveFind(top, new List<Chuzzle>(), chuzzles);

            var posibleCombination = new List<Chuzzle>();
            posibleCombination.AddRange(bottomPart);
            posibleCombination.AddRange(middlePart);
            posibleCombination.AddRange(topPart);

            Debug.Log("Combination 1");
            isHorizontalMove = new IntVector2(bottom.Current.x, bottom.Current.y + 1);
            chuzzleToMove = middlePart.First();
            return posibleCombination;
        }

        var left = chuzzles.FirstOrDefault(x => BetweenXCheck(x, chuzzles));

        if (left != null && left.Current.Left != null && left.Current.Left.Type != CellTypes.Block)
        {
            var right = chuzzles.First(ch => ch.Current == left.Current.Right.Right);

            var leftPart = RecursiveFind(left, new List<Chuzzle>(), chuzzles);
            var middlePart = GetVerticalLineChuzzles(left.Current.x + 1, left.Type, chuzzles);
            var rightPart = RecursiveFind(right, new List<Chuzzle>(), chuzzles);

            var posibleCombination = new List<Chuzzle>();
            posibleCombination.AddRange(leftPart);
            posibleCombination.AddRange(middlePart);
            posibleCombination.AddRange(rightPart);

            Debug.Log("Combination 2");
            isHorizontalMove = new IntVector2(left.Current.x + 1, left.Current.y);
            chuzzleToMove = middlePart.First();
            return posibleCombination;
        }

        var combinations = FindCombinations(chuzzles, 2);

        foreach (var combination in combinations)
        {
            var first = combination[0];
            var second = combination[1];

            //vertical combination
            if (first.Current.x == second.Current.x)
            {
                //try left
                if ((first.Current.Left != null && first.Current.Left.Type != CellTypes.Block) ||
                    (second.Current.Left != null && second.Current.Left.Type != CellTypes.Block))
                {
                    var leftPart = GetVerticalLineChuzzles(first.Current.x - 1, first.Type, chuzzles).ToList();
                    if (leftPart.Any())
                    {
                        var possibleCombination = new List<Chuzzle>();
                        possibleCombination.AddRange(combination);
                        possibleCombination.AddRange(leftPart);

                        Debug.Log("Combination 3");
                        isHorizontalMove = new IntVector2(first.Current.x - 1, first.Current.y);
                        chuzzleToMove = leftPart.First();
                        return possibleCombination;
                    }
                }

                //try right
                if ((first.Current.Right != null && first.Current.Right.Type != CellTypes.Block) ||
                    (second.Current.Right != null && second.Current.Right.Type != CellTypes.Block))
                {
                    var rightPart = GetVerticalLineChuzzles(first.Current.x + 1, first.Type, chuzzles).ToList();
                    if (rightPart.Any())
                    {
                        var possibleCombination = new List<Chuzzle>();
                        possibleCombination.AddRange(combination);
                        possibleCombination.AddRange(rightPart);

                        Debug.Log("Combination 4");
                        isHorizontalMove = new IntVector2(first.Current.x + 1, first.Current.y);
                        chuzzleToMove = rightPart.First();
                        return possibleCombination;
                    }
                }

                //try top
                if (second.Current.Top != null && second.Current.Top.Type != CellTypes.Block &&
                    chuzzles.Any(x => x.Current == second.Current.Top))
                {
                    var topPart = GetHorizontalLineChuzzles(second.Current.Top.y, second.Type, chuzzles).ToList();
                    if (topPart.Any())
                    {
                        var possibleCombination = new List<Chuzzle>();
                        possibleCombination.AddRange(combination);
                        possibleCombination.AddRange(topPart);

                        Debug.Log("Combination 5");
                        isHorizontalMove = new IntVector2(second.Current.x, second.Current.Top.y);
                        chuzzleToMove = topPart.First();
                        return possibleCombination;
                    }
                }

                //try bottom
                if (first.Current.Bottom != null && first.Current.Bottom.Type != CellTypes.Block &&
                    chuzzles.Any(x => x.Current == first.Current.Bottom))
                {
                    var bottomPart = GetHorizontalLineChuzzles(first.Current.Bottom.y, first.Type, chuzzles).ToList();
                    if (bottomPart.Any())
                    {
                        var possibleCombination = new List<Chuzzle>();
                        possibleCombination.AddRange(combination);
                        possibleCombination.AddRange(bottomPart);

                        Debug.Log("Combination 6");
                        isHorizontalMove = new IntVector2(second.Current.x, second.Current.Bottom.y);
                        chuzzleToMove = bottomPart.First();
                        return possibleCombination;
                    }
                }
            }
            else
            {
                //horizontal combinations

                //try left
                if (first.Current.Left != null && first.Current.Left.Type != CellTypes.Block)
                {
                    var leftPart = GetVerticalLineChuzzles(first.Current.x - 1, first.Type, chuzzles).ToList();
                    if (leftPart.Any())
                    {
                        var possibleCombination = new List<Chuzzle>();
                        possibleCombination.AddRange(combination);
                        possibleCombination.AddRange(leftPart);

                        Debug.Log("Combination 7");
                        isHorizontalMove = new IntVector2(first.Current.x - 1, first.Current.y);
                        chuzzleToMove = leftPart.First();
                        return possibleCombination;
                    }
                }

                //try right
                if (second.Current.Right != null && second.Current.Right.Type != CellTypes.Block)
                {
                    var rightPart = GetVerticalLineChuzzles(second.Current.x + 1, second.Type, chuzzles).ToList();
                    if (rightPart.Any())
                    {
                        var possibleCombination = new List<Chuzzle>();
                        possibleCombination.AddRange(combination);
                        possibleCombination.AddRange(rightPart);

                        Debug.Log("Combination 8");
                        isHorizontalMove = new IntVector2(second.Current.x + 1, second.Current.y);
                        chuzzleToMove = rightPart.First();
                        return possibleCombination;
                    }
                }

                //try top
                if (
                    (first.Current.Top != null && first.Current.Top.Type != CellTypes.Block &&
                     chuzzles.Any(x => x.Current == first.Current.Top)) ||
                    (second.Current.Top != null && second.Current.Top.Type != CellTypes.Block &&
                     chuzzles.Any(x => x.Current == second.Current.Top))
                    )
                {
                    var topPart = GetHorizontalLineChuzzles(second.Current.y + 1, second.Type, chuzzles).ToList();
                    if (topPart.Any())
                    {
                        var possibleCombination = new List<Chuzzle>();
                        possibleCombination.AddRange(combination);
                        possibleCombination.AddRange(topPart);

                        Debug.Log("Combination 9");
                        isHorizontalMove = new IntVector2(second.Current.x, second.Current.y + 1);
                        chuzzleToMove = topPart.First();
                        return possibleCombination;
                    }
                }

                //try bottom
                if (
                    (first.Current.Bottom != null && first.Current.Bottom.Type != CellTypes.Block &&
                     chuzzles.Any(x => x.Current == first.Current.Bottom)) ||
                    (second.Current.Bottom != null && second.Current.Bottom.Type != CellTypes.Block &&
                     chuzzles.Any(x => x.Current == second.Current.Bottom))
                    )
                {
                    var bottomPart = GetHorizontalLineChuzzles(first.Current.y - 1, first.Type, chuzzles).ToList();
                    if (bottomPart.Any())
                    {
                        var possibleCombination = new List<Chuzzle>();
                        possibleCombination.AddRange(combination);
                        possibleCombination.AddRange(bottomPart);

                        Debug.Log("Combination 10");
                        isHorizontalMove = new IntVector2(first.Current.x, first.Current.y - 1);
                        chuzzleToMove = bottomPart.First();
                        return possibleCombination;
                    }
                }
            }
        }
        Debug.Log("Combination NOOOOOOOOOO 11");
        isHorizontalMove = new IntVector2();
        chuzzleToMove = null;
        return new List<Chuzzle>();
    }
Example #46
0
 public static IntVector2 ToRealCoordinates(Chuzzle chuzzle)
 {
     return new IntVector2(Mathf.RoundToInt(chuzzle.transform.localPosition.x/chuzzle.Scale.x),
         Mathf.RoundToInt(chuzzle.transform.localPosition.y/chuzzle.Scale.y));
 }
Example #47
0
 public void CalculateRealCoordinatesFor(Chuzzle chuzzle)
 {
     chuzzle.Real = Gamefield.Level.GetCellAt(Mathf.RoundToInt(chuzzle.transform.localPosition.x / chuzzle.Scale.x),
         Mathf.RoundToInt(chuzzle.transform.localPosition.y / chuzzle.Scale.y), false);
 }