public IEnumerator LoadChunk(Vector3 posIn, ChunkInfo info) { Vector3Int chunkStart = new Vector3Int((int)posIn.x, 0, (int)posIn.z); GameObject newChunk = Instantiate(GManager.self.empty, chunkStart, Quaternion.identity, transform); Chunk ch = newChunk.GetComponent <Chunk>(); newChunk.name = "chunk" + posIn.x + " " + posIn.z + ""; int selName = 0; Loader.loading = true; float[,] heights = new float[GManager.MAX_CHUNK_SIZE, GManager.MAX_CHUNK_SIZE]; for (int i = 0; i < info.tiles.Keys.Count; i++, selName++) { info.tiles.TryGetValue(i, out TileInfo tile); float z = (int)(i / 25f); float x = (int)(i % 25f); Vector3 pos = new Vector3(x - 12f, tile.height, z - 12f); GameObject newTile = Instantiate(GManager.self.TilePrefab, pos, Quaternion.identity, newChunk.transform); adjustment adj = newTile.GetComponent <adjustment>(); GameObject arrSel = newTile.transform.GetChild(3).gameObject; arrSel.name = posIn.x + "" + posIn.z + selName; adj.type = tile.type; if (tile.placerName != string.Empty) { adj.SetPlacer(GManager.items[tile.placerName] as IPlaceable); } heights[(int)x, (int)z] = pos.y; if ((int)x == GManager.MAX_CHUNK_SIZE - 1) { yield return(null); } } ch.setAllHeights(heights); chunks.Add(new Vector2Int((int)posIn.x, (int)posIn.z), ch); Loader.loading = false; if (!doneFirstload) { doneFirstload = true; GManager.playerLocation.gameObject.GetComponent <Rigidbody>().isKinematic = false; GManager.GSState = GS.N; } yield return(null); }
public IEnumerator generate(Vector2 start) { start = start * MAX_CHUNK_SIZE; Vector2Int startInt = new Vector2Int((int)start.x, (int)start.y); if (start == Vector2Int.zero) { LoadDefault(); } int selName = 0; Vector3Int chunkStart = new Vector3Int((int)start.x, 0, (int)start.y); GameObject newChunk = Instantiate(empty, chunkStart, Quaternion.identity, GridObject.transform); Chunk ch = newChunk.GetComponent <Chunk>(); ch.setPos(startInt); newChunk.name = "chunk" + start.x + " " + start.y + ""; ChunkManager.chunks.Add(startInt, ch); Vector2 scaleS = ((Vector2)start) / 10f; float halfSize = (MAX_CHUNK_SIZE / 2) / 10f; int at1 = 0; float[,] heights = new float[MAX_CHUNK_SIZE, MAX_CHUNK_SIZE]; for (float z = -halfSize; z <= halfSize; z += .1f, at1++) { z = (float)Math.Round(z, 2); float[] prevTiles = new float[MAX_CHUNK_SIZE + 1]; int at = 0; for (float x = -halfSize; x <= halfSize; x += .1f, at++) { Loader.loading = true; x = (float)Math.Round(x, 2); float tile = 0f, tile2 = 0f; tile = Mathf.PerlinNoise((x + scaleS.x + randX + randZ), (z + scaleS.y + randY + randZ)) * 5f; tile2 = Mathf.PerlinNoise((x + scaleS.x + gRandX + gRandZ), (z + scaleS.y + gRandY + gRandZ)) * 4f; //tile = (float)Math.Round(tile, 3); tile += prevTiles[at]; prevTiles[at] = tile; int t = (int)clampF(tile2, 0, 3); if (tile <= 0f) { tile = 0.01f; } Vector3 pos = new Vector3(x * 10f, tile, z * 10f); if (x == 0f && z == 0f && start == Vector2.zero) { playerLocation.position = Vector3.up * (tile + 1f); } /*if (x %1f == 0f && z == 1f){ * GameObject newPickup = Instantiate (PickupPrefab, pos, Quaternion.identity, newChunk.transform); * newPickup.transform.localPosition = new Vector3 (pos.x, tile + .5f, pos.z); * int type = UnityEngine.Random.Range (0, items.Length); * Item copy = items [type].copy (1); * addPickup (newPickup, copy, Vector3.zero); * pickups.Add (new Pos(newPickup)); * //newPickup.SetActive (false); * }*/ GameObject newTile = Instantiate(TilePrefab, pos, Quaternion.identity, newChunk.transform); adjustment adj = newTile.GetComponent <adjustment> (); adj.type = t; newTile.transform.localPosition = pos; GameObject arrSel = newTile.transform.GetChild(3).gameObject; arrSel.name = start.x + "" + start.y + selName; selName++; heights[at, at1] = tile; float chance = (tile + tile2); if (chance >= 3.595f && chance <= 3.6f) { adj.SetPlacer(items["Path1"] as IPlaceable); int id = (int)map(chance, 3.595f, 3.6f, 0, enemyPrefabs.Count() - 1); GameObject enemySpawn = Instantiate(enemyPrefabs[(EnemyType)id]); enemySpawn.GetComponent <Rigidbody>().isKinematic = false; Vector3 pp = Vector3.up * 2f; enemySpawn.transform.position = pos + (chunkStart) + pp; enemySpawn.SetActive(false); enemiesL.Add(new Pos(enemySpawn)); } } yield return(null); } ch.setAllHeights(heights); yield return(null); Loader.loading = false; if (start == Vector2.zero) { ChunkManager.worldInfo.playerPos = GManager.playerLocation.position; ChunkManager.currAreas.Add(new ChunkArea(0, 0)); ChunkManager.self.SaveWorld(); LoadPlayerInfo(); playerLocation.gameObject.GetComponent <Rigidbody>().isKinematic = false; ChunkManager.doneFirstload = true; GSState = GS.N; } yield return(null); }