示例#1
0
        private static bool SmoothMap(Vector3Int centerPoint, int radiusToSmooth = int.MaxValue)
        {
            Dictionary <TerrainTile, TerrainType> changesToMake =
                new Dictionary <TerrainTile, TerrainType>();
            bool ringEmpty = false;
            int  i         = 0;

            while (i <= radiusToSmooth && !ringEmpty)
            {
                ringEmpty = true;
                List <Vector3Int> ring = HexTools.GetRing(centerPoint, i++);

                foreach (Vector3Int ringTile in ring)
                {
                    TerrainTile ttb = mapGen.GetTile(ringTile);
                    if (ttb == null)
                    {
                        continue;
                    }

                    ringEmpty = false;

                    int           wallCount        = 0;
                    TerrainTile[] surroundingTiles = mapGen.GetSurroundingTiles(ringTile);
                    foreach (TerrainTile tile in surroundingTiles)
                    {
                        if (tile == null || tile.terrainType == TerrainType.WaterGenerator)
                        {
                            ++wallCount;
                        }
                    }


                    if (wallCount > rules.minNeighboursToTurnToWater && ttb.terrainType != TerrainType.WaterGenerator)
                    {
                        changesToMake[ttb] = TerrainType.WaterGenerator;
                    }
                    else if (wallCount < rules.minNeighboursToTurnToWater && ttb.terrainType != TerrainType.LandGenerator)
                    {
                        changesToMake[ttb] = TerrainType.LandGenerator;
                    }
                }
            }

            foreach (var change in changesToMake)
            {
                mapGen.CreateAndSetTile(change.Key.coordinates, mapGen.GetTerrainData(change.Value).tile, change.Key);
            }

            return(changesToMake.Count > 0);
        }
示例#2
0
        private List <List <Vector3Int> > GetRegionsRingSearch(TerrainType regionType, int distanceToCheck)
        {
            List <List <Vector3Int> >     regions  = new List <List <Vector3Int> >();
            Dictionary <Vector3Int, bool> mapFlags = new Dictionary <Vector3Int, bool>();

            Vector3Int centerCoords = Vector3Int.zero;
            bool       ringEmpty    = false;
            int        i            = 0;

            while (i <= distanceToCheck && !ringEmpty)
            {
                ringEmpty = true;
                List <Vector3Int> ring = HexTools.GetRing(centerCoords, i++);
                foreach (Vector3Int ringTile in ring)
                {
                    TerrainTile ttb = GetTile(ringTile);
                    if (ttb == null)
                    {
                        continue;
                    }

                    ringEmpty = false;

                    if (mapFlags.TryGetValue(ringTile, out bool searched) == true && searched == true)
                    {
                        continue;
                    }

                    if (ttb.terrainType == regionType)
                    {
                        List <Vector3Int> newRegion = GetRegionTiles(ringTile);
                        foreach (Vector3Int coord in newRegion)
                        {
                            mapFlags[coord] = true;
                        }

                        regions.Add(newRegion);
                    }
                }
            }

            return(regions);
        }
示例#3
0
        void Update()
        {
            Vector3 mousepos = Input.mousePosition;
            Ray     ray      = cam.ScreenPointToRay(mousepos);
            Plane   plane    = new Plane(Vector3.back, Vector3.zero);

            if (plane.Raycast(ray, out float hitDist))
            {
                Vector3    worldpoint = ray.GetPoint(hitDist);
                Vector3Int tilepoint  = tilemap.WorldToCell(worldpoint);

                TerrainTile tile = GetTile(tilepoint);

                if (startTile != null)
                {
                    foreach (Vector3Int lineTile in lastLine)
                    {
                        tilemap.SetColor(lineTile, Color.white);
                    }
                    lastLine.Clear();
                    foreach (Vector3Int radiusTile in lastRadius)
                    {
                        tilemap.SetColor(radiusTile, Color.white);
                    }
                    lastRadius.Clear();
                    foreach (Vector3Int ringTile in lastRing)
                    {
                        tilemap.SetColor(ringTile, Color.white);
                    }
                    lastRing.Clear();


                    if (Input.GetMouseButtonUp(0))
                    {
                        startTile = null;
                    }
                    else
                    {
                        int n = HexTools.DistanceInTiles(startTile.coordinates, tilepoint);
                        List <Vector3Int> radius =
                            HexTools.GetSpiral(startTile.coordinates, n);
                        //HexTools.GetTilesInRange(startTile.coordinates, n);
                        List <Vector3Int> line = HexTools.GetLine(startTile.coordinates, tilepoint);
                        List <Vector3Int> ring = HexTools.GetRing(startTile.coordinates, n);

                        foreach (Vector3Int radiusTile in radius)
                        {
                            tilemap.SetColor(radiusTile, Color.yellow);
                        }

                        foreach (Vector3Int radiusTile in ring)
                        {
                            tilemap.SetColor(radiusTile, Color.red);
                        }

                        foreach (Vector3Int lineTile in line)
                        {
                            tilemap.SetColor(lineTile, Color.magenta);
                        }



                        lastLine   = line;
                        lastRadius = radius;
                        lastRing   = ring;
                    }
                }
                if (tile == null && Input.GetMouseButtonDown(0))
                {
                    RevealArea(tilepoint, revealRadius);
                }
                else
                {
                    if (tile != null)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            Debug.Log("World: " + worldpoint + " Coords: " + tilepoint);
                            startTile = tile;
                        }
                    }
                    else
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            Debug.Log("Null Tilepoint: " + tilepoint);
                        }
                    }
                }
            }
        }