public override void Update(GameTime gameTime) { //TODO: Possibly make a proper input manager instead of this KeyboardState kb = Keyboard.GetState(); int pressedKeyCount = kb.GetPressedKeyCount(); if (previousPressedCount != pressedKeyCount) { IEnumerable <Keys> pressedKeys = kb.GetPressedKeys().Except(previousPressedKeys); if (pressedKeys.Contains(Keys.Down)) { currentIndex++; } if (pressedKeys.Contains(Keys.Up)) { currentIndex--; } if (pressedKeys.Contains(Keys.Enter)) { OnItemSelected(currentIndex); } int numItems = menuItems.Length; currentIndex = (currentIndex % numItems + numItems) % numItems; previousPressedKeys = kb.GetPressedKeys(); previousPressedCount = kb.GetPressedKeyCount(); } base.Update(gameTime); }
public void Update() { _prevKeyState = _currentKeyState; _currentKeyState = Keyboard.GetState(); if (_currentKeyState.GetPressedKeyCount() > 0) { CurrentKey = _currentKeyState.GetPressedKeys()[_currentKeyState.GetPressedKeyCount() - 1]; } if (_prevKeyState.IsKeyUp(CurrentKey)) { CurrentKey = Keys.None; IsReceivingInput = false; } }
public bool GetPress(string _key) { for (int i = 0; i < newKeyboard.GetPressedKeyCount(); i++) { if (newKeyboard.GetPressedKeys()[i].ToString() == _key) { return(true); } } return(false); }
public void TestGetPressedKeysGarbageless(Keys[] keys) { var state = new KeyboardState(keys); int count = state.GetPressedKeyCount(); Assert.AreEqual(keys.Length, count); Keys[] newKeysArray = new Keys[count]; state.GetPressedKeys(newKeysArray); CollectionAssert.AreEquivalent(keys, newKeysArray); }
/// <summary> /// Méthode gérant le déplacement du player dans un level /// </summary> /// <param name="keyboardState"></param> /// <returns></returns> private Vector2 DeplacementLevel(KeyboardState keyboardState) { if (keyboardState.GetPressedKeyCount() == 0) { animationPlayer.PlayAnimation(_idle_Animation); return(PositionLevel); } else { Vector2 NextPositionTmp = PositionLevel; System.Diagnostics.Debug.WriteLine("[level] " + this); System.Diagnostics.Debug.WriteLine("[level] //DEBUT UPDATE// : Position - X = " + PositionLevel.X + " | Y= " + PositionLevel.Y); if (keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.Q)) { NextPositionTmp = Vector2.Add(PositionLevel, new Vector2(-(_speed), 0)); animationPlayer.PlayAnimation(_walk_Left_Animation); } if (keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D)) { NextPositionTmp = Vector2.Add(PositionLevel, new Vector2(_speed, 0)); animationPlayer.PlayAnimation(_walk_Right_Animation); } if (keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.Z)) { NextPositionTmp = Vector2.Add(PositionLevel, new Vector2(0, -(_speed))); animationPlayer.PlayAnimation(_walk_Top_Animation); } if (keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.S)) { NextPositionTmp = Vector2.Add(PositionLevel, new Vector2(0, (_speed))); animationPlayer.PlayAnimation(_walk_Bot_Animation); } System.Diagnostics.Debug.WriteLine("[level] //FIN UPDATE NEXT POSITION// : Position - X = " + NextPositionTmp.X + " | Y= " + NextPositionTmp.Y); System.Diagnostics.Debug.WriteLine("[level] //FIN UPDATE POSITION// : Position - X = " + PositionLevel.X + " | Y= " + PositionLevel.Y); return(NextPositionTmp); } }
public void Update(Point mouseLoc, KeyboardState currentKeyboardState, KeyboardState previousKeyboardState, bool leftClick, bool rightClick) { if (Inactive) { return; } if (rect.Contains(mouseLoc)) { if (leftClick) { Selected = true; } if (Selected) { window.TextInput += TextInput; } if (Util.KeyPressed(Keys.Back, currentKeyboardState, previousKeyboardState)) { RemoveChar(); } else if (font.Characters.Contains(inputChar) && currentKeyboardState.GetPressedKeyCount() > 0 && Util.KeyPressed(currentKeyboardState.GetPressedKeys()[0], currentKeyboardState, previousKeyboardState)) { AddChar(inputChar); } } else if (leftClick || rightClick) { Selected = false; } }
public void Update(KeyboardState keyboardState, GameTime gameTime, IEnumerable <ICollidable> collidables) { MouseState mouseState = Mouse.GetState(); Hitbox = new Rectangle((int)Position.X - 16, (int)Position.Y - 16, 32, 32); rectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height); // W // Move up if (keyboardState.IsKeyDown(Keys.W)) { Vector2 newPosiiton = new Vector2(Position.X, Position.Y - playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds); Rectangle newRectangle = new Rectangle((int)newPosiiton.X - 16, (int)newPosiiton.Y - 16, 32, 32); bool movementPossible = true; foreach (var item in collidables) { if (item.Hitbox.Intersects(newRectangle) && item.IsSolid) { movementPossible = false; return; } } if (movementPossible) { Position.Y -= playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; isMoving = true; } } // S // Move down if (keyboardState.IsKeyDown(Keys.S)) { Vector2 newPosiiton = new Vector2(Position.X, Position.Y + playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds); Rectangle newRectangle = new Rectangle((int)newPosiiton.X - 16, (int)newPosiiton.Y - 16, 32, 32); bool movementPossible = true; foreach (var item in collidables) { if (item.Hitbox.Intersects(newRectangle) && item.IsSolid) { movementPossible = false; return; } } if (movementPossible) { Position.Y += playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; isMoving = true; } } // A // Move left if (keyboardState.IsKeyDown(Keys.A)) { Vector2 newPosiiton = new Vector2(Position.X - playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds, Position.Y); Rectangle newRectangle = new Rectangle((int)newPosiiton.X - 16, (int)newPosiiton.Y - 16, 32, 32); bool movementPossible = true; foreach (var item in collidables) { if (item.Hitbox.Intersects(newRectangle) && item.IsSolid) { movementPossible = false; return; } } if (movementPossible) { Position.X -= playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; isMoving = true; } } // D // Move right if (keyboardState.IsKeyDown(Keys.D)) { Vector2 newPosiiton = new Vector2(Position.X + playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds, Position.Y); Rectangle newRectangle = new Rectangle((int)newPosiiton.X - 16, (int)newPosiiton.Y - 16, 32, 32); bool movementPossible = true; foreach (var item in collidables) { if (item.Hitbox.Intersects(newRectangle) && item.IsSolid) { movementPossible = false; return; } } if (movementPossible) { Position.X += playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; isMoving = true; } } // LMB // Shoot if (mouseState.LeftButton == ButtonState.Pressed) { Shoot(Position, mouseState.Position.ToVector2(), GameState.mapObjects, GameState.entities); } // 1 // Change gun to 0 if (keyboardState.IsKeyDown(Keys.D1)) { CurrentGun = 0; } // 2 // Change gun to 1 if (keyboardState.IsKeyDown(Keys.D2)) { CurrentGun = 1; } // 3 // Change gun to 2 if (keyboardState.IsKeyDown(Keys.D3)) { CurrentGun = 2; } // 4 // Change gun to 3 if (keyboardState.IsKeyDown(Keys.D4)) { CurrentGun = 3; } // No keys // Reset movement if (keyboardState.GetPressedKeyCount() == 0) { isMoving = false; } //Hitmarks remove if (HM.Count > 15) { HM.Dequeue(); } currentFrame++; if (currentFrame == totalFrames) { currentFrame = 0; } GunCooldown++; }
public void Load(InputState last, KeyboardState keys, MouseState mouse, float elapsedms, float repeatdelay, float repeatinterval) { last.CopyFrom(this); Keys = keys; Mouse = mouse; MousePos = mouse.Position; PressedKeysCount = Keys.GetPressedKeyCount(); if (PressedKeys.Length < PressedKeysCount) { PressedKeys = new Keys[PressedKeysCount]; } Keys.GetPressedKeys(PressedKeys); FreshKeysCount = 0; Keys currentKey; float time = 0; int repeats = 0; for (int i = 0; i < PressedKeysCount; i++) { currentKey = PressedKeys[i]; // check if this key was pressed in the last state time = 0; for (int o = 0; o < last.PressedKeysCount; o++) { if (last.PressedKeys[o] == currentKey) { time = last.PressedKeysTime[o] + elapsedms; repeats = last.PressedKeysRepeats[o]; break; } } PressedKeysTime[i] = time; if (time == 0) { while (FreshKeys.Length < FreshKeysCount) { Keys[] nfks = new Keys[FreshKeys.Length * 2]; for (int o = 0; o < FreshKeys.Length; o++) { nfks[o] = FreshKeys[o]; } FreshKeys = nfks; } FreshKeys[FreshKeysCount] = currentKey; FreshKeysCount++; PressedKeysRepeats[i] = 0; } else { if (time >= repeatdelay + repeatinterval * repeats) { while (FreshKeys.Length < FreshKeysCount) { Keys[] nfks = new Keys[FreshKeys.Length * 2]; for (int o = 0; o < FreshKeys.Length; o++) { nfks[o] = FreshKeys[o]; } FreshKeys = nfks; } FreshKeys[FreshKeysCount] = currentKey; FreshKeysCount++; repeats++; } PressedKeysRepeats[i] = repeats; } } }
public void Update() { KeyboardState state = Keyboard.GetState(); if (manager.getState() == 0) { direcPressed = Keys.I; //run the command associated with any key pressed. foreach (Keys k in state.GetPressedKeys()) { //only attempt to execute if the key is present in the dictionary if (PlayingKeyMap.ContainsKey(k) && !prevState.IsKeyDown(k)) { PlayingKeyMap[k].Execute(); if (k.Equals(Keys.W) || k.Equals(Keys.A) || k.Equals(Keys.S) || k.Equals(Keys.D) || k.Equals(Keys.Up) || k.Equals(Keys.Left) || k.Equals(Keys.Right) || k.Equals(Keys.Down)) { direcPressed = k; } } } // if ((!prevState.IsKeyDown(Keys.A) && !state.IsKeyUp(Keys.A)) || // (!prevState.IsKeyDown(Keys.W) && !state.IsKeyUp(Keys.W)) || // (!prevState.IsKeyDown(Keys.D) && !state.IsKeyUp(Keys.D)) || // (!prevState.IsKeyDown(Keys.S) && !state.IsKeyUp(Keys.S))) Move.Execute(); // if ((prevState.IsKeyDown(Keys.A) && state.IsKeyUp(Keys.A)) || // (prevState.IsKeyDown(Keys.W) && state.IsKeyUp(Keys.W)) || // (prevState.IsKeyDown(Keys.D) && state.IsKeyUp(Keys.D)) || // (prevState.IsKeyDown(Keys.S) && state.IsKeyUp(Keys.S))) Idle.Execute(); handleMovement(state); } //item selection else if (manager.getState() == 1) { foreach (Keys k in state.GetPressedKeys()) { if (prevState.GetPressedKeyCount() == 0 && ItemSelectionKeyMap.ContainsKey(k)) { ItemSelectionKeyMap[k].Execute(); } } } //pause else if (manager.getState() == 2) { foreach (Keys k in state.GetPressedKeys()) { if (PauseKeyMap.ContainsKey(k)) { PauseKeyMap[k].Execute(); } } } //win and lose else if (manager.getState() == 3 || manager.getState() == 4) { if (prevState.GetPressedKeyCount() == 0) { s2reset.Execute(); } } prevState = state; }
public void Update(GameTime gameTime) { #region Set states priorKeyboardState = currentKeyboardState; priorGamePadState = currentGamePadState; currentKeyboardState = Keyboard.GetState(); currentGamePadState = GamePad.GetState(PlayerIndex.One); #endregion #region Gamepad input Direction = new Vector2(currentGamePadState.ThumbSticks.Left.X, currentGamePadState.ThumbSticks.Left.Y * -1) * (float)gameTime.ElapsedGameTime.TotalSeconds; // Check if player is using gamepad if (Direction != Vector2.Zero) { PreferGamePad = true; } #endregion #region Keyboard input // Check if player is using keyboard if (currentKeyboardState.GetPressedKeyCount() > 0) { PreferGamePad = false; } // Check input on WASD and arrow keys if (currentKeyboardState.IsKeyDown(Keys.Up) || currentKeyboardState.IsKeyDown(Keys.W)) { Direction += new Vector2(0, -1 * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (currentKeyboardState.IsKeyDown(Keys.Left) || currentKeyboardState.IsKeyDown(Keys.A)) { Direction += new Vector2(-1 * (float)gameTime.ElapsedGameTime.TotalSeconds, 0); } if (currentKeyboardState.IsKeyDown(Keys.Down) || currentKeyboardState.IsKeyDown(Keys.S)) { Direction += new Vector2(0, (float)gameTime.ElapsedGameTime.TotalSeconds); } if (currentKeyboardState.IsKeyDown(Keys.Right) || currentKeyboardState.IsKeyDown(Keys.D)) { Direction += new Vector2((float)gameTime.ElapsedGameTime.TotalSeconds, 0); } #endregion #region Exit detection if (currentGamePadState.Buttons.Back == ButtonState.Pressed || currentKeyboardState.IsKeyDown(Keys.Escape)) { Exit = true; } #endregion #region Restart detection if (currentKeyboardState.IsKeyDown(Keys.Enter) || currentGamePadState.IsButtonDown(Buttons.Start)) { Restarting = true; } else { Restarting = false; } #endregion }
public override void Update(GameTime gameTime) { if (!Game.IsActive || (inputCapture?.InputCaptured ?? false)) { currentKeyboardState = default; return; } KeyboardState newState = Keyboard.GetState(); #region keyboard update //if (currentKeyboardState != previousKeyboardState || currentKeyboardState.GetPressedKeyCount() != 0) if (currentKeyboardState != newState || newState.GetPressedKeyCount() != 0) { (currentKeyboardState, previousKeyboardState) = (previousKeyboardState, currentKeyboardState); (currentModifiers, previousModifiers) = (previousModifiers, currentModifiers); currentKeyboardState = newState; currentModifiers = KeyModifiers.None; if (currentKeyboardState.IsKeyDown(Keys.LeftShift) || currentKeyboardState.IsKeyDown(Keys.RightShift)) { currentModifiers |= KeyModifiers.Shift; } if (currentKeyboardState.IsKeyDown(Keys.LeftControl) || currentKeyboardState.IsKeyDown(Keys.RightControl)) { currentModifiers |= KeyModifiers.Control; } if (currentKeyboardState.IsKeyDown(Keys.LeftAlt) || currentKeyboardState.IsKeyDown(Keys.LeftAlt)) { currentModifiers |= KeyModifiers.Alt; } Keys[] currentKeys = currentKeyboardState.GetPressedKeys(); foreach (Keys key in currentKeys) { //if (key == Keys.LeftShift || key == Keys.RightShift || key == Keys.LeftControl || key == Keys.RightControl || key == Keys.LeftAlt || key == Keys.RightAlt) if ((int)key > 159 && (int)key < 166) { continue; } if (previousKeyboardState.IsKeyDown(key) && (currentModifiers == previousModifiers)) { // Key (still) down int lookup = (int)key << KeyDownShift ^ (int)currentModifiers; inputActionHandler?.Invoke(lookup, gameTime, KeyEventType.KeyDown, currentModifiers); } if (previousKeyboardState.IsKeyDown(key) && (currentModifiers != previousModifiers)) { // Key Up, state may have changed due to a modifier changed int lookup = (int)key << KeyUpShift ^ (int)previousModifiers; inputActionHandler?.Invoke(lookup, gameTime, KeyEventType.KeyReleased, previousModifiers); } if (!previousKeyboardState.IsKeyDown(key) || (currentModifiers != previousModifiers)) { //Key just pressed int lookup = (int)key << KeyPressShift ^ (int)currentModifiers; inputActionHandler?.Invoke(lookup, gameTime, KeyEventType.KeyPressed, currentModifiers); } int previousIndex = Array.IndexOf(previousKeys, key);//not great, but considering this is mostly very few (<5) acceptable if (previousIndex > -1) { previousKeys[previousIndex] = Keys.None; } } foreach (Keys key in previousKeys) { if (key == Keys.None) { continue; } //if (key == Keys.LeftShift || key == Keys.RightShift || key == Keys.LeftControl || key == Keys.RightControl || key == Keys.LeftAlt || key == Keys.RightAlt) if ((int)key > 159 && (int)key < 166) { continue; } // Key Up, not in current set of Keys Downs int lookup = (int)key << KeyUpShift ^ (int)previousModifiers; inputActionHandler?.Invoke(lookup, gameTime, KeyEventType.KeyReleased, previousModifiers); } previousKeys = currentKeys; } #endregion base.Update(gameTime); }
public void Update() { //dont do anything if we are on cooldown from entering win or loss state if (!manager.IsWaitingWinLossState()) { KeyboardState state = Keyboard.GetState(); if (manager.getState() == 0) { direcPressed = Keys.I; //run the command associated with any key pressed. foreach (Keys k in state.GetPressedKeys()) { //only attempt to execute if the key is present in the dictionary if (PlayingKeyMap.ContainsKey(k) && !prevState.IsKeyDown(k)) { PlayingKeyMap[k].Execute(); if (k.Equals(Keys.W) || k.Equals(Keys.A) || k.Equals(Keys.S) || k.Equals(Keys.D) || k.Equals(Keys.Up) || k.Equals(Keys.Left) || k.Equals(Keys.Right) || k.Equals(Keys.Down)) { direcPressed = k; } } } handleMovement(state); checkSpecialMoves(state); } //item selection else if (manager.getState() == 1) { foreach (Keys k in state.GetPressedKeys()) { if (prevState.GetPressedKeyCount() == 0 && ItemSelectionKeyMap.ContainsKey(k)) { ItemSelectionKeyMap[k].Execute(); } } //check for cheat codes checkCheatCodes(state); } //pause else if (manager.getState() == 2) { foreach (Keys k in state.GetPressedKeys()) { if (PauseKeyMap.ContainsKey(k)) { PauseKeyMap[k].Execute(); } } } //win and lose else if (manager.getState() == 3 || manager.getState() == 4) { if (prevState.GetPressedKeyCount() == 0 && state.GetPressedKeyCount() > 0) { s2reset.Execute(); } } //Start else if (manager.getState() == 6) { foreach (Keys k in state.GetPressedKeys()) { if (prevState.GetPressedKeyCount() == 0 && StartKeyMap.ContainsKey(k)) { StartKeyMap[k].Execute(); } } } prevState = state; } }