Exemplo n.º 1
0
    // TODO: almost the same as UpdateEntities, need to refactor
    public void UpdateFurniture()
    {
        GameObject furnObj = GameObject.Find("furniture");

        if (furnObj != null)
        {
            ClearGameObject(furnObj);
        }
        else
        {
            furnObj = new GameObject("furniture");
        }

        Vector3Int playerPos = DDA.playerPos();
        int        size      = 60;
        Vector2Int from      = new Vector2Int(playerPos.x - size, playerPos.z - size);
        Vector2Int to        = new Vector2Int(playerPos.x + size, playerPos.z + size);

        Tile[] tiles = DDA.GetTilesBetween(from, to).tiles;

        foreach (var tile in tiles)
        {
            if (tile.furn == 0)
            {
                continue;
            }
            string stringId;
            furnIds.TryGetValue(tile.furn, out stringId);
            string name = stringId == null ? "f_unknown" : stringId;

            GameObject obj = new GameObject(name);
            obj.transform.parent = furnObj.transform;
            var pos = new Vector3(tile.loc.x, 0, tile.loc.z);
            obj.transform.Translate((pos - startingPoint) * tileSize);
            // probably should prepare GameObjects and do their clones instead
            var mr = obj.AddComponent <MeshRenderer>();
            var mf = obj.AddComponent <MeshFilter>();
            mf.sharedMesh     = voxModelsCache[name].ToMesh();
            mr.sharedMaterial = terrainMaterial;
            mf.sharedMesh.RecalculateNormals();
        }
    }
Exemplo n.º 2
0
        public void Create(Vector3Int chunkSize)
        {
            if (_created)
            {
                return;
            }
            var sw        = new Stopwatch();
            var chunkFrom = new Vector2Int((int)(Location.x * chunkSize.x), (int)(Location.y * chunkSize.y));
            var chunkTo   = new Vector2Int((int)(chunkFrom.x + chunkSize.x - 1), (int)(chunkFrom.y + chunkSize.y - 1));
            var map       = DDA.GetTilesBetween(chunkFrom, chunkTo);

            foreach (var tile in map.tiles)
            {
                // _blocks.Add(tile.loc, BlockLoader.CreateBlock(tile.ter, tile.loc, this)); // tymalu: changed tile.ter to int type, so this does not work anymore
            }

            _created = true;
            sw.Stop();
            CreateTiming = sw;
        }
Exemplo n.º 3
0
    public void Rebuild(int sleepMs = 0)
    {
        if (!needRebuild)
        {
            return;
        }
        var parentMap = gameObject.GetComponentInParent <TestMap>();

        if (start.x < 0 || start.y < 0 || end.x < 0 || end.y < 0)
        {
            return;             // will cause fatal error
        }
        mapData = DDA.GetTilesBetween(start, end);

        if (mapData == null)
        {
            return;
        }
        float tileSize = parentMap.tileSize;

        if (thread != null && thread.IsAlive)
        {
            return;
        }

        thread = new Thread(() =>
        {
            Thread.Sleep(sleepMs);
            MeshDraft chunkMesh = new MeshDraft();
            int gameObjectCount = 0;
            foreach (var tile in mapData.tiles)
            {
                //if (!tile.seen) continue;
                MeshDraft tileMesh = parentMap.GetCachedTerMesh(tile.ter); // probably will not work in non main thread
                if (tileMesh != null)
                {
                    if (chunkMesh.vertexCount + tileMesh.vertexCount < 65000)
                    {
                        MeshDraft tileMeshCopy = tileMesh.Clone();
                        tileMeshCopy.Move(new Vector3((tile.loc.x - start.x) * tileSize, tile.loc.y * tileSize, (tile.loc.z - start.y) * tileSize));
                        chunkMesh.Add(tileMeshCopy);
                    }
                    else
                    {
                        AssignMesh(chunkMesh, "subchunk" + gameObjectCount.ToString("D2"));
                        gameObjectCount++;
                        chunkMesh = new MeshDraft();
                    }
                }
            }

            if (chunkMesh.vertexCount > 0 && gameObjectCount == 0)
            {
                AssignMesh(chunkMesh);
            }
            else if (chunkMesh.vertexCount > 0)
            {
                AssignMesh(chunkMesh, "subchunk" + gameObjectCount.ToString("D2"));
            }
        });
        thread.Start();
    }