Beispiel #1
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime, Vector2 PlayerVector)
        {
            //Sound Pausieren
            if (Game1.gameState == GameState.Paused && _GetHitted.State == SoundState.Playing) _GetHitted.Pause();
            if (Game1.gameState == GameState.Game && _GetHitted.State == SoundState.Paused) _GetHitted.Resume();

            if (Game1.gameState == GameState.Game)
            {
                Random rnd = new Random(gameTime.TotalGameTime.Milliseconds);
                float _elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                _moveTime -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;

                //DrawRect updaten
                _drawRect = new Rectangle((int)_vector.X, (int)_vector.Y, (int)(_animation.Width * _scale), (int)(_animation.Height * _scale));

                //Zombie bewegen
                _isPlayerNear = IsPlayerNear(PlayerVector);
                if (_isPlayerNear)
                {
                    Vector2 distanz = PlayerVector - _vector;
                    distanz.Normalize();

                    _move = distanz * _velocity;
                }
                else if (_moveTime <= 0)
                {
                    _direction = RandomDirection();
                    if (_direction == viewDirection.Down) _move = new Vector2(0, _velocity / 2);
                    else if (_direction == viewDirection.Left) _move = new Vector2(_velocity / 2, 0);
                    else if (_direction == viewDirection.Right) _move = new Vector2(-_velocity / 2, 0);
                    else if (_direction == viewDirection.Up) _move = new Vector2(0, -_velocity / 2);
                    _moveTime = 4000;
                }
                _vector += _move * _elapsed;

                //Zombie direction
                if (_move.X < 0 && (_move.Y < 0.5f && _move.Y > -0.5f)) _direction = viewDirection.Left;
                else if (_move.X > 0 && (_move.Y < 0.5f && _move.Y > -0.5f)) _direction = viewDirection.Right;
                else if (_move.Y > 0 && (_move.X < 0.5f && _move.X > -0.5f)) _direction = viewDirection.Down;
                else if (_move.Y < 0 && (_move.X < 0.5f && _move.X > -0.5f)) _direction = viewDirection.Up;

                //Zombie verhindern, dass er vom Feld geht
                if (_vector.X < 0)
                {
                    _vector.X = 0;
                    _move *= -1;
                }
                else if (_vector.X > 1000 - _drawRect.Width)
                {
                    _vector.X = 1000 - _drawRect.Width;
                    _move *= -1;
                }
                if (_vector.Y < 0)
                {
                    _vector.Y = 0;
                    _move *= -1;
                }
                else if (_vector.Y > 1000 - _drawRect.Height)
                {
                    _vector.Y = 1000 - _drawRect.Height;
                    _move *= -1;
                }

                //Zombie animieren
                if (_move != Vector2.Zero)
                {
                    _frameTime -= _elapsed;
                    if (_frameTime <= 0)
                    {
                        _frame++;
                        _frameTime = 0.2f;
                    }
                    if (_frame > 3) _frame = 0;
                }
                _animation = new Rectangle((_Enemytexture.Width / 4) * _frame, (_Enemytexture.Height / 4) * (int)_direction, _Enemytexture.Width / 4, _Enemytexture.Height / 4);

                //HealthBar aktualisieren
                _healthBox = new Rectangle((int)_vector.X, (int)_vector.Y - 10, (int)((_health / _maxhealth) * _drawRect.Width), _healthTexture.Height / 2);
                _healthSource = new Rectangle(0, _healthTexture.Height / 2, (int)((_health / _maxhealth) * _drawRect.Width), _healthTexture.Height / 2);
            }

            base.Update(gameTime);
        }
Beispiel #2
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if(Game1.gameState == GameState.Game)
            {
            _newKbState = Keyboard.GetState();
            float _elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //Player Bewegung
            if (_newKbState.IsKeyDown(Keys.S)) _move.Y = _velocity * _elapsed;
            else if (_newKbState.IsKeyDown(Keys.W)) _move.Y = -_velocity * _elapsed;
            else _move.Y = 0;
            
            if (_newKbState.IsKeyDown(Keys.D)) _move.X = _velocity * _elapsed;
            else if (_newKbState.IsKeyDown(Keys.A)) _move.X = -_velocity * _elapsed;
            else _move.X = 0;

            _vector += _move;

            _drawRect = new Rectangle((int)_vector.X, (int)_vector.Y, (int)(32 * _scale), (int)(48 * _scale));

            //PlayerDirection
            float rotation = MathHelper.ToDegrees((float)Math.Atan2(Mouse.GetState().Y - _drawRect.Center.Y - Camera._position.Y, Mouse.GetState().X - _drawRect.Center.X - Camera._position.X));
            
            if (rotation > -135 && rotation <= -45) _direction = viewDirection.Up;
            else if (rotation > -45 && rotation <= 45) _direction = viewDirection.Right;
            else if (rotation > 45 && rotation <= 135) _direction = viewDirection.Down;
            else if ((rotation > 135||rotation <= 180) && (rotation <= -135 || rotation >= -180)) _direction = viewDirection.Left;

            //Player verhindern vom Feld zu gehen
            if (_vector.X < 0) _vector.X = 0;
            else if (_vector.X > 1000 - _drawRect.Width) _vector.X = 1000 - _drawRect.Width;
            if (_vector.Y < 0) _vector.Y = 0;
            else if (_vector.Y > 1000 - _drawRect.Height) _vector.Y = 1000 - _drawRect.Height;

            //player animieren
            if (_move != Vector2.Zero)
            {
                _frameTime -= _elapsed;
                if (_frameTime <= 0)
                {
                    _frameTime = 0.1f;
                    _frame++;
                }

                if (_frame > 3) _frame = 0;
            }
            else _frame = 0;

            _animation = new Rectangle(32 * _frame, 48 * (int)_direction, 32, 48);

            //Waffe Updaten
            _weapon.Update(gameTime, new Vector2(_drawRect.Center.X, _drawRect.Center.Y));

            _oldKbState = _newKbState;
        }

            base.Update(gameTime);
        }