微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用RoadArchitect

如何解决尝试使用RoadArchitect

在使用RoadArchitect创建道路的过程中,我需要帮助:https://github.com/FritzsHero/RoadArchitect/tree/RewrittenAPI 我正在尝试从纹理中检测所有道路。我创建了一个模型类,以使用一点递归来存储每条检测到的道路:

    [Serializable]
    public class RoadNode : IPathNode
    {
        //[JsonConverter(typeof(RoadNodeConverter))]
        public ConcurrentBag<int> Connections { get; set; } // Todo: IPathNode<T> + RoadNode : IPathNode<RoadNode> + Connections (ConcurrentBag<RoadNode>),but can't be serialized due to StackOverflow and OutOfMemory exceptions

        public Point Position { get; set; }
        public bool Invalid { get; set; }
        public int Thickness { get; set; }
        public ConcurrentBag<int> ParentNodes { get; set; }

        public RoadNode()
        {
            //Connections = new List<RoadNode>();
        }

        public RoadNode(Point position,int thickness)
        //: this()
        {
            Position = position;
            Thickness = thickness;
        }

        public RoadNode(Point position,bool invalid,int thickness)
        //: this()
        {
            Position = position;
            Invalid = invalid;
            Thickness = thickness;
        }

        public RoadNode(int x,int y,int thickness)
            : this(new Point(x,y),thickness)
        {
        }

        public void SetThickness(int thickness)
        {
            // Todo: Call this when needed and thickness == -1
            Thickness = thickness;
        }

        public int GetKey()
        {
            return F.P(Position.x,Position.y,mapWidth,mapHeight);
        }
    }
    public interface IPathNode
    {
        ConcurrentBag<int> Connections { get; }
        Point Position { get; }
        bool Invalid { get; }
    }

在每个加载的块上(我正在使用SebLeague教程创建块),我都获得了该块的所有路径。然后,我尝试对其进行迭代。但是我无法解释结果。

我已经完成了部分任务:

        public static IEnumerable<Road> CreateRoad(this IEnumerable<Point> points,StringBuilder builder)
        {
            if (points.IsNullOrEmpty())
            {
                builder?.AppendLine($"\tskipped...");
                yield break; // This chunk doesn't contain any road. Exiting.
            }

            //var dict = points.Select(p => new {Index = p.GetKey(),Point = p}).ToDictionary(x => x.Index,x => x.Point);
            //var builder = new StringBuilder();
            var roads = GetRoads(points,builder);
            foreach (var list in roads)
            {
                if (list.IsNullOrEmpty()) continue;
                //var first = road.First();
                //var backIndex = ((Point)CityGenerator.SConv.GetRealPositionOnMap(first)).GetKey();

                var road = CreateIndependantRoad(list);
                builder?.AppendLine($"\t... finished road ({road.name}) with {list.Count} nodes.");
                yield return road;
            }
            //Debug.Log(builder?.ToString());
        }

        private static IEnumerable<List<Vector3>> GetRoads(IEnumerable<Point> points,StringBuilder builder)
        {
            var model = RoadGenerator.RoadModel;

            var queue = new Queue<Point>(points);
            int i = 0;

            builder?.AppendLine($"\tcount: {queue.Count}");

            var dictionary = new Dictionary<int,List<Vector3>>();

            while (queue.Count > 0)
            {
                var list = new List<Vector3>();

                var pt = queue.Dequeue();
                var itemIndex = pt.GetKey();
                dictionary.Add(itemIndex,list);

                var node = model.SimplifiedRoadNodes[itemIndex];

                builder?.AppendLine($"\troad iteration: {i}");

                //var conn = node.Connections;
                var nodes = GetRoadNodes(node,ptVal =>
                {
                    if (ptVal.HasValue) queue = new Queue<Point>(queue.Remove(ptVal.Value));
                    return queue;
                },parentNodeIndex => { return dictionary[parentNodeIndex]; },builder);

                foreach (var point in nodes)
                    list.Add(CityGenerator.SConv.GetRealPositionOnMap((Vector2)point).GetHeightForPoint());

                yield return list;
                ++i;
            }
        }

        private static IEnumerable<Point> GetRoadNodes(RoadNode node,Func<Point?,Queue<Point>> queueFunc,Func<int,List<Vector3>> parentFunc,StringBuilder builder,int level = -1)
        {
            if (queueFunc == null) throw new ArgumentNullException(nameof(queueFunc));
            if (parentFunc == null) throw new ArgumentNullException(nameof(parentFunc));

            var conn = node.Connections;
            if (conn.IsNullOrEmpty())
            {
                yield return node.Position;
                yield break;
            }
            if (queueFunc(null).Count == 0) yield break;

            ++level;
            builder?.AppendLine($"{new string('\t',2)}level: {level} -> {queueFunc(null).Count} items");

            //if (conn.Count == 1)
            //{
            //    var firstNode = conn.First().GetNode();
            //    ////var firstPoint = conn.First().GetPoint();

            //    var list = parentFunc(firstNode.ParentNodes.First()); // Todo: parent nodes should be one...
            //    list.Add(CityGenerator.SConv.GetRealPositionOnMap((Vector2)conn.First().GetPoint()).GetHeightForPoint());
            //}
            //else
            {
                foreach (var item in conn)
                {
                    var pt = item.GetPoint();
                    if (!queueFunc(null).Contains(pt)) yield break;
                    yield return pt;
                    if (queueFunc(pt).Count == 0) yield break;

                    var subnode = pt.GetKey().GetNode();
                    var pts = GetRoadNodes(subnode,queueFunc,parentFunc,builder,level);
                    foreach (var point in pts)
                        yield return point;
                }
            }
        }

您可以在此处看到StringBuilder的结果:https://pastebin.com/1tW4V4BC

但是如您所见,我在道路上遇到问题。有些道路只有一个节点,另一些道路只有2个节点...

我认为我目前的实现方式根本不对,因为不是将所有节点分组到一条主要道路上,而是将它们毫无意义地分成路段。

基于这个原因,我创建了这个主题是为了澄清自己,因为我不确定自己在做什么以及应该采取哪种方式。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。