Exemplo n.º 1
0
    private void SaveTerrainData()
    {
        string filePath = cell.GetCellSaveFileName(world.WorldName);

        Debug.LogWarning("Trying ot write terrain data to " + filePath);
        for (int count = 0; count < terrain.Length; count++)
        {
            RAKTerrainSavedData.saveTerrainBytes(terrain[count], filePath + "T" + count + ".area");
        }
    }
Exemplo n.º 2
0
        public static bool saveTerrainBytes(RAKTerrain terrain, string path)
        {
            TerrainData data = terrain.getTerrainData();

            float[,,] alphaMap = data.GetAlphamaps(0, 0, data.alphamapWidth, data.alphamapHeight);
            float[,] heightMap = data.GetHeights(0, 0, data.heightmapWidth, data.heightmapHeight);
            SplatPrototype[] splats = data.splatPrototypes;
            Debug.LogWarning("Saving terrain");

            Vector3             terrainDataSize = terrain.terrain.terrainData.size;
            RAKTerrainSavedData saveData        = new RAKTerrainSavedData(alphaMap, heightMap, splats, data.treePrototypes,
                                                                          data.treeInstances, terrain.nonTerrainObjects, terrainDataSize);

            saveTerrainObject(saveData, path);
            terrain.savedData = saveData;
            return(true);
        }
Exemplo n.º 3
0
 private static bool saveTerrainObject(RAKTerrainSavedData terrain, string path)
 {
     binarySerialize(path, terrain);
     return(true);
 }
Exemplo n.º 4
0
    public void Initialize(World world, HexCell cell)
    {
        if (debug)
        {
            InitializeDebugTerrain(world, cell);
            return;
        }
        // Set static data //
        RAKTerrainMaster.world = world;
        RAKTerrainMaster.cell  = cell;
        DateTime startTime = DateTime.Now;

        terrain = new RAKTerrain[worldSize];
        // Check if files are already present for this cell //
        bool saveDataAvail = IsSaveDataAvailable(cell, world.WorldName);

        if (forceGenerate)
        {
            saveDataAvail = false;
        }
        if (cell.GetChunkMaterial() == HexGridChunk.ChunkMaterial.GRASS)
        {
            currentBiome = RAKBiome.getForestBiome();
        }
        else
        {
            currentBiome = RAKBiome.getForestBiome();
        }

        #region TERRAIN GENERATION
        // Load single terrain loop //
        for (int count = 0; count < worldSize; count++)
        {
            TerrainData         td;
            RAKTerrainSavedData savedTerrain = null;
            // If no save data available, go ahead and generate a new terrain //
            if (!saveDataAvail) //  generate //
            {
                TerrainData previousTD = null;
                if (count > 0)
                {
                    currentBiome.GetNewOffsets();
                }
                td = generateTerrain(width, height, currentBiome.depth, currentBiome.scale, currentBiome.offsetX,
                                     currentBiome.offsetY);
                generateSplatPrototypes(td);
            }
            // Save data is available //
            else
            {
                savedTerrain = RAKTerrainSavedData.loadTerrain(cell.GetCellSaveFileName(world.WorldName) + "T" + count + ".area");
                td           = savedTerrain.generateTerrainDataFromFlatMap();
                Debug.LogWarning("Loading terrain from disk tdsize - " + td.size.x + "-" + td.size.y + "-" + td.size.z);
            }
            // TerrainData generation complete, finish setting up Unity objects //
            GameObject go = Terrain.CreateTerrainGameObject(td);
            go.isStatic = false;
            go.transform.SetParent(transform);
            go.name = "Terrain" + count;
            logDebug("Terrain created - " + go.name);
            Terrain terrainComp = go.GetComponent <Terrain>();
            terrain[count] = go.AddComponent <RAKTerrain>();
            terrain[count].initialize(this);
            terrain[count].savedData = savedTerrain;
            mapPositions(count);
            terrain[count].setBiome(RAKBiome.getForestBiome());
        }
        // Tell Unity which terrain objects are next to each other //
        setNeighbors();
        // If we are generating new world terrains, fix the sides and seam together //
        if (!saveDataAvail)
        {
            fixGaps(16);
        }
        // Generate the terrain details //
        for (int count = 0; count < terrain.Length; count++)
        {
            generateSplatMap(terrain[count].getTerrainData());
            // If no save data, generate new details //
            if (!saveDataAvail)
            {
                generateNonTerrainDetailPrefabs(terrain[count].getTerrainComponenet());
            }
            // Save data avail, load details from disk //
            else
            {
                Debug.Log("Loading terrain from disk");
                generateNonTerrainDetailsFromDisk(terrain[count]);
            }
        }
        #endregion

        float currentTime = Time.time;

        /*List<NavMeshSurface> surfaces = new List<NavMeshSurface>();
         * surfaces.Add(gameObject.AddComponent<NavMeshSurface>());
         * surfaces[0].collectObjects = CollectObjects.Children;
         * RAKMeshBaker.Bake(surfaces.ToArray());
         * Debug.LogWarning("Build nav mesh took - " + (Time.time - currentTime));*/

        // Save to disk if needed //
        if (!saveDataAvail)
        {
            SaveTerrainData();
        }

        // Finish loading //
        RAKTerrainMaster.generateCreatures(cell, world);
        RAKTerrainMaster.sun = gameObject.AddComponent <RAKWeather>();
        RAKTerrainMaster.sun.start(transform, this, sun.gameObject);
        TimeSpan difference = startTime.Subtract(DateTime.Now);
        Debug.LogWarning("LOAD TIME - " + difference.TotalMilliseconds);
    }