public LcNode PunchNewNode(PathPos pp, string newptname = "", bool deleteparentlink = false) { var gcr = GetGraphCtrl(); var lpt = gcr.PunchNewNode(pp, newptname, deleteparentlink); return(lpt); }
// Update is called once per frame void Update() { if (followBird) { bird = GameObject.Find("Bird"); if (bird != null) { Follow(Time.deltaTime); } } var npsph = GameObject.Find("nearpathsph-go"); if (npsph != null) { var pathdst = -1f; var nearestPointRef = Camera.main.transform.position; rman.pathctrl.FindClosestWegOnPath(nearestPointRef, out pathdst); PathPos pp = rman.pathctrl.path.MovePositionAlongPath(pathdst); CamClosestPathDist = pathdst; if (pp != null) { nearPathPosLocal = pp.pt; var ptwc = rman.transform.TransformPoint(pp.pt); // we don't want the followsphere to scale! // Thus rman.transform and not rman.rgo.transform nearPathPosWc = ptwc; nearSpherePosition = npsph.transform.position; nearPathIdx = pp.index; nearPathLambda = pp.lambda; var lnk = pp.weg.link; nearPathWegname = lnk.name; } } }
// Expand open list, getting neighbours of NewPos private void AddToOpenList(List <PathPos> OpenList, List <PathPos> ClosedList, PathPos NewPos, Vector3 TargetPos) { int k = 0; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { //if (PathMap[i + NewPos.x, k + NewPos.y, j + NewPos.z] != '#') if (TileType(i + NewPos.x, k + NewPos.y, j + NewPos.z) != '#') { k = 0; if (i == 0 && j == 0) { if (TileType(i + NewPos.x, k + NewPos.y, j + NewPos.z) == '>' || TileType(i + NewPos.x, k + NewPos.y, j + NewPos.z) == ' ') { k = -yscale; } if (TileType(i + NewPos.x, k + NewPos.y, j + NewPos.z) == '<') { k = yscale; } } PathPos AddPos = new PathPos(i + NewPos.x, k + NewPos.y, j + NewPos.z, NewPos.cost + 1, TargetPos, NewPos); if (!ClosedList.Contains(AddPos)) { if (!OpenList.Contains(AddPos)) { OpenList.Add(AddPos); } } } } } }
public LcNode PunchNewLinkPt(PathPos pp, string newptname = "", bool deleteparentlink = false) { var lcld = getLinkCloud(); var lpt = lcld.PunchNewLinkPt(pp, newptname, deleteparentlink); return(lpt); }
Vector3 GetPathPoint(float gpprdist, bool setpathpos = true) { if (path == null) { return(Vector3.zero); } var pp = path.MovePositionAlongPath(gpprdist); if (setpathpos) { pathpos = pp; } var ptlc = new Vector3(pp.pt.x, pp.pt.y + BirdFlyHeight, pp.pt.z); return(ptlc); }
// Update is called once per frame void Update() { if (path != null) { // public Vector3 startpt = Vector3.zero; //public Vector3 endpt = Vector3.zero; //public Vector3 weg1Pt1 = Vector3.zero; //public Vector3 weg1Pt2 = Vector3.zero; this.startpt = path.startpt; this.endpt = path.endpt; this.weg1Pt1 = path.weg1Pt1; this.weg1Pt2 = path.weg1Pt2; this.weg1FrPt = path.weg1FrPt; this.weg1ToPt = path.weg1ToPt; } if (updateNearestPathPointWithMainCamPos)// very expensive { nearestPointRef = Camera.main.transform.position; var npsph = GameObject.Find("nearpathsph-go"); var pathdst = -1f; PathPos pp = null; var npt = Vector3.zero; var nsphpos = Vector3.zero; if (npsph != null) { FindClosestWegOnPath(nearestPointRef, out pathdst); pp = path.MovePositionAlongPath(pathdst); nsphpos = sman.rgo.transform.TransformPoint(pp.pt); npsph.transform.position = nsphpos; if (tm != null) { var text = ""; text += "nsphpos:" + nsphpos; text += "\npt:" + pp.pt; var ptwc = sman.rgo.transform.TransformPoint(pp.pt); text += "\nptwc:" + ptwc; text += "\nidx:" + pp.index; text += "\nlambda:" + pp.lambda; text += "\nstart:" + this.startnodename; text += "\nend:" + this.endnodename; text += "\npathdist:" + pathdst.ToString("F1") + "/" + path.pathLength.ToString("F1"); tm.text = text; } //RegionMan.Log("pathdst:"+pathdst); } } }
public void InitTexture() { for (int col = 0; col < m_width; col++) { for (int row = 0; row < m_height; row++) { m_texure.SetPixel(row, col, m_clear_color); } } foreach (KeyValuePair <int, Dictionary <int, List <PathPos> > > vehicle_pair in m_all_path) { int vid = vehicle_pair.Key; foreach (KeyValuePair <int, List <PathPos> > path_pair in vehicle_pair.Value) { List <PathPos> pos_list = path_pair.Value; for (int i = 0; i + PATH_STEP < pos_list.Count - 1; i += PATH_STEP) { PathPos pt1 = pos_list[i]; PathPos pt2 = pos_list[i + PATH_STEP]; Vector2 start_pt = CoordTrans_MapMeter_Texture2D(new Vector2((float)pt1.x, (float)pt1.y)); Vector2 end_pt = CoordTrans_MapMeter_Texture2D(new Vector2((float)pt2.x, (float)pt2.y)); for (float j = 0; j < 1; j = j + 0.05f) { Vector2 temp = Vector2.Lerp(start_pt, end_pt, j); m_texure.SetPixel(Convert.ToInt32(temp.x), Convert.ToInt32(temp.y), m_lineColor); } } } } m_texure.Apply(); MapBackground.material.mainTexture = m_texure; }
Vector3 GetPathPoint(float gpprdist, bool curpos = true) { if (path == null) { return(Vector3.zero); } var pp = path.MovePositionAlongPath(gpprdist); if (curpos) { pathpos = pp; pathweg = pp.weg; spathweg = pathweg.frNode.name + " to " + pathweg.toNode.name; wegguid = pp.weg.id; swegguid = wegguid.ToString(); wegdistance = pp.wegDistSoFar; cntgp++; } var pt = new Vector3(pp.pt.x, pp.pt.y + BirdFlyHeight, pp.pt.z); return(pt); }
/// <summary> /// 根据 m_file_list 填充 m_all_path /// </summary> public void ReadPathFiles() { m_all_path.Clear(); foreach (KeyValuePair <int, string> path_file in m_file_list) { int vid = path_file.Key; string file_name = path_file.Value; if (!File.Exists(file_name)) { continue; } char[] spliter = { ' ' }; StreamReader sr = new StreamReader(file_name); Dictionary <int, List <PathPos> > dict_path = new Dictionary <int, List <PathPos> >(); while (sr.Peek() != -1) { string line = sr.ReadLine(); string[] part = line.Split(spliter, StringSplitOptions.RemoveEmptyEntries); if (part.Length != DATA_LEN) { continue; } int path_id = -1; int.TryParse(part[3], out path_id); double x, y; double.TryParse(part[0], out x); double.TryParse(part[1], out y); PathPos pos = new PathPos(x, y); if (!dict_path.ContainsKey(path_id)) { dict_path.Add(path_id, new List <PathPos> { pos }); } else { dict_path[path_id].Add(pos); } if (x < m_min_x) { m_min_x = x; } else if (x > m_max_x) { m_max_x = x; } if (y < m_min_y) { m_min_y = y; } else if (y > m_max_y) { m_max_y = y; } } sr.Close(); m_all_path.Add(vid, dict_path); } // 计算地图基本数据 m_min_y -= 10; m_max_y += 10; m_logic_length = Math.Max((m_max_x - m_min_x), (m_max_y - m_min_y)); m_pixel_per_meter = (double)map_size / m_logic_length; ori_x = 0.5 * (m_max_x - m_min_x) + m_min_x; ori_y = 0.5 * (m_max_y - m_min_y) + m_min_y; }
public List <Vector3> AStarPath(Vector3 CurrentPos, Vector3 TargetPos) { // List of positions along path List <Vector3> ReturnList = new List <Vector3>(); // Initialize open and closed list List <PathPos> OpenList = new List <PathPos>(); List <PathPos> ClosedList = new List <PathPos>(); PathPos EndLocation = new PathPos(TargetPos, -1, TargetPos); // Add start position ClosedList.Add(new PathPos(CurrentPos, 0, TargetPos)); AddToOpenList(OpenList, ClosedList, ClosedList[0], TargetPos); int loopbreaker = 0; bool success = false; // Loop while the list doesn't contain the thing, but not too many times while (!ClosedList.Contains(EndLocation) && loopbreaker < 1000) { loopbreaker++; if (OpenList.Count > 0) { OpenList.Sort(); ClosedList.Add(OpenList[0]); //Debug.Log(OpenList[0].PrevPos); AddToOpenList(OpenList, ClosedList, OpenList[0], TargetPos); if (OpenList[0] == EndLocation) { EndLocation.PrevPos = OpenList[0].PrevPos; } else { OpenList.RemoveAt(0); } } else { break; } } success = ClosedList.Contains(EndLocation); if (success) { PathPos thispos; thispos = OpenList[0];//EndLocation; int iii = 0; do { //Debug.Log(thispos.GetVector3()); //Debug.Log(iii++); ReturnList.Add(thispos.GetVector3()); //Instantiate(debugblock, thispos.GetVector3(), Quaternion.identity); thispos = thispos.PrevPos; if (iii > 1000) { break; } } while (thispos != null); //Debug.Log(ReturnList.Count); ReturnList.Reverse(); /*for (int i = 0; i < ReturnList.Count; i++) * { * Debug.Log(ReturnList[i].ToString()); * }*/ return(ReturnList); } else { return(null); } }