Пример #1
0
        public int[,] Generate() {
            for(int i = 0; i < a; i++)
                for(int j = 0; j < a; j++)
                    cells[i, j] = 0;


            for(int i = 0; i < a * 10; i++) {
                IntBounds room = new IntBounds(rnd.Next(0, a), rnd.Next(0, a), rnd.Next(5, 30), rnd.Next(5, 30));
                if(room.LeftTop.X < 0 || room.LeftTop.Y < 0 || room.LeftTop.X >= a || room.LeftTop.Y >= a ||
                    room.RightBottom.X < 0 || room.RightBottom.Y < 0 || room.RightBottom.X >= a || room.RightBottom.Y >= a)
                    continue;
                if(!IsIntersectWithOtherRooms(room))
                    rooms.Add(room);
            }


            foreach(IntBounds room in rooms) {

                for(int i = room.LeftTop.X; i < room.RightBottom.X + 1; i++)
                    for(int j = room.LeftTop.Y; j < room.RightBottom.Y + 1; j++)
                        cells[i, j] = 2;

                for(int i = room.LeftTop.X; i < room.RightBottom.X + 1; i++) {
                    cells[i, room.LeftTop.Y] = 1;
                    cells[i, room.RightBottom.Y] = 1;
                }
                for(int j = room.LeftTop.Y; j < room.RightBottom.Y + 1; j++) {
                    cells[room.LeftTop.X, j] = 1;
                    cells[room.RightBottom.X, j] = 1;
                }
            }

            List<IntPoint> hdoors = new List<IntPoint>();
            List<IntPoint> vdoors = new List<IntPoint>();


            for(int i = 0; i < a; i++)
                for(int j = 0; j < a; j++)
                    if(cells[i, j] == 0)
                        if(i - 1 >= 0 && i + 1 < a && j - 1 >= 0 && j + 1 < a)
                            if(cells[i - 1, j - 1] == 1 && cells[i + 1, j + 1] == 1 && cells[i + 1, j - 1] == 1 && cells[i - 1, j + 1] == 1) {

                                if(cells[i - 1, j] == 1 && cells[i + 1, j] == 1)
                                    hdoors.Add(new IntPoint(i, j));

                                if(cells[i, j - 1] == 1 && cells[i, j + 1] == 1)
                                    vdoors.Add(new IntPoint(i, j));



                            }
            //cells[i, j] = 2;


            for(int i = 0; i < hdoors.Count / 5; i++) {
                int k = rnd.Next(0, hdoors.Count - 1);
                cells[hdoors[k].X, hdoors[k].Y - 1] = 1;
                cells[hdoors[k].X, hdoors[k].Y + 1] = 1;
                cells[hdoors[k].X - 1, hdoors[k].Y] = 2;
                cells[hdoors[k].X + 1, hdoors[k].Y] = 2;
                cells[hdoors[k].X, hdoors[k].Y] = 2;
                hdoors.RemoveAt(k);
            }
            for(int i = 0; i < vdoors.Count / 5; i++) {
                int k = rnd.Next(0, vdoors.Count - 1);

                cells[vdoors[k].X, vdoors[k].Y - 1] = 2;
                cells[vdoors[k].X, vdoors[k].Y + 1] = 2;
                cells[vdoors[k].X - 1, vdoors[k].Y] = 1;
                cells[vdoors[k].X + 1, vdoors[k].Y] = 1;
                cells[vdoors[k].X, vdoors[k].Y] = 2;
                vdoors.RemoveAt(k);
            }


            return cells;

        }
Пример #2
0
 private bool IsIntersectWithOtherRooms(IntBounds room) {
     foreach(var r in rooms)
         if(room.Intersect(r))
             return true;
     return false;
 }
Пример #3
0
        //public IntPoint[] GetIntPoints() {
        //    return new IntPoint[] {
        //        LeftTop,
        //        LeftTop + new IntPoint(Width, 0),
        //        RightBottom,
        //        RightBottom + new IntPoint(0, Height) };
        //}
        public bool Intersect(IntBounds bounds) {

            return !(RightBottom.X < bounds.LeftTop.X || bounds.RightBottom.X < LeftTop.X || RightBottom.Y < bounds.LeftTop.Y || bounds.RightBottom.Y < LeftTop.Y);
            //var IntPoints = bounds.GetIntPoints();
            //for(int i = 0; i < IntPoints.Length; i++)
            //    if(bounds.Contains(IntPoints[i]))
            //        return true;
            //return false;
        }