コード例 #1
0
            public void Split(int minimumRoomSize)
            {
                int direction = -1;

                if (Size.Width > Size.Height)
                {
                    direction = 0;
                }
                else if (Size.Height > Size.Width)
                {
                    direction = 1;
                }
                else
                {
                    direction = Program.Game.Random.Next(2);
                }

                if (direction == 0)
                {
                    var childRoomWidth = (Size.Width / 2) + Program.Game.Random.Next(-3, 4);

                    if (childRoomWidth >= minimumRoomSize && Size.Width - childRoomWidth >= minimumRoomSize)
                    {
                        ChildA = new BspNode(this, new Rectangle(Size.Left, Size.Top, childRoomWidth, Size.Height));
                        ChildA.Split(minimumRoomSize);

                        ChildB = new BspNode(this, new Rectangle(Size.Left + childRoomWidth, Size.Top, Size.Width - childRoomWidth, Size.Height));
                        ChildB.Split(minimumRoomSize);
                    }
                }
                else
                {
                    var childRoomHeight = (Size.Height / 2) + Program.Game.Random.Next(-3, 4);

                    if (childRoomHeight >= minimumRoomSize && Size.Height - childRoomHeight >= minimumRoomSize)
                    {
                        ChildA = new BspNode(this, new Rectangle(Size.Left, Size.Top, Size.Width, childRoomHeight));
                        ChildA.Split(minimumRoomSize);

                        ChildB = new BspNode(this, new Rectangle(Size.Left, Size.Top + childRoomHeight, Size.Width, Size.Height - childRoomHeight));
                        ChildB.Split(minimumRoomSize);
                    }
                }
            }
コード例 #2
0
        public override Map Generate(int width, int height)
        {
            var map = new Map(width, height);

            rooms = new List <Rectangle>();

            var entireMap = new Rectangle(0, 0, map.Width, map.Height);

            var root = new BspNode(null, entireMap);

            root.Split(MinimumRoomSize);

            CreateRooms(root, map);

            var spawnRoom = root.GetRoom();

            Program.Game.Player.X = spawnRoom.Center.X;
            Program.Game.Player.Y = spawnRoom.Center.Y;

            var stairsRoom = rooms.Last();

            var stairs = ItemFactory.CreateStairs(stairsRoom.Center.X, stairsRoom.Center.Y, Program.Game.DungeonLevel);

            Program.Game.Entities.Add(stairs);

            foreach (var room in rooms)
            {
                // Don't spawn monsters in the player's room.
                if (room != spawnRoom)
                {
                    SpawnMonsters(room);
                }

                SpawnItems(map, room);
            }

            return(map);
        }