void Start() { TerrainManager manager = FindObjectOfType <TerrainManager>(); if (manager == null || !manager.enabled) { Debug.LogError("Not found TerrainManager or is enabled on scene!"); enabled = false; return; } Vector3 chunkSize = manager.chunkSize; Chunk.Coords startCoords = manager.ToChunkCoords(transform.position); Vector3 newPos = transform.position; if (startOnCenter) { newPos.x = startCoords.x * chunkSize.x + chunkSize.x / 2.0f; newPos.z = startCoords.y * chunkSize.z + chunkSize.z / 2.0f; } if (startOnSurface) { Chunk chunk = manager.GetChunk(startCoords); newPos.y = chunk.terrain.SampleHeight(newPos); } transform.position = newPos; }
private bool IsOutOfRange(Chunk.Coords currentCoords, Chunk.Coords otherCoords) { float distanceX = Mathf.Abs(currentCoords.x - otherCoords.x); float distanceY = Mathf.Abs(currentCoords.y - otherCoords.y); return(distanceX > destoryRange || distanceY > destoryRange); }
public bool ShowChunk(Chunk.Coords coords) { if (!chunkMap.ContainsKey(coords)) { CreateChunk(coords); return(true); } return(false); }
void Update() { if (ShouldCreateChunks()) { Chunk.Coords coords = GetCurrentCoords(); OnChangeChunk(coords); lastCreatedPosition = focus.position; } }
public Chunk?TryGetChunk(Chunk.Coords coords) { Chunk chunk; if (chunkMap.TryGetValue(coords, out chunk)) { return(chunk); } return(null); }
private void OnChangeChunk(Chunk.Coords coords) { if (showChunksCoroutine != null) { StopCoroutine(showChunksCoroutine); } showChunksCoroutine = StartCoroutine(StartShowChunks(coords)); RemoveChunksIfOutOfRange(coords); }
private Terrain CreateTerrainForChunk(Chunk.Coords coords) { TerrainData terrainData = CreateAndSetupTerrainData(); GameObject terrainObject = Terrain.CreateTerrainGameObject(terrainData); terrainObject.transform.position = new Vector3(chunkSize.x * coords.x, 0.0f, chunkSize.z * coords.y); terrainObject.transform.parent = gameObject.transform; terrainObject.transform.name = string.Format("Chunk ({0}, {1})", coords.x, coords.y); return(GetAndSetupTerrain(terrainObject)); }
private void RemoveChunksIfOutOfRange(Chunk.Coords currentCoords) { var chunkCoords = manager.LoadedChunksCoords; foreach (var coords in chunkCoords) { if (IsOutOfRange(currentCoords, coords)) { manager.RemoveChunk(coords); } } }
public Chunk GetChunk(Chunk.Coords coords) { Chunk?chunkOrNull = TryGetChunk(coords); if (chunkOrNull != null) { return((Chunk)chunkOrNull); } ShowChunk(coords); return((Chunk)TryGetChunk(coords)); }
public void RemoveChunk(Chunk.Coords coords) { Chunk?chunkOrNull = TryGetChunk(coords); if (chunkOrNull != null) { Chunk chunk = (Chunk)chunkOrNull; Destroy(chunk.terrain.gameObject); Destroy(chunk.terrain.terrainData); Destroy(chunk.terrain); chunkMap.Remove(coords); } }
private IEnumerator StartShowChunks(Chunk.Coords cuurentCoords) { chunksToLoad = GetChunkCoordsToLoad(cuurentCoords); chunksLoadedCount = 0; foreach (var coords in chunksToLoad) { manager.ShowChunk(coords); chunksLoadedCount++; yield return(null); } chunksToLoad = null; chunksLoadedCount = 0; }
private void SetupNeighbors(int x, int y) { Chunk.Coords coords = new Chunk.Coords(x, y); Chunk? chunk = TryGetChunk(x, y); if (chunk == null) { return; } Terrain left = GetTerrainOrNull(coords.x - 1, coords.y); Terrain top = GetTerrainOrNull(coords.x, coords.y + 1); Terrain right = GetTerrainOrNull(coords.x + 1, coords.y); Terrain bottom = GetTerrainOrNull(coords.x, coords.y - 1); chunk.Value.terrain.SetNeighbors(left, top, right, bottom); }
protected override void OnGenerateTerrain() { Chunk.Coords c = chunk.coords; heightmap = heightmapNoise.GetTiledMap(c.x, c.y, width, height); alphamap = data.GetAlphamaps(0, 0, width, height); SetupSplats(); SetupTrees(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float h = heightmap[y, x]; PaintTexture(x, y, h); PlaceTree(x, y, h); } } }
private void CreateChunk(Chunk.Coords coords) { Terrain terrain = CreateTerrainForChunk(coords); Chunk chunk = new Chunk(); chunk.coords = coords; chunk.terrain = terrain; generator.GenerateChunk(chunk); chunk.terrain.Flush(); chunkMap.Add(chunk.coords, chunk); if (setupsNeighbors) { SetupNeighbors(chunk.coords.x, chunk.coords.y); SetupNeighbors(chunk.coords.x + 1, chunk.coords.y); SetupNeighbors(chunk.coords.x, chunk.coords.y + 1); SetupNeighbors(chunk.coords.x - 1, chunk.coords.y); SetupNeighbors(chunk.coords.x, chunk.coords.y - 1); } }
private List <Chunk.Coords> GetChunkCoordsToLoad(Chunk.Coords coords) { List <Chunk.Coords> loadedChunk = manager.LoadedChunksCoords; List <Chunk.Coords> list = new List <Chunk.Coords>(); int r2 = createRange * createRange; for (int x = -createRange; x <= createRange; x++) { for (int y = -createRange; y <= createRange; y++) { Chunk.Coords c = new Chunk.Coords(coords.x + x, coords.y + y); bool inRange = x * x + y * y < r2; bool isLoaded = loadedChunk.Contains(c); if (inRange && !isLoaded) { list.Add(c); } } } return(list); }
void Start() { Chunk.Coords coords = GetCurrentCoords(); OnChangeChunk(coords); lastCreatedPosition = focus.position; }
protected override void GenerateHeightmap() { Chunk.Coords c = chunk.coords; heightmap = noise.GetTiledMap(c.x, c.y, width, height); }