コード例 #1
0
        private static void FillMapArea(Vector3Int spawnCenter, int radius)
        {
            noiseMap = Noise.GenerateNoiseMap(
                radius * 2, radius * 2,
                mapGen.noiseSettings, new Vector2(spawnCenter.y, -spawnCenter.x));

            for (int x = 0; x < noiseMap.GetLength(0); ++x)
            {
                for (int y = 0; y < noiseMap.GetLength(1); ++y)
                {
                    Vector3Int coord = spawnCenter
                                       + new Vector3Int(
                        Mathf.CeilToInt(-noiseMap.GetLength(1) * .5f) + y,
                        Mathf.CeilToInt(-noiseMap.GetLength(0) * .5f) + x, 0);

                    if (mapGen.GetTile(coord) != null || HexTools.DistanceInTiles(coord, spawnCenter) > radius)
                    {
                        continue;
                    }

                    CreateAndSetTileFromNoise(coord, noiseMap[x, y]);
                }
            }
        }
コード例 #2
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);
                        }
                    }
                }
            }
        }
コード例 #3
0
        private static void ConnectClosestRegions(List <Region> allRegions, bool forceAccessibilityFromMainRegion = false)
        {
            List <Region> regionListA = new List <Region>();
            List <Region> regionListB = new List <Region>();

            if (forceAccessibilityFromMainRegion)
            {
                foreach (Region region in allRegions)
                {
                    if (!region.isAccessibleFromMainRegion)
                    {
                        regionListA.Add(region);
                    }
                    else
                    {
                        regionListB.Add(region);
                    }
                }
            }
            else
            {
                regionListA = allRegions;
                regionListB = allRegions;
            }

            int        shortestDist = 0;
            Vector3Int bestTileA    = new Vector3Int();
            Vector3Int bestTileB    = new Vector3Int();
            Region     bestRegionA  = new Region();
            Region     bestRegionB  = new Region();

            bool possibleConnectionFound = false;

            foreach (Region regionA in regionListA)
            {
                if (!forceAccessibilityFromMainRegion)
                {
                    possibleConnectionFound = false;
                    if (regionA.connectedRegions.Count > 0)
                    {
                        continue;
                    }
                }

                foreach (Region regionB in regionListB)
                {
                    if (regionA == regionB || regionA.IsConnected(regionB))
                    {
                        continue;
                    }

                    for (int tileIndexA = 0; tileIndexA < regionA.edgeTiles.Count; ++tileIndexA)
                    {
                        for (int tileIndexB = 0; tileIndexB < regionB.edgeTiles.Count; ++tileIndexB)
                        {
                            Vector3Int tileA = regionA.edgeTiles[tileIndexA];
                            Vector3Int tileB = regionB.edgeTiles[tileIndexB];
                            int        distanceBetweenRooms = HexTools.DistanceInTiles(tileA, tileB);

                            if (distanceBetweenRooms < shortestDist || !possibleConnectionFound)
                            {
                                shortestDist            = (int)distanceBetweenRooms;
                                possibleConnectionFound = true;
                                bestTileA   = tileA;
                                bestTileB   = tileB;
                                bestRegionA = regionA;
                                bestRegionB = regionB;
                            }
                        }
                    }
                }

                if (possibleConnectionFound && !forceAccessibilityFromMainRegion)
                {
                    CreatePassage(bestRegionA, bestRegionB, bestTileA, bestTileB);
                }
            }

            if (possibleConnectionFound && forceAccessibilityFromMainRegion)
            {
                CreatePassage(bestRegionA, bestRegionB, bestTileA, bestTileB);
                ConnectClosestRegions(allRegions, true);
            }

            if (!forceAccessibilityFromMainRegion)
            {
                ConnectClosestRegions(allRegions, true);
            }
        }