/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } MouseState currentMouseState = Mouse.GetState(); _camera.Update(currentMouseState.X - _previousMouseState.X, currentMouseState.Y - _previousMouseState.Y); if (currentMouseState.LeftButton == ButtonState.Pressed) { Ray pickingRay = Cursor.CalculateCursorRay(_projectionMatrix, _viewMatrix, new Vector2(_screenCenterX, _screenCenterY), GraphicsDevice); Vector3 cubePosition; Vector3 cubeNeighbor; if (_gameWorld.SelectCube(pickingRay, out cubePosition, out cubeNeighbor)) { if (!_lastCubeAdded.HasValue || _lastCubeAdded.Value != cubePosition) { _gameWorld.AddCube(cubeNeighbor, _activeCubeModelIndex); _lastCubeAdded = cubeNeighbor; } } } if (currentMouseState.LeftButton == ButtonState.Released && _previousMouseState.LeftButton == ButtonState.Pressed) { _lastCubeAdded = null; } if (currentMouseState.RightButton == ButtonState.Released && _previousMouseState.RightButton == ButtonState.Pressed && currentMouseState.X > _previousMouseState.X - 2 && currentMouseState.X < _previousMouseState.X + 2 && currentMouseState.Y > _previousMouseState.Y - 2 && currentMouseState.Y < _previousMouseState.Y + 2) { Ray pickingRay = Cursor.CalculateCursorRay(_projectionMatrix, _viewMatrix, new Vector2(currentMouseState.X, currentMouseState.Y), GraphicsDevice); Vector3 cubePosition; Vector3 cubeNeighbor; if (_gameWorld.SelectCube(pickingRay, out cubePosition, out cubeNeighbor)) { _gameWorld.DeleteCube(cubePosition); } } Mouse.SetPosition(_screenCenterX, _screenCenterY); _previousMouseState = Mouse.GetState(); KeyboardState keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Keys.W)) { _camera.MoveForward(gameTime, keyState.IsKeyDown(Keys.LeftShift)); } if (keyState.IsKeyDown(Keys.S)) { _camera.MoveBackward(gameTime, keyState.IsKeyDown(Keys.LeftShift)); } if (keyState.IsKeyDown(Keys.A)) { _camera.StrafeLeft(gameTime, keyState.IsKeyDown(Keys.LeftShift)); } if (keyState.IsKeyDown(Keys.D)) { _camera.StrafeRight(gameTime, keyState.IsKeyDown(Keys.LeftShift)); } if (keyState.IsKeyDown(Keys.Space)) { _camera.FlyUp(gameTime); } if (keyState.IsKeyDown(Keys.D1)) { if (_gameWorld.Cubes.Length > 0) { _activeCubeModelIndex = 0; } } if (keyState.IsKeyDown(Keys.D2)) { if (_gameWorld.Cubes.Length > 1) { _activeCubeModelIndex = 1; } } if (keyState.IsKeyDown(Keys.D3)) { if (_gameWorld.Cubes.Length > 2) { _activeCubeModelIndex = 2; } } if (keyState.IsKeyDown(Keys.D4)) { if (_gameWorld.Cubes.Length > 3) { _activeCubeModelIndex = 3; } } if (keyState.IsKeyDown(Keys.D5)) { if (_gameWorld.Cubes.Length > 4) { _activeCubeModelIndex = 4; } } if (keyState.IsKeyDown(Keys.D6)) { if (_gameWorld.Cubes.Length > 5) { _activeCubeModelIndex = 5; } } if (keyState.IsKeyDown(Keys.D7)) { if (_gameWorld.Cubes.Length > 6) { _activeCubeModelIndex = 6; } } if (keyState.IsKeyDown(Keys.D8)) { if (_gameWorld.Cubes.Length > 7) { _activeCubeModelIndex = 7; } } if (keyState.IsKeyDown(Keys.D9)) { if (_gameWorld.Cubes.Length > 8) { _activeCubeModelIndex = 8; } } if (keyState.IsKeyDown(Keys.D0)) { if (_gameWorld.Cubes.Length > 9) { _activeCubeModelIndex = 9; } } if (keyState.IsKeyDown(Keys.LeftControl) && keyState.IsKeyUp(Keys.S) && _previousKeyboardState.IsKeyDown(Keys.S)) { _gameWorld.Save("test.level"); } if (keyState.IsKeyDown(Keys.LeftControl) && keyState.IsKeyUp(Keys.R) && _previousKeyboardState.IsKeyDown(Keys.R)) { ComputeShadowMesh(_lightWorldPosition); } _previousKeyboardState = keyState; _viewMatrix = Matrix.CreateLookAt(_camera.Position, _camera.FocalPoint, _camera.UpVector); //_renderWithShadowsEffect.Parameters["ModelViewProjection"].SetValue(_viewMatrix * _projectionMatrix); base.Update(gameTime); }