Example #1
0
    public static Room CreateMinRoom(ref FloorType[,] blocks, BlueprintNode blueprint, VecInt2 doorPos, Direction doorDir)
    {
        Room room = new Room(doorPos, blueprint.type, FakeRandom.Range(blueprint.sizeMin, blueprint.sizeMax));

        room.range = GetRange(doorPos, blueprint.edgeLenghtMin, blueprint.edgeLenghtMin, doorDir);
        room.dir   = doorDir;
        Array2DTool.SetRange(blocks, room.range, blueprint.type);
        Array2DTool.SetOne(blocks, room.doorPos.x, room.doorPos.y, doorType[(int)room.dir]);
        return(room);
    }
Example #2
0
 public static bool Extend(ref FloorType[,] blocks, Room room)
 {
     Direction[] dirs = GetExtendDirections(room.dir);
     dirs = RandomSort(dirs, FakeRandom.Range(0, 6));
     for (int i = 0; i < 3; i++)
     {
         Direction dir = dirs[i];
         if (dir == Direction.Left)
         {
             RectInt range = new RectInt(room.range.x - 1, room.range.y, 1, room.range.h);
             if (range.x >= Gap && Array2DTool.RangeIsAll(blocks, range, FloorType.Out))  //边上留空Gap空隙,为了计算方便
             {
                 room.range.x -= 1;
                 room.range.w += 1;
                 Array2DTool.SetRange(blocks, range, room.type);
                 return(true);
             }
         }
         else if (dir == Direction.Right)
         {
             RectInt range = new RectInt(room.range.x + room.range.w, room.range.y, 1, room.range.h);
             if (range.x < blocks.GetLength(0) - Gap && Array2DTool.RangeIsAll(blocks, range, FloorType.Out))
             {
                 room.range.w += 1;
                 Array2DTool.SetRange(blocks, range, room.type);
                 return(true);
             }
         }
         else if (dir == Direction.Down)
         {
             RectInt range = new RectInt(room.range.x, room.range.y - 1, room.range.w, 1);
             if (range.y >= Gap && Array2DTool.RangeIsAll(blocks, range, FloorType.Out))
             {
                 room.range.y -= 1;
                 room.range.h += 1;
                 Array2DTool.SetRange(blocks, range, room.type);
                 return(true);
             }
         }
         else if (dir == Direction.Up)
         {
             RectInt range = new RectInt(room.range.x, room.range.y + room.range.h, room.range.w, 1);
             if (range.y < blocks.GetLength(1) - Gap && Array2DTool.RangeIsAll(blocks, range, FloorType.Out))
             {
                 room.range.h += 1;
                 Array2DTool.SetRange(blocks, range, room.type);
                 return(true);
             }
         }
     }
     return(false);
 }
Example #3
0
    private static List <Rect> GetLayerRects(bool[,] layerBlocks)
    {
        int         size  = layerBlocks.GetLength(0);
        List <Rect> rects = new List <Rect>();

        int[,] lineEnd = new int[size, size];//如果水平方向连续格子最后一格,则是连续格子的长度,否则为0
        for (int y = 0; y < size; y++)
        {
            int startX = 0;
            for (int x = 0; x <= size; x++)
            {
                bool cur  = x == size ? false : layerBlocks[x, y];
                bool left = x == 0 ? false : layerBlocks[x - 1, y];

                if (cur && !left)
                {
                    startX = x;
                }
                else if (!cur && left)
                {
                    lineEnd[x - 1, y] = x - startX;
                }
            }
        }
        for (int y = 0; y < size; y++)
        {
            for (int x = 0; x < size; x++)
            {
                int curEndNum = lineEnd[x, y];//当前记录格子结尾数
                if (curEndNum != 0)
                {
                    for (int ty = y + 1; ty <= size; ty++)
                    {
                        int nextLineEndNum = ty == size ? 0 : lineEnd[x, ty];//下一行的结尾数
                        if (nextLineEndNum != curEndNum)
                        {
                            RectInt rectInt = new RectInt(x - curEndNum + 1, y, curEndNum, ty - y);
                            Array2DTool.SetRange(lineEnd, x, y, 1, ty - y, 0);
                            rects.Add(new Rect(rectInt.x / (float)size, rectInt.y / (float)size, rectInt.w / (float)size, rectInt.h / (float)size));
                            break;
                        }
                    }
                }
            }
        }
        return(rects);
    }