예제 #1
0
    private void FillCaveGaps(int steps, int minNeighboursToFill, int blockId, FindNeighboursMode findMode = FindNeighboursMode.NEIGHBOURS_4)
    {
        CavesGenerator caveGenerator = new CavesGenerator(this);

        caveGenerator.FillGaps(steps, blockId, findMode, minNeighboursToFill);
        GenerateImg();
    }
    public void GenerateCave(int blockId, int voidBlockId, int steps, float density, FindNeighboursMode findMode = FindNeighboursMode.NEIGHBOURS_4, bool unique = false)
    {
        //Select the first point
        int randX = (int)(Random.value * mapSize.x);
        int randY = (int)(Random.value * mapSize.y);

        while (map[randX, randY] == 0)
        {
            randX = (int)(Random.value * mapSize.x);
            randY = (int)(Random.value * mapSize.y);
        }

        int uniqueId = 0;

        if (unique)
        {
            uniqueId = blockId;
            blockId  = Random.Range(int.MaxValue - 1000, int.MaxValue);
        }

        //Create the first point
        mapGenerator.map[randX, randY] = blockId;

        //Create the cave
        for (int step = 0; step < steps; step++)
        {
            for (int x = 0; x < (int)mapSize.x; x++)
            {
                for (int y = 0; y < (int)mapSize.y; y++)
                {
                    if (map[x, y] != blockId && map[x, y] != voidBlockId)
                    {
                        if (FindNeighbours(x, y, blockId, findMode) >= 1)
                        {
                            if (Random.Range(0f, 1f) > density)
                            {
                                map[x, y] = blockId;
                            }
                        }
                    }
                }
            }
        }

        if (unique)
        {
            for (int x = 0; x < (int)mapSize.x; x++)
            {
                for (int y = 0; y < (int)mapSize.y; y++)
                {
                    if (map[x, y] == blockId)
                    {
                        map[x, y] = uniqueId;
                    }
                }
            }
        }

        mapGenerator.SetMap(map);
    }
    //Neighbours
    private int FindNeighbours(int x, int y, int blockId, FindNeighboursMode mode)
    {
        List <Vector2> neigh = new List <Vector2>();
        int            count = 0;

        //Mode: 1 = 4 neighbours (Up, down, right, left)
        //Mode: 1 = 8 neighbours (Up, down, right, left, Up-Right, Up-Left, Down-Right, Down-Left)

        if (mode == FindNeighboursMode.NEIGHBOURS_4)   //4 Neightbours
        {
            neigh.Add(new Vector2(x - 1, y));
            neigh.Add(new Vector2(x + 1, y));
            neigh.Add(new Vector2(x, y - 1));
            neigh.Add(new Vector2(x, y + 1));
        }
        else     //8 Neightbours
        {
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    if (!(i == 0 && j == 0))
                    {
                        neigh.Add(new Vector2(x + i, y + j));
                    }
                }
            }
        }

        //Check for outside blocks and remove it from the list
        List <Vector2> newNeigh = new List <Vector2>();

        for (int i = 0; i < neigh.Count; i++)
        {
            if (IsInsideMap(neigh[i]))
            {
                newNeigh.Add(neigh[i]);
            }
        }
        neigh = newNeigh;

        //Count the neighbours
        for (int i = 0; i < neigh.Count; i++)
        {
            if (map[(int)neigh[i].x, (int)neigh[i].y] == blockId)
            {
                count++;
            }
        }

        return(count);
    }
    public void FillGaps(int steps, int blockId, FindNeighboursMode findMode, int minNeighboursToFill)
    {
        map = mapGenerator.GetMap();

        for (int step = 0; step < steps; step++)
        {
            for (int x = 0; x < (int)mapSize.x; x++)
            {
                for (int y = 0; y < (int)mapSize.y; y++)
                {
                    if (map[x, y] != blockId)
                    {
                        if (FindNeighbours(x, y, blockId, findMode) >= minNeighboursToFill)
                        {
                            map[x, y] = blockId;
                        }
                    }
                }
            }
        }

        mapGenerator.SetMap(map);
    }
예제 #5
0
    //Old code
    //private void GenerateCaves (int caves = 1) {
    //    for (int i = 0; i < caves; i++) {
    //        CavesGenerator caveGenerator = new CavesGenerator(this);
    //        List<Vector2> caveBlocks = caveGenerator.GenerateCave();

    //        for (int j = 0; j < caveBlocks.Count; j++) {
    //            int x = (int)caveBlocks[j].x;
    //            int y = (int)caveBlocks[j].y;

    //            //print("X|Y: " + x + "|" + y);

    //            if (map[x,y] == 1) {
    //                map[x, y] = 3;
    //            }
    //        }
    //    }

    //    GenerateImg();
    //}

    private void GenerateCaves(int cavesAmmount = 1, int blockId = 3, int voidBlockId = 0, int stps = 3, float density = .5f, FindNeighboursMode findMode = FindNeighboursMode.NEIGHBOURS_4)
    {
        CavesGenerator caveGenerator = new CavesGenerator(this);

        for (int i = 0; i < cavesAmmount; i++)
        {
            caveGenerator.GenerateCave(blockId, voidBlockId, steps, density, findMode, true);
        }

        GenerateImg();
    }