private void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f)) { Vector3 p = hit.point - hit.normal / 2.0f; Vector3 blockPos = worldGenerator.WorldToBlockPosition(p); Chunk chunk = worldGenerator.GetChunk(p); chunk.SetBlock(blockPos, BlockType.Air); } } if (Input.GetMouseButtonDown(2)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f)) { Vector3 p = hit.point + hit.normal / 2.0f; Vector3 chunkPos = worldGenerator.WorldToChunkPosition(p); Vector3 blockPos = worldGenerator.WorldToBlockPosition(p); worldGenerator.AddChunk(chunkPos, true); Chunk chunk = worldGenerator.GetChunk(p); chunk.SetBlock(blockPos, BlockType.Dirt); } } }
//Create the landscape for the chunk private void CreateLandscapeFromGenerator(VisualChunk visualChunk) { var generatedChunk = _worldGenerator.GetChunk(visualChunk.Position); //Assign The Block generated to the Chunk visualChunk.BlockData.SetBlockBytes(generatedChunk.BlockData.GetBlocksBytes(), generatedChunk.BlockData.GetTags()); visualChunk.BlockData.ColumnsInfo = generatedChunk.BlockData.ColumnsInfo; visualChunk.BlockData.ChunkMetaData = generatedChunk.BlockData.ChunkMetaData; //Copy the entities visualChunk.Entities.Import(generatedChunk.Entities, true); }
public ServerChunk GenerateChunk(Vector3I chunkPos) { var generatedChunk = _generator.GetChunk(chunkPos); if (generatedChunk != null) { return(new ServerChunk(generatedChunk) { Position = chunkPos, LastAccess = DateTime.Now }); } return(null); }
//Creates a chunk at specified index, note that the chunk's position will be chunkIndex * chunkSize private void CreateChunk(IntVector2 chunkIndex, bool buildMesh) { if (loadedChunks.TryGetValue(chunkIndex, out Tuple <Chunk, bool, bool> tuple)) //Chunk already created { if (buildMesh && !tuple.Item2) //But maybe we need to build a mesh for it? { loadedChunks[chunkIndex] = new Tuple <Chunk, bool, bool>(tuple.Item1, true, tuple.Item3); chunksToUpdate.AddLast(chunkIndex); } if (!tuple.Item3) // If node not created, but it is in memory { tuple.Item1.Visible = true; } } else { byte[,,] blocks = worldGenerator.GetChunk(chunkIndex, Chunk.SIZE); Chunk chunk = new Chunk(this, chunkIndex, blocks); this.AddChild(chunk); loadedChunks[chunkIndex] = new Tuple <Chunk, bool, bool>(chunk, buildMesh, true); if (buildMesh) { chunksToUpdate.AddLast(chunkIndex); } chunkNo++; //We take data from ANIMAL_CHUNK_RANGE closest chunks to player, throw it in a normal distribution, and generate animals from the dist. if (chunkNo == (int)ANIMAL_CHUNK_RANGE) { chunkNo = 0; // Spawn animals Vector3 playerPos = player.GetTranslation(); IntVector2 playerChunk = new IntVector2((int)(playerPos.x / (Chunk.SIZE.x * Block.SIZE)), (int)(playerPos.z / (Chunk.SIZE.z * Block.SIZE))); Dictionary <string, int> animalCount = CountAnimalsInChunk(playerChunk); Random rand = new Random(); foreach (KeyValuePair <string, int> pair in animalCount) { //number to spawn double u1 = 1.0 - rand.NextDouble(); //uniform(0,1] random doubles double u2 = 1.0 - rand.NextDouble(); double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); //random normal(0,1) double randNormal = (pair.Value + (pair.Value / 4.0f) * randStdNormal); int number = (int)(Math.Max(0, Math.Round(randNormal / ANIMAL_CHUNK_RANGE))); for (int i = 0; i < number; i++) { double sexNum = rand.NextDouble(); AnimalBehaviourComponent.AnimalSex sex = (sexNum > 0.5 ? AnimalBehaviourComponent.AnimalSex.Male : AnimalBehaviourComponent.AnimalSex.Female); Vector3 chunkOrigin = (new Vector3(chunkIndex.x * Chunk.SIZE.x, Chunk.SIZE.y / 2.0f, chunkIndex.y * Chunk.SIZE.z) * Block.SIZE); Vector3 chunkSize = Chunk.SIZE * Block.SIZE; Vector3 randomPosition = new Vector3(rand.Next(0, (int)chunkSize.x), 100.0f, rand.Next(0, (int)chunkSize.z)); // ugly, TODO: fix GetNode(Game.GAME_PATH).GetNode("AnimalSpawner").Call("SpawnAnimal", pair.Key, sex, chunkOrigin + randomPosition); } } } } }