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); } }
public void SelectGemWithPosition(Vector2 position) { if (!_busy) { _selectedIndex = _grid.GetIndexFromWorldPosition(position); } }
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)); }
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)); }
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); } }
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); }
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); }
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); } }
public Vector2 GetWorldPositionFromIndex(MatrixIndex index) { var myTransform = transform; return((Vector2)myTransform.position + new Vector2(index.j, -index.i) * myTransform.lossyScale); }
public void SetValue(MatrixIndex index, Gem gem) { _grid[index.j, index.i] = gem; }
public Gem GetValue(MatrixIndex index) { return(GetValue(index.i, index.j)); }
/// <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) { }
public bool WouldCreateMatch(MatrixIndex index, Gem gem) { return(GetColumnMatch(index, gem).Count() > 0 || GetRowMatch(index, gem).Count() > 0); }
public void ClearGemSelection() { _selectedIndex = new MatrixIndex(-1, -1); }