protected override void Update(GameTime gameTime) { var deltaTime = gameTime.GetElapsedSeconds(); var keyboardState = Keyboard.GetState(); var mouseState = Mouse.GetState(); if (keyboardState.IsKeyDown(Keys.Escape)) { Exit(); } if (_player != null && !_player.IsDestroyed) { const float acceleration = 5f; if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up)) { _player.Accelerate(acceleration); } if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down)) { _player.Accelerate(-acceleration); } if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left)) { _player.Rotation -= deltaTime * 3f; } if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right)) { _player.Rotation += deltaTime * 3f; } if (keyboardState.IsKeyDown(Keys.Space) || mouseState.LeftButton == ButtonState.Pressed) { _player.Fire(); } if (_previousMouseState.X != mouseState.X || _previousMouseState.Y != mouseState.Y) { _player.LookAt(_camera.ScreenToWorld(new Vector2(mouseState.X, mouseState.Y))); } _camera.LookAt(_player.Position + _player.Velocity * 0.2f); _camera.Zoom = 1.0f - (_player.Velocity.Length() / 500f); } _entityManager.Update(gameTime); CheckCollisions(); _previousMouseState = mouseState; base.Update(gameTime); }
protected override void Update(GameTime gameTime) { var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; var keyboardState = Keyboard.GetState(); var mouseState = Mouse.GetState(); if (keyboardState.IsKeyDown(Keys.Escape)) { Exit(); } const float acceleration = 5f; if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up)) { _player.Accelerate(acceleration); } if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down)) { _player.Accelerate(-acceleration); } if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left)) { _player.Rotation -= deltaTime * 3f; } if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right)) { _player.Rotation += deltaTime * 3f; } if (keyboardState.IsKeyDown(Keys.Space) || mouseState.LeftButton == ButtonState.Pressed) { _player.Fire(); } if (_previousMouseState.X != mouseState.X || _previousMouseState.Y != mouseState.Y) { _player.LookAt(_camera.ScreenToWorld(new Vector2(mouseState.X, mouseState.Y))); } _entityManager.Update(gameTime); foreach (var laser in _entityManager.Entities.Where(e => e is Laser).Cast <Laser>().ToArray()) { foreach (var meteor in _entityManager.Entities.Where(e => e is Meteor).Cast <Meteor>().ToArray()) { if (meteor.Contains(laser.Position)) { meteor.Damage(1); laser.Destroy(); var animator = Content.Load <SpriteSheetAnimator>("explosion-animations"); _entityManager.AddEntity(new Explosion(animator, meteor.Position)); return; } } } _previousMouseState = mouseState; _camera.LookAt(_player.Position + _player.Velocity * 0.2f); base.Update(gameTime); }