public void Update()
        {
            var cameraPosition = Vector2.Lerp(_characterEntity.Position, Scene.Camera2D.Position, 0.125f);

            if (_impulse == 0f)
            {
                _impulse = _characterBodyComponent.Mass * 10 / Scene.DeltaTime;
            }

            // Moving updates
            if (Scene.KeyboardInputManager.IsKeyDown(Keys.Right))
            {
                _characterBodyComponent.ApplyTorque(-_impulse * 5);

                if (Scene.Camera2D.Position.X < _characterEntity.Position.X)
                {
                    Scene.Camera2D.Position = new Vector2(cameraPosition.X, Scene.Camera2D.Position.Y);
                }
            }

            if (Scene.KeyboardInputManager.IsKeyDown(Keys.Left))
            {
                _characterBodyComponent.ApplyTorque(_impulse * 5);

                if (Scene.Camera2D.Position.X > Scene.ScreenCenter.X)
                {
                    Scene.Camera2D.Position = new Vector2(cameraPosition.X, Scene.Camera2D.Position.Y);
                }
            }

            // Jumping updates
            if (Scene.KeyboardInputManager.IsKeyPressed(Keys.Up) || Scene.KeyboardInputManager.IsKeyPressed(Keys.Space))
            {
                _characterBodyComponent.ApplyLinearImpulseY(_impulse);
            }

            if (_characterEntity.Position.Y < Scene.ScreenCenter.Y)
            {
                Scene.Camera2D.Position = new Vector2(cameraPosition.X, cameraPosition.Y + Scene.ScreenCenter.Y * 0.45f);
            }
        }
Ejemplo n.º 2
0
        public override void Update(GameTime gameTime)
        {
            MouseInputManager.Begin();
            KeyboardInputManager.Begin();

            // Change Gravity
            if (KeyboardInputManager.IsKeyPressed(Keys.VolumeUp))
            {
                Gravity += new Vector2(0f, -5);
            }
            if (KeyboardInputManager.IsKeyPressed(Keys.VolumeDown))
            {
                Gravity -= new Vector2(0f, -5);
            }

            // Change Impulse
            if (KeyboardInputManager.IsKeyPressed(Keys.Up))
            {
                _impulse += 10f;
            }
            if (KeyboardInputManager.IsKeyPressed(Keys.Down))
            {
                _impulse -= 10f;
            }

            if (_impulse == 0f)
            {
                _impulse = _playerBodyComponent.Mass * 10 / DeltaTime;
            }

            #region Camera
            _cameraPosition.X = MouseInputManager.GetPosition().X;
            _cameraPosition.Y = InvertPositionY(MouseInputManager.GetPosition().Y);

            Camera2D.Position = _cameraPosition;

            if (KeyboardInputManager.IsKeyDown(Keys.OemPlus))
            {
                Camera2D.Zoom -= new Vector2(0.01f);
            }

            if (KeyboardInputManager.IsKeyDown(Keys.OemMinus))
            {
                Camera2D.Zoom += new Vector2(0.01f);
            }

            if (KeyboardInputManager.IsKeyPressed(Keys.R))
            {
                Camera2D.Reset();
            }
            #endregion

            #region Player
            if (KeyboardInputManager.IsKeyDown(Keys.Right))
            {
                _playerBodyComponent.SetLinearVelocityX(100f);
                _isMoving = true;
            }

            if (KeyboardInputManager.IsKeyDown(Keys.Left))
            {
                _playerBodyComponent.SetLinearVelocityX(-100f);
                _isMoving = true;
            }

            if (!_isMoving)
            {
                _playerBodyComponent.SetLinearVelocityX(0f);
                _impulse = _playerBodyComponent.Mass * 10 / DeltaTime;
            }

            if (KeyboardInputManager.IsKeyPressed(Keys.Space))
            {
                _playerBodyComponent.ApplyLinearImpulseY(_impulse);
            }
            #endregion

            KeyboardInputManager.End();
            MouseInputManager.End();

            _textComponent.Text = BuildDebugText();
            _isMoving           = false;

            base.Update(gameTime);
        }