Exemple #1
0
    public void ThreadStart()
    {
        Logger.Log("Initializing World Thread");

        while (IsRunning)
        {
            if (GameManager._Instance.StateOfTheGame == GameManager.GameState.RUNNING)
            {
                try
                {
                    CanMakeATree = false;
                    Int3 ChunkToTreePos = new Int3(0, 0, 0);

                    if (!RanOnce)
                    {
                        RanOnce = true;
                        for (int x = -RenderDistanceChunks; x < RenderDistanceChunks; x++)
                        {
                            for (int y = 0; y < ChunksInYAxis; y++)
                            {
                                for (int z = -RenderDistanceChunks; z < RenderDistanceChunks; z++)
                                {
                                    Int3 newchunkpos = new Int3(PlayerPos.x, 0, PlayerPos.z);
                                    newchunkpos.AddPos(new Int3(x * Chunk.ChunkWidth, y * Chunk.ChunkHeight, z * Chunk.ChunkWidth));
                                    newchunkpos.ToChunkCoordinates();

                                    if (System.IO.File.Exists(FileManager.GetChunkString(newchunkpos.x, newchunkpos.y, newchunkpos.z)))
                                    {
                                        try
                                        {
                                            _LoadedChunks.Add(new Chunk(newchunkpos.x, newchunkpos.y, newchunkpos.z, Serializer.Deserialize_From_File <int[, , ]>(FileManager.GetChunkString(newchunkpos.x, newchunkpos.y, newchunkpos.z)), this));
                                        }
                                        catch (System.Exception e)
                                        {
                                            Debug.Log(e.ToString());
                                        }
                                    }
                                    else
                                    {
                                        CanMakeATree = true;
                                        _LoadedChunks.Add(new Chunk(newchunkpos.x, newchunkpos.y, newchunkpos.z, this));
                                        ChunkToTreePos = newchunkpos;
                                        //Debug.Log("Cant find " + FileManager.GetChunkString(x, y, z));
                                    }
                                }
                            }
                        }

                        foreach (Chunk c in _LoadedChunks)
                        {
                            c.Start();
                        }
                    }

                    if (!PlayerCanBeLoaded)
                    {
                        PlayerCanBeLoaded = true;
                    }

                    if (GameManager.PlayerLoaded())
                    {
                        PlayerPos = new Int3(GameManager._Instance.PlayerPosition);
                    }


                    foreach (Chunk c in new List <Chunk>(_LoadedChunks))
                    {
                        if (Vector2.Distance(new Vector2(c.PosX * Chunk.ChunkWidth, c.PosZ * Chunk.ChunkWidth), new Vector2(PlayerPos.x, PlayerPos.z)) > ((RenderDistanceChunks * 2) * Chunk.ChunkWidth))
                        {
                            c.Degenerate();
                        }
                    }

                    for (int x = -RenderDistanceChunks; x < RenderDistanceChunks; x++)
                    {
                        for (int y = 0; y < ChunksInYAxis; y++)
                        {
                            for (int z = -RenderDistanceChunks; z < RenderDistanceChunks; z++)
                            {
                                Int3 newchunkpos = new Int3(PlayerPos.x, 0, PlayerPos.z);
                                newchunkpos.AddPos(new Int3(x * Chunk.ChunkWidth, y * Chunk.ChunkHeight, z * Chunk.ChunkWidth));
                                newchunkpos.ToChunkCoordinates();
                                if (!ChunkExists(newchunkpos.x, newchunkpos.y, newchunkpos.z))
                                {
                                    //make function for this

                                    if (System.IO.File.Exists(FileManager.GetChunkString(newchunkpos.x, newchunkpos.y, newchunkpos.z)))
                                    {
                                        try
                                        {
                                            Chunk c = new Chunk(newchunkpos.x, newchunkpos.y, newchunkpos.z, Serializer.Deserialize_From_File <int[, , ]>(FileManager.GetChunkString(newchunkpos.x, newchunkpos.y, newchunkpos.z)), this);
                                            c.Start();
                                            _LoadedChunks.Add(c);
                                        }
                                        catch (System.Exception e)
                                        {
                                            Debug.Log(e.ToString());
                                        }
                                    }
                                    else
                                    {
                                        Chunk c = new Chunk(newchunkpos.x, newchunkpos.y, newchunkpos.z, this);
                                        c.Start();
                                        _LoadedChunks.Add(c);
                                        CanMakeATree   = true;
                                        ChunkToTreePos = newchunkpos;
                                        //Debug.Log("Cant find " + FileManager.GetChunkString(x, z));
                                    }
                                }
                            }
                        }
                    }

                    if (CanMakeATree)
                    {
                        GenerateTree(ChunkToTreePos);
                        CanMakeATree = false;
                    }

                    foreach (Chunk c in _LoadedChunks)
                    {
                        c.Update();
                    }

                    if (!firstCheck)
                    {
                        ChunksGenerated = true;
                        firstCheck      = true;
                    }
                }
                catch (System.Exception e)
                {
                    UnityEngine.Debug.Log(e.StackTrace);
                    Logger.Log(e);
                }
            }
        }

        Logger.Log("Wordl Thread succesfully stopped");
        Logger.MainLog.Update();
    }
 public void Start()
 {
     IsRunning   = true;
     WorldThread = new Thread(() =>
     {
         Logger.Log("Initializing world thread");
         while (IsRunning)
         {
             //Logger.Log(new System.Exception("Unneeded exception"));
             try
             {
                 if (!RanOnce)
                 {
                     RanOnce = true;
                     for (int x = -RenderDistanceInChunks; x < RenderDistanceInChunks; x++)
                     {
                         for (int z = -RenderDistanceInChunks; z < RenderDistanceInChunks; z++)
                         {
                             Int3 newchunkpos = new Int3(PlayerPos.x, PlayerPos.y, PlayerPos.z);
                             newchunkpos.AddPos(new Int3(x * Chunk.ChunkWidth, 0, z * Chunk.ChunkWidth));
                             newchunkpos.ToChunkCoordinates();
                             if (System.IO.File.Exists(FileManager.GetChunkString(newchunkpos.x, newchunkpos.z)))
                             {
                                 try
                                 {
                                     _LoadedChunks.Add(new Chunk(newchunkpos.x, newchunkpos.z, Serializer.Deseralize_From_File <int[, , ]>(FileManager.GetChunkString(newchunkpos.x, newchunkpos.z)), this));
                                 }
                                 catch (System.Exception E)
                                 {
                                     Debug.Log(E.ToString());
                                 }
                             }
                             else
                             {
                                 _LoadedChunks.Add(new Chunk(newchunkpos.x, newchunkpos.z, this));
                                 //Debug.Log("Cant find saves: " + "Data/C_" + x + "_" + z + ".chk");
                             }
                         }
                     }
                     foreach (Chunk c in _LoadedChunks)
                     {
                         c.Start();
                     }
                 }
                 if (GameManager.PlayerLoaded())
                 {
                     PlayerPos = new Int3(GameManager.instance.playerpos);
                 }
                 foreach (Chunk c in _LoadedChunks)
                 {
                     if (Vector2.Distance(new Vector2(c.PosX * Chunk.ChunkWidth, c.PosZ * Chunk.ChunkWidth), new Vector2(PlayerPos.x, PlayerPos.z)) > ((RenderDistanceInChunks * 2) * Chunk.ChunkWidth))
                     {
                         c.Degenerate();
                     }
                 }
                 for (int x = -RenderDistanceInChunks; x < RenderDistanceInChunks; x++)
                 {
                     for (int z = -RenderDistanceInChunks; z < RenderDistanceInChunks; z++)
                     {
                         Int3 newchunkpos = new Int3(PlayerPos.x, PlayerPos.y, PlayerPos.z);
                         newchunkpos.AddPos(new Int3(x * Chunk.ChunkWidth, 0, z * Chunk.ChunkWidth));
                         newchunkpos.ToChunkCoordinates();
                         if (!ChunkExists(newchunkpos.x, newchunkpos.z))
                         {
                             if (System.IO.File.Exists(FileManager.GetChunkString(newchunkpos.x, newchunkpos.z)))
                             {
                                 try
                                 {
                                     Chunk c = new Chunk(newchunkpos.x, newchunkpos.z, Serializer.Deseralize_From_File <int[, , ]>(FileManager.GetChunkString(newchunkpos.x, newchunkpos.z)), this);
                                     c.Start();
                                     _LoadedChunks.Add(c);
                                 }
                                 catch (System.Exception E)
                                 {
                                     Debug.Log(E.ToString());
                                 }
                             }
                             else
                             {
                                 Chunk c = new Chunk(newchunkpos.x, newchunkpos.z, this);
                                 c.Start();
                                 _LoadedChunks.Add(c);
                                 //Debug.Log("Cant find saves: " + "Data/C_" + x + "_" + z + ".chk");
                             }
                         }
                     }
                 }
                 foreach (Chunk c in new List <Chunk>(_LoadedChunks))
                 {
                     c.Update();
                 }
             }
             catch (System.Exception E)
             {
                 UnityEngine.Debug.Log(E.StackTrace.ToString() + ":" + E.Data.ToString());
                 Logger.Log(E);
             }
         }
         Logger.Log("World thread succesfully stopped");
         Logger.MainLog.Update(); //Rerun last log
     });
     WorldThread.Start();
 }