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)); }
public void SpawnPlayer(Vector2Int relativePosition) { this.RelativePosition = relativePosition; this.Position = new Vector3(relativePosition.X*Chunk.WidthInBlocks, 150, relativePosition.Z*Chunk.LenghtInBlocks); this._world.SpawnPlayer(relativePosition); }
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> /// Creates a new chunk instance. /// </summary> /// <param name="relativePosition">The relative position of the chunk.</param> public Chunk(Vector2Int relativePosition) { this.ChunkState = ChunkState.AwaitingGenerate; // set initial state to awaiting generation. this.RelativePosition = relativePosition; // set the relative position. // calculate the real world position. this.WorldPosition = new Vector2Int(this.RelativePosition.X*WidthInBlocks, this.RelativePosition.Z*LenghtInBlocks); // calculate bounding-box. this.BoundingBox = new BoundingBox(new Vector3(WorldPosition.X, 0, WorldPosition.Z), new Vector3(this.WorldPosition.X + WidthInBlocks, HeightInBlocks, this.WorldPosition.Z + LenghtInBlocks)); // create vertex & index lists. this.VertexList = new List<BlockVertex>(); this.IndexList = new List<short>(); }