public void Draw(Camera camera, ChunkManager chunkManager) { const int size = sizeof(float) * 4; var chunks = chunkManager._neighborAssignerPipelineBlock.ChunkList.ToList(); var destinationRects = new RawRectangleF[chunks.Count]; var sourceRects = new RawRectangle[chunks.Count]; var colors = new RawColor4[chunks.Count]; for (var i = 0; i < chunks.Count; i++) { var result = chunks[i].Rect(camera.CurrentChunkIndex); destinationRects[i] = result.Dest; sourceRects[i] = result.Source; colors[i] = result.Color; } _graphics.D2DeviceContext.AntialiasMode = AntialiasMode.Aliased; _graphics.D2DeviceContext.BeginDraw(); _graphics.SpriteBatch.Clear(); _graphics.SpriteBatch.AddSprites(chunks.Count, destinationRects, sourceRects, colors, null, size, size, size, 0); _graphics.D2DeviceContext.DrawSpriteBatch(_graphics.SpriteBatch, 0, chunks.Count, _textures, BitmapInterpolationMode.Linear, SpriteOptions.ClampToSourceRectangle); _graphics.D2DeviceContext.EndDraw(); }
public void Update(GameTime gameTime, ChunkManager chunkManager, World world) { if (!chunkManager.IsInitialized) { return; } _currentKeyboardState = Keyboard.GetCurrentState(); _currentMouseState = Mouse.GetCurrentState(); if (!IsPaused) { CollisionHelper.ApplyPlayerPhysics(this, chunkManager, (float)gameTime.ElapsedGameTime.TotalSeconds); UpdateMove(gameTime, chunkManager, world); UpdateMatrices(); UpdateCurrentCursor(chunkManager, world); } HandleDefaultInputs(chunkManager); _lastKeyboardState = _currentKeyboardState; _lastMouseState = _currentMouseState; }
private void UpdateMove(GameTime gameTime, ChunkManager chunkManager, World world) { const float MouseSensitivity = .01f; const float MovementSensitivity = 2f; const float JumpVelocity = 12; const float MovmentFriction = 0.8f; const float SprintSpeedFactor = 3f; var prevPos = Position; var t = (float)gameTime.ElapsedGameTime.TotalSeconds; Velocity += WorldSettings.Gravity * t; if (IsInWater) { Velocity += WorldSettings.Gravity * -t; } Yaw = MathUtil.Mod2PI(Yaw + _currentMouseState.X * -MouseSensitivity); // MathUtil.Mod2PI( Pitch = MathUtil.Clamp(Pitch + _currentMouseState.Y * -MouseSensitivity, -MathUtil.PiOverTwo, MathUtil.PiOverTwo); var direction = Vector3.UnitX.Rotate(Vector3.UnitY, Yaw); var directionNormal = new Vector3(-direction.Z, 0, direction.X); var translationVector = Vector3.Zero; if (_currentKeyboardState.IsPressed(Key.W)) { translationVector += direction; } if (_currentKeyboardState.IsPressed(Key.S)) { translationVector -= direction; } if (_currentKeyboardState.IsPressed(Key.A)) { translationVector -= directionNormal; } if (_currentKeyboardState.IsPressed(Key.D)) { translationVector += directionNormal; } if (translationVector != Vector3.Zero) { Velocity += Vector3.Normalize(translationVector) * MovementSensitivity * (_currentKeyboardState.IsPressed(Key.LeftShift) ? SprintSpeedFactor : 1); } const float lilStep = 0.001f; if (_currentKeyboardState.IsPressed(Key.NumberPad0)) { Hofman.SunIntensity += lilStep; } if (_currentKeyboardState.IsPressed(Key.NumberPad1)) { Hofman.SunIntensity -= lilStep; } if (_currentKeyboardState.IsPressed(Key.NumberPad2)) { Hofman.Turbitity += lilStep; } if (_currentKeyboardState.IsPressed(Key.NumberPad3)) { Hofman.Turbitity -= lilStep; } if (_currentKeyboardState.IsPressed(Key.NumberPad4)) { Hofman.InscatteringMultiplier += lilStep; } if (_currentKeyboardState.IsPressed(Key.NumberPad5)) { Hofman.InscatteringMultiplier -= lilStep; } if (_currentKeyboardState.IsPressed(Key.NumberPad6)) { Hofman.BetaRayMultiplier += lilStep; } if (_currentKeyboardState.IsPressed(Key.NumberPad7)) { Hofman.BetaRayMultiplier -= lilStep; } if (_currentKeyboardState.IsPressed(Key.NumberPad8)) { Hofman.BetaMieMultiplier += lilStep / 100f; } if (_currentKeyboardState.IsPressed(Key.NumberPad9)) { Hofman.BetaMieMultiplier -= lilStep / 100f; } if (_currentKeyboardState.IsPressed(Key.Up)) { Hofman.SunDirection += lilStep / 1; } if (_currentKeyboardState.IsPressed(Key.Down)) { Hofman.SunDirection -= lilStep / 1; } if (_currentKeyboardState.IsPressed(Key.Up)) { world.IncreaseTime(); } if (_currentKeyboardState.IsPressed(Key.Down)) { world.DecreaseTime(); } if ((!IsInAir || IsInWater) && _currentKeyboardState.IsPressed(Key.Space)) { IsInAir = true; Velocity = new Vector3(Velocity.X, JumpVelocity * (_currentKeyboardState.IsPressed(Key.LeftShift) ? SprintSpeedFactor : 1), Velocity.Z); } Velocity = new Vector3(Velocity.X * MovmentFriction, IsInWater ? Velocity.Y * MovmentFriction : Velocity.Y, Velocity.Z * MovmentFriction); Position = Position + Velocity * t; var currentBlock = new Int3((int)Math.Round(Position.X), (int)Math.Round(Position.Y), (int)Math.Round(Position.Z)); if (ChunkManager.TryGetChunkIndexByAbsoluteVoxelIndex(currentBlock, out var chunkIndex)) { CurrentChunkIndex = chunkIndex; var currentChunk = chunkManager.Chunks[chunkIndex]; CurrentChunkIndexVector = currentChunk.BoundingBox.Center; } }
private void HandleDefaultInputs(ChunkManager chunkManager) { if (_lastMouseState == null) { return; } const int leftClickIndex = 0; const int rightClickIndex = 1; if (!_currentKeyboardState.IsPressed(Key.Escape) && _lastKeyboardState.IsPressed(Key.Escape)) { IsPaused = !IsPaused; } if (!_currentKeyboardState.IsPressed(Key.F1) && _lastKeyboardState.IsPressed(Key.F1)) { Game.RenderSolid = !Game.RenderSolid; } if (!_currentKeyboardState.IsPressed(Key.F2) && _lastKeyboardState.IsPressed(Key.F2)) { Game.RenderWater = !Game.RenderWater; } if (!_currentKeyboardState.IsPressed(Key.F3) && _lastKeyboardState.IsPressed(Key.F3)) { Game.RenderSprites = !Game.RenderSprites; } if (!_currentKeyboardState.IsPressed(Key.F4) && _lastKeyboardState.IsPressed(Key.F4)) { Game.RenderBoxes = !Game.RenderBoxes; } if (!_currentKeyboardState.IsPressed(Key.F5) && _lastKeyboardState.IsPressed(Key.F5)) { Game.ShowChunkBoundingBoxes = !Game.ShowChunkBoundingBoxes; } if (!_currentKeyboardState.IsPressed(Key.F6) && _lastKeyboardState.IsPressed(Key.F6)) { Game.RenderSky = !Game.RenderSky; } if (!_currentKeyboardState.IsPressed(Key.F7) && _lastKeyboardState.IsPressed(Key.F7)) { Game.ShowPipelineVisualization = !Game.ShowPipelineVisualization; } if (!_currentKeyboardState.IsPressed(Key.F12) && _lastKeyboardState.IsPressed(Key.F12)) { Game.Debug = !Game.Debug; } var delta = _currentMouseState.Z / 120; if (delta != 0) { _voxelDefinitionIndexInHand += delta; if (_voxelDefinitionIndexInHand < 0) { _voxelDefinitionIndexInHand = VoxelDefinition.RegisteredDefinitions.Count - 1; } else { _voxelDefinitionIndexInHand %= VoxelDefinition.RegisteredDefinitions.Count; } VoxelInHand = VoxelDefinition.DefinitionByType[VoxelDefinition.RegisteredDefinitions[_voxelDefinitionIndexInHand]]; } if (DateTime.Now - _lastModification > BuildCooldown) { if (_currentMouseState.Buttons[leftClickIndex]) { if (CurrentCursor != null) { _lastModification = DateTime.Now; chunkManager.SetBlock(CurrentCursor.AbsoluteVoxelIndex, 0); } } if (_currentMouseState.Buttons[rightClickIndex]) { if (CurrentCursor != null) { chunkManager.TryGetVoxel(CurrentCursor.AbsoluteVoxelIndex, out var directTargetVoxel); var targetBlock = directTargetVoxel.GetDefinition().IsSprite ? CurrentCursor.AbsoluteVoxelIndex : CurrentCursor.AbsoluteVoxelIndex + CurrentCursor.Direction; var hasPositionConflict = false; var min = (WorldSettings.PlayerMin + Position).Round(); var max = (WorldSettings.PlayerMax + Position).Round(); for (var i = min.X; i <= max.X && !hasPositionConflict; i++) { for (var j = min.Y; j <= max.Y && !hasPositionConflict; j++) { for (var k = min.Z; k <= max.Z && !hasPositionConflict; k++) { hasPositionConflict = targetBlock == new Int3(i, j, k); } } } if (!hasPositionConflict) { _lastModification = DateTime.Now; chunkManager.SetBlock(targetBlock, VoxelInHand.Type); } } } } }