Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

how to make a chunk loader in c#

    public void CheckChunks()
    {
        int StartX = (int)Math.Floor(ScreenPosition.X / (ChunkSizeX * 16)) - 2;
        int StartY = (int)Math.Floor(ScreenPosition.Y / (ChunkSizeY * 16)) - 2;

        if (StartX < 0) StartX = 0;
        if (StartY < 0) StartY = 0;

        int EndX = (int)Math.Floor((ScreenPosition.X + GraphicsDevice.Viewport.Width) / (ChunkSizeX * 16)) + 2;
        int EndY = (int)Math.Floor((ScreenPosition.Y + GraphicsDevice.Viewport.Height) / (ChunkSizeY * 16)) + 2;
        if (EndX > MaxChunksX) EndX = MaxChunksX;
        if (EndY > MaxChunksY) EndY = MaxChunksY;

        for (int x = StartX; x < EndX; x++)
        {
            for (int y = StartY; y < EndY; y++)
            {
                if (WorldChunks[x, y] == null)
                {

                    LoadChunk(x, y, true);

                    if (WorldChunks[x, y] != null)
                    {

                        LoadedChunks.Add(WorldChunks[x, y]);
                    }
                     break;
                }

            }
        }

            foreach (Chunk cc in LoadedChunks)
            {

                if (cc.X < StartX  || cc.Y < StartY || cc.X >EndX || cc.Y > EndY)
                {
                    System.Diagnostics.Debug.WriteLine("Chunk to be unloaded..");
                    ChunkBuffer.Add(cc);
                    UnloadChunk(cc);

                }
            }

            foreach (Chunk c in ChunkBuffer)
            {
                LoadedChunks.Remove(c);
            }
        ChunkBuffer.Clear();

    }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #chunk #loader
ADD COMMENT
Topic
Name
8+3 =