Exemple #1
0
    public static NBTChunk LoadChunk(int chunkX, int chunkZ)
    {
        UnityEngine.Profiling.Profiler.BeginSample("ChunkChecker.Update");

        key.Set(chunkX, chunkZ);
        if (!chunkDict.ContainsKey(key))
        {
            TagNodeCompound Chunk = GetChunkNode(chunkX, chunkZ);
            if (Chunk != null)
            {
                TagNodeCompound Level = Chunk["Level"] as TagNodeCompound;

                TagNodeList Sections = Level["Sections"] as TagNodeList;
                NBTChunk    chunk    = ChunkPool.GetChunk();
                chunk.SetData(chunkX, chunkZ, Sections);
                chunkDict.Add(key, chunk);
            }
        }

        UnityEngine.Profiling.Profiler.EndSample();

        if (chunkDict.ContainsKey(key))
        {
            return(chunkDict[key]);
        }
        return(null);
    }
Exemple #2
0
    public void SetActive(bool active, Mesh mesh = null)
    {
        gameObject.SetActive(active);
        meshFilter.sharedMesh = mesh;

        if (active)
        {
            if (addCollider == AddColliderMode.Delayed)
            {
                Invoke("AddCollider", ChunkPool.GetBusyCount() * delay);
            }
            else if (addCollider == AddColliderMode.Immediate)
            {
                AddCollider();
            }
            else if (addCollider == AddColliderMode.DelayedOnReuse)
            {
                AddCollider();
                addCollider = AddColliderMode.Delayed;
            }
        }
        else
        {
            meshCollider.sharedMesh = null;
        }
    }
Exemple #3
0
 void InitChunks()
 {
     for (int i = 0; i < CountChunksAtStart; i++)
     {
         CurrentChunks.Add(ChunkPool.GetChunk());
     }
 }
    public void Initialize()
    {
        int poolWidth, poolHeight;

        poolWidth  = Screen.width / pixelsPerScreenPixel / pixelsPerTile / visualChunkSize + 2 * poolWidthBorder;
        poolHeight = Screen.height / pixelsPerScreenPixel / pixelsPerTile / visualChunkSize + 2 * poolHeightBorder;

        visualChunkPool =
            new ChunkPool <VisualChunk>(poolWidth, poolHeight, visualChunkPrefab, visualChunkContainer,
                                        visualChunkSize)
        {
            prevCamPosition = RoundToChunkPos(new Int2(cam.transform.position), visualChunkSize)
        };


        poolWidth  = Screen.width / pixelsPerScreenPixel / pixelsPerTile / lightChunkSize + 2 * poolWidthBorder + 2;
        poolHeight = Screen.height / pixelsPerScreenPixel / pixelsPerTile / lightChunkSize + 2 * poolHeightBorder + 2;

        lightChunkPool =
            new ChunkPool <LightChunk>(poolWidth, poolHeight, lightChunkPrefab, lightChunkContainer, lightChunkSize)
        {
            prevCamPosition = RoundToChunkPos(new Int2(cam.transform.position), lightChunkSize)
        };

        GenerateChunks(ref visualChunkPool);
        GenerateChunks(ref lightChunkPool);
    }
Exemple #5
0
 private void OnDestroy()
 {
     ChunkPool.Uninit();
     InputManager.Destroy();
     UISystem.DestroyUIRoot();
     PlayerController.Destroy();
     NBTHelper.Uninit();
 }
Exemple #6
0
    private T CreateChunk <T>(Int2 pos, ref ChunkPool <T> chunkPool) where T : Chunk
    {
        T newChunk = Instantiate(chunkPool.prefab, new Vector2(pos.x, pos.y), Quaternion.identity, chunkPool.container).GetComponent <T>();

        chunkPool.dictionary.Add(pos, newChunk);
        newChunk.Position = pos;
        return(newChunk);
    }
Exemple #7
0
    public static void LoadChunk(CSChunk csChunk)
    {
        //Debug.Log("loadChunk,x=" + csChunk.Position.x + ",z=" + csChunk.Position.y);
        Chunk chunk = ChunkPool.GetChunk();

        chunk.SetData(csChunk.Position.x, csChunk.Position.y, csChunk.BlocksInBytes);
        AddToChunkDict(chunk);
        ChunkRefresher.Add(chunk);
    }
Exemple #8
0
    private void Start()
    {
        Instance = this;

        regionManager = GetComponent <RegionManager>();

        chunkPool       = new ChunkPool((int)Mathf.Pow(loadRange * 2, 3), transform);
        visibleChunks   = new Dictionary <Vector3Int, ChunkObject>();
        markedForUnload = new HashSet <Vector3Int>();
    }
Exemple #9
0
 private void GenerateChunks <T>(ref ChunkPool <T> chunkPool) where T : Chunk
 {
     for (int x = 0; x < chunkPool.poolWidth; x++)
     {
         for (int y = 0; y < chunkPool.poolHeight; y++)
         {
             Int2 pos = new Int2((x - chunkPool.poolWidth / 2) * chunkPool.chunkSize + (int)cam.transform.position.x, (y - chunkPool.poolHeight / 2) * chunkPool.chunkSize + (int)cam.transform.position.y);
             chunkPool.chunkArray[x, y] = CreateChunk(pos, ref chunkPool);
         }
     }
 }
Exemple #10
0
 public Chunk(Vector3 position, Material material, byte ID)
 {
     indexID   = ID;
     this.mesh = new Mesh();
     chunk     = ChunkPool.SpawnGameObject();
     chunk.SetActive(false);
     chunk.name = string.Concat("Chunk: ", position.ToString());
     chunk.transform.position = new Vector3(position.x, 0, position.z);
     chunk.GetComponent <MeshRenderer>().material = material;
     meshFilter = chunk.GetComponent <MeshFilter>();
     toUpdate   = true;
 }
Exemple #11
0
 public static void RemoveChunk(int chunkX, int chunkZ)
 {
     key.Set(chunkX, chunkZ);
     if (chunkDict.ContainsKey(key))
     {
         NBTChunk chunk = chunkDict[key];
         ChunkRefresher.Remove(chunk);
         chunk.ClearData();
         ChunkPool.Recover(chunk);
         chunkDict.Remove(key);
     }
 }
Exemple #12
0
    private void UpdateChunk <T>(int x, int y, ChunkPool <T> chunkPool) where T : Chunk
    {
        T chunk;

        Int2 pos = RoundToChunkPos(new Int2(x, y), chunkPool.chunkSize);

        chunkPool.dictionary.TryGetValue(pos, out chunk);
        if (chunk)
        {
            chunk.UpdateChunk();
        }
    }
Exemple #13
0
    public static void UnloadChunk(int x, int z)
    {
        //Debug.Log("UnloadChunk,x=" + x + ",z=" + z);
        Chunk chunk = GetChunk(x, z);

        if (chunk != null)
        {
            ChunkRefresher.Remove(chunk);
            chunk.ClearData();
            RemoveFromChunkDict(chunk);
            ChunkPool.Recover(chunk);
        }
    }
Exemple #14
0
        public Chunk GetChunk(int x, int z)
        {
            bool haveData = false;

            if (db == null)
            {
                throw new InvalidOperationException("Open Db first");
            }

            var subChunks = new Dictionary <byte, byte[]>();

            var key = CreateKey(x, z);

            for (byte subChunkIdx = 0; subChunkIdx < 15; subChunkIdx++)
            {
                key[9] = subChunkIdx;
                UIntPtr length;
                var     data = db.Get(key, out length);
                if (data != null)
                {
                    subChunks[subChunkIdx] = data;
                    haveData = true;
                }
            }

            if (!haveData)
            {
                return(null);
            }

            Chunk c;

            if (ChunkPool != null)
            {
                c   = ChunkPool.Get();
                c.X = x;
                c.Z = z;
            }
            else
            {
                c = new Chunk(x, z);
            }

            foreach (var subChunkRaw in subChunks)
            {
                CopySubChunkToChunk(c, subChunkRaw);
            }

            return(c);
        }
Exemple #15
0
    void Start()
    {
        ChunkChecker.Init();
        ChunkRefresher.Init();
        ChunkManager.Init();
        ChunkPool.Init();
        OtherPlayerManager.Init();
        ItemSelectPanel.Show();
        ChatPanel.ShowChatPanel();

        List <Vector2Int> preloadChunks = Utilities.GetSurroudingChunks(PlayerController.GetCurrentChunk());

        ChunkManager.ChunksEnterLeaveViewReq(preloadChunks);
    }
Exemple #16
0
    public void Initialize()
    {
        chunkPool = FindObjectOfType <ChunkPool>();
        chunks    = new HashSet <Chunk>();
        coords    = new HashSet <Vector3Int>();

        chunkSize    = voxelSettings.VoxelsPerChunk;
        invChunkSize = new Vector3(
            1f / (float)chunkSize.x,
            1f / (float)chunkSize.y,
            1f / (float)chunkSize.z);
        bounds            = new BoundsCollection(chunkSize, extents);
        updateSqrDistance = updateDistance * updateDistance;
        isInitialized     = true;
    }
    private void UpdateChunk <T>(int x, int y, ChunkPool <T> chunkPool) where T : Chunk
    {
        T chunk;
        // Find the chunk that the position is located in and update it if it exists
        Int2 pos = RoundToChunkPos(new Int2(x, y), chunkPool.chunkSize);

        chunkPool.dictionary.TryGetValue(pos, out chunk);
        if (chunk)
        {
            chunk.UpdateChunk();
        }
        else
        {
            Debug.Log("not here");
        }
    }
Exemple #18
0
    void Start()
    {
        SettingsPanel.Init();
        ChunkRefresher.Init();
        ChunkPool.Init();
        ItemSelectPanel.Show();
        ChatPanel.ShowChatPanel();
        InventorySystem.Init();
        GameModeManager.Init();

        // load chunk here
        ChunkChecker.Init();

        PlayerController.Init();
        LocalNavMeshBuilder.Init();
        ChunkRefresher.ForceRefreshAll();
    }
 private void GenerateChunks <T>(ref ChunkPool <T> chunkPool) where T : Chunk
 {
     chunkPool.ResetRows();
     chunkPool.dictionary.Clear();
     for (int x = 0; x < chunkPool.poolWidth; x++)
     {
         for (int y = 0; y < chunkPool.poolHeight; y++)
         {
             Int2 camPosition = RoundToChunkPos(new Int2(cam.transform.position), chunkPool.chunkSize);
             Int2 pos         = new Int2((x - chunkPool.poolWidth / 2) * chunkPool.chunkSize,
                                         (y - chunkPool.poolHeight / 2) * chunkPool.chunkSize);
             pos += camPosition;
             chunkPool.dictionary.Add(pos, chunkPool.chunkArray[x, y]);
             chunkPool.chunkArray[x, y].transform.position = new Vector2(pos.x, pos.y);
             chunkPool.chunkArray[x, y].Position           = pos;
         }
     }
 }
Exemple #20
0
    public void Initialize()
    {
        cam = Camera.main;

        int poolWidth  = Screen.width / pixelsPerScreenPixel / pixelsPerTile / visualChunkSize + poolWidthBorder;
        int poolHeight = Screen.height / pixelsPerScreenPixel / pixelsPerTile / visualChunkSize + poolHeightBorder;

        visualChunkPool = new ChunkPool <VisualChunk>(poolWidth, poolHeight, 0, poolWidth - 1, poolHeight - 1, 0, visualChunkPrefab, visualChunkContainer, visualChunkSize);
        visualChunkPool.prevCamPosition = RoundToChunkPos(new Int2((int)cam.transform.position.x, (int)cam.transform.position.y), visualChunkSize);

        poolWidth  = Screen.width / pixelsPerScreenPixel / pixelsPerTile / colliderChunkSize + poolWidthBorder;
        poolHeight = Screen.height / pixelsPerScreenPixel / pixelsPerTile / colliderChunkSize + poolHeightBorder;

        colliderChunkPool = new ChunkPool <ColliderChunk>(poolWidth, poolHeight, 0, poolWidth - 1, poolHeight - 1, 0, colliderChunkPrefab, colliderChunkContainer, colliderChunkSize);
        colliderChunkPool.prevCamPosition = RoundToChunkPos(new Int2((int)cam.transform.position.x, (int)cam.transform.position.y), colliderChunkSize);


        GenerateChunks <VisualChunk>(ref visualChunkPool);
        GenerateChunks <ColliderChunk>(ref colliderChunkPool);
    }
Exemple #21
0
 public PoolArena(string name, int pageSize, int maxOrder, int chunkSize)
 {
     _name = name;
     _pageSize = pageSize;
     _maxOrder = maxOrder;
     _chunkSize = chunkSize;
     _pageShifts = Utils.Log2(pageSize);
     _subpageOverflowMask = ~(pageSize - 1);
     _tinySubpagePools = new PoolPage[32];
     for (var i = 0; i < _tinySubpagePools.Length; i++)
     {
         _tinySubpagePools[i] = this.CreateSubpagePoolHead();
     }
     _smallSubpagePools = new PoolPage[_pageShifts - 9];
     for (var i = 0; i < _smallSubpagePools.Length; i++)
     {
         _smallSubpagePools[i] = this.CreateSubpagePoolHead();
     }
     _chunkPools = new ChunkPool();
 }
Exemple #22
0
 public PoolArena(string name, int pageSize, int maxOrder, int chunkSize)
 {
     _name                = name;
     _pageSize            = pageSize;
     _maxOrder            = maxOrder;
     _chunkSize           = chunkSize;
     _pageShifts          = Utils.Log2(pageSize);
     _subpageOverflowMask = ~(pageSize - 1);
     _tinySubpagePools    = new PoolPage[32];
     for (var i = 0; i < _tinySubpagePools.Length; i++)
     {
         _tinySubpagePools[i] = this.CreateSubpagePoolHead();
     }
     _smallSubpagePools = new PoolPage[_pageShifts - 9];
     for (var i = 0; i < _smallSubpagePools.Length; i++)
     {
         _smallSubpagePools[i] = this.CreateSubpagePoolHead();
     }
     _chunkPools = new ChunkPool();
 }
Exemple #23
0
 void Start()
 {
     master         = Master;
     Pool           = new ChunkPool(MaxInstanceCount);
     ChunkGrid      = new Chunk[3, 3, 3];
     maxsize        = Mathf.Pow(16, Visibility);
     MinSize        = Mathf.Pow(16, Resolution);
     DrawList       = new List <Chunk>();
     PrecomputeList = new List <PrecomputeAttrib>();
     for (int i = -1; i <= 1; i++)
     {
         for (int j = -1; j <= 1; j++)
         {
             for (int k = -1; k <= 1; k++)
             {
                 ChunkGrid[(i + 3) % 3, (j + 3) % 3, (k + 3) % 3] = Pool.GetInstance(new Vector3(i, j, k), maxsize, "INIT");
                 ChunkGrid[(i + 3) % 3, (j + 3) % 3, (k + 3) % 3].Dfs(Player.transform.position);
             }
         }
     }
 }
Exemple #24
0
        public Chunk GetChunk(int x, int z, ChunkData data)
        {
            Chunk c;

            if (ChunkPool != null)
            {
                c   = ChunkPool.Get();
                c.X = x;
                c.Z = z;
            }
            else
            {
                c = new Chunk(x, z);
            }

            foreach (var subChunkRaw in data.SubChunks)
            {
                CopySubChunkToChunk(c, subChunkRaw.Index, subChunkRaw.Data);
            }

            return(c);
        }
Exemple #25
0
    void CheckLast()
    {
        if (CurrentChunks.Count <= 1)
        {
            return;
        }
        CurrentDistance.x = Mathf.Abs(Block.transform.position.x - Rocket.transform.position.x);
        CurrentDistance.y = Mathf.Abs(Block.transform.position.y - Rocket.transform.position.y);
        if (CurrentDistance.x > Distance || CurrentDistance.y > Distance)
        {
            var chunk = CurrentChunks[0];


            Block.position = chunk.transform.position;
            CurrentChunks.Remove(chunk);
            chunk.ChunkDisable();
            CurrentChunks.Add(ChunkPool.GetChunk());
            if (!lockSpeed)
            {
                ChangeSpeed();
            }
        }
    }
Exemple #26
0
    public static async Task <NBTChunk> LoadChunkAsync(int chunkX, int chunkZ)
    {
        Vector2Int key = new Vector2Int(chunkX, chunkZ);

        if (!chunkDict.ContainsKey(key))
        {
            TagNodeCompound Chunk = await GetChunkNodeAsync(chunkX, chunkZ);

            if (Chunk != null)
            {
                TagNodeCompound Level = Chunk["Level"] as TagNodeCompound;
                NBTChunk        chunk = ChunkPool.GetChunk();
                chunk.SetData(chunkX, chunkZ, Level);
                chunkDict.Add(key, chunk);
            }
        }

        if (chunkDict.ContainsKey(key))
        {
            return(chunkDict[key]);
        }
        return(null);
    }
 void Start()
 {
     memoryManager = new SpatialChunkPool(chunkBuilderAsset);
     InitializeFirstChunks();
 }
Exemple #28
0
    private void Awake()
    {
        Instance = this;

        chunkPool = new List <CubeChunk>();
    }
Exemple #29
0
        public BitmapRenderBlock(Dictionary <string, Texture> textureDictionary, string texturePath, ChunkPool pool,
                                 RenderSettings renderSettings, IGraphicsApi <TImage> graphics, int chunkSize, int chunksPerDimension,
                                 ExecutionDataflowBlockOptions options)
        {
            int tileSize = chunksPerDimension * chunkSize;

            graphics.SetPoolDimensions(tileSize, tileSize);
            int chunkRenderedCounter = 0;
            ThreadLocal <RendererCombi <TImage> > renderCombi = new ThreadLocal <RendererCombi <TImage> >(() =>
                                                                                                          new RendererCombi <TImage>(textureDictionary, texturePath, renderSettings, graphics));

            Block = new TransformBlock <IEnumerable <ChunkAndData>, ImageInfo <TImage> >(chunkAndData =>
            {
                var b = graphics.GetPooledImage();
                {
                    var chunkList     = chunkAndData.ToList();
                    var firstX        = chunkList.First().C.X;
                    var firstZ        = chunkList.First().C.Z;
                    var chunkRenderer = renderCombi.Value.ChunkRenderer;

                    foreach (var chunk in chunkList)
                    {
                        var x = chunk.C.X % chunksPerDimension;
                        var z = chunk.C.Z % chunksPerDimension;
                        if (x < 0)
                        {
                            x += chunksPerDimension;
                        }
                        if (z < 0)
                        {
                            z += chunksPerDimension;
                        }
                        chunkRenderer.RenderChunk(b, chunk.C, x * chunkSize, z * chunkSize);

                        pool?.Return(chunk.C);
                    }


                    var fx = CoordHelpers.GetGroupedCoordinate(firstX, chunksPerDimension);
                    var fz = CoordHelpers.GetGroupedCoordinate(firstZ, chunksPerDimension);

                    Interlocked.Increment(ref processedCount);
                    Interlocked.Add(ref chunkRenderedCounter, chunkList.Count);

                    if (chunkRenderedCounter >= 32)
                    {
                        var v = chunkRenderedCounter;
                        ChunksRendered?.Invoke(this, new ChunksRenderedEventArgs(v));
                        Interlocked.Add(ref chunkRenderedCounter, -v);
                    }

                    return(new ImageInfo <TImage>()
                    {
                        Image = b, X = fx, Z = fz, Cd = chunkList.SelectMany(x => x.Cd)
                    });
                }
            }, options);
        }
 private void InitializeChunkPool()
 {
     chunkPool = new ChunkPool(chunkPoolSize);
 }
Exemple #31
0
    private void MoveCamera <T>(Int2 dist, ref ChunkPool <T> chunkPool) where T : Chunk
    {
        if (dist.x != 0)
        {
            T   chunk;
            int col;

            int moveDirection = (int)Mathf.Sign(dist.x);

            for (int x = 0; x < Mathf.Abs(dist.x); x++)
            {
                if (moveDirection == 1)
                {
                    col = chunkPool.leftCol;
                }
                else
                {
                    col = chunkPool.rightCol;
                }

                for (int y = 0; y < chunkPool.poolHeight; y++)
                {
                    chunk = chunkPool.chunkArray[col, y];
                    chunkPool.dictionary.Remove(chunk.Position);
                    chunk.transform.Translate(new Vector3(moveDirection * chunkPool.poolWidth * chunkPool.chunkSize, 0, 0));

                    chunk.Position = new Int2(chunk.transform.position);
                    chunkPool.dictionary.Add(chunk.Position, chunk);
                }

                chunkPool.leftCol  = Utilities.Mod((moveDirection + chunkPool.leftCol), chunkPool.poolWidth);
                chunkPool.rightCol = Utilities.Mod((moveDirection + chunkPool.rightCol), chunkPool.poolWidth);
            }
        }
        if (dist.y != 0)
        {
            T   chunk;
            int row;

            int moveDirection = (int)Mathf.Sign(dist.y);

            for (int y = 0; y < Mathf.Abs(dist.y); y++)
            {
                if (moveDirection == 1)
                {
                    row = chunkPool.botRow;
                }
                else
                {
                    row = chunkPool.topRow;
                }

                for (int x = 0; x < chunkPool.poolWidth; x++)
                {
                    chunk = chunkPool.chunkArray[x, row];
                    chunkPool.dictionary.Remove(chunk.Position);
                    chunk.transform.Translate(new Vector3(0, moveDirection * chunkPool.poolHeight * chunkPool.chunkSize, 0));

                    chunk.Position = new Int2(chunk.transform.position);
                    chunkPool.dictionary.Add(chunk.Position, chunk);
                }

                chunkPool.topRow = Utilities.Mod((moveDirection + chunkPool.topRow), chunkPool.poolHeight);
                chunkPool.botRow = Utilities.Mod((moveDirection + chunkPool.botRow), chunkPool.poolHeight);
            }
        }
    }