Exemplo n.º 1
0
        private IEnumerator WaitAndAddChunk(Int3 batchId, int level)
        {
            yield return(new WaitForSeconds(0.5f));

            Chunk chunk = new Chunk(batchId, level);

            if (!loadedChunks.Contains(chunk))
            {
                loadedChunks.Add(chunk);
                added.Add(chunk);
                chunkAwarePacketReceiver.ChunkLoaded(chunk);
            }
        }
Exemplo n.º 2
0
        internal void UpdateChunks()
        {
            var newChunks = new List <Coordinates2D>();

            for (int x = -ChunkRadius; x < ChunkRadius; x++)
            {
                for (int z = -ChunkRadius; z < ChunkRadius; z++)
                {
                    newChunks.Add(new Coordinates2D(
                                      ((int)Entity.Position.X >> 4) + x,
                                      ((int)Entity.Position.Z >> 4) + z));
                }
            }
            // Unload extraneous columns
            lock (LoadedChunks)
            {
                var currentChunks = new List <Coordinates2D>(LoadedChunks);
                foreach (Coordinates2D chunk in currentChunks)
                {
                    if (!newChunks.Contains(chunk))
                    {
                        UnloadChunk(chunk);
                    }
                }
                // Load new columns
                foreach (Coordinates2D chunk in newChunks)
                {
                    if (!LoadedChunks.Contains(chunk))
                    {
                        LoadChunk(chunk);
                    }
                }
            }
            ((EntityManager)Server.GetEntityManagerForWorld(World)).UpdateClientEntities(this);
        }
Exemplo n.º 3
0
        internal void UpdateChunks(bool block = false)
        {
            var newChunks = new HashSet <Coordinates2D>();
            var toLoad    = new List <Tuple <Coordinates2D, IChunk> >();

            Profiler.Start("client.new-chunks");
            for (int x = -ChunkRadius; x < ChunkRadius; x++)
            {
                for (int z = -ChunkRadius; z < ChunkRadius; z++)
                {
                    var coords = new Coordinates2D(
                        ((int)Entity.Position.X >> 4) + x,
                        ((int)Entity.Position.Z >> 4) + z);
                    newChunks.Add(coords);
                    if (!LoadedChunks.Contains(coords))
                    {
                        toLoad.Add(new Tuple <Coordinates2D, IChunk>(
                                       coords, World.GetChunk(coords, generate: block)));
                    }
                }
            }
            Profiler.Done();
            var encode = new Action(() =>
            {
                Profiler.Start("client.encode-chunks");
                foreach (var tup in toLoad)
                {
                    var coords = tup.Item1;
                    var chunk  = tup.Item2;
                    if (chunk == null)
                    {
                        chunk = World.GetChunk(coords);
                    }
                    chunk.LastAccessed = DateTime.UtcNow;
                    LoadChunk(chunk);
                }
                Profiler.Done();
            });

            if (block)
            {
                encode();
            }
            else
            {
                Task.Factory.StartNew(encode);
            }
            Profiler.Start("client.old-chunks");
            LoadedChunks.IntersectWith(newChunks);
            Profiler.Done();
            Profiler.Start("client.update-entities");
            ((EntityManager)Server.GetEntityManagerForWorld(World)).UpdateClientEntities(this);
            Profiler.Done();
        }
Exemplo n.º 4
0
 /// <summary>
 /// Updates which chunks are loaded on the client.
 /// </summary>
 public virtual void UpdateChunks(bool forceUpdate)
 {
     if (!forceUpdate && PauseChunkUpdates)
     {
         return;
     }
     if (forceUpdate ||
         (int)(Entity.Position.X) >> 4 != (int)(Entity.OldPosition.X) >> 4 ||
         (int)(Entity.Position.Z) >> 4 != (int)(Entity.OldPosition.Z) >> 4)
     {
         var newChunks = new List <Coordinates2D>();
         for (int x = -Settings.ViewDistance; x < Settings.ViewDistance; x++)
         {
             for (int z = -Settings.ViewDistance; z < Settings.ViewDistance; z++)
             {
                 newChunks.Add(new Coordinates2D(
                                   ((int)Entity.Position.X >> 4) + x,
                                   ((int)Entity.Position.Z >> 4) + z));
             }
         }
         // Unload extraneous columns
         lock (LoadedChunks)
         {
             var currentChunks = new List <Coordinates2D>(LoadedChunks);
             foreach (Coordinates2D chunk in currentChunks)
             {
                 if (!newChunks.Contains(chunk))
                 {
                     UnloadChunk(chunk);
                 }
             }
             // Load new columns
             foreach (Coordinates2D chunk in newChunks)
             {
                 if (!LoadedChunks.Contains(chunk))
                 {
                     LoadChunk(chunk);
                 }
             }
         }
     }
 }
Exemplo n.º 5
0
        public void Load(Chunk item, Transform parent)
        {
            if (!GeneratedChunks.ContainsID(item.ID) || LoadedChunks.Contains(item))
            {
                return;
            }

            LoadedChunks.Add(item);

            var chunkObj = new GameObject("Chunk:" + item.IDStr);

            item.Object      = GameObject.Instantiate(chunkObj, parent);
            item.Object.name = item.Object.name.Substring(0, item.Object.name.Length - 7);

            if (GameSystem.TileSelected && item.Contains(GameSystem.SelectedTile))
            {
                GameObject selector = VisualContainer.CreateObject("ui:selector", GameSystem.SelectedTile, 1.1f, item.Object.transform);
                item.Objects.Add(selector);
            }

            foreach (var block in item.Blocks)
            {
                if (!item.Contains(block.Location))
                {
                    Debug.Log(string.Format("Attempting to load Block {0},{1} into Chunk {2} has failed.", block.Location.X.ToString(), block.Location.Y.ToString(), item.IDStr));
                }

                GameObject obj = VisualContainer.CreateObject(block.Tile.Type.IdName, block.Location, 0, item.Object.transform);
                item.Objects.Add(obj);

                if (block.Overlay.Type != ObjectID.ENV.VOID)
                {
                    GameObject overlayObj = VisualContainer.CreateObject(block.Overlay.Type.IdName, block.Location, 0.1f, item.Object.transform);
                    item.Objects.Add(overlayObj);
                }
            }

            GameObject.Destroy(chunkObj);
        }