Exemple #1
0
        /// <summary>
        /// State behaviour logic
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="texture"></param>
        private void Behavior()
        {
            _animator.Animate(_entityUID, "SmileyWalkAtlas", 4, 4, _frameTime);
            if (_soundTime > 0.3)
            {
                _audioPlayer.PlaySound("Jump");
                _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 up arrow, W, or space
                if (k == Keys.Up || k == Keys.Space || k == Keys.W)
                {
                    // set jump value and inAir status to true
                    force.Y = -_ySpeed;
                }
            }
            // apply force to the physics component to move entity
            _physicsComponent.ApplyForce(force);
        }
Exemple #2
0
        /// <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);
        }