/// <summary> /// Tries to convert keyboard input to characters and prevents repeatedly returning the /// same character if a key was pressed last frame, but not yet unpressed this frame. /// /// Code courtesy of: http://roy-t.nl/index.php/2010/02/11/code-snippet-converting-keyboard-input-to-text-in-xna/ /// </summary> /// <param name="keyboard">The current KeyboardState</param> /// <param name="oldKeyboard">The KeyboardState of the previous frame</param> /// <param name="key">When this method returns, contains the correct character if conversion succeeded. /// Else contains the null, (000), character.</param> /// <returns>True if conversion was successful</returns> public static bool TryLinkToKeyboardInput(ref string text, KeyboardState keyboard, KeyboardState oldKeyboard) { Keys[] keysPressed = keyboard.GetPressedKeys(); List <char> charactersList = new List <char>(); if (keysPressed.Length > 0) { for (int i = 0; i < keysPressed.Length; i++) { if (!oldKeyboard.IsKeyDown(keysPressed[i]) || _keyPressedTimer.TimeElapsedInMilliSeconds > 500) { char tempChar; FindPressedKey(keysPressed[i], out tempChar); if (tempChar != 0) { charactersList.Add(tempChar); } // If backspace is pressed, delete last character. if (InputSystem.IsKeyDown(Keys.Back)) { if (text.Length != 0) { text = text.Remove(text.Length - 1, 1); } } } } // Check for long key presses. if (keysPressed.Length == 1) { } else { _keyPressedTimer.Reset(); } } else { // No input pressed. _keyPressedTimer.Reset(); return(false); } // Add pressed characters to string. for (int i = 0; i < charactersList.Count; i++) { text += charactersList[i]; } return(true); }
public override void Update(Tile t) { t.AnimationStopped = !_isOpen; foreach (Player player in GameWorld.GetPlayers()) { if (player.GetCollRectangle().Intersects(_collRectangle) && !_isOpen) { // If player presses open button, open chest. if (InputSystem.IsKeyDown(Keys.W)) { OnPlayerAction(t, player); } } } }
/// <summary> /// Updates the camera by following the rectangle given in a smooth manner. /// </summary> /// <param name="rectangle"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="active">Should the camera follow the rectangle.</param> public void UpdateSmoothly(Rectangle rectangle, int width, int height, bool active) { Vector3 currentLeftCorner; // If the camera is not active, which is used to show the player is dead, the camera stops moving. if (active) { Vector2 playerPos = new Vector2(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2); currentLeftCorner = new Vector3(-playerPos.X + _defRes.X / _zoom / 2, -playerPos.Y + (3 * _defRes.Y / _zoom / 5), 0); } else { currentLeftCorner = LastCameraLeftCorner; } if (LockedX) { currentLeftCorner.X = LastCameraLeftCorner.X; } if (LockedY) { currentLeftCorner.Y = LastCameraLeftCorner.Y; } if (RestricedToGameWorld) { if (currentLeftCorner.X > 0) { currentLeftCorner.X = 0; } if (currentLeftCorner.X < -(width * TMBAW_Game.Tilesize - _defRes.X)) { currentLeftCorner.X = -(width * TMBAW_Game.Tilesize - _defRes.X); } if (currentLeftCorner.Y > 0) { currentLeftCorner.Y = 0; } if (currentLeftCorner.Y < -(height * TMBAW_Game.Tilesize - _defRes.Y)) { currentLeftCorner.Y = -(height * TMBAW_Game.Tilesize - _defRes.Y); } } if (InputSystem.IsKeyDown(Keys.OemMinus)) { ButtonBar.MinusButton_MouseClicked(null); } if (InputSystem.IsKeyDown(Keys.OemPlus)) { ButtonBar.PlusButton_MouseClicked(null); } if (InputSystem.IsKeyDown(Keys.R)) { ResetZoom(); } Velocity = (currentLeftCorner - LastCameraLeftCorner) / 5; Vector3 cameraLeftCorner = LastCameraLeftCorner; // Used if the camera zoom changed. if (_updateInstantly) { cameraLeftCorner = currentLeftCorner; _updateInstantly = false; } else { cameraLeftCorner += Velocity; } CenterGameCoords = new Vector2(-currentLeftCorner.X, -currentLeftCorner.Y); LeftTopGameCoords = CenterGameCoords; CenterGameCoords.X += TMBAW_Game.DefaultResWidth / 2; CenterGameCoords.Y += TMBAW_Game.DefaultResHeight * 2 / 3; TileIndex = (int)((int)CenterGameCoords.Y / TMBAW_Game.Tilesize * GameWorld.WorldData.LevelWidth) + (int)((int)CenterGameCoords.X / TMBAW_Game.Tilesize); LastCameraLeftCorner = cameraLeftCorner; _lastVelocity = Velocity; int shakeOffset = 1; if (_shakeTimer.TimeElapsedInMilliSeconds < 100) { switch (TMBAW_Game.Random.Next(0, 5)) { case 0: cameraLeftCorner.X += shakeOffset; break; case 1: cameraLeftCorner.X -= shakeOffset; break; case 2: cameraLeftCorner.Y += shakeOffset; break; case 3: cameraLeftCorner.Y -= shakeOffset; break; } } //cameraLeftCorner = new Vector3(DrawRectangle.X, DrawRectangle.Y, 0); cameraLeftCorner = new Vector3((int)cameraLeftCorner.X, (int)cameraLeftCorner.Y, 0); _translation = Matrix.CreateTranslation(cameraLeftCorner) * Matrix.CreateScale(new Vector3(_zoom, _zoom, 0)); HalfTranslate = Matrix.CreateTranslation(cameraLeftCorner * 2); }