CellState[] states; // true if open, false if closed #endregion Fields #region Constructors public PathGrid( Vector2 min, Vector2 max, int rows, int cols, WalkablePredicate walkablePredicate ) { smoothStep = .25f * ( max.X - min.X ) / cols; Min = min; Max = max; Rows = rows; Cols = cols; openList = new List<int>( 100 ); int cellCount = Rows * Cols; cells = new PathCell[cellCount]; costs = new PathCellCost[cellCount]; states = new CellState[cellCount]; float xStep = ( Max.X - Min.X ) / Cols; float yStep = ( Max.Y - Min.Y ) / Rows; AABB aabb = new AABB(); aabb.Min.Y = Min.Y; aabb.Max.Y = Min.Y + yStep; for ( int r = 0; r < Rows; ++r ) { aabb.Min.X = Min.X; aabb.Max.X = Min.X + xStep; for ( int c = 0; c < Cols; ++c ) { int index = r * Cols + c; bool walkable = walkablePredicate != null ? walkablePredicate( aabb, Building.Buildings ) : true; cells[index] = new PathCell( walkable, index, Vector2.Lerp( aabb.Min, aabb.Max, .5f ) ); aabb.Min.X += xStep; aabb.Max.X += xStep; } aabb.Min.Y += yStep; aabb.Max.Y += yStep; } // do the a* stuffs here for ( int r = 0; r < Rows; ++r ) { for ( int c = 0; c < Cols; ++c ) { int index = r * Cols + c; if ( cells[index].Walkable ) { for ( int r2 = 0; r2 < Rows; ++r2 ) { for ( int c2 = 0; c2 < Cols; ++c2 ) { //FindPath( r, c, r2, c2 ); } } } } } debugGrid = new DebugGrid( Min, Max, Rows, Cols, Color.LightGray, ZombieCraft.Instance.GraphicsDevice ); }
private void CheckCell( ref PathCell cell, ref PathCellCost cost, float stepCost ) { if ( cell.Walkable ) { CellState state = states[cell.Index]; if ( state == CellState.Untouched ) { openList.Add( cell.Index ); states[cell.Index] = CellState.Open; cost.Heuristic = ComputeHeuristicCost( ref cell.Center, ref cells[curDest].Center ); cost.Walk = costs[curCell].Walk + stepCost; cost.Total = cost.Heuristic + cost.Walk; cell.Parent = curCell; } else if ( state == CellState.Open ) { float walkCost = costs[curCell].Walk + stepCost; if ( walkCost < cost.Walk ) { cost.Walk = walkCost; cost.Total = cost.Heuristic + cost.Walk; cell.Parent = curCell; } } } }