Пример #1
0
        Vector2Int OutLineGenerate(Grid2DInt grid)
        {
            var        HV       = Random.Range(0, 2);
            int        pos      = 0;
            var        TBLR     = Random.Range(0, 2);
            Vector2Int pointPos = new Vector2Int();

            if (HV == 0)
            {
                pos = Random.Range(1, grid.Width - 1);
            }
            else
            {
                pos = Random.Range(1, grid.Height - 1);
            }
            if ((HV == 0) && (TBLR == 0))
            {
                pointPos = new Vector2Int(pos, 0);
            }
            if ((HV == 0) && (TBLR == 1))
            {
                pointPos = new Vector2Int(pos, grid.Height - 1);
            }

            if ((HV == 1) && (TBLR == 0))
            {
                pointPos = new Vector2Int(0, pos);
            }
            if ((HV == 1) && (TBLR == 1))
            {
                pointPos = new Vector2Int(grid.Width - 1, pos);
            }
            return(pointPos);
        }
Пример #2
0
 public string PrintBSP(Grid2DInt map)
 {
     foreach (var r in rooms)
     {
         Grid2DPaint.Rect <int>(map, r, 0);
     }
     return(map.Print());
 }
Пример #3
0
    public void Init2D()
    {
        grid2D = new Grid2DInt(GridSize, GridSize, 0);
        dla2   = new DLA2D();
        dla2.SetSeedPoint(grid2D, grid2D.Center);

        //Grid2DPaint.Line(grid2D, SrcPoint,DstPoint,LineWidth);

        text.text = grid2D.Print();
        render.Draw(grid2D);
    }
Пример #4
0
        public static string SelfTest()
        {
            Grid2DInt map = new Grid2DInt(64, 64, 255);

            if (map == null)
            {
                Debug.LogError("null map");
            }
            Division(map, new RectInt(0, 0, 64, 64));
            return(map.Print());
        }
Пример #5
0
        public static NMap RoomWallMap(int width, int height)
        {
            Grid2DInt map = Grid2D <int> .Create(width, height, 255) as Grid2DInt;

            NBSP.Division(map, new RectInt(0, 0, map.Width, map.Height));
            var blobs = NBlob.Find(map, 0);

            blobs.FillByLeftBlob(map, 12);
            blobs.AxisConnect(map, 0);
            //NLocationRecogition.FindDoor(map, 5);
            return(map);
        }
Пример #6
0
        public void Step(Grid2DInt grid, Grid2DInt.CorridorType type = Grid2DInt.CorridorType.Ortho)
        {
            //var walkerPos = OutLineGenerate(grid);
            var        walkerPos = RandomGenerate(grid);
            Walker2D   walker    = new Walker2D(walkerPos);
            Vector2Int curPos    = Vector2Int.zero;

            do
            {
                curPos = walker.Step(grid);
            } while(!grid.NextTo(curPos, type, 255));
            Grid2DPaint.Diamond(grid, curPos.x, curPos.y, 2);
            //grid[curPos.x,curPos.y] = 255;
        }
Пример #7
0
    public void Draw(Grid2DInt grid, float MaxValue = 1f)
    {
        texture            = new Texture2D(grid.Width, grid.Height);
        texture.filterMode = FilterMode.Point;
        Color[] colors = new Color[grid.Width * grid.Height];
        int     index  = 0;

        for (int row = 0; row < grid.Height; row++)
        {
            for (int col = 0; col < grid.Width; col++)
            {
                float gray = grid[col, row] / MaxValue;
                colors[index] = colorRegion.Evaluate(gray);
                index++;
            }
        }
        texture.SetPixels(colors);
        texture.Apply();
        image.sprite = Sprite.Create(texture, new Rect(0, 0, grid.Width, grid.Height), Vector2.zero);
    }
Пример #8
0
        public static void Division(Grid2DInt map, RectInt rect, int minSideSize = 12, int maxSideSize = 24)
        {
            if (rect.width <= maxSideSize && rect.height <= maxSideSize)
            {
                if (Random.value < 0.5f)
                {
                    Grid2DPaint.Rect(map, RandomSubRect(rect), 0);
                    return;
                }
            }

            if (rect.width <= minSideSize && rect.height <= minSideSize)
            {
                Grid2DPaint.Rect(map, RandomSubRect(rect), 0);
                return;
            }

            bool isVer = Random.value > .5f;

            isVer = (float)rect.width / (float)rect.height > 1.25f;

            int posX = Random.Range(rect.width / 4, rect.width * 3 / 4) + rect.x;
            int posY = Random.Range(rect.height / 4, rect.height * 3 / 4) + rect.y;

            if (isVer)
            {
                //map.SetBlock(posX, rect.Y, 1, rect.H, 0);
                Division(map, new RectInt(rect.x, rect.x, posX - rect.x, rect.height));
                Division(map, new RectInt(posX + 1, rect.y, rect.x + rect.width - posX - 1, rect.height));
            }
            else
            {
                //map.SetBlock(rect.X, posY, rect.W, 1, 0);
                Division(map, new RectInt(rect.x, rect.y, rect.width, posY - rect.y));
                Division(map, new RectInt(rect.x, posY + 1, rect.width, rect.height - posY + rect.y - 1));
            }
        }
