public TerrainManager Initialize(SimManager simMan, TerrainGenerator terrainGenerator, int cellsPerChunk, int numberOfChunks, Material chunkMaterial) { this.simMan = simMan; this.terrainGenerator = terrainGenerator; this.cellsPerChunk = cellsPerChunk; this.numberOfChunks = numberOfChunks; transform.name = "TerrainManager"; activeChunks = new TerrainChunk[numberOfChunks, numberOfChunks]; ChunkLocation[] locs = new ChunkLocation[numberOfChunks * numberOfChunks]; for (int i = 0; i < numberOfChunks; i++) { for (int j = 0; j < numberOfChunks; j++) { locs[i * numberOfChunks + j] = new ChunkLocation(i, j, cellsPerChunk); activeChunks[i, j] = new GameObject().AddComponent <TerrainChunk>().Initialize(this, cellsPerChunk, locs[i * numberOfChunks + j], terrainGenerator, chunkMaterial); activeChunks[i, j].transform.parent = transform; } } foreach (TerrainChunk chunk in activeChunks) { chunk.UpdateChunkMesh(); } return(this); }
public TerrainChunk Initialize(TerrainManager terrainManager, int cellsPerChunk, ChunkLocation location, TerrainGenerator terrainGenerator, Material chunkMaterial) { this.terrainManager = terrainManager; transform.position = new Vector3(location.GetX() * cellsPerChunk, 0, location.GetZ() * cellsPerChunk); transform.name = "Chunk"; gameObject.layer = 8; this.location = location; this.cellsPerChunk = cellsPerChunk; cellHeights = new float[cellsPerChunk + 1, cellsPerChunk + 1]; for (int i = 0; i < cellsPerChunk + 1; i++) { for (int j = 0; j < cellsPerChunk + 1; j++) { cellHeights[i, j] = terrainGenerator.GetTerrainHeight(i + location.GetAbsX(), j + location.GetAbsZ()); } } chunkCells = new TerrainCell[cellsPerChunk, cellsPerChunk]; for (int i = 0; i < cellsPerChunk; i++) { for (int j = 0; j < cellsPerChunk; j++) { CellLocation loc = new CellLocation(i, j, location); chunkCells[i, j] = new TerrainCell(loc, terrainGenerator); } } m_material = chunkMaterial; GetComponent <MeshRenderer>().material = m_material; return(this); }
public CellLocation(int x, int z, ChunkLocation parent) { this.x = x; this.z = z; this.parent = parent; }