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 GameClient(); this._config = new EngineConfig(); if(Engine.Core.Engine.Instance!=null) // if there exists already an engine instance, dispose it first. Engine.Core.Engine.Instance.Dispose(); this._engine = new Engine.Core.Engine(this._game, this._config); this._chunkStorage = new ChunkStorage(_game); this._chunk = new Chunk(new Vector2Int(0, 0)); }
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.LengthInBlocks), new Vector3((this.Chunks.NorthEastEdge.X + 1) * Chunk.WidthInBlocks, Chunk.HeightInBlocks, (this.Chunks.NorthEastEdge.Z + 1) * Chunk.LengthInBlocks)); }
/// <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; }
/// <summary> /// Returns the chunk in given neighborhood. /// </summary> /// <param name="origin">The origin chunk.</param> /// <param name="edge">The neighbor edge.</param> /// <returns></returns> public Chunk GetNeighborChunk(Chunk origin, Chunk.Edges edge) { switch (edge) { case Chunk.Edges.XDecreasing: return this.GetChunkByRelativePosition(origin.RelativePosition.X - 1, origin.RelativePosition.Z); case Chunk.Edges.XIncreasing: return this.GetChunkByRelativePosition(origin.RelativePosition.X + 1, origin.RelativePosition.Z); case Chunk.Edges.ZDecreasing: return this.GetChunkByRelativePosition(origin.RelativePosition.X, origin.RelativePosition.Z - 1); case Chunk.Edges.ZIncreasing: return this.GetChunkByRelativePosition(origin.RelativePosition.X, origin.RelativePosition.Z + 1); } return null; }
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; } }
/// <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; } }
private void RecacheChunks() { this._player.CurrentChunk = this.GetChunkByWorldPosition((int)_player.Position.X, (int)_player.Position.Z); if (this._player.CurrentChunk == null) return; 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.LengthInBlocks), new Vector3((northEastEdge.X + 1)*Chunk.WidthInBlocks, Chunk.HeightInBlocks, (northEastEdge.Z + 1) * Chunk.LengthInBlocks)); }
/// <summary> /// Returns a boolean stating if chunk is current in cache range. /// </summary> /// <param name="chunk">Chunk to check.</param> /// <returns><see cref="bool"/></returns> public bool IsChunkInCacheRange(Chunk chunk) { return CacheRangeBoundingBox.Contains(chunk.BoundingBox) == ContainmentType.Contains; }