Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to make a chunk loader in c#

    public void LoadChunk(int xx, int yy)
    {

        string chunkname = xx + "_" + yy;
        if (File.Exists(Main.ChunkPath + "Chunks" + chunkname + ".chnk"))
        {
            //If the chunk is not loaded, Create a new chunk and begin loading it's tiles.
            if (WorldChunks[xx, yy] == null)
            {
                WorldChunks[xx, yy] = new Chunk(xx, yy);
            }

            System.Diagnostics.Debug.WriteLine("Loading chunk [" + xx + "," + yy + "]");
            Stream stream = File.Open(Main.ChunkPath + "Chunks" + chunkname + ".chnk", FileMode.Open);
            StreamReader reader = new StreamReader(stream);
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.TypeNameHandling = TypeNameHandling.Objects;
            WorldChunks[xx, yy].myTiles = JsonConvert.DeserializeObject<byte[,]>(reader.ReadToEnd(), settings);
            stream.Close();
            System.Diagnostics.Debug.WriteLine("Chunk loaded.. [" + xx + "," + yy + "]");
            int x, y;
            for (x = 0; x < Main.ChunkSizeX; x++)
            {
                for (y = 0; y < Main.ChunkSizeY; y++)
                {
                    byte _Type = WorldChunks[xx, yy].myTiles[x, y];
                    int tx = (xx * ChunkSizeX) + x;
                    int ty = (yy * ChunkSizeY) + y;
                    if (_Type > 0)
                    {
                        if (Tiles[tx, ty] == null && tx < MaxTilesX && ty < MaxTilesY)
                        {
                            Tiles[x + (xx * ChunkSizeX), (yy * ChunkSizeY) + y] = new Tile();
                            Tiles[(xx * ChunkSizeX) + x, (yy * ChunkSizeY) + y].Type = _Type;
                            if (ty > GROUND_LEVEL[tx] + 1)
                            {
                                //System.Diagnostics.Debug.Write("Below level:" + x + "|" + tx);
                                Tiles[tx, ty].HasBG = true;
                            }

                        }
                        else continue;
                    }

                }
            }

        }

    }
Comment

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();

    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: Delayed respawn timer 
Csharp :: c# null check 
Csharp :: remove numericUpDown white space 
Csharp :: c# bool? to bool 
Csharp :: disable button netbeans 
Csharp :: c# insert today datetime 
Csharp :: C# print all properties of an object including children objects 
Csharp :: <link rel="stylesheet" href="styles/kendo.common.min.css" / 
Csharp :: c# winform get access token facebook 
Csharp :: setxkbmap 
Csharp :: c# Least prime factor of numbers till n 
Csharp :: c# accept any enum 
Csharp :: c# return statement 
Csharp :: c# XmlElement from string 
Csharp :: save string to file c# 
Csharp :: pyqt send message to another instance 
Csharp :: how to edit a c# list 
Csharp :: F# tuple get item 
Csharp :: c# run program as an administrator 
Csharp :: hur delar man upp en e post på string c# 
Csharp :: tostring vb.net format decimal value with comma 
Csharp :: c# linq query map to entity 
Csharp :: C# resize window without title bar 
Csharp :: C# string array in setter 
Csharp :: duplicate global system runtime versioning targetframeworkattribute 
Csharp :: c# user and password verification 
Csharp :: return a list of list from yaml via C# 
Csharp :: Rotate Object/Camera by Mouse 
Csharp :: c# formula from string 
Csharp :: asp net route attribute vs httpget 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =