Exemple #1
0
    // internal
    private void TerrainUpdate(Vector3Int centerCell)
    {
        // remove far objects
        List <Vector3Int> removed = new List <Vector3Int>();

        foreach (KeyValuePair <Vector3Int, MapModifier.TileGameObject> entry in modifier.tileObjects)
        {
            if (Mathf.Abs(centerCell.x - entry.Key.x) > streamingCellRadius || Mathf.Abs(centerCell.y - entry.Key.y) > streamingCellRadius)
            {
                removed.Add(entry.Key);
            }
        }
        foreach (Vector3Int cell in removed)
        {
            MapModifier.JobModifier job = new MapModifier.JobModifier();
            job.jobType      = MapModifier.JobType.RemoveTile;
            job.cellPosition = cell;
            modifier.jobs.Enqueue(job);
        }

        // create new tiles
        for (int x = centerCell.x - streamingCellRadius; x < centerCell.x + streamingCellRadius; x++)
        {
            for (int z = centerCell.y - streamingCellRadius; z < centerCell.y + streamingCellRadius; z++)
            {
                Vector3Int cellPosition = new Vector3Int(x, z, (int)modifier.tilemap.transform.position.y);
                if (modifier.tilemap.HasTile(cellPosition) && !modifier.tileObjects.ContainsKey(cellPosition))
                {
                    MapModifier.JobModifier job = new MapModifier.JobModifier();
                    job.jobType      = MapModifier.JobType.PlaceTile;
                    job.cellPosition = cellPosition;
                    job.tile         = modifier.tilemap.GetTile <ScriptableTile>(cellPosition);
                    modifier.jobs.Enqueue(job);
                }
            }
        }

        // update grid chunks visibility
        int radius = (int)(streamingCellRadius + MapChunk.chunkSize / modifier.tilemap.layoutGrid.cellSize.x);

        foreach (KeyValuePair <Vector2Int, MapChunk> entry in modifier.grid.grid)
        {
            Vector3Int chunkCellPos = modifier.tilemap.WorldToCell(MapChunk.CellToWorld(entry.Key));

            if (Mathf.Abs(centerCell.x - chunkCellPos.x) > radius || Mathf.Abs(centerCell.y - chunkCellPos.y) > radius)
            {
                entry.Value.gameObject.SetActive(false);
            }
            else
            {
                entry.Value.gameObject.SetActive(true);
            }
        }
    }
Exemple #2
0
    public void AddGameObject(GameObject go, ConstructionLayer.LayerType layer, bool objectIsBatchable, bool forceUpdate)
    {
        Vector2Int cell = MapChunk.WorldToCell(go.transform.position);

        // create new chunk if needed
        if (!grid.ContainsKey(cell))
        {
            GameObject chunkGo = new GameObject();
            chunkGo.name                    = "chunk " + cell.ToString();
            chunkGo.transform.parent        = chunkContainer;
            chunkGo.transform.position      = MapChunk.CellToWorld(cell);
            chunkGo.transform.localRotation = Quaternion.identity;
            chunkGo.transform.localScale    = Vector3.one;
            chunkGo.SetActive(true);

            MapChunk newchunk = chunkGo.AddComponent <MapChunk>();
            newchunk.InitContainers();
            grid.Add(cell, newchunk);
        }

        // add object and schedule batching job if needed
        MapChunk chunk = grid[cell];

        chunk.AddGameObject(go, (int)layer, objectIsBatchable);
        if (forceUpdate && chunk.batchUpdate.Count != 0)
        {
            foreach (Material m in chunk.batchUpdate)
            {
                JobGrid job = new JobGrid();
                job.jobType   = JobType.Rebake;
                job.chunkCell = cell;
                job.material  = m;
                jobs.Enqueue(job);
            }
        }
    }