private static void Generate(Node<Building> root, City city) { var stackX = new Stack<Tuple<Node<Building>, Position, int>>(); stackX.Push(new Tuple<Node<Building>, Position, int>(root,new Position(200,200),1)); bool u = true; double currentUp = 0; double currentDown = 0; double currentLeft = 0; double currentRigh = 0; while (stackX.Count > 0) { var node = stackX.Pop(); currentUp = 0; currentDown = 0; currentLeft = 0; currentRigh = 0; foreach (var item in node.Item1.Children) { if (item.Value != null) { item.Value.Position = GeneratePosition(u, ref currentUp, ref currentDown, ref currentLeft, ref currentRigh, node.Item3, item.Value.Size, node.Item2); u = !u; city.Buildings.Add(item.Value); } else { if (node.Item3 == 1) { Position position; if (u) position = new Position(node.Item2.X + currentUp, node.Item2.Y); else position = new Position(node.Item2.X + currentDown, node.Item2.Y); var tuple = new Tuple<Node<Building>, Position, int>(item, position, u ? 2 : 4); stackX.Push(tuple); } else if (node.Item3 == 3) { Position position; if (u) position = new Position(node.Item2.X - currentUp, node.Item2.Y); else position = new Position(node.Item2.X - currentDown, node.Item2.Y); var tuple = new Tuple<Node<Building>, Position, int>(item, position, u ? 2 : 4); stackX.Push(tuple); } else if (node.Item3 == 2) { Position position; if (u) position = new Position(node.Item2.X, node.Item2.Y + currentLeft); else position = new Position(node.Item2.X, node.Item2.Y + currentRigh); var tuple = new Tuple<Node<Building>, Position, int>(item, position, u ? 1 : 3); stackX.Push(tuple); } else if (node.Item3 == 4) { Position position; if (u) position = new Position(node.Item2.X, node.Item2.Y - currentLeft); else position = new Position(node.Item2.X, node.Item2.Y - currentRigh); var tuple = new Tuple<Node<Building>, Position, int>(item, position, u ? 1 : 3); stackX.Push(tuple); } u = !u; } } } }
public static City Generate(Node<Building> node) { City city = new City(); Generate(node, city); return city; }