Example #1
0
        public static Matrix GenerateMap(DeviceGroup[] deviceGroups, Device[] devices)
        {
            Node rootNode = new Node("Default", "", "Red");

            DeviceGroup[] orderedDeviceGroups = deviceGroups.OrderBy(g => g.Level).ToArray();

            List<Node> nodes = new List<Node>();
            nodes.Add(rootNode);

            foreach (DeviceGroup deviceGroup in orderedDeviceGroups)
            {
                AddGroup(rootNode, deviceGroup, nodes, devices);
            }

            Matrix matrix = RoomGenerator.Generate(rootNode);

            matrix.Tiles[matrix.Tiles.Count/2][3].Add(new Tile(TileType.Player));

            //Closing the front door
            foreach (var tiles in matrix.Tiles)
            {
                if (tiles[0].Any(t => t.TileType == TileType.Door))
                {
                    tiles[0].RemoveAll(t => t.TileType == TileType.Door);
                    tiles[0].Add(new Tile(TileType.Wall));
                }
            }

            return matrix;
        }
        public static Matrix Generate(Node parentNode)
        {
            Dictionary<Node, int> offsets = new Dictionary<Node, int>();

            int depth = parentNode.GetMaxDepth();
            int height = depth * (Settings.DefaultHeight + Settings.RoomCoridorLenght);

            var allNodes = new[] { parentNode }.FancyFlatten(n => true, n => n.Nodes).ToArray();

            var biggestLevel = allNodes.GroupBy(n => n.Level).OrderBy(g => g.Sum(n => n.GetSize())).Last();

            Console.WriteLine("Biggest level id " + biggestLevel.Key);
            Console.WriteLine("Biggest level count " + biggestLevel.Sum(n => n.GetSize()));

            int labyrinthWidth = parentNode.GetSize();

            Matrix matrix = new Matrix(labyrinthWidth, height);

            int yOffset = 0;
            int xOffset = 0;

            foreach (var levelGroup in allNodes.GroupBy(n => n.Level))
            {
                Node currentParent = null;

                foreach (Node groupNode in levelGroup)
                {
                    if (groupNode.Parent != null)
                    {
                        if (currentParent != groupNode.Parent)
                        {
                            yOffset = 0;
                        }
                    }

                    currentParent = groupNode.Parent;

                    Room room = new Room(groupNode.GetSize(), groupNode.Name, groupNode.Color, groupNode.Devices);

                    var parentOffset = 0;

                    if (groupNode.Parent != null)
                        parentOffset = offsets[groupNode.Parent];

                    matrix.Merge(room.AsMatrix(), yOffset + parentOffset, xOffset);

                    offsets.Add(groupNode, yOffset + parentOffset);

                    yOffset += groupNode.GetSize() + Settings.Scale;
                }

                xOffset += Settings.DefaultHeight + Settings.RoomCoridorLenght - 1;
                yOffset = 0;
            }

            return matrix;
        }
Example #3
0
        private static void AddGroup(Node topNode, DeviceGroup deviceGroup, List<Node> processedNodes, Device[] devices)
        {
            string[] paths = deviceGroup.Path.Split(new[] {@"\", @"\\"}, StringSplitOptions.RemoveEmptyEntries);

            Node groupNode = null;

            if (paths.Length == 1)
            {
                groupNode = new Node(topNode, deviceGroup.Path, deviceGroup.Name, deviceGroup.Icon);

                groupNode.SetDevices(devices.Where(d => d.Path == deviceGroup.Path).Select(d => new DeviceInfo
                {
                    DeviceId = d.DeviceId
                }));

                topNode.Nodes.Add(groupNode);
                processedNodes.Add(groupNode);

                return;
            }

            int indexOfName = deviceGroup.Path.LastIndexOf(@"\", StringComparison.Ordinal);

            string parentPath = deviceGroup.Path.Remove(indexOfName);

            Node parentNode = processedNodes.Single(n => n.Path == parentPath);

            groupNode = new Node(parentNode, deviceGroup.Path, deviceGroup.Name, deviceGroup.Icon);

            groupNode.SetDevices(devices.Where(d => d.Path == deviceGroup.Path).Select(d => new DeviceInfo
            {
                DeviceId = d.DeviceId
            }));

            parentNode.Nodes.Add(groupNode);
            processedNodes.Add(groupNode);
        }
Example #4
0
 public Node(Node parent, string path, string name, string color)
     : this(path, name, color)
 {
     Parent = parent;
 }