Exemplo n.º 1
0
    private IEnumerator Swap(MatrixIndex indexA, MatrixIndex indexB, bool returning = false)
    {
        SetBusy(true);

        AudioSource.PlayClipAtPoint(swapClip, _camera.transform.position);

        var speed = swapSpeed * transform.lossyScale.x;

        _grid.GetValue(indexA).SetPosition(_grid.GetWorldPositionFromIndex(indexB), speed);
        _grid.GetValue(indexB).SetPosition(_grid.GetWorldPositionFromIndex(indexA), speed);

        var tempGem = _grid.GetValue(indexA);

        _grid.SetValue(indexA, _grid.GetValue(indexB));
        _grid.SetValue(indexB, tempGem);

        yield return(new WaitForSeconds(stepTime));

        if (!returning)
        {
            ProcessSwap(indexA, indexB);
        }
        else
        {
            SetBusy(false);
        }
    }
Exemplo n.º 2
0
 public void SelectGemWithPosition(Vector2 position)
 {
     if (!_busy)
     {
         _selectedIndex = _grid.GetIndexFromWorldPosition(position);
     }
 }
Exemplo n.º 3
0
    public Match GetRowMatch(MatrixIndex index, Gem gem)
    {
        var matchIndexes = new List <MatrixIndex> {
            index
        };

        matchIndexes.AddRange(GetRowRightGems(index, gem));
        matchIndexes.AddRange(GetRowLeftGems(index, gem));

        if (matchIndexes.Count < 3)
        {
            matchIndexes.Clear();
        }

        return(new Match(matchIndexes));
    }
Exemplo n.º 4
0
    public Match GetColumnMatch(MatrixIndex index, Gem gem)
    {
        var matchIndexes = new List <MatrixIndex> {
            index
        };

        matchIndexes.AddRange(GetColumnDownGems(index, gem));
        matchIndexes.AddRange(GetColumnUpGems(index, gem));

        if (matchIndexes.Count < 3)
        {
            matchIndexes.Clear();
        }

        return(new Match(matchIndexes));
    }
Exemplo n.º 5
0
    private void CheckForMatches()
    {
        var matches = new List <Match>();

        for (int i = 0; i < _grid.GetHeight(); i++)
        {
            for (int j = 0; j < _grid.GetWidth(); j++)
            {
                var index = new MatrixIndex(i, j);

                var gem         = _grid.GetValue(index);
                var rowMach     = _finder.GetRowMatch(index, gem);
                var columnMatch = _finder.GetColumnMatch(index, gem);

                if (rowMach.Count() > 0)
                {
                    if (!matches.Exists(match => match == rowMach))
                    {
                        matches.Add(rowMach);
                    }
                }

                if (columnMatch.Count() > 0)
                {
                    if (!matches.Exists(match => match == columnMatch))
                    {
                        matches.Add(columnMatch);
                    }
                }
            }
        }

        if (matches.Count > 0)
        {
            ClearMatches(matches);
        }
        else
        {
            if (!_finder.HasPossibleMatch())
            {
                ShuffleGrid();
            }

            SetBusy(false);
        }
    }
Exemplo n.º 6
0
    public List <MatrixIndex> GetRowLeftGems(MatrixIndex index, Gem gem)
    {
        var gems = new List <MatrixIndex>();

        while (index.j > 0)
        {
            index.j--;
            var testGem = _grid.GetValue(index);
            if (!testGem || !testGem.CompareTag(gem.tag))
            {
                break;
            }

            gems.Add(index);
        }

        return(gems);
    }
Exemplo n.º 7
0
    public List <MatrixIndex> GetColumnDownGems(MatrixIndex index, Gem gem)
    {
        var gems = new List <MatrixIndex>();

        while (index.i < _grid.GetHeight() - 1)
        {
            index.i++;
            var testGem = _grid.GetValue(index);
            if (!testGem || !testGem.CompareTag(gem.tag))
            {
                break;
            }

            gems.Add(index);
        }

        return(gems);
    }
Exemplo n.º 8
0
    private void ProcessSwap(MatrixIndex indexA, MatrixIndex indexB)
    {
        var gemA             = _grid.GetValue(indexA);
        var rowMachFromA     = _finder.GetRowMatch(indexA, gemA);
        var columnMatchFromA = _finder.GetColumnMatch(indexA, gemA);

        var gemB             = _grid.GetValue(indexB);
        var rowMachFromB     = _finder.GetRowMatch(indexB, gemB);
        var columnMatchFromB = _finder.GetColumnMatch(indexB, gemB);

        if (rowMachFromA.Count() == 0 && columnMatchFromA.Count() == 0 &&
            rowMachFromB.Count() == 0 && columnMatchFromB.Count() == 0)
        {
            StartCoroutine(Swap(indexA, indexB, true));
        }
        else
        {
            var matches = new List <Match>();

            if (rowMachFromA.Count() > 0)
            {
                matches.Add(rowMachFromA);
            }

            if (columnMatchFromA.Count() > 0)
            {
                matches.Add(columnMatchFromA);
            }

            if (rowMachFromB.Count() > 0)
            {
                matches.Add(rowMachFromB);
            }

            if (columnMatchFromB.Count() > 0)
            {
                matches.Add(columnMatchFromB);
            }

            ClearMatches(matches);
        }
    }
Exemplo n.º 9
0
    public Vector2 GetWorldPositionFromIndex(MatrixIndex index)
    {
        var myTransform = transform;

        return((Vector2)myTransform.position + new Vector2(index.j, -index.i) * myTransform.lossyScale);
    }
Exemplo n.º 10
0
 public void SetValue(MatrixIndex index, Gem gem)
 {
     _grid[index.j, index.i] = gem;
 }
Exemplo n.º 11
0
 public Gem GetValue(MatrixIndex index)
 {
     return(GetValue(index.i, index.j));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Matrix Index Assignment operator.
 /// </summary>
 /// <param name="MatrixIndex">Matrix Index</param>
 /// <param name="Operand">Operand.</param>
 /// <param name="Start">Start position in script expression.</param>
 /// <param name="Length">Length of expression covered by node.</param>
 /// <param name="Expression">Expression containing script.</param>
 public MatrixIndexAssignment(MatrixIndex MatrixIndex, ScriptNode Operand, int Start, int Length, Expression Expression)
     : base(MatrixIndex.LeftOperand, MatrixIndex.MiddleOperand, MatrixIndex.RightOperand, Operand, Start, Length, Expression)
 {
 }
Exemplo n.º 13
0
 public bool WouldCreateMatch(MatrixIndex index, Gem gem)
 {
     return(GetColumnMatch(index, gem).Count() > 0 || GetRowMatch(index, gem).Count() > 0);
 }
Exemplo n.º 14
0
 public void ClearGemSelection()
 {
     _selectedIndex = new MatrixIndex(-1, -1);
 }