public bool IsCollidedWithNeighbour(RaycastHit2D hit) { var position = GetPoint(hit.collider.transform); var neighbours = GetNeighbours(selectedTile); if (neighbours.Any(pos => pos.X == position.X && pos.Y == position.Y)) { tiles[selectedTile].StopSpinning(); swappedTile = position; SwapTiles(selectedTile, swappedTile); if (tiles.FindMatches().ToList().Any()) { return(false); } } else { tiles[selectedTile].StopSpinning(); selectedTile = null; } return(true); }
// Update is called once per frame private void Update() { gameState = GetGameState(); switch (gameState) { case (GameState.TileMoving): return; case GameState.HasEmptyTiles: { Collapse(); FillField(); break; } case GameState.TileSelected: { if (!Input.GetMouseButtonDown(0)) { return; } var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit.collider != null) { var position = GetPoint(hit.collider.transform); var neighbours = GetNeighbours(selectedTile); if (neighbours.Any(pos => pos.X == position.X && pos.Y == position.Y)) { tiles[selectedTile].StopSpinning(); swappedTile = position; SwapTiles(selectedTile, swappedTile); } else { tiles[selectedTile].StopSpinning(); selectedTile = null; } } break; } case GameState.MovedTile: { var matches = tiles.FindMatches().ToList(); if (matches.Any()) { foreach (var tile in matches) { tile.Remove(); ScoreManager.Add(1); } } else { SwapTiles(selectedTile, swappedTile); } selectedTile = null; swappedTile = null; break; } case (GameState.None): { var matches = tiles.FindMatches().ToList(); if (matches.Any()) { foreach (var tile in matches) { tile.Remove(); ScoreManager.Add(1); } break; } if (Input.GetMouseButtonDown(0)) { var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit.collider != null) { var position = GetPoint(hit.collider.transform); selectedTile = position; tiles[selectedTile].StartSpinning(); } } break; } } }