Example #1
0
    public List <int> FindWay(Point pa, Point pb)
    {
        List <int> resultPath = new List <int>();

        openList.Add(pa);
        while (!(IsInOpenList(pb.x, pb.y) || openList.Count == 0))
        {
            Point p0 = GetMinFFromOpenList();
            if (p0 == null)
            {
                return(resultPath);
            }
            openList.Remove(p0);
            p0.isInOpenList = false;
            pointDic[MapUtil.GetInstance().GetIndex(p0.y, p0.x)].isVisited = true;
            if (p0 == pb)
            {
                break;
            }
            CheckP8(p0, pa, ref pb);
        }

        //将结果以倒序加入result
        Point p = GetPointFromOpenList(pb.x, pb.y);

        while (p.father != null)
        {
            p = p.father;
            resultPath.Add(MapUtil.GetInstance().GetIndex(p.y, p.x));
        }
        //第一个点会出现坐标误差,删除
        resultPath.RemoveAt(resultPath.Count - 1);
        return(resultPath);
    }
Example #2
0
    void Start()
    {
        wwwReady.Add(new WWW(shaderUrl));

        InitGameScene();
        MapUtil.GetInstance().LoadMapDataFromXML(SceneConfig.GetInstance().mapDataPath);
    }
Example #3
0
 //判断一个点是否为障碍物
 private bool IsBar(Point p)
 {
     if (pointDic[MapUtil.GetInstance().GetIndex(p.y, p.x)].isActive)
     {
         return(true);
     }
     return(false);
 }
Example #4
0
 //检查当前节点附近的节点
 private void CheckP8(Point p0, Point pa, ref Point pb)
 {
     for (int xt = p0.x - 1; xt <= p0.x + 1; xt++)
     {
         for (int yt = p0.y - 1; yt <= p0.y + 1; yt++)
         {
             //排除超过边界和等于自身的点
             if ((xt >= 0 && xt < cols && yt >= 0 && yt < rows) && !(xt == p0.x && yt == p0.y))
             {
                 //排除障碍点和已经访问过的点
                 var index = MapUtil.GetInstance().GetIndex(yt, xt);
                 if (pointDic.ContainsKey(index) && pointDic[index].isActive && !IsVisited(xt, yt))
                 {
                     Point pt = pointDic[index];
                     if (IsInOpenList(xt, yt))
                     {
                         //Point pt = pointDic[MapUtil.GetInstance().GetIndex(yt, xt)];
                         int G_new = 0;
                         if (p0.x == pt.x || p0.y == pt.y)
                         {
                             G_new = p0.G + 10;
                         }
                         else
                         {
                             G_new = p0.G + 14;
                         }
                         if (G_new < pt.G)
                         {
                             pt.father = p0;
                             pt.G      = G_new;
                             openList.Add(pt);
                             pt.isInOpenList = true;
                         }
                     }
                     else
                     {
                         //不在开启列表中
                         //Point pt = pointDic[MapUtil.GetInstance().GetIndex(yt, xt)];
                         pt.father = p0;
                         pt.G      = GetG(pt);
                         pt.H      = GetH(pt, pb);
                         openList.Add(pt);
                         pt.isInOpenList = true;
                     }
                 }
             }
         }
     }
 }
Example #5
0
    public PathSearch(Dictionary <int, MapData> mapDic, int rows, int cols)
    {
        this.cols = cols;
        this.rows = rows;
        var itr = mapDic.GetEnumerator();

        while (itr.MoveNext())
        {
            //通过地图信息生成搜索表
            var point = new Point();
            MapUtil.GetInstance().CaculatePointIndex(itr.Current.Key, out point.y, out point.x);
            point.isVisited = false;
            point.isActive  = itr.Current.Value.isActive;
            pointDic.Add(itr.Current.Key, point);
        }
    }
Example #6
0
 //判断开启列表是否包含一个坐标的点
 private bool IsInOpenList(int x, int y)
 {
     return(pointDic[MapUtil.GetInstance().GetIndex(y, x)].isInOpenList);
 }
Example #7
0
 private bool IsVisited(int x, int y)
 {
     return(pointDic[MapUtil.GetInstance().GetIndex(y, x)].isVisited);
 }
Example #8
0
    void Update()
    {
        MapUtil.GetInstance().ShowAllMapPoint();

        if (textureWaitingList.Count == 0 && wwwReady.Count == 0)
        {
            isLoadTextureDone = true;
        }
        if (matWaitingList.Count == 0 && wwwReady.Count == 0)
        {
            isLoadMatDone = true;
        }
        if (assetWaitingList.Count > 0 && wwwReady.Count < SceneConfig.GetInstance().maxListSize)
        {
            string url = null;
            if (textureWaitingList.Count > 0)
            {
                url = textureWaitingList[0];
                textureWaitingList.Remove(url);
            }
            else
            {
                if (isLoadTextureDone)
                {
                    if (matWaitingList.Count > 0)
                    {
                        url = matWaitingList[0];
                        matWaitingList.Remove(url);
                    }
                    else
                    {
                        if (isLoadMatDone)
                        {
                            if (prefabWaitingList.Count > 0)
                            {
                                url = prefabWaitingList[0];
                                prefabWaitingList.Remove(url);
                            }
                        }
                    }
                }
            }
            if (url != null)
            {
                ;
                assetWaitingList.Remove(url);
                GetWWW(url);
            }
        }
        //遍历LightMapList,将其中已经完成的www放入done列表
        var itr = lightMapList.GetEnumerator();

        while (itr.MoveNext())
        {
            if (itr.Current.Value.isDone)
            {
                if (!wwwDone.ContainsKey(itr.Current.Key))
                {
                    wwwDone.Add(itr.Current.Key, itr.Current.Value);
                }
            }
        }

        //遍历ready列表,如果加载完成,放入wwwDone
        for (int i = 0; i < wwwReady.Count; i++)
        {
            if (wwwReady[i].isDone)
            {
                if (!string.IsNullOrEmpty(wwwReady[i].error))
                {
                    Debug.Log(wwwReady[i].url + wwwReady[i].error);
                }
                var www = wwwReady[i];
                Debug.Log("Add Url to wwwDone " + www.url);
                wwwDone.Add(www.url, www);
                wwwReady.Remove(www);
            }
        }
        //所有资源全部加载完成,创建地图
        if (wwwDone.Count == totalAssetNum)
        {
            SetLightMap();
            Debug.Log("wwwDone total :" + wwwDone.Count);
            InitialGameObject();
            MapUtil.GetInstance().AddjustMapPoint();
            SetPlayer();
        }
        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (!hit.transform.tag.Contains("surface"))
                {
                    return;
                }


                if (MapUtil.GetInstance().isPointEffective(hit.point))
                {
                    Vector3 startPoint = Vector3.zero;
                    if (MapUtil.GetInstance().isPointEffective(goPlayer.transform.position))
                    {
                        startPoint = goPlayer.transform.position;
                    }
                    else
                    {
                        startPoint = tarPoint;
                    }
                    path = MapUtil.GetInstance().GeneratePath(startPoint, hit.point);
                    if (path.Count > 0)
                    {
                        SetGameState(HeroState.HERO_RUN);
                    }
                }
            }
        }
        //调整摄像机
        if (isPlayerInitialed)
        {
            transform.position = Vector3.SmoothDamp(transform.position, goPlayer.transform.position + cameraOffset, ref cameraVelocity, 0.01f);
            transform.LookAt(goPlayer.transform.position);
        }
        //右键换图
        if (Input.GetMouseButtonDown(1))
        {
            LoadNextScene();
        }
    }