public void CreateChunk(int x, int y, int z) { //the coordinates of this chunk in the world MapPosition mapPos = new MapPosition(x, y, z); //Instantiate the chunk at the coordinates using the chunk prefab GameObject newChunkObject = Instantiate( chunkPrefab, new Vector3(mapPos.x, mapPos.y, mapPos.z), Quaternion.Euler(Vector3.zero) ) as GameObject; //Set this map as the parent gameobject newChunkObject.GetComponent<Transform>().parent = this.GetComponent<Transform>(); //Get the object's chunk component Chunk newChunk = newChunkObject.GetComponent<Chunk>(); //Assign its values newChunk.pos = mapPos; newChunk.world = this; //Add it to the chunks dictionary with the position as the key chunks.Add(mapPos, newChunk); var terrainGenerator = new TerrainGenerator(); newChunk = terrainGenerator.ChunkGenerator(newChunk); newChunk.SetBlocksUnmodified(); //Serializer.Load(newChunk); }
public static MapPosition GetBlockPos(Vector3 pos) { MapPosition blockPos = new MapPosition( Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y), Mathf.RoundToInt(pos.z) ); return blockPos; }
public void UpdateOrientation(Chunk chunk, MapPosition pos) { Block westBlock = chunk.world.GetBlock(pos.x - 1, pos.y + 1, pos.z); Block eastBlock = chunk.world.GetBlock(pos.x + 1, pos.y + 1, pos.z); Block northBlock = chunk.world.GetBlock(pos.x, pos.y + 1, pos.z + 1); Block southBlock = chunk.world.GetBlock(pos.x, pos.y + 1, pos.z - 1); this.orientation[1] = northBlock.IsRoad() || northBlock is BlockHighlight; this.orientation[3] = eastBlock.IsRoad() || eastBlock is BlockHighlight; this.orientation[5] = southBlock.IsRoad() || southBlock is BlockHighlight; this.orientation[7] = westBlock.IsRoad() || westBlock is BlockHighlight; }
public Chunk GetChunk(int x, int y, int z) { MapPosition pos = new MapPosition(); float multiple = Chunk.chunkSize; pos.x = Mathf.FloorToInt(x / multiple) * Chunk.chunkSize; pos.y = Mathf.FloorToInt(y / multiple) * Chunk.chunkSize; pos.z = Mathf.FloorToInt(z / multiple) * Chunk.chunkSize; Chunk containerChunk = null; chunks.TryGetValue(pos, out containerChunk); return containerChunk; }
public Save(Chunk chunk) { for (int x = 0; x < Chunk.chunkSize; x++) { for (int y = 0; y < Chunk.chunkSize; y++) { for (int z = 0; z < Chunk.chunkSize; z++) { if (!chunk.blocks[x, y, z].changed) continue; MapPosition pos = new MapPosition(x, y, z); blocks.Add(pos, chunk.blocks[x, y, z]); } } } }
void FindChunksToLoad() { //Get the position of this gameobject to generate around MapPosition playerPos = new MapPosition( Mathf.FloorToInt(transform.position.x / Chunk.chunkSize) * Chunk.chunkSize, Mathf.FloorToInt(transform.position.y / Chunk.chunkSize) * Chunk.chunkSize, Mathf.FloorToInt(transform.position.z / Chunk.chunkSize) * Chunk.chunkSize ); //If there aren't already chunks to generate if (updateList.Count == 0) { //Cycle through the array of positions for (int i = 0; i < chunkPositions.Length; i++) { //translate the player position and array position into chunk position MapPosition newChunkPos = new MapPosition(chunkPositions[i].x * Chunk.chunkSize + playerPos.x, 0, chunkPositions[i].z * Chunk.chunkSize + playerPos.z); //Get the chunk in the defined position Chunk newChunk = world.GetChunk(newChunkPos.x, newChunkPos.y, newChunkPos.z); //If the chunk already exists and it's already //rendered or in queue to be rendered continue if (newChunk != null && (newChunk.rendered || updateList.Contains(newChunkPos))) continue; //-4 - 4 //load a column of chunks in this position for (int y = 0; y < 2; y++) { for (int x = newChunkPos.x - Chunk.chunkSize; x <= newChunkPos.x + Chunk.chunkSize; x += Chunk.chunkSize) { for (int z = newChunkPos.z - Chunk.chunkSize; z <= newChunkPos.z + Chunk.chunkSize; z += Chunk.chunkSize) { buildList.Add(new MapPosition(x, y * Chunk.chunkSize, z)); } } updateList.Add(new MapPosition(newChunkPos.x, y * Chunk.chunkSize, newChunkPos.z)); } return; } } }
void BuildChunk(MapPosition pos) { if (world.GetChunk(pos.x, pos.y, pos.z) == null) world.CreateChunk(pos.x, pos.y, pos.z); }
public static string FileName(MapPosition chunkLocation) { string fileName = chunkLocation.x + "," + chunkLocation.y + "," + chunkLocation.z + ".bin"; return fileName; }
//TODO: ORIENTATION //http://gamedev.stackexchange.com/questions/29524/choose-tile-based-on-adjacent-tiles //http://www.angryfishstudios.com/2011/04/adventures-in-bitmasking/ private static bool _setRoad(Chunk chunk, MapPosition pos) { if (chunk.world.GetBlock(pos.x, pos.y, pos.z).IsSolid(Direction.up) && chunk.world.GetBlock(pos.x, pos.y + 1, pos.z) is BlockHighlight) { chunk.world.SetBlock(pos.x, pos.y + 1, pos.z, new BlockRoad()); return true; } return false; }
void UpdateIfEqual(int value1, int value2, MapPosition pos) { if (value1 == value2) { Chunk chunk = GetChunk(pos.x, pos.y, pos.z); if (chunk != null) chunk.update = true; } }