private void GenerateStructures(chunkCenter cCenter, List <float> noiseValues) { /* * int randDex = random.Next(noiseValues.Count); * if(noiseValues[randDex] > structuresOdds) * { * int index = random.Next(structures.Count); * //structures[index] * int ind = 0; * List<List<Vector3Int>> positionLists = new List<List<Vector3Int>>(); * foreach(var positionList in structures[index].positionLists) * { * positionLists.Add(new List<Vector3Int>()); * foreach(Vector3Int vector in positionList.list) * { * positionLists[ind].Add(vector + new Vector3Int(cCenter.boundsCenterX, cCenter.boundsCenterY,0)); * } * ind++; * } * int ind2 = 0; * foreach (var tilemap in structureTilemaps) * { * tilemap.SetTiles(positionLists[ind2].ToArray(),structures[index].tileLists[ind2].list.ToArray()); * ind2++; * } * }*/ }
private IEnumerator cullChunk(chunkCenter cCenter, float stagger) { yield return(stagger); Debug.Log("culling chunk: (" + cCenter.boundsCenterX.ToString() + "," + cCenter.boundsCenterY.ToString() + ")"); int boundsCenterX = cCenter.boundsCenterX; int boundsCenterY = cCenter.boundsCenterY; int size = defaultChunkSize; List <Vector3Int> positionList = new List <Vector3Int>(); List <TileBase> nullList = new List <TileBase>(); for (int x = boundsCenterX - size / 2; x < boundsCenterX + size / 2; x++) { for (int y = boundsCenterY - size / 2; y < boundsCenterY + size / 2; y++) { positionList.Add(new Vector3Int(x, y, 0)); nullList.Add(null); } } foreach (var tilemap in tilemaps) { tilemap.SetTiles(positionList.ToArray(), nullList.ToArray()); } }
// Start is called before the first frame update void Start() { camFollow = CameraFollow.instance; loadedChunks = new HashSet <chunkCenter>(); loadedChunks.Add(new chunkCenter(qInit.x, qInit.y)); chunkCenter origin = new chunkCenter(qInit.x, qInit.y); random = new System.Random(); moistureNoiseIndex = 1; heightNoiseIndex = 0; int index = 0; foreach (var nosP in noiseParameterLists) { if (randomizeSeed) { nosP.setSeed((int)UnityEngine.Random.Range(-123456789f, 123456789f)); } /*if (nosP.name == "Moisture") moistureNoiseIndex = index; * else if (nosP.name == "Height") heightNoiseIndex = index;*/ index++; } biomesDict = new Dictionary <Tuple <int, int>, string>(); GenerateMap(new List <chunkCenter> { origin }, defaultChunkSize); StartCoroutine(chunkManager()); StartCoroutine(chunkCuller()); }
private bool chunkExistsOnFile(chunkCenter c) { Debug.Log("Checking if chunk " + c.boundsCenterX + ", " + c.boundsCenterY + " exists on file"); DecompressedChunk chunkParams = cpHandler.LoadChunkFromDisk(c.boundsCenterX / defaultChunkSize, c.boundsCenterY / defaultChunkSize, defaultChunkSize); if (chunkParams == null) { return(false); } SetTilesForChunk(chunkParams.tilemapIndices, chunkParams.tileLists, chunkParams.positionLists); return(true); //Chunk.ChunkExists(c.boundsCenterX/defaultChunkSize, c.boundsCenterY/defaultChunkSize); }
private JobHandle GenerateNewChunk(chunkCenter c, int size, List <NativeArray <float> > noiseLists, NoiseParameters nosP) { NativeArray <float> NoiseValues = new NativeArray <float>(size * size, Allocator.TempJob); GenerateMapJob job = new GenerateMapJob ( c.boundsCenterX, c.boundsCenterY, size, nosP, NoiseValues ); noiseLists.Add(NoiseValues); Debug.Log("Generating new chunk noise values at " + c.boundsCenterX.ToString() + ", " + c.boundsCenterY.ToString()); return(job.Schedule()); }
private void changeCenterChunk(Vector3 playerPos, chunkCenter qNE, chunkCenter qE, chunkCenter qW, chunkCenter qSE, chunkCenter qS, chunkCenter qN, chunkCenter qNW, chunkCenter qSW) { //top right corner if (playerPos.x > qInit.x + defaultChunkSize / 2 && playerPos.y > qInit.y + defaultChunkSize / 2) { //qNE qInit = new Vector2Int(qNE.boundsCenterX, qNE.boundsCenterY); } else if (playerPos.x > qInit.x + defaultChunkSize / 2 && playerPos.y < qInit.y - defaultChunkSize / 2) { //qSE qInit = new Vector2Int(qSE.boundsCenterX, qSE.boundsCenterY); } else if (playerPos.y > qInit.y + defaultChunkSize / 2 && playerPos.x < qInit.x - defaultChunkSize / 2) { //qNW qInit = new Vector2Int(qNW.boundsCenterX, qNW.boundsCenterY); } else if (playerPos.x < qInit.x - defaultChunkSize / 2 && playerPos.y < qInit.y - defaultChunkSize / 2) { //qSW qInit = new Vector2Int(qSW.boundsCenterX, qSW.boundsCenterY); } else if (playerPos.x > qInit.x + defaultChunkSize / 2) { //qE qInit = new Vector2Int(qE.boundsCenterX, qE.boundsCenterY); } else if (playerPos.x < qInit.x - defaultChunkSize / 2) { //qW qInit = new Vector2Int(qW.boundsCenterX, qW.boundsCenterY); } else if (playerPos.y > qInit.y + defaultChunkSize / 2) { //qN qInit = new Vector2Int(qN.boundsCenterX, qN.boundsCenterY); } else if (playerPos.y < qInit.y - defaultChunkSize / 2) { //qS qInit = new Vector2Int(qS.boundsCenterX, qS.boundsCenterY); } }
// Update is called once per frame private IEnumerator chunkManager() { Vector2Int lastGenerationPosition = new Vector2Int((int)camFollow.playerPos.x, (int)camFollow.playerPos.y); chunkCenter qNE = new chunkCenter(qInit.x + defaultChunkSize, qInit.y + defaultChunkSize); chunkCenter qE = new chunkCenter(qInit.x + defaultChunkSize, qInit.y); chunkCenter qW = new chunkCenter(qInit.x - defaultChunkSize, qInit.y); chunkCenter qSE = new chunkCenter(qInit.x + defaultChunkSize, qInit.y - defaultChunkSize); chunkCenter qS = new chunkCenter(qInit.x, qInit.y - defaultChunkSize); chunkCenter qN = new chunkCenter(qInit.x, qInit.y + defaultChunkSize); chunkCenter qNW = new chunkCenter(qInit.x - defaultChunkSize, qInit.y + defaultChunkSize); chunkCenter qSW = new chunkCenter(qInit.x - defaultChunkSize, qInit.y - defaultChunkSize); chunkManagerHelper(lastGenerationPosition, qNE, qE, qW, qSE, qS, qN, qNW, qSW); while (true) { playerPos = camFollow.playerPos; //List<chunkCenter> chunksToGenerate = new List<chunkCenter>(); chunksToGenerate = new List <chunkCenter>(); qNE = new chunkCenter(qInit.x + defaultChunkSize, qInit.y + defaultChunkSize); qE = new chunkCenter(qInit.x + defaultChunkSize, qInit.y); qW = new chunkCenter(qInit.x - defaultChunkSize, qInit.y); qSE = new chunkCenter(qInit.x + defaultChunkSize, qInit.y - defaultChunkSize); qS = new chunkCenter(qInit.x, qInit.y - defaultChunkSize); qN = new chunkCenter(qInit.x, qInit.y + defaultChunkSize); qNW = new chunkCenter(qInit.x - defaultChunkSize, qInit.y + defaultChunkSize); qSW = new chunkCenter(qInit.x - defaultChunkSize, qInit.y - defaultChunkSize); if (Vector2Int.Distance(qInit, new Vector2Int((int)playerPos.x, (int)playerPos.y)) > defaultChunkSize / 2) { changeCenterChunk(playerPos, qNE, qE, qW, qSE, qS, qN, qNW, qSW); } if (Vector2Int.Distance(lastGenerationPosition, new Vector2Int((int)playerPos.x, (int)playerPos.y)) > defaultChunkSize / 4) { chunkManagerHelper(lastGenerationPosition, qNE, qE, qW, qSE, qS, qN, qNW, qSW); } yield return(new WaitForSeconds(1f)); } }
private void chunkManagerHelper(Vector2Int lastGenerationPosition, chunkCenter qNE, chunkCenter qE, chunkCenter qW, chunkCenter qSE, chunkCenter qS, chunkCenter qN, chunkCenter qNW, chunkCenter qSW) { checkForChunks(chunksToGenerate, playerPos, qNE, qE, qW, qSE, qS, qN, qNW, qSW); GenerateMap(chunksToGenerate, defaultChunkSize); lastGenerationPosition = new Vector2Int((int)playerPos.x, (int)playerPos.y); }
private void checkForChunks(List <chunkCenter> chunksToGenerate, Vector3 playerPos, chunkCenter qNE, chunkCenter qE, chunkCenter qW, chunkCenter qSE, chunkCenter qS, chunkCenter qN, chunkCenter qNW, chunkCenter qSW) { //top right corner if (!loadedChunks.Contains(qNE)) { //qNE chunksToGenerate.Add(qNE); loadedChunks.Add(qNE); } if (!loadedChunks.Contains(qE)) { //qE chunksToGenerate.Add(qE); loadedChunks.Add(qE); } if (!loadedChunks.Contains(qW)) { //qW chunksToGenerate.Add(qW); loadedChunks.Add(qW); } if (!loadedChunks.Contains(qSE)) { //qSE chunksToGenerate.Add(qSE); loadedChunks.Add(qSE); } if (!loadedChunks.Contains(qN)) { //qN chunksToGenerate.Add(qN); loadedChunks.Add(qN); } if (!loadedChunks.Contains(qNW)) { //qNW chunksToGenerate.Add(qNW); loadedChunks.Add(qNW); } if (!loadedChunks.Contains(qS)) { //qS chunksToGenerate.Add(qS); loadedChunks.Add(qS); } if (!loadedChunks.Contains(qSW)) { //qSW chunksToGenerate.Add(qSW); loadedChunks.Add(qSW); } }