Beispiel #1
0
        /// <summary>
        /// Select 3 tiles based on the touch position
        /// </summary>
        void SelectTileGroup(Vector3 centerPos)
        {
            _gameState      = GameState.selecting;
            debugText.text  = "";
            debugText1.text = "";

            //Casts a circle with the touch position as origin.
            List <Collider2D> candidateTiles = Physics2D.OverlapCircleAll(centerPos, tileSize.x).ToList();

            //Gets the closest 3 tiles from where the circle was cast.
            var closestTiles = candidateTiles.OrderBy(t => (t.transform.position - centerPos).sqrMagnitude)
                               .Take(3)
                               .ToArray();

            //Check the edges and make sure the 3 selected tiles are not in line(3 same rows, 3 same columns).
            var column = closestTiles.First().GetComponent <Tile>().column;
            var row    = closestTiles.First().GetComponent <Tile>().row;

            //If same column
            if (closestTiles.All(x => x.GetComponent <Tile>().column == column))
            {
                candidateTiles.Remove(closestTiles[2]);
                closestTiles = candidateTiles.OrderBy(t => (t.transform.position - centerPos).sqrMagnitude)
                               .Take(3)
                               .ToArray();
            }
            //if same row
            if (closestTiles.All(x => x.GetComponent <Tile>().row == row))
            {
                candidateTiles.Remove(closestTiles[2]);
                closestTiles = candidateTiles.OrderBy(t => (t.transform.position - centerPos).sqrMagnitude)
                               .Take(3)
                               .ToArray();
            }

            List <Tile> selectedTiles = new List <Tile>(3);

            tileGroup = new TileGroup();


            tileGroupCenterObj.position = Helpers.FindGroupCenter(closestTiles);


            for (int i = 0; i < 3; i++)
            {
                selectedTiles.Add(closestTiles[i].GetComponent <Tile>());
                selectionBorders[i].transform.position = closestTiles[i].transform.position;
            }

            tileGroup.tiles = selectedTiles;

            //Reorder selected tiles to rotate them easier later.
            tileGroup.OrderTiles();
            _gameState = GameState.idle;
        }
Beispiel #2
0
        /// <summary>
        /// 2 overload functions for checking a single tile
        /// and checking a selection group.
        /// </summary>
        public TileMatches CheckTileForMaches(TileGroup group)
        {
            TileMatches matches = new TileMatches();

            //_gameState = GameState.matching;
            foreach (var go in group.tiles)
            {
                matches.AddObjectRange(CheckTileForMaches(go.gameObject).MatchedTiles);
            }

            return(matches);
        }