public void updateChunksToLoad() { //create a blank chunk list List <Chunk> newChunks = new List <Chunk>(); for (int i = ChunkHelpers.getRealtiveChunkCoord(player.transform.position) - (LookUpData.renderDisctance * LookUpData.chunkWidth); i <= ChunkHelpers.getRealtiveChunkCoord(player.transform.position) + (LookUpData.renderDisctance * LookUpData.chunkWidth); i += LookUpData.chunkWidth) { // get the chunk at this position Chunk newLoadedChunk = generateChunk(i); // set it to active if it is not newLoadedChunk.setLoadStatus(true); // add this chunk to the newly loaded chunks newChunks.Add(newLoadedChunk); } // go through all the loaded chunks in the last frame for (int i = 0; i < currentLoadedChunks.Count; i++) { // if the chunk is not in the new list of chunks to // load then set active to false (unloading) if (!newChunks.Contains(currentLoadedChunks[i])) { currentLoadedChunks[i].setLoadStatus(false); } } // set the current loaded chunks to the new list of chunks currentLoadedChunks = newChunks; }
public Chunk getChunk(Vector3 worldPosition) { Chunk found; // get what chunk is at this x position, if there is none then it is null chunks.TryGetValue(ChunkHelpers.getRealtiveChunkCoord(worldPosition), out found); return(found); }
public void Update() { // get the chunkX of the chunk that the player is in int playerChunkX = ChunkHelpers.getRealtiveChunkCoord(world.player.transform.position); // get the chunk variable of the chunkX Chunk playerChunk; world.chunks.TryGetValue(playerChunkX, out playerChunk); // set all the fields in the debug screen to the correct values playerX.SetText("" + Mathf.RoundToInt(world.player.transform.position.x)); playerY.SetText("" + Mathf.RoundToInt(world.player.transform.position.y)); chunk.SetText("" + playerChunk.chunkX); totalChunks.SetText("" + world.chunks.Count); biome.SetText("" + playerChunk.biomeRefrence.biome.biomeName); seed.SetText("" + world.seed); FPS.SetText("" + Mathf.RoundToInt(1 / Time.deltaTime)); }
// add a tile to a position in this chunk public bool addTile(Vector3Int worldPos, ItemGroup itemGroup) { // get the locaion of this tile but to the local chunk Coord Vector3Int localPos = ChunkHelpers.convertWorldCoordToLocalCoord(worldPos, chunkX); // if the tile is outside the chunk then do not add it, or it is air if (!isThisTileInThisChunk(localPos) || itemGroup.id == 0) { return(false); } // if there is no tile in this area then add the new one else return false if (tilemap.GetTile(worldPos) == null) { // set the correct value in the tile id array tileIDs[localPos.x, localPos.y] = itemGroup.id; // change the tile map to fit that change tilemap.SetTile(worldPos, itemGroup.tile); // check if this tile needs a respective tile entity GameObject blockEntity = itemGroup.blockEntity; if (blockEntity != null) { // create a copy of the block entity needed and add that to the // block entity list GameObject createdBlockEntity = Instantiate(blockEntity, new Vector3(worldPos.x + 0.5f, worldPos.y + 0.5f, worldPos.z), new Quaternion(), transform); blockEntities.Add(createdBlockEntity); } return(true); } else { return(false); } }
// removes a tile from the location asked and returns the // id of that tile public int removeTile(Vector3Int worldPos) { Vector3Int localPos = ChunkHelpers.convertWorldCoordToLocalCoord(worldPos, chunkX); // if this tile is in the chunk bounds then return the tileID // at that location, else return the flag - 1 if (isThisTileInThisChunk(localPos) && tilemap.GetTile(worldPos) != null) { // get the id of the tile that is there int idOfThatTile = tileIDs[localPos.x, localPos.y]; // if the tile is indestructable then return minus and do not remove the tile if (refrenceManager.itemGroups[idOfThatTile].indestructable) { return(-1); } // remove that tile from the til map tilemap.SetTile(worldPos, null); // set the id of the tile in out tileID array to 0 tileIDs[localPos.x, localPos.y] = 0; // if the item has an associated blovk entity if (refrenceManager.itemGroups[idOfThatTile].blockEntity != null) { // go through each block entity and see if is in the same position as the tile we are removing foreach (GameObject blockEntity in blockEntities) { if (Mathf.FloorToInt(blockEntity.transform.position.x) == worldPos.x && Mathf.FloorToInt(blockEntity.transform.position.y) == worldPos.y) { GameObject.Destroy(blockEntity); } } } int itemDropId = refrenceManager.itemGroups[idOfThatTile].dropItemId; // if the tile can drop an item if (itemDropId != 0) { // create an ItemEntity GameObject itemObject = Instantiate(itemEntityPrefab, new Vector3(worldPos.x + 0.5f, worldPos.y + 0.5f, worldPos.z), new Quaternion(0f, 0f, 0f, 0f), transform); ItemEntity itemEntity = itemObject.GetComponent <ItemEntity>(); itemEntity.itemGroup = refrenceManager.itemGroups[itemDropId]; // initzialize the entity itemEntity.init(); // add the item object to the chunk entities itemEntities.Add(itemObject); } // return the id of the tile we broke return(idOfThatTile); } else { return(-1); } }