Exemple #1
0
        public IEnumerable<PlacedHexagon> GetComplementaryHexagons(bool shouldPlaceBomb)
        {
            var alreadyPlacedBomb = false;
            var complementaryHexagons = new List<PlacedHexagon>();
            for (var columnIndex = 0; columnIndex < _gridSize.x; columnIndex++)
            for (var rowIndex = 0; rowIndex < _gridSize.y; rowIndex++)
            {
                var position = new Vector3Int(rowIndex, columnIndex, 0);
                var value = _placedHexagons.Find(placedHexagon => placedHexagon.Cell == position);
                if (value != null) continue;

                if (shouldPlaceBomb && !alreadyPlacedBomb)
                {
                    alreadyPlacedBomb = true;
                    var hexagon = bombHexagons[Random.Range(0, bombHexagons.Count)];
                    if (NeighborHood.FindNeighbor(position, _placedHexagons, _grid, NeighborType.BottomLeft,
                        out var neighbor))
                        while (neighbor.Hexagon.color.Equals(hexagon.color))
                            hexagon = bombHexagons[Random.Range(0, bombHexagons.Count)];

                    var tile = CreateTile(hexagon);
                    _tilemap.SetTile(position, tile);
                    var item = new BombHexagon(hexagon, position, Random.Range(6, 10));
                    complementaryHexagons.Add(item);
                    _placedHexagons.Add(item);
                }
                else
                {
                    var hexagon = hexagons[Random.Range(0, hexagons.Count)];
                    if (NeighborHood.FindNeighbor(position, _placedHexagons, _grid, NeighborType.BottomLeft,
                        out var neighbor))
                        while (neighbor.Hexagon.color.Equals(hexagon.color))
                            hexagon = hexagons[Random.Range(0, hexagons.Count)];

                    var tile = CreateTile(hexagon);
                    _tilemap.SetTile(position, tile);
                    var item = new PlacedHexagon(hexagon, position);
                    complementaryHexagons.Add(item);
                    _placedHexagons.Add(item);
                }
            }

            return complementaryHexagons;
        }
        public static bool FindNeighbor(Vector3Int cell, List <PlacedHexagon> hexagons, Grid grid,
                                        NeighborType neighborType, out PlacedHexagon neighbor)
        {
            var neighbors = GetNeighborsIndexed(cell, hexagons, grid);

            neighbor = new PlacedHexagon(null, Vector3Int.down);
            switch (neighborType)
            {
            case NeighborType.Top:
                if (!neighbors.ContainsKey(0))
                {
                    return(false);
                }
                neighbor = neighbors[0];
                break;

            case NeighborType.Down:
                if (!neighbors.ContainsKey(3))
                {
                    return(false);
                }
                neighbor = neighbors[3];
                break;

            case NeighborType.BottomLeft:
                if (!neighbors.ContainsKey(4))
                {
                    return(false);
                }
                neighbor = neighbors[4];
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(neighborType), neighborType, null);
            }

            return(true);
        }