private bool ComputeBlockInfo(Chunk chunk, ChunkPosition chunkPosition, Chunk[] siblingChunks, byte x, byte z, short y, Chunk.ChunkBlockInformation chunkBlockInformation, out BlockInfo?blockInfo) { var blockVisibility = _blocksProvider.GetBlockForId(chunkBlockInformation.Id).IsOpaque ? 0 : 0x3F00; if (blockVisibility == 0) { if ((x > 0 && _blocksProvider.GetBlockForId(_chunkManager.GetBlockId(chunk, (byte)(x - 1), (byte)y, z))?.IsOpaque != true) || (x == 0 && _blocksProvider.GetBlockForId(siblingChunks[0] != null ? _chunkManager.GetBlockId(siblingChunks[0], 15, (byte)y, z) : (byte)0)?.IsOpaque != true)) { blockVisibility |= (int)BlockVisibility.Left; } if ((x < 15 && _blocksProvider.GetBlockForId(_chunkManager.GetBlockId(chunk, (byte)(x + 1), (byte)y, z))?.IsOpaque != true) || (x == 15 && _blocksProvider.GetBlockForId(siblingChunks[2] != null ? _chunkManager.GetBlockId(siblingChunks[2], 0, (byte)y, z) : (byte)0)?.IsOpaque != true)) { blockVisibility |= (int)BlockVisibility.Right; } if ((z > 0 && _blocksProvider.GetBlockForId(_chunkManager.GetBlockId(chunk, x, (byte)y, (byte)(z - 1)))?.IsOpaque != true) || (z == 0 && _blocksProvider.GetBlockForId(siblingChunks[3] != null ? _chunkManager.GetBlockId(siblingChunks[3], x, (byte)y, 15) : (byte)0)?.IsOpaque != true)) { blockVisibility |= (int)BlockVisibility.Front; } if ((z < 15 && _blocksProvider.GetBlockForId(_chunkManager.GetBlockId(chunk, x, (byte)y, (byte)(z + 1)))?.IsOpaque != true) || (z == 15 && _blocksProvider.GetBlockForId(siblingChunks[1] != null ? _chunkManager.GetBlockId(siblingChunks[1], x, (byte)y, 0) : (byte)0)?.IsOpaque != true)) { blockVisibility |= (int)BlockVisibility.Back; } if (y == 0 || _blocksProvider.GetBlockForId(_chunkManager.GetBlockId(chunk, x, (byte)(y - 1), z))?.IsOpaque != true) { blockVisibility |= (int)BlockVisibility.Bottom; } if (y == 255 || _blocksProvider.GetBlockForId(_chunkManager.GetBlockId(chunk, x, (byte)(y + 1), z))?.IsOpaque != true) { blockVisibility |= (int)BlockVisibility.Top; } } if (blockVisibility != 0) { blockInfo = new BlockInfo { BlockIdAndBlockVisibilityAndMetadata = blockVisibility | chunkBlockInformation.Id | (chunkBlockInformation.Metadata << 14), HumidityAndTemperature = chunkBlockInformation.Humidity << 8 | chunkBlockInformation.Temperature, Position = new Vector3((chunkPosition.X << 4) + x, y, (chunkPosition.Z << 4) + z) }; return(true); } blockInfo = null; return(false); }
public void Update(double deltaTime) { var chunks = _worldManager.GetLoadedChunks(_world); foreach (var chunk in chunks) { var chunkPos = chunk.Item1; var chunkPositionInWorldCoordinates = new Vector2(chunkPos.X * 16 + 8, chunkPos.Z * 16 + 8); if ((_player.Position.Xz - chunkPositionInWorldCoordinates).LengthFast > _renderDistance * 16 + 16) { _worldManager.SetChunkToUnload(_world, chunkPos.X, chunkPos.Z); } } _worldManager.UnloadChunks(_world); CleanMeshes(); _player.Update(deltaTime); if (KeyboardHelper.IsKeyPressed(Key.P)) { StaticReferences.ParallelMode = !StaticReferences.ParallelMode; } if (KeyboardHelper.IsKeyPressed(Key.Plus)) { _renderDistance += 1; } if (KeyboardHelper.IsKeyPressed(Key.Minus)) { _renderDistance = _renderDistance > 1 ? _renderDistance - 1 : _renderDistance; } if (KeyboardHelper.IsKeyPressed(Key.Escape)) { OnUnload(); _gameStateManager.SetGameState(null); } if (MouseHelper.IsKeyPressed(MouseButton.Left)) { HitInfo hitInfo; if (_worldManager.CastRay(_world, _player.Position + new Vector3(0, 1.7f, 0f), _player.EyeForward, 20f, out hitInfo)) { _worldManager.SetBlockIdAt(_world, hitInfo.X, hitInfo.Y, hitInfo.Z, 0); } } if (MouseHelper.IsKeyPressed(MouseButton.Right)) { HitInfo hitInfo; if (_worldManager.CastRay(_world, _player.Position + new Vector3(0, 1.7f, 0f), _player.EyeForward, 20f, out hitInfo)) { var x = hitInfo.X; var y = hitInfo.Y; var z = hitInfo.Z; if (hitInfo.Face == HitInfo.FaceEnum.Left) { x--; } else if (hitInfo.Face == HitInfo.FaceEnum.Right) { x++; } else if (hitInfo.Face == HitInfo.FaceEnum.Top) { y++; } else if (hitInfo.Face == HitInfo.FaceEnum.Bottom) { y--; } else if (hitInfo.Face == HitInfo.FaceEnum.Front) { z++; } else if (hitInfo.Face == HitInfo.FaceEnum.Back) { z--; } _worldManager.SetBlockIdAndMetadataAt(_world, x, y, z, (byte)_selectedBlockId, (byte)_selectedBlockMetadata); } } if (KeyboardHelper.IsKeyPressed(Key.Q)) { _selectedBlockId = _blockSelector.GetPreviousBlock(_selectedBlockId); _selectedBlockMetadata = 0; } if (KeyboardHelper.IsKeyPressed(Key.E)) { _selectedBlockId = _blockSelector.GetNextBlock(_selectedBlockId); _selectedBlockMetadata = 0; } if (KeyboardHelper.IsKeyPressed(Key.R)) { _selectedBlockMetadata = _selectedBlockMetadata >= 255 ? 0 : _selectedBlockMetadata + 1; } if (KeyboardHelper.IsKeyPressed(Key.F)) { _selectedBlockMetadata = _selectedBlockMetadata <= 0 ? 255 : _selectedBlockMetadata - 1; } if (KeyboardHelper.IsKeyPressed(Key.BracketRight)) { _destroySphereRadius = _destroySphereRadius < 30 ? _destroySphereRadius + 1 : _destroySphereRadius; } if (KeyboardHelper.IsKeyPressed(Key.BracketLeft)) { _destroySphereRadius = _destroySphereRadius > 1 ? _destroySphereRadius - 1 : _destroySphereRadius; } if (KeyboardHelper.IsKeyPressed(Key.X)) { HitInfo hitInfo; if (_worldManager.CastRay(_world, _player.Position + new Vector3(0, 1.7f, 0f), _player.EyeForward, 800f, out hitInfo)) { var impactPoint = new Vector3(hitInfo.X, hitInfo.Y, hitInfo.Z); for (int x = hitInfo.X - _destroySphereRadius + 1; x <= hitInfo.X + _destroySphereRadius - 1; x++) { for (int y = hitInfo.Y - _destroySphereRadius + 1; y <= hitInfo.Y + _destroySphereRadius - 1; y++) { for (int z = hitInfo.Z - _destroySphereRadius + 1; z <= hitInfo.Z + _destroySphereRadius - 1; z++) { if ((new Vector3(x, y, z) - impactPoint).LengthFast <= _destroySphereRadius) { _worldManager.SetBlockIdAt(_world, x, y, z, 0); } } } } } } if (KeyboardHelper.IsKeyPressed(Key.C)) { HitInfo hitInfo; if (_worldManager.CastRay(_world, _player.Position + new Vector3(0, 1.7f, 0f), _player.EyeForward, 800f, out hitInfo)) { var impactPoint = new Vector3(hitInfo.X, hitInfo.Y, hitInfo.Z); for (int x = hitInfo.X - _destroySphereRadius + 1; x <= hitInfo.X + _destroySphereRadius - 1; x++) { for (int y = hitInfo.Y - _destroySphereRadius + 1; y <= hitInfo.Y + _destroySphereRadius - 1; y++) { for (int z = hitInfo.Z - _destroySphereRadius + 1; z <= hitInfo.Z + _destroySphereRadius - 1; z++) { if ((new Vector3(x, y, z) - impactPoint).LengthFast <= _destroySphereRadius) { _worldManager.SetBlockIdAndMetadataAt(_world, x, y, z, (byte)_selectedBlockId, (byte)_selectedBlockMetadata); } } } } } } if (KeyboardHelper.IsKeyPressed(Key.V)) { HitInfo hitInfo; if (_worldManager.CastRay(_world, _player.Position + new Vector3(0, 1.7f, 0f), _player.EyeForward, 800f, out hitInfo)) { int[,] cupil = new[, ] { { -1, -1, -1, -1, -1, 07, 07, 07, 07, -1, -1, -1, -1 }, { -1, -1, -1, 07, 07, 08, 08, 08, 08, 07, -1, -1, -1 }, { -1, -1, 07, 00, 00, 00, 00, 00, 08, 08, 07, -1, -1 }, { -1, 07, 08, 00, 00, 08, 08, 08, 08, 08, 08, 07, -1 }, { 07, 07, 08, 00, 08, 08, 08, 08, 08, 08, 08, 07, -1 }, { 07, 08, 00, 08, 08, 08, 08, 15, 08, 08, 15, 07, -1 }, { -1, 07, 08, 08, 08, 07, 06, 08, 08, 08, 08, 06, 07 }, { 07, 08, 08, 08, 08, 08, 07, 07, 07, 07, 07, 07, -1 }, { 07, 00, 08, 07, 08, 08, 08, 08, 08, 08, 07, -1, -1 }, { 07, 00, 07, 08, 07, 07, 08, 07, 07, 07, 08, 07, -1 }, { -1, 07, 08, 08, 07, -1, 07, -1, -1, -1, 07, -1, -1 }, { -1, -1, 07, 07, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, }; var impactPoint = new Vector3(hitInfo.X, hitInfo.Y, hitInfo.Z); for (int x = 0; x < 13; x++) { for (int y = 11; y >= 0; y--) { if (cupil[y, x] != -1) { _worldManager.SetBlockIdAndMetadataAt(_world, hitInfo.X - 7 + x, hitInfo.Y + 12 - y, hitInfo.Z, (byte)35, (byte)cupil[y, x]); } } } } } var txt = $"Framerate: {_framerate:0.0}\nDirection : {_player.EyeForward.X:0.00} ; {_player.EyeForward.Y:0.00} ; {_player.EyeForward.Z:0.00}\nPosition: {_player.Position.X:0.00} ; {_player.Position.Y:0.00} ; {_player.Position.Z:0.00}\nParallel Mode: {StaticReferences.ParallelMode}\nRender distance: {_renderDistance} chunks\nDestroy sphere radius: {_destroySphereRadius}\nHand: {_blocksProvider.GetBlockForId((byte)_selectedBlockId).Name}:{_selectedBlockMetadata}"; txt += $"\nVisible chunks: {_visibleChunks}"; txt += $"\nVisible blocks: {_gpuBlocks}"; _text.Str = txt; _text.Position = new Vector2(5, _height - 5); }