public bool Equals(GridLocation g) { if (this.X == g.X && this.Y == g.Y) return true; return false; }
public bool Equals(GridLocation o) { if (o == null) return false; return this == o; }
/// <summary> /// /// </summary> /// <param name="gridLocation"></param> /// <returns> /// A list of the <see cref="GridLocation"/>s surrounding /// <paramref name="gridLocation"/> so long as they exist in the world. /// </returns> private List<GridLocation> GetSurrounding(GridLocation gridLocation) { GridLocation g = gridLocation; List<GridLocation> surrounding = new List<GridLocation>(8); surrounding.Add(new GridLocation((Int16)(g.X - 1), (Int16)(g.Y - 1), cellSize)); surrounding.Add(new GridLocation((Int16)(g.X - 1), (Int16)g.Y, cellSize)); surrounding.Add(new GridLocation((Int16)(g.X - 1), (Int16)(g.Y + 1), cellSize)); surrounding.Add(new GridLocation((Int16)g.X, (Int16)(g.Y - 1), cellSize)); //surrounding.Add(new GridLocation((Int16)g.X, (Int16)g.Y, cellSize)); - the cell itself surrounding.Add(new GridLocation((Int16)g.X, (Int16)(g.Y + 1), cellSize)); surrounding.Add(new GridLocation((Int16)(g.X + 1), (Int16)(g.Y - 1), cellSize)); surrounding.Add(new GridLocation((Int16)(g.X + 1), (Int16)g.Y, cellSize)); surrounding.Add(new GridLocation((Int16)(g.X + 1), (Int16)(g.Y + 1), cellSize)); // check to make sure the surrounding cells are inside the world, remove any that don't surrounding.RemoveAll( s => s.X < minGrid.X || s.X > maxGrid.X || s.Y < minGrid.Y || s.Y > maxGrid.Y ); return surrounding; }
/// <summary> /// /// </summary> /// <param name="worldObject"></param> /// <returns>A list of all <see cref="GridLocation"/>s containing the <see cref="IWorldObject"/></returns> public List<GridLocation> Intersects(IWorldObject worldObject) { List<GridLocation> found = new List<GridLocation>(gridSize.X * gridSize.Y); List<GridLocation> missed = new List<GridLocation>(gridSize.X * gridSize.Y); UniqueList<GridLocation> toTest = new UniqueList<GridLocation>(gridSize.X * gridSize.Y); UniqueList<GridLocation> newToBeTested = new UniqueList<GridLocation>(gridSize.X * gridSize.Y); // the GridLocation that contains the center of the object GridLocation hasCenter; // STEP 1 - determine the GridLocation of the object's center Point cellCoord; // Use integer division to find cell coordinates. // Since the grid cell coords are in the upperleft corner // 1 must be subtracted from the division if we are in the negative (left or up) // direction - effectively rounding up in absolute value. if (worldObject.Position.X < 0) cellCoord.X = (Int16)(((Int16)worldObject.Position.X / (Int16)cellSize.X) - 1); else cellCoord.X = (Int16)(((Int16)worldObject.Position.X / (Int16)cellSize.X)); if (worldObject.Position.Y < 0) cellCoord.Y = (Int16)(((Int16)worldObject.Position.Y / (Int16)cellSize.Y) - 1); else cellCoord.Y = (Int16)(((Int16)worldObject.Position.Y / (Int16)cellSize.Y)); // check to makes sure these coordinates are in the world if (minGrid.X <= cellCoord.X && cellCoord.X <= maxGrid.X && minGrid.Y <= cellCoord.Y && cellCoord.Y <= maxGrid.Y) { hasCenter = new GridLocation((Int16)cellCoord.X, (Int16)cellCoord.Y, cellSize); } // if they are not in the world, return the empty list else { return found; } // add it to the found list found.Add(hasCenter); // STEP 2 - determine all surrounding GridLocations that contain the object toTest.UnionWith(GetSurrounding(hasCenter)); while (toTest.Count != 0) { foreach (GridLocation gridLocation in toTest) { // if a surrounding GridLocation intersects the object do 4 things if (worldObject.Bounds.Intersects(gridLocation.Bounds)) { // 1. Add it to the found list found.Add(gridLocation); // 2. Get its surrounding GridLocations newToBeTested.UnionWith(GetSurrounding(gridLocation)); // 3. Remove any that are already known to not contain the object foreach (GridLocation g in missed) { newToBeTested.Remove(g); } // 4. Remove any that are already known to contain the object foreach (GridLocation g in found) { newToBeTested.Remove(g); } } // if a surrounding GridLocation DOES NOT intersect the object add it to missed else { missed.Add(gridLocation); } } // since we have checked everything in toTest flush it toTest.Clear(); // add the newly found surrounding candidates to toTest toTest.UnionWith(newToBeTested); // flush the temp list newToBeTested.Clear(); } return found; }
/// <summary> /// /// </summary> /// <param name="gridLocation"></param> /// <returns><see cref="IWorldObject"/>s associated with a given <paramref name="gridLocation"/></returns> public List<IWorldObject> getLocationObjectsOf(GridLocation gridLocation) { return grid[gridLocation]; }
/// <summary> /// /// </summary> /// <param name="gridLocation"></param> /// <returns> /// A list of the <see cref="GridLocation"/>s surrounding /// <paramref name="gridLocation"/> so long as they exist in the world. /// </returns> private List<GridLocation> GetSurrounding(GridLocation gridLocation) { GridLocation g = gridLocation; List<GridLocation> surrounding = new List<GridLocation>(); surrounding.Add(new GridLocation((UInt16)(g.X - g.Width), (UInt16)(g.Y - g.Height), g.Width, g.Height)); surrounding.Add(new GridLocation((UInt16)(g.X - g.Width), (UInt16)g.Y , g.Width, g.Height)); surrounding.Add(new GridLocation((UInt16)(g.X - g.Width), (UInt16)(g.Y + g.Height), g.Width, g.Height)); surrounding.Add(new GridLocation((UInt16)g.X , (UInt16)(g.Y - g.Height), g.Width, g.Height)); //surrounding.Add(new GridLocation((UInt16)g.X , (UInt16)g.Y , g.Width, g.Height)); - the cell itself surrounding.Add(new GridLocation((UInt16)g.X , (UInt16)(g.Y + g.Height), g.Width, g.Height)); surrounding.Add(new GridLocation((UInt16)(g.X + g.Width), (UInt16)(g.Y - g.Height), g.Width, g.Height)); surrounding.Add(new GridLocation((UInt16)(g.X + g.Width), (UInt16)g.Y , g.Width, g.Height)); surrounding.Add(new GridLocation((UInt16)(g.X + g.Width), (UInt16)(g.Y + g.Height), g.Width, g.Height)); // check to make sure the surrounding cells are inside the world foreach (GridLocation s in surrounding) { if (s.X < minWorld.X && s.X > maxWorld.X && s.Y < minWorld.Y && s.Y > maxWorld.Y) { surrounding.Remove(s); } } return surrounding; }
/// <summary> /// /// </summary> /// <param name="worldObject"></param> /// <returns>A list of all <see cref="GridLocation"/>s containing the <see cref="IWorldObject"/></returns> public List<GridLocation> Intersects(IWorldObject worldObject) { List<GridLocation> found = new List<GridLocation>(); //List<GridLocation> missed = new List<GridLocation>(); - i forgot why we need this HashSet<GridLocation> toTest = new HashSet<GridLocation>(); // determine the GridLocation of the object's center GridLocation hasCenter = new GridLocation((UInt16)((worldObject.Position.X / cellSize.X) * cellSize.X), (UInt16)((worldObject.Position.Y / cellSize.Y) * cellSize.Y), cellSize.X, cellSize.Y); // add it to the found list found.Add(hasCenter); // determine all surrounding GridLocations that contain the object toTest.UnionWith(GetSurrounding(hasCenter)); while (toTest.Count != 0) { foreach (GridLocation gridLocation in toTest) { if (worldObject.Bounds.Intersects(gridLocation.Bounds)) { found.Add(gridLocation); toTest.UnionWith(GetSurrounding(gridLocation)); toTest.Remove(gridLocation); } else { // missed.Add(g) Why do we need this? } } } return found; }