Пример #9
0
    public void Generator()
    {
        grid = new Grid2DInt(mapSize, mapSize);
        grid.Load();

        var border = WaterShed.Run <int>(grid.GetGrid(), Vector2Int.zero, 0, 1);

        Grid2D <byte> tmpGrid = Grid2D <byte> .Create(grid.Width, grid.Height, 0);

        Grid2D <int> src = Grid2D <int> .Create(grid.Width, grid.Height, 255);

        src.data = grid.data;

        var allfloor = ObjBuilder.AddNode("Floor", transform);
        var allWall  = ObjBuilder.AddNode("Wall", transform);

        for (int y = 0; y < grid.Height; y++)
        {
            for (int x = 0; x < grid.Width; x++)
            {
                if (grid[x, y] == 255)
                {
                    var pos   = new Vector3(x, 0, y);
                    var floor = ObjBuilder.AddNode("floor", allfloor, pos);
                    //ObjBuilder.AddObject(floorPack.get(), new Vector3(floorOffset.x, 0, floorOffset.y), floor);
                    var f = ObjBuilder.AddObj(floorPack.get(), Vector3.zero, floor);
                    ObjBuilder.AdjFace2(f.transform, (ObjBuilder.FaceToward)Random.Range(0, 4));
                    tmpGrid[x, y] = 1;


                    //ToDoAddWall;
                    if (grid.Is(new Vector2Int(x, y) + Vector2Int.up, 0))
                    {
                        if (border.Is(new Vector2Int(x, y) + Vector2Int.up, 1))
                        {
                            var wall = ObjBuilder.AddObj(caveWall, pos + new Vector3(0, 0, .5f), allWall.transform);
                            wall.transform.Rotate(new Vector3(0, 180, 0));
                            tmpGrid[x, y] = 2;
                        }
                    }
                    if (grid.Is(new Vector2Int(x, y) + Vector2Int.down, 0))
                    {
                        if (border.Is(new Vector2Int(x, y) + Vector2Int.down, 1))
                        {
                            var wall = ObjBuilder.AddObj(caveWall, pos + new Vector3(0, 0, -.5f), allWall.transform);
                            tmpGrid[x, y] = 3;
                        }
                    }
                    if (grid.Is(new Vector2Int(x, y) + Vector2Int.left, 0))
                    {
                        if (border.Is(new Vector2Int(x, y) + Vector2Int.left, 1))
                        {
                            var wall = ObjBuilder.AddObj(caveWall, pos + new Vector3(-.5f, 0, 0), allWall.transform);
                            wall.transform.Rotate(new Vector3(0, 90, 0));
                            tmpGrid[x, y] = 4;
                        }
                    }
                    if (grid.Is(new Vector2Int(x, y) + Vector2Int.right, 0))
                    {
                        if (border.Is(new Vector2Int(x, y) + Vector2Int.right, 1))
                        {
                            var wall = ObjBuilder.AddObj(caveWall, pos + new Vector3(.5f, 0, 0), allWall.transform);
                            wall.transform.Rotate(new Vector3(0, -90, 0));
                            tmpGrid[x, y] = 5;
                        }
                    }
                }
                else
                {
                    if (grid.Is(new Vector2Int(x, y) + Vector2Int.up, 255) &&
                        grid.Is(new Vector2Int(x, y) + Vector2Int.down, 255) &&
                        grid.Is(new Vector2Int(x, y) + Vector2Int.left, 255) &&
                        grid.Is(new Vector2Int(x, y) + Vector2Int.right, 255))
                    {
                        var floor = ObjBuilder.AddNode("floor", allfloor, new Vector3(x, 0, y));
                        ObjBuilder.AddObj(floorPack.get(), new Vector3(floorOffset.x, 0, floorOffset.y), floor);
                        tmpGrid[x, y] = 6;
                    }
                }
            }
        }
        var renders = allfloor.GetComponentsInChildren <MeshRenderer>();

        foreach (var r in renders)
        {
            r.gameObject.AddComponent <MeshCollider>().convex = true;
            if (Random.value < lavaRate)
            {
                r.material = material;
                var l = Instantiate(light, r.transform);
                l.transform.Translate(new Vector3(-tileSize / 2, 0, tileSize / 2));
            }
            r.material.color = new Color(0.3f, 0.3f, 0.3f, 1);
        }

        for (int raw = 0; raw < tmpGrid.Height; raw++)
        {
            for (int col = 0; col < tmpGrid.Width; col++)
            {
                var pos = new Vector3(col, 0, raw);
                if (tmpGrid[col, raw] == 6)
                {
                    ObjBuilder.AddObj(centerCorner, pos, transform);
                }
                if (tmpGrid[col, raw] == 1)
                {
                    if (Random.value < .6f)
                    {
                        var misc = ObjBuilder.AddObj(floorMisc.get(), pos, transform);
                        misc.transform.Translate(new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)) * tileSize / 2);
                    }
                }
            }
        }
    }
Пример #10
0
 Vector2Int RandomGenerate(Grid2DInt grid)
 {
     return(new Vector2Int(Random.Range(0, grid.Width),
                           Random.Range(0, grid.Height)));
 }
Пример #11
0
 public void SetSeedPoint(Grid2DInt grid, Vector2Int seed, Walker2D.WalkType type = Walker2D.WalkType.Direction4)
 {
     Grid2DPaint.Diamond(grid, seed.x, seed.y, 2);
     Walker2D.InitMovTab(type);
 }
Пример #12
0
 public void DungeonStep()
 {
     bspMap = new Grid2DInt(64, 64, 255);
     bsp.Step();
     text.text = bsp.PrintBSP(bspMap);
 }