public WorldGrid() { _chunks = new Chunk[NumberOfChunksHigh * NumberOfChunksWide]; for (int y = 0; y < NumberOfChunksHigh; y++) { for (int x = 0; x < NumberOfChunksWide; x++) { var chunkCoordinate = new ChunkCoordinate(x, y); this[chunkCoordinate] = ChunkLoader.LoadChunk(chunkCoordinate); } } }
public static Chunk LoadChunk(ChunkCoordinate chunkCoordinate) { var chunk = new Chunk(chunkCoordinate); // for (int y = 0; y < Chunk.NumberOfGridItemsHigh; y++) // { // for (int x = 0; x < Chunk.NumberOfGridItemsWide; x++) // { // var gridPosition = new GridCoordinate(chunkCoordinate, new InnerChunkGridCoordinate(x, y)); // var gridItem = CreateGridItemFor(gridPosition); // // chunk[gridPosition.InnerChunkGridCoordinate] = gridItem; // } // } return(chunk); }
/// <summary> /// Gets the chunk at the specified coordinate. /// </summary> public ref Chunk this[ChunkCoordinate coordinate] => ref _chunks[CalculateAbsoluteChunkIndex(coordinate.X, coordinate.Y)];
/// <summary> /// Returns true if the given coordinate is a valid coordinate that can be used in this grid. /// </summary> public bool IsValid(ChunkCoordinate chunkCoordinate) { return(chunkCoordinate.X >= 0 && chunkCoordinate.X < NumberOfChunksWide && chunkCoordinate.Y >= 0 && chunkCoordinate.Y < NumberOfChunksWide); }
/// <summary> Constructor. </summary> /// <param name="position"> The position of the given chunk. </param> public Chunk(ChunkCoordinate position) { Position = position; _items = new UnsafeGridItemArray(NumberOfGridItemsWide * NumberOfGridItemsHigh); AbsoluteIndex = WorldGrid.CalculateAbsoluteChunkIndex(Position.X, Position.Y); }
public GridCoordinate(ChunkCoordinate chunkCoordinate, InnerChunkGridCoordinate innerCoordinate) { X = (chunkCoordinate.X << Chunk.XGridCoordinateToChunkCoordinateBitShift) + innerCoordinate.X; Y = (chunkCoordinate.Y << Chunk.YGridCoordinateToChunkCoordinateBitShift) + innerCoordinate.Y; }