Пример #1
0
    void OnDrawGizmos()
    {
        if (!m_isInit || cityPoints == null || pathInfo == null)
        {
            return;
        }

        Gizmos.color = new Color(1, 0, 0, 1F);

        IEnumerator enumerator = cityPoints.Data.Values.GetEnumerator();

        while (enumerator.MoveNext())
        {
            XMLDataCityPoints info       = (XMLDataCityPoints)enumerator.Current;
            string[]          linkPoints = info.Path.Split(',');
            for (int i = 0; i < linkPoints.Length - 1; i++)
            {
                if (linkPoints[i] == "")
                {
                    continue;
                }
                XMLDataPathInfo p1 = pathInfo.GetInfoById(System.Convert.ToInt32(linkPoints[i]));
                XMLDataPathInfo p2 = pathInfo.GetInfoById(System.Convert.ToInt32(linkPoints[i + 1]));

                Vector3 point1 = Utility.GetPoint(p1.Position);
                Vector3 point2 = Utility.GetPoint(p2.Position);

                Gizmos.DrawLine(point1, point2);
            }
        }
    }
Пример #2
0
    static int GetInfoByName(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        XMLLoader <XMLDataPathInfo> obj = LuaScriptMgr.GetNetObject <XMLLoader <XMLDataPathInfo> >(L, 1);
        string          arg0            = LuaScriptMgr.GetLuaString(L, 2);
        XMLDataPathInfo o = obj.GetInfoByName(arg0);

        LuaScriptMgr.PushObject(L, o);
        return(1);
    }
Пример #3
0
    static int GetInfoById(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        XMLLoader <XMLDataPathInfo> obj = LuaScriptMgr.GetNetObject <XMLLoader <XMLDataPathInfo> >(L, 1);
        int             arg0            = (int)LuaScriptMgr.GetNumber(L, 2);
        XMLDataPathInfo o = obj.GetInfoById(arg0);

        LuaScriptMgr.PushObject(L, o);
        return(1);
    }
Пример #4
0
    static int set_ID(IntPtr L)
    {
        object o = LuaScriptMgr.GetLuaObject(L, 1);

        if (o == null)
        {
            LuaDLL.luaL_error(L, "unknown member name ID");
        }

        XMLDataPathInfo obj = (XMLDataPathInfo)o;

        obj.ID = (int)LuaScriptMgr.GetNumber(L, 3);
        return(0);
    }
Пример #5
0
    static int get_LinkPoints(IntPtr L)
    {
        object o = LuaScriptMgr.GetLuaObject(L, 1);

        if (o == null)
        {
            LuaDLL.luaL_error(L, "unknown member name LinkPoints");
        }

        XMLDataPathInfo obj = (XMLDataPathInfo)o;

        LuaScriptMgr.Push(L, obj.LinkPoints);
        return(1);
    }
Пример #6
0
    static int set_Position(IntPtr L)
    {
        object o = LuaScriptMgr.GetLuaObject(L, 1);

        if (o == null)
        {
            LuaDLL.luaL_error(L, "unknown member name Position");
        }

        XMLDataPathInfo obj = (XMLDataPathInfo)o;

        obj.Position = LuaScriptMgr.GetString(L, 3);
        return(0);
    }
