Ejemplo n.º 1
0
 public static string[,] ConvertDungeonToText(List <NodeDungeon> dungeons, int height, int width)
 {
     string[,] level = new string[height, width];
     foreach (NodeDungeon node in dungeons)
     {
         if (node.DungeonData.Height > 0 && node.DungeonData.Width > 0 && (node.LeftChild != null || node.RightChild != null))
         {
             throw new Exception("ERROR: non-leaf has area.");
         }
         if (node.DungeonData.Height == 0 && node.DungeonData.Width == 0 && node.LeftChild == null && node.RightChild == null)
         {
             throw new Exception("ERROR: leaf is without size || nDTH=" + node.DungeonData.Height +
                                 " || nDTW=" + node.DungeonData.Width + " || nLC=" + node.LeftChild + " || nRC=" + node.RightChild +
                                 " || rW=" + node.DungeonData.DungeonRoom.Width + " || rH=" + node.DungeonData.DungeonRoom.Height);
         }
         DungeonData dData = node.DungeonData;
         for (int y = dData.TopY; y < dData.TopY + dData.Height; y++)
         {
             for (int x = dData.TopX; x < dData.TopX + dData.Width; x++)
             {
                 level[y, x] = "X";
             }
         }
         DungeonRoom dRoom = node.DungeonData.DungeonRoom;
         for (int y = dRoom.TopY; y < dRoom.TopY + dRoom.Height; y++)
         {
             for (int x = dRoom.TopX; x < dRoom.TopX + dRoom.Width; x++)
             {
                 level[y, x] = "R";
             }
         }
     }
     return(level);
 }
Ejemplo n.º 2
0
        public static Level CreateLevel(int height, int width)
        {
            string[,] textRepr = new string[height, width];
            //This has a size of 0 * 100 and placement in the middle to have a starting point for children.
            DungeonData parentData = new DungeonData {
                TopX = 0, TopY = 100, Width = 0, Height = 0
            };
            DungeonData leftData = new DungeonData {
                TopX = 0, TopY = 0, Width = 250, Height = 100
            };
            DungeonData rightData = new DungeonData {
                TopX = 0, TopY = 100, Width = 250, Height = 100
            };
            NodeDungeon parentNode = new NodeDungeon(parentData);
            TreeDungeon tree       = new TreeDungeon(parentNode);

            tree.InsertNewNode(leftData.TopY, leftData);
            tree.InsertNewNode(rightData.TopY, rightData);
            InsertNewNodeDungeons(tree);

            List <NodeDungeon> dNodes = new List <NodeDungeon>();

            dNodes   = tree.GetDungeonNodes(tree.rootNode, dNodes);
            dNodes   = CreateDungeonRooms(dNodes);
            textRepr = ConvertDungeonToText(dNodes, height, width);

            Level newLevel = new Level(textRepr);

            return(newLevel);
        }
Ejemplo n.º 3
0
        public static DungeonRoom CreateRoom(DungeonData dData)
        {
            DungeonRoom room      = new DungeonRoom();
            int         MAXHEIGHT = dData.Height;
            int         MAXWIDTH  = dData.Width;
            int         width;
            int         height;
            int         topY;
            int         topX;

            int size = Main.randomize.Next(0, 100);

            if (size <= 20)   // Small room.
            {
                height = Main.randomize.Next(0, MAXHEIGHT);
                width  = Main.randomize.Next(0, MAXWIDTH);
            }
            else if (size > 20 && size <= 50)     // Medium-sized room.
            {
                int wideOrTall = Main.randomize.Next(0, 1);
                if (wideOrTall == 0)   // Wide
                {
                    height = Main.randomize.Next(1, MAXHEIGHT);
                    width  = Main.randomize.Next(MAXWIDTH / 2, MAXWIDTH);
                }
                else     // Tall
                {
                    height = Main.randomize.Next(MAXHEIGHT / 2, MAXHEIGHT);
                    width  = Main.randomize.Next(1, MAXWIDTH);
                }
            }
            else     // Large room.
            {
                height = Main.randomize.Next(MAXHEIGHT / 2, MAXHEIGHT);
                width  = Main.randomize.Next(MAXWIDTH / 2, MAXWIDTH);
            }
            topY = MAXHEIGHT == height ? dData.TopY : (Main.randomize.Next(dData.TopY, dData.TopY + MAXHEIGHT - height));
            topX = MAXWIDTH == width ? dData.TopX : (Main.randomize.Next(dData.TopX, dData.TopX + MAXWIDTH - width));
            room = new DungeonRoom {
                Height = height,
                Width  = width,
                TopY   = topY,
                TopX   = topX,
            };
            return(room);
        }
Ejemplo n.º 4
0
        public void InsertNewNode(int nKey, DungeonData dungeonData)
        {
            NodeDungeon newNode = new NodeDungeon(dungeonData);

            if (rootNode == null)
            {
                rootNode = newNode;
            }
            else
            {
                NodeDungeon currentDungeon = rootNode;
                NodeDungeon parentDungeon;
                while (true)
                {
                    parentDungeon = currentDungeon;

                    if (nKey < currentDungeon.NKey)
                    {
                        currentDungeon = currentDungeon.LeftChild;
                        if (currentDungeon == null)
                        {
                            parentDungeon.LeftChild = newNode;
                            return;
                        }
                    }
                    else
                    {
                        currentDungeon = currentDungeon.RightChild;
                        if (currentDungeon == null)
                        {
                            parentDungeon.RightChild = newNode;
                            return;
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public void CreateNewNodeDungeonsFromLeaf(NodeDungeon nodeDungeon)
 {
     if (nodeDungeon.LeftChild == null && nodeDungeon.RightChild == null)
     {
         int MAXHEIGHT = nodeDungeon.DungeonData.Height;
         int MAXWIDTH  = nodeDungeon.DungeonData.Width;
         if (MAXHEIGHT < 4 || MAXWIDTH < 5)   // Too small to make new Dungeon, so ignore.
         {
             return;
         }
         DungeonData leftChildData  = null;
         DungeonData rightChildData = null;
         NodeDungeon leftChild      = null;
         NodeDungeon rightChild     = null;
         DungeonData nodeData       = nodeDungeon.DungeonData;
         int         splitDirection = Main.randomize.Next(0, 2);
         if (splitDirection == 0)   // Split vertically: ||
         {
             int split;
             if (MAXWIDTH == 6)
             {
                 split = 3;
             }
             else
             {
                 split = Main.randomize.Next(1, MAXWIDTH);
             }
             leftChildData = new DungeonData {
                 TopX = nodeData.TopX, TopY = nodeData.TopY, Width = split, Height = nodeData.Height
             };
             rightChildData = new DungeonData {
                 TopX   = nodeData.TopX + split,
                 TopY   = nodeData.TopY,
                 Width  = nodeData.Width - split,
                 Height = nodeData.Height
             };
             leftChild  = new NodeDungeon(leftChildData);
             rightChild = new NodeDungeon(rightChildData);
         }
         else if (splitDirection == 1)     // Split horizontally: ---
         {
             int split;
             if (MAXHEIGHT == 6)
             {
                 split = 3;
             }
             else
             {
                 split = Main.randomize.Next(1, MAXHEIGHT);
             }
             leftChildData = new DungeonData {
                 TopX = nodeData.TopX, TopY = nodeData.TopY, Width = nodeData.Width, Height = split
             };
             rightChildData = new DungeonData {
                 TopX   = nodeData.TopX,
                 TopY   = nodeData.TopY + split,
                 Width  = nodeData.Width,
                 Height = nodeData.Height - split
             };
             leftChild  = new NodeDungeon(leftChildData);
             rightChild = new NodeDungeon(rightChildData);
         }
         nodeDungeon.LeftChild   = leftChild;
         nodeDungeon.RightChild  = rightChild;
         nodeDungeon.DungeonData = null;
     }
 }