/// <summary> /// Gets a 3x3 chunk of PartitionTiles from a specific position. /// </summary> /// <param name="tileList">A pre-initialized List of PartitionTiles for allocation.</param> /// <param name="position">Position of the center PartitionTile.</param> /// <returns>List of valid PartitionTiles within a 3x3 area.</returns> internal void GetAdjacentTiles(List <PartitionTile> tileList, Vector2 position) { PartitionTile t = GetTile(position); if (t != null) { GetAdjacentTiles(tileList, t); } }
/// <summary> /// Gets a 3x3 chunk of PartitionTiles, using a PartitionTile as the centre of the chunk. /// </summary> /// <param name="tileList">A pre-initialized List of PartitionTiles for allocation.</param> /// <param name="tile">The center PartitionTile.</param> /// <returns>List of valid PartitionTiles within a 3x3 area. NULL if no valid PartitionTiles are found.</returns> internal void GetAdjacentTiles(List <PartitionTile> tileList, PartitionTile tile) { Vector2 partitionTilePos = new Vector2(tile.Bounds.X, tile.Bounds.Y); Point partitionTileIndex = GetTileIndex(partitionTilePos); if (partitionTileIndex.X == -1 && partitionTileIndex.Y == -1) { return; } tileList.Add(tile); // North if (partitionTileIndex.Y - 1 >= 0) { tileList.Add(GridPartitionTiles[partitionTileIndex.X][partitionTileIndex.Y - 1]); } // North east if (partitionTileIndex.Y - 1 >= 0 && partitionTileIndex.X + 1 < partitionTilesX) { tileList.Add(GridPartitionTiles[partitionTileIndex.X + 1][partitionTileIndex.Y - 1]); } // East if (partitionTileIndex.X + 1 < partitionTilesX) { tileList.Add(GridPartitionTiles[partitionTileIndex.X + 1][partitionTileIndex.Y]); } // South east if (partitionTileIndex.Y + 1 < partitionTilesY && partitionTileIndex.X + 1 < partitionTilesX) { tileList.Add(GridPartitionTiles[partitionTileIndex.X + 1][partitionTileIndex.Y + 1]); } // South if (partitionTileIndex.Y + 1 < partitionTilesY) { tileList.Add(GridPartitionTiles[partitionTileIndex.X][partitionTileIndex.Y + 1]); } // South west if (partitionTileIndex.Y + 1 < partitionTilesY && partitionTileIndex.X - 1 >= 0) { tileList.Add(GridPartitionTiles[partitionTileIndex.X - 1][partitionTileIndex.Y + 1]); } // West if (partitionTileIndex.X - 1 >= 0) { tileList.Add(GridPartitionTiles[partitionTileIndex.X - 1][partitionTileIndex.Y]); } // North west if (partitionTileIndex.Y - 1 >= 0 && partitionTileIndex.X - 1 >= 0) { tileList.Add(GridPartitionTiles[partitionTileIndex.X - 1][partitionTileIndex.Y - 1]); } }
private bool ShouldPrune() { for (int x = 0; x < partitionTilesX; x++) { for (int y = 0; y < partitionTilesY; y++) { PartitionTile tile = GridPartitionTiles[x][y]; if (tile.PartitionObjects.Count > 0) { return(false); } } } return(true); }
/// <summary> /// Gets a list of PartitionTiles inside of a line. Allocates less memory than the standard method. /// </summary> /// <param name="tiles">Existing list of PartitionTiles to allocate.</param> /// <param name="start">Starting point of the line.</param> /// <param name="end">Destination point of the line.</param> /// <returns></returns> internal void GetTilesFromLine(List <PartitionTile> tiles, Vector2 start, Vector2 end) { Point pStart = GetTileIndex(start); Point pEnd = GetTileIndex(end); Point[] points = null; //Core.Physics.BresenhamLine(pStart, pEnd); for (int i = 0; i < points.Length; i++) { PartitionTile t = GetTile(points[i]); if (t != null) { tiles.Add(GetTile(points[i])); } } }
internal SpatialPartition(int tSize, Rectangle bounds) { Bounds = bounds; Position = new Vector2(bounds.X, bounds.Y); GridPartitionTiles = null; partitionTileSize = tSize; partitionTilesX = bounds.Width / partitionTileSize; partitionTilesY = bounds.Height / partitionTileSize; GridPartitionTiles = new PartitionTile[partitionTilesX][]; for (int x = 0; x < partitionTilesX; x++) { GridPartitionTiles[x] = new PartitionTile[partitionTilesX]; for (int y = 0; y < partitionTilesY; y++) { Rectangle b = new Rectangle(x * partitionTileSize, y * partitionTileSize, partitionTileSize, partitionTileSize); GridPartitionTiles[x][y] = new PartitionTile(b); } } pruneOffset = SE.Utility.Random.Next(0, 120); }
/// <summary> /// Removes a GameObject from the grid system. /// </summary> /// <param name="go">GameObject to remove.</param> internal void Remove(IPartitionObject obj) { PartitionTile partitionTile = obj.CurrentPartitionTile; partitionTile?.Remove(obj); }
/// <summary> /// Inserts a GameObject into the grid system. /// </summary> /// <param name="go">GameObject to add.</param> internal void Insert(IPartitionObject obj) { PartitionTile partitionTile = GetTile(obj.PartitionPosition - Position); partitionTile?.Insert(obj); }