public void OnNewKeyboardInput(object sender, IKeyboardInput args) { if (!_ignoreInput) { if (args.GetInputKey().Contains(Keys.P)) { _ignoreInput = true; // Spawn Player _demo.Spawn <Player>("Player", _demo.LoadTexture("Smiley"), 0, 500); } else if (args.GetInputKey().Contains(Keys.H)) { _ignoreInput = true; // Spawn Hostile _demo.Spawn <Hostile>("Hostile", _demo.LoadTexture("Hostile"), 100, 300); } else if (args.GetInputKey().Contains(Keys.M)) { _ignoreInput = true; // Load background music _demo.PlaySong("Background_Music", 1.0f, true); } } }
public string Trigger() { if (!(_args.GetInputKey().Contains(Keys.Up))) { return("Idle"); } return(null); }
/// <summary> /// Receives keyboard event data to control entity movement /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void OnNewKeyboardInput(object sender, IKeyboardInput args) { // SET '_args' to 'args' _args = args; // SET '_inputFlag' to true _inputFlag = true; // IF '_inputFlag' is set to true if (_inputFlag) { // FOR each key that is currently being pressed foreach (Keys k in _args.GetInputKey()) { // IF the UP key is pressed AND the paddle is not at the top border THEN move the paddle up if (k == Keys.Up && ((IPosition)args.GetEntityComponents()[_guid][typeof(Position)]).YPos > 0) { ((IPosition)args.GetEntityComponents()[_guid][typeof(Position)]).YPos -= ((IMove)args.GetEntityComponents()[_guid][typeof(Move)]).Speed.Y; } // IF the DOWN key is pressed AND the paddle is not at the bottom of the screen THEN move the paddle down if (k == Keys.Down && ((IPosition)args.GetEntityComponents()[_guid][typeof(Position)]).YPos < 900 - 100) { ((IPosition)args.GetEntityComponents()[_guid][typeof(Position)]).YPos += ((IMove)args.GetEntityComponents()[_guid][typeof(Move)]).Speed.Y; } } } }
/// <summary> /// Updates state /// </summary> public void Update(GameTime gameTime) { _gameTime = gameTime; _animator.Animate(_entityUID, "SmileyWalkAtlas", 4, 4, _frameTime); // Calculate elapsed game time for audio _soundTime += (float)gameTime.ElapsedGameTime.TotalSeconds; if (_soundTime > 0.3) { _audioPlayer.PlaySound("Run"); _soundTime = 0f; } //Declare a vector to store the force needed to move Vector2 force = new Vector2(0, 0); // Player input controlling movement, only active on key down foreach (Keys k in _args.GetInputKey()) { // if player presses right arrow or D if (k == Keys.Right || k == Keys.D) { // set facing direction to right(1) _facingDirectionX = 1; force.X = _xSpeed * _facingDirectionX; } // if player presses left arrow or A if (k == Keys.Left || k == Keys.A) { // set facing direction to left(-1) _facingDirectionX = -1; force.X = _xSpeed * _facingDirectionX; } } // update facing direction in entity to update texture orientation _invertTexture(_facingDirectionX); // apply force to the physics component to move entity _physicsComponent.ApplyForce(force); }
// TranslateX override for player specific movement public override float TranslateX() { // Player input controlling movement, only active on key down foreach (Keys k in _args.GetInputKey()) { // if player presses right arrow or D if (k == Keys.Right || k == Keys.D) { // check for collision on the right side of the player if (_rightCollide) { // if colliding move the opposite direction return((_speed * _facingDirectionX * -1) * _velocity.X); } // set facing direction to right(1) _facingDirectionX = 1; // move the player return((_speed * _facingDirectionX) * _velocity.X); } // if player presses left arrow or A if (k == Keys.Left || k == Keys.A) { // check for collision on the left side of the player if (_leftCollide) { // if colliding move the opposite direction return((_speed * _facingDirectionX * -1) * _velocity.X); } // set facing direction to left(-1) _facingDirectionX = -1; // move the player return((_speed * _facingDirectionX) * _velocity.X); } } return(0); }
/// <summary> /// State behaviour logic /// </summary> /// <param name="entity"></param> /// <param name="texture"></param> public string Trigger() { foreach (Keys k in _args.GetInputKey()) { if (k == Keys.Up || k == Keys.Space || k == Keys.W) { return("Jump"); } else if (k == Keys.Left || k == Keys.Right || k == Keys.A || k == Keys.D) { return("Run"); } } return(null); }