Пример #7
0
    static void Execute()
    {
        if (Selection.activeTransform == null)
        {
            return;
        }
        if (Selection.activeTransform.childCount == 0)
        {
            return;
        }
        if (Selection.activeTransform.name != "CityPoints")
        {
            return;
        }

        Transform root = Selection.activeTransform;

        Transform[] points = new Transform[root.childCount];
        for (int i = 0; i < root.childCount; i++)
        {
            points[i] = root.GetChild(i);
        }

        XMLLoader <XMLDataPathInfo> pathInfo = new XMLLoader <XMLDataPathInfo>(XMLConfigPath.PathInfo);
        XMLLoader <XMLDataCity>     cityInfo = new XMLLoader <XMLDataCity>(XMLConfigPath.City);

        XmlDocument xmlDoc = new XmlDocument();

        XmlDeclaration xmldecl;

        xmldecl          = xmlDoc.CreateXmlDeclaration("1.0", null, null);
        xmldecl.Encoding = "UTF-8";
        xmlDoc.AppendChild(xmldecl);

        XmlElement rootElement = xmlDoc.CreateElement("root");

        xmlDoc.AppendChild(rootElement);

        int cityNum = points.Length;

        int[] cityPoints = new int[cityNum];
        int[] cityID     = new int[cityNum];

        int step = GeneratePathInfo.m_step;

        int[] xStep = new int[] { 0, 0, step, step, step, 0, -step, -step, -step };
        int[] yStep = new int[] { 0, step, step, 0, -step, -step, -step, 0, step };

        for (int i = 0; i < points.Length; i++)
        {
            if (points[i].name == "")
            {
                continue;
            }
            if (!cityInfo.Data.ContainsKey(System.Convert.ToInt32(points[i].name)))
            {
                continue;
            }

            Vector3 position = points[i].transform.position;
            position.x = ((int)position.x / step) * step;
            position.y = ((int)position.y / step) * step;

            string p     = "";
            int    index = 0;

            bool isFind = false;

            for (int n = 0; n < 9; n++)
            {
                p = ((int)(position.x + xStep[n])).ToString() + "," + ((int)(position.y + yStep[n])).ToString();

                IEnumerator enumerator = pathInfo.Data.Keys.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    XMLDataPathInfo current = pathInfo.GetInfoById((int)enumerator.Current);
                    if (current.Position == p)
                    {
                        isFind = true;
                        index  = (int)enumerator.Current;
                        int id = System.Convert.ToInt32(points[i].name);
                        cityID[i]     = id;
                        cityPoints[i] = index;
                        break;
                    }
                }
                if (isFind)
                {
                    break;
                }
            }

            if (!isFind)
            {
                string errorMessage = "城市点 " + points[i].name + " 不在寻路点上!" + p;
                Debug.LogError(errorMessage);
                EditorUtility.DisplayDialog("失败", errorMessage, "确定");
                return;
            }
        }

        //输出城市间的路线
        int ID = 1;

        for (int i = 0; i < cityNum - 1; i++)
        {
            for (int j = i + 1; j < cityNum; j++)
            {
                List <int> path = PathFinding.GetPath(cityPoints[i], cityPoints[j], pathInfo.Data);

                XMLDataCity currentInfo = cityInfo.GetInfoById(cityID[i]);
                XMLDataCity nextInfo    = cityInfo.GetInfoById(cityID[j]);
                if (path == null)
                {
                    string errorMessage = "城市 " + currentInfo.Name + " 到城市 " + nextInfo.Name + " 的路不通!";
                    Debug.LogError(errorMessage);
                    EditorUtility.DisplayDialog("失败", errorMessage, "确定");
                    return;
                }

                // 去掉不是相邻的城市的点
                bool flag = false;
                for (int n = 0; n < path.Count; n++)
                {
                    for (int k = 0; k < cityNum; k++)
                    {
                        if (k == i || k == j)
                        {
                            continue;
                        }
                        Vector3 p1 = Utility.GetPoint(pathInfo.GetInfoById(cityPoints[k]).Position);
                        Vector3 p2 = Utility.GetPoint(pathInfo.GetInfoById(path[n]).Position);
                        if (Vector3.Distance(p1, p2) < 4 * step)
                        {
                            flag = true;
                        }
                    }
                }

                if (flag)
                {
                    continue;
                }

                int[]    intArr  = path.ToArray();
                string[] strArr  = intArr.Select(v => v.ToString()).ToArray();
                string   pathstr = string.Join(",", strArr);

                XmlElement node = xmlDoc.CreateElement("RECORD");
                node.SetAttribute("ID", ID.ToString());
                node.SetAttribute("FromCity", cityID[i].ToString());
                node.SetAttribute("ToCity", cityID[j].ToString());
                node.SetAttribute("FromPoint", cityPoints[i].ToString());
                node.SetAttribute("ToPoint", cityPoints[j].ToString());
                node.SetAttribute("Path", pathstr);

                rootElement.AppendChild(node);

                ID++;
                EditorUtility.DisplayProgressBar("Processing", currentInfo.Name, 1.0f * i / cityNum);
            }
        }

        xmlDoc.Save(m_configPath);
        AssetDatabase.Refresh();

        EditorUtility.ClearProgressBar();
        EditorUtility.DisplayDialog("成功", "城市位置信息生成完成!", "确定");
    }
Пример #8
0
    public static List <int> GetPath(int start, int end, Dictionary <object, object> pathInfo)
    {
        if (start == end)
        {
            Debugging.LogError("Function:GetPath start == end");
            return(null);
        }
        if ((!pathInfo.ContainsKey(start)) || (!pathInfo.ContainsKey(end)))
        {
            Debugging.LogError("Function:GetPath pathInfoContainsKey(start || end)");
            return(null);
        }

        List <int> queue      = new List <int>(); // 搜索序列
        List <int> queueIndex = new List <int>(); // 当前点的索引
        List <int> visitList  = new List <int>(); // 保存所有搜索过的节点
        List <int> visitIndex = new List <int>(); // visit的父节点

        bool[] flags = new bool[pathInfo.Count];  // 标志该点是否被搜索过

        int searchIndex = -1;

        // 加入起始点
        queue.Add(start);
        visitList.Add(start);
        visitIndex.Add(0);
        queueIndex.Add(visitList.Count - 1);
        flags[start - 1] = true;

        bool isFind = false;

        while (!isFind)
        {
            //全部相邻点搜索完毕,没有找到合适的点
            if (queue.Count == 0)
            {
                break;
            }

            searchIndex = queueIndex[0];

            XMLDataPathInfo point      = (XMLDataPathInfo)pathInfo[queue[0]];
            string[]        nextPoints = point.LinkPoints.Split(',');

            List <int> listNext = new List <int>();
            for (int i = 0; i < nextPoints.Length; i++)
            {
                int pointIdx = System.Convert.ToInt32(nextPoints[i]);

                if (flags[pointIdx - 1] == false)
                {
                    //标志该点
                    flags[pointIdx - 1] = true;
                }
                else
                {
                    //如果该点已经搜索过了就继续下一个
                    continue;
                }

                listNext.Add(pointIdx);

                if (pointIdx == end)
                {
                    isFind = true;
                    break;
                }
            }

            //对相邻的点进行排序
            for (int i = 0; i < listNext.Count - 1; i++)
            {
                for (int j = i + 1; j < listNext.Count; j++)
                {
                    Vector3 p1       = Utility.GetPoint((pathInfo[listNext[i]] as XMLDataPathInfo).Position);
                    Vector3 p2       = Utility.GetPoint((pathInfo[listNext[j]] as XMLDataPathInfo).Position);
                    Vector3 endPoint = Utility.GetPoint((pathInfo[end] as XMLDataPathInfo).Position);

                    if (Vector3.Distance(p1, endPoint) > Vector3.Distance(p2, endPoint))
                    {
                        int temp = listNext[i];
                        listNext[i] = listNext[j];
                        listNext[j] = temp;
                    }
                }
            }

            for (int i = 0; i < listNext.Count; i++)
            {
                queue.Add(listNext[i]);
                visitList.Add(listNext[i]);
                visitIndex.Add(searchIndex);
                queueIndex.Add(visitList.Count - 1);
            }

            queue.RemoveAt(0);
            queueIndex.RemoveAt(0);
        }

        // 此路不通,返回空
        if (!isFind)
        {
            return(null);
        }

        List <int> points = new List <int>();

        // 从终点开始搜索返回起点
        int currentPos = end;
        int visit      = visitIndex.Count - 1; //最后的一个点

        do
        {
            currentPos = visitList[visit];
            points.Add(currentPos);

            visit = visitIndex[visit];
        } while (currentPos != start);

        points.Reverse();
        return(points);
    }