public void TestChunkStorage() { _chunkStorage[0, 0] = _chunk; Assert.IsTrue(_chunkStorage.ContainsKey(0,0)); // check if we can index back the chunk. Assert.AreEqual(_chunk, _chunkStorage[0, 0]); // check if we can access it back. _chunkStorage.Remove(0, 0); Assert.IsFalse(_chunkStorage.ContainsKey(0, 0)); // check if it's correctly removed. _chunkStorage[-1, -1] = new Chunk(new Vector2Int(-1, -1)); Assert.IsTrue(_chunkStorage.ContainsKey(-1, -1)); // check if we can index negative coordinates for chunks. }
public void Init() { _game = new SampleGame(); this._config = new EngineConfig(); if(Engine.Instance!=null) // if there exists already an engine instance, dispose it first. Engine.Instance.Dispose(); this._engine = new Engine(this._game, this._config); this._chunkStorage = new ChunkStorage(_game); this._chunk = new Chunk(new Vector2Int(0, 0)); }
private void RecacheChunks() { this._player.CurrentChunk = this.GetChunk((int) _player.Position.X, (int) _player.Position.Z); for (int z = -CacheRange; z <= CacheRange; z++) { for (int x = -CacheRange; x <= CacheRange; x++) { if (this._chunkStorage.ContainsKey(this._player.CurrentChunk.RelativePosition.X + x, this._player.CurrentChunk.RelativePosition.Z + z)) continue; var chunk = new Chunk(new Vector2Int(this._player.CurrentChunk.RelativePosition.X + x, this._player.CurrentChunk.RelativePosition.Z + z)); this._chunkStorage[chunk.RelativePosition.X, chunk.RelativePosition.Z] = chunk; } } var southWestEdge = new Vector2Int(this._player.CurrentChunk.RelativePosition.X - ViewRange, this._player.CurrentChunk.RelativePosition.Z - ViewRange); var northEastEdge = new Vector2Int(this._player.CurrentChunk.RelativePosition.X + ViewRange, this._player.CurrentChunk.RelativePosition.Z + ViewRange); BoundingBox = new BoundingBox( new Vector3(southWestEdge.X*Chunk.WidthInBlocks, 0, southWestEdge.Z*Chunk.LenghtInBlocks), new Vector3((northEastEdge.X + 1)*Chunk.WidthInBlocks, Chunk.HeightInBlocks, (northEastEdge.Z + 1)*Chunk.LenghtInBlocks)); }
private void ProcessChunkInViewRange(Chunk chunk) { if (chunk.ChunkState == ChunkState.Ready || chunk.ChunkState == ChunkState.AwaitingRemoval) return; switch (chunk.ChunkState) // switch on the chunk state. { case ChunkState.AwaitingGenerate: Generator.Generate(chunk); break; case ChunkState.AwaitingLighting: case ChunkState.AwaitingRelighting: Lightning.Process(chunk); break; case ChunkState.AwaitingBuild: case ChunkState.AwaitingRebuild: this.VertexBuilder.Build(chunk); break; default: break; } }
private void ProcessChunkInCacheRange(Chunk chunk) { if (chunk.ChunkState == ChunkState.Ready || chunk.ChunkState != ChunkState.AwaitingGenerate) return; switch (chunk.ChunkState) // switch on the chunk state. { case ChunkState.AwaitingGenerate: Generator.Generate(chunk); break; case ChunkState.AwaitingLighting: Lightning.Process(chunk); break; default: break; } }
public bool IsChunkInViewRange(Chunk chunk) { return ViewRangeBoundingBox.Contains(chunk.BoundingBox) == ContainmentType.Contains; }
/// <summary> /// Returns block index by relative position of block in chunk. /// </summary> /// <param name="chunk">The chunk block belongs to.</param> /// <param name="x">Block's relative x position in chunk.</param> /// <param name="y">Block's y position in chunk.</param> /// <param name="z">Block's relative x position in chunk.</param> /// <returns></returns> public static int BlockIndexByRelativePosition(Chunk chunk, byte x, byte y, byte z) { var xIndex = chunk.WorldPosition.X + x; var zIndex = chunk.WorldPosition.Z + z; var wrapX = xIndex%CacheWidthInBlocks; if (wrapX < 0) wrapX += CacheWidthInBlocks; var wrapZ = zIndex%CacheLenghtInBlocks; if (wrapZ < 0) wrapZ += CacheLenghtInBlocks; var flattenIndex = wrapX * XStep + wrapZ * ZStep + y; return flattenIndex; }
public void SpawnPlayer(Vector2Int relativePosition) { //Profiler.Start("terrain-generation"); for (int z = -ChunkCache.CacheRange; z <= ChunkCache.CacheRange; z++) { for (int x = -ChunkCache.CacheRange; x <= ChunkCache.CacheRange; x++) { var chunk = new Chunk(new Vector2Int(relativePosition.X + x, relativePosition.Z + z)); this.Chunks[chunk.RelativePosition.X, chunk.RelativePosition.Z] = chunk; if (chunk.RelativePosition == relativePosition) this._player.CurrentChunk = chunk; } } this.Chunks.SouthWestEdge = new Vector2Int(relativePosition.X - ChunkCache.ViewRange, relativePosition.Z - ChunkCache.ViewRange); this.Chunks.NorthEastEdge = new Vector2Int(relativePosition.X + ChunkCache.ViewRange, relativePosition.Z + ChunkCache.ViewRange); ChunkCache.BoundingBox = new BoundingBox( new Vector3(this.Chunks.SouthWestEdge.X*Chunk.WidthInBlocks, 0, this.Chunks.SouthWestEdge.Z*Chunk.LenghtInBlocks), new Vector3((this.Chunks.NorthEastEdge.X + 1)*Chunk.WidthInBlocks, Chunk.HeightInBlocks, (this.Chunks.NorthEastEdge.Z + 1)*Chunk.LenghtInBlocks)); }
/// <summary> /// Processes chunks in cache range and generates or lightens them. /// </summary> /// <param name="chunk"><see cref="Chunk"/></param> /// <remarks>Note that chunks in cache range only gets generated or lightened. They are built once they get in view-range.</remarks> private void ProcessChunkInCacheRange(Chunk chunk) { if (chunk.ChunkState != ChunkState.AwaitingGenerate && chunk.ChunkState != ChunkState.AwaitingLighting) return; // only generate or lighten the chunks. // note: we don't care about chunks that await re-lighting because re-lightig only occurs a chunk gets modified. switch (chunk.ChunkState) { case ChunkState.AwaitingGenerate: Generator.Generate(chunk); break; case ChunkState.AwaitingLighting: Lightning.Process(chunk); break; default: break; } }