public OpenPoint CreateOpenList(Vector2 start, Vector2 end, float c, OpenPoint parent) { OpenPoint openPoint = new OpenPoint(start, end, c, parent); m_pointList.Add(openPoint); return(m_pointList.Last()); }
public bool Equals(OpenPoint p) { // If parameter is null return false: if ((object)p == null) { return(false); } // Return true if the fields match: return((m_x == p.m_x) && (m_y == p.m_y)); }
public OpenPoint(Vector2 start, Vector2 end, float c, OpenPoint parent) { float relativeX = Math.Abs(end.X - start.X); float relativeY = Math.Abs(end.Y - start.Y); float delta = relativeX - relativeY; m_x = (Int32)start.X; m_y = (Int32)start.Y; m_cost = c; m_pred = Math.Max(relativeX, relativeY) * 14.0 - Math.Abs(delta) * 4.0 + c; m_parent = parent; //parent = this; }
public bool CompareTo(object iObj) { OpenPoint openPoint = (OpenPoint)iObj; double result = this.m_pred - openPoint.m_pred; if (result > 0.0f) { return(true); } else if (result == 0.0f) { return(true); } return(false); }
public void Open(OpenPoint pointToOpen, Vector2 end) { m_current = m_openList[0]; // Remove parent from openList m_openList.Remove(m_current); if (m_openList.Count >= 1) { Console.WriteLine("Remove after x:{0:d} y:{1:d}", m_openList[0].m_x, m_openList[0].m_y); } ++m_depth; // check direction for (int i = 0; i < 4; i++) { float toOpenX = pointToOpen.m_x + m_direction[i, 0]; float toOpenY = pointToOpen.m_y + m_direction[i, 1]; Console.WriteLine("4Direction after x:{0:d} y:{1:d}", m_direction[i, 0], m_direction[i, 1]); Vector2 pos = new Vector2(toOpenX, toOpenY); if (!InBarrierAndCloseList(pos)) { m_openList.Add(CreateOpenList(pos, end, pointToOpen.m_cost + 10, m_current)); m_closeAndBarrierList[pointToOpen.m_x, pointToOpen.m_y] = true; } } // check for (int i = 4; i < 8; i++) { float toOpenX = pointToOpen.m_x + m_direction[i, 0]; float toOpenY = pointToOpen.m_y + m_direction[i, 1]; Console.WriteLine("4-8Direction after x:{0:d} y:{1:d}", m_direction[i, 0], m_direction[i, 1]); Vector2 pos = new Vector2(toOpenX, toOpenY); if (!InBarrierAndCloseList(pos)) { m_openList.Add(CreateOpenList(pos, end, pointToOpen.m_cost + 14, pointToOpen)); m_closeAndBarrierList[pointToOpen.m_x, pointToOpen.m_y] = true; } } // Add point to close list m_closeAndBarrierList[pointToOpen.m_x, pointToOpen.m_y] = true; }
public override bool Equals(System.Object obj) { // If parameter is null return false. if (obj == null) { return(false); } // If parameter cannot be cast to Point return false. OpenPoint p = obj as OpenPoint; if ((System.Object)p == null) { return(false); } // Return true if the fields match: return((m_x == p.m_x) && (m_y == p.m_y)); }
// Path finding. public List <OpenPoint> FindPath(Vector2 start, Vector2 end) { List <OpenPoint> path = new List <OpenPoint>(); OpenPoint pointToOpen = null; m_openList.Add(CreateOpenList(start, end, 0, pointToOpen)); OpenPoint toOpen = null; while (m_openList.Count != 0) { m_openList = m_openList.OrderBy(n => n.m_pred).ToList <OpenPoint>(); toOpen = m_openList.First(); Console.WriteLine("x:{0:d} y:{1:d}", toOpen.m_x, toOpen.m_y); // Out of depth. if (toOpen.m_x == (Int32)end.X && toOpen.m_y == (Int32)end.Y) { break; } if (m_depth >= m_depthLimit) { toOpen = null; break; } Open(toOpen, end); } if (toOpen != null) { while (toOpen.m_parent != null) { path.Add(toOpen.m_parent); } path.Reverse(); } return(path); }