Example #1
0
        protected override void Move(GameTime gameTime, Map map, int[,] hazardMap)
        {
            base.Move(gameTime, map, hazardMap);

            #region Bomb

            if ((HasBadEffect && BadEffect == BadEffect.BombDrop) ||
                ((Config.PlayersUsingController[Id] && InputHandler.ButtonDown(Buttons[4], PlayerIndex.One)) || InputHandler.KeyPressed(Keys[4]) &&
                 (!HasBadEffect || (HasBadEffect && BadEffect != BadEffect.NoBomb))))
            {
                // Do we still have a bomb to plant?
                if (CurrentBombAmount <= 0)
                {
                    return;
                }

                // Is there already a bomb here?
                var bo = GameManager.BombList.Find(b => b.CellPosition == CellPosition);
                if (bo != null)
                {
                    return;
                }

                // Plant a new bomb
                var bomb = new Bomb(Id, CellPosition, BombPower, BombTimer, Speed);
                CurrentBombAmount--;
                bomb.Initialize(GameManager.CurrentMap.Size, GameManager.CurrentMap.CollisionLayer, GameManager.HazardMap);

                GameManager.AddBomb(bomb);
            }

            #endregion
        }
Example #2
0
        protected override void Move(GameTime gameTime, Map map, int[,] hazardMap)
        {
            #region Moving input

            _motionVector = Vector2.Zero;

            // Up
            if ((Config.PlayersUsingController[Id] && InputHandler.ButtonDown(Buttons[0], PlayerIndex.One)) ||
                InputHandler.KeyDown(Keys[0]))
            {
                Sprite.CurrentAnimation = AnimationKey.Up;
                CurrentDirection        = LookDirection.Up;
                _motionVector.Y         = -1;
            }
            // Down
            else if ((Config.PlayersUsingController[Id] && InputHandler.ButtonDown(Buttons[1], PlayerIndex.One)) ||
                     InputHandler.KeyDown(Keys[1]))
            {
                Sprite.CurrentAnimation = AnimationKey.Down;
                CurrentDirection        = LookDirection.Down;
                _motionVector.Y         = 1;
            }
            // Left
            else if ((Config.PlayersUsingController[Id] && InputHandler.ButtonDown(Buttons[2], PlayerIndex.One)) ||
                     InputHandler.KeyDown(Keys[2]))
            {
                Sprite.CurrentAnimation = AnimationKey.Left;
                CurrentDirection        = LookDirection.Left;
                _motionVector.X         = -1;
            }
            // Right
            else if ((Config.PlayersUsingController[Id] && InputHandler.ButtonDown(Buttons[3], PlayerIndex.One)) ||
                     InputHandler.KeyDown(Keys[3]))
            {
                Sprite.CurrentAnimation = AnimationKey.Right;
                CurrentDirection        = LookDirection.Right;
                _motionVector.X         = 1;
            }
            else
            {
                CurrentDirection = LookDirection.Idle;
            }

            #endregion

            if (_motionVector != Vector2.Zero)
            {
                IsMoving = true;

                Position += _motionVector * GetMovementSpeed();
            }
            else
            {
                IsMoving = false;
            }

            Sprite.IsAnimating = IsMoving;

            base.Move(gameTime, map, hazardMap);
        }
        public virtual void Update(GameTime gameTime)
        {
            // Decrease the cool down time because when coolDownTimeTicks == 0,
            // the gun is allowed to fire
            // ----------------------------------------------------------------
            if (coolDownTimeTicks > 0)
            {
                coolDownTimeTicks -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }

            // If the shoot key is pressed or the Xbox trigger is pulled
            // ---------------------------------------------------------
            if (InputHandler.KeyDown(shootKey) || InputHandler.ButtonDown(Buttons.RightTrigger, owner.PlayerIndex) ||
                owner.GetType() == typeof(ComputerPlayer))
            {
                // Only shoot if the gun is cooled down
                if (coolDownTimeTicks < 0)
                {
                    OnTrigger(owner, null);
                    coolDownTimeTicks = coolDownTime;
                    fired             = true;
                }
            }
            else
            {
                fired = false;
            }

            // Here's the bullets's foreach method with a lambda!
            // ---------------------------------------------------
            // Update each of the guns bullets and if the bullet's
            // destroyMe variable returns true, delete the bullet.
            // ---------------------------------------------------
            bullets.ForEach((b) =>
            {
                b.Update(gameTime);
                if (b.DestroyMe)
                {
                    // If the gun is a missile launcher, make an explosion on impact
                    if (this.GetType() == typeof(MissileLauncher))
                    {
                        SoundManager.Boom.Play();
                        EffectManager.AddExplosion(b.Position, Vector2.Zero, 15, 20, 4, 6, 40f, 50, new Color(1.0f, 0.3f, 0f, 0.5f), Color.Black * 0f);
                    }
                    // Otherwise, make sparks
                    else
                    {
                        SoundManager.Hit.Play();
                        EffectManager.AddSparksEffect(b.Position, new Vector2(400));
                    }
                }

                if (b.DestroyMe || b.RemoveMe)
                {
                    bullets.Remove(b);
                }
            });
        }
Example #4
0
 public override void Update(GameTime gameTime)
 {
     base.Update(gameTime);
     if (!InputHandler.KeyDown(shootKey) && !InputHandler.ButtonDown(Buttons.RightTrigger, owner.PlayerIndex))
     {
         laser.IsActive = false;
     }
     laser.Update(gameTime);
 }
Example #5
0
        protected override void Move(GameTime gameTime, Map map, int[,] hazardMap)
        {
            base.Move(gameTime, map, hazardMap);

            SendMovement();

            #region Movement interpolation
            // If a new position has been received from server
            if (_nextPosition != Vector2.Zero)
            {
                // If the position received from server is not reached yet
                if (Math.Abs(Position.X - _nextPosition.X) > 0.1f &&
                    Math.Abs(Position.Y - _nextPosition.Y) > 0.1f)
                {
                    // First time that we interpolate => save the initial position
                    if (!_isInterpolating)
                    {
                        _isInterpolating = true;
                        _initialPosition = Position;
                    }

                    _interpolationTimer += gameTime.ElapsedGameTime;

                    // We interpolate
                    float interpolationAmount = MathHelper.Clamp((float)_interpolationTimer.TotalMilliseconds / (float)_movementInterpolationTime.TotalMilliseconds, 0f, 1f);
                    PositionX = MathHelper.Lerp(_initialPosition.X, _nextPosition.X, interpolationAmount);
                    PositionY = MathHelper.Lerp(_initialPosition.Y, _nextPosition.Y, interpolationAmount);

                    if (_interpolationTimer >= _movementInterpolationTime)
                    {
                        _interpolationTimer = TimeSpan.Zero;
                        _nextPosition       = Vector2.Zero;
                        _isInterpolating    = false;
                    }
                }
            }
            #endregion

            #region Bomb

            if ((HasBadEffect && BadEffect == BadEffect.BombDrop) ||
                ((Config.PlayersUsingController[Id] && InputHandler.ButtonDown(Buttons[4], PlayerIndex.One)) || InputHandler.KeyPressed(Keys[4]) &&
                 (!HasBadEffect || (HasBadEffect && BadEffect != BadEffect.NoBomb))))
            {
                if (this.CurrentBombAmount > 0)
                {
                    var bo = GameServer.Instance.GameManager.BombList.Find(b => b.CellPosition == this.CellPosition);
                    if (bo == null)
                    {
                        // Send to server that we want to plant a bomb
                        GameServer.Instance.SendBombPlacing();
                    }
                }
            }

            #endregion
        }
Example #6
0
        public void Update(GameTime gameTime)
        {
            if (CameraMode == CameraMode.Fixed)
            {
                return;
            }

            if (InputHandler.KeyReleased(Keys.PageUp) ||
                InputHandler.ButtonReleased(Buttons.LeftShoulder, PlayerIndex.One))
            {
                ZoomIn();
            }
            else if (InputHandler.KeyReleased(Keys.PageDown) ||
                     InputHandler.ButtonReleased(Buttons.RightShoulder, PlayerIndex.One))
            {
                ZoomOut();
            }

            if (CameraMode == CameraMode.Follow)
            {
                return;
            }

            Vector2 diff = Vector2.Zero;

            if (InputHandler.KeyDown(Keys.Left) ||
                InputHandler.ButtonDown(Buttons.RightThumbstickLeft, PlayerIndex.One))
            {
                diff.X -= speed;
            }
            else if (InputHandler.KeyDown(Keys.Right) ||
                     InputHandler.ButtonDown(Buttons.RightThumbstickRight, PlayerIndex.One))
            {
                diff.X += speed;
            }

            if (InputHandler.KeyDown(Keys.Up) ||
                InputHandler.ButtonDown(Buttons.RightThumbstickUp, PlayerIndex.One))
            {
                diff.Y -= speed;
            }
            else if (InputHandler.KeyDown(Keys.Down) ||
                     InputHandler.ButtonDown(Buttons.RightThumbstickDown, PlayerIndex.One))
            {
                diff.Y += speed;
            }

            if (diff != Vector2.Zero)
            {
                diff.Normalize();
                position += diff * speed;

                LockCamera();
            }
        }
Example #7
0
        private static InputData ReadInputFromPad()
        {
            var bulletTime = InputHandler.ButtonDown(Config.PlayerGamepadInput[1], PlayerIndex.One);

            var fire = InputHandler.GamePadStates[0].ThumbSticks.Right.Length() > 0;

            var direction = Vector2.Zero;

            direction.X = InputHandler.GamePadStates[0].ThumbSticks.Left.X;
            direction.Y = (-1) * InputHandler.GamePadStates[0].ThumbSticks.Left.Y;

            var rotation = (float)
                           Math.Atan2(InputHandler.GamePadStates[0].ThumbSticks.Right.Y * (-1),
                                      InputHandler.GamePadStates[0].ThumbSticks.Right.X) + MathHelper.PiOver2;

            return(new InputData(fire, direction, rotation, Vector2.Zero, false));
        }
Example #8
0
        /* The Update() function for the camera handles its movement in "free" camera mode. */
        public void Update(GameTime gameTime)
        {
            /* If the CameraMode is set to "follow", that means the camera position is handled elsewhere, so nothing else needs updating. */
            if (mode == CameraMode.Follow)
            {
                return;
            }

            /* The motion vector is reset to zero, as movement is separate for each frame.
             * After this, the position vector has the speed added or subtracted depending on which arrow key is pressed.
             * This has the effect of the motion vector representing upwards movement when the up arrow is pressed, for example. */
            Vector2 motion = Vector2.Zero;

            if (InputHandler.KeyDown(Keys.Left) || InputHandler.ButtonDown(Buttons.RightThumbstickLeft, PlayerIndex.One))
            {
                position.X -= speed;
            }
            else if (InputHandler.KeyDown(Keys.Right) || InputHandler.ButtonDown(Buttons.RightThumbstickRight, PlayerIndex.One))
            {
                position.X += speed;
            }
            if (InputHandler.KeyDown(Keys.Up) || InputHandler.ButtonDown(Buttons.RightThumbstickUp, PlayerIndex.One))
            {
                position.Y -= speed;
            }
            else if (InputHandler.KeyDown(Keys.Down) || InputHandler.ButtonDown(Buttons.RightThumbstickDown, PlayerIndex.One))
            {
                position.Y += speed;
            }

            /* If the motion has movement in it, the motion is normalized (set to a length of one).
             * After this, (motion * speed) is added to the position.
             * This means the position will move in motion's direction, by speed's distance.
             * Finally, the camera is locked so it can't move outside the bounds of the map. */
            if (motion != Vector2.Zero)
            {
                motion.Normalize();
                position += motion * speed;
                LockCamera();
            }
        }
Example #9
0
        public void Update(GameTime gameTime)
        {
            Vector2 motion = new Vector2();

            if (InputHandler.KeyDown(Keys.W) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickUp, PlayerIndex.One))
            {
                Sprite.CurrentAnimation = AnimationKey.Up;
                motion.Y = -1;
            }

            if (InputHandler.KeyDown(Keys.S) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickDown, PlayerIndex.One))
            {
                Sprite.CurrentAnimation = AnimationKey.Down;
                motion.Y = 1;
            }

            if (InputHandler.KeyDown(Keys.A) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickLeft, PlayerIndex.One))
            {
                Sprite.CurrentAnimation = AnimationKey.Left;
                motion.X = -1;
            }

            if (InputHandler.KeyDown(Keys.D) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickRight, PlayerIndex.One))
            {
                Sprite.CurrentAnimation = AnimationKey.Right;
                motion.X = 1;
            }

            motion.Normalize();

            Sprite.IsAnimating = motion != Vector2.Zero;
            Sprite.Position   += motion * Sprite.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            Sprite.LockToMap();
            Sprite.Update(gameTime);
        }
Example #10
0
        public void Update(GameTime gameTime)
        {
            if (mode == CameraMode.Follow)
            {
                return;
            }

            Vector2 motion = Vector2.Zero;

            if (InputHandler.KeyDown(Keys.Left) ||
                InputHandler.ButtonDown(Buttons.RightThumbstickLeft, PlayerIndex.One))
            {
                motion.X = -speed;
            }
            else if (InputHandler.KeyDown(Keys.Right) ||
                     InputHandler.ButtonDown(Buttons.RightThumbstickRight, PlayerIndex.One))
            {
                motion.X = speed;
            }

            if (InputHandler.KeyDown(Keys.Up) ||
                InputHandler.ButtonDown(Buttons.RightThumbstickUp, PlayerIndex.One))
            {
                motion.Y = -speed;
            }
            else if (InputHandler.KeyDown(Keys.Down) ||
                     InputHandler.ButtonDown(Buttons.RightThumbstickDown, PlayerIndex.One))
            {
                motion.Y = speed;
            }

            if (motion != Vector2.Zero)
            {
                motion.Normalize();
                position += motion * speed;

                LockCamera();
            }
        }
Example #11
0
        private void AnimateSprite()
        {
            Vector2 motion = new Vector2();

            if (InputHandler.KeyDown(Keys.W) || InputHandler.ButtonDown(Buttons.LeftThumbstickUp, PlayerIndex.One))
            {
                _sprite.CurrentAnimation = AnimationKey.Up;
                motion.Y = -1;
            }
            else if (InputHandler.KeyDown(Keys.S) || InputHandler.ButtonDown(Buttons.LeftThumbstickDown, PlayerIndex.One))
            {
                _sprite.CurrentAnimation = AnimationKey.Down;
                motion.Y = 1;
            }
            if (InputHandler.KeyDown(Keys.A) || InputHandler.ButtonDown(Buttons.LeftThumbstickLeft, PlayerIndex.One))
            {
                _sprite.CurrentAnimation = AnimationKey.Left;
                motion.X = -1;
            }
            else if (InputHandler.KeyDown(Keys.D) || InputHandler.ButtonDown(Buttons.LeftThumbstickRight, PlayerIndex.One))
            {
                _sprite.CurrentAnimation = AnimationKey.Right;
                motion.X = 1;
            }
            if (motion != Vector2.Zero)
            {
                _sprite.IsAnimating = true;
                motion.Normalize();
                _sprite.Position += motion * _sprite.Speed;
                _sprite.LockToMap();
                _player.Camera.LockToSprite(_sprite);
            }
            else
            {
                _sprite.IsAnimating = false;
            }
        }
Example #12
0
        /// <summary>
        /// Handles key inputs from the player
        /// </summary>
        protected virtual void HandleInput()
        {
            // Instead of changing pos directly, make it look nice by changing velocity
            var newVelocity = Vector2.Zero;

            if (InputHandler.KeyDown(movingKeys[0]) || InputHandler.ButtonDown(Buttons.LeftThumbstickUp, playerIndex))
            {
                // When changing y values, change pos directly
                pos.Y -= maxspeed;
            }
            if (InputHandler.KeyDown(movingKeys[1]) || InputHandler.ButtonDown(Buttons.LeftThumbstickDown, playerIndex))
            {
                pos.Y += maxspeed;
            }

            // By changing velocity, this allows us to add the slow down effect
            if (InputHandler.KeyDown(movingKeys[2]) || InputHandler.ButtonDown(Buttons.LeftThumbstickLeft, playerIndex))
            {
                newVelocity.X -= .5f;
                if (angle > -.08f)
                {
                    angle -= .005f;                 // Magic numbers are bad and I should feel bad
                }
            }
            if (InputHandler.KeyDown(movingKeys[3]) || InputHandler.ButtonDown(Buttons.LeftThumbstickRight, playerIndex))
            {
                newVelocity.X += .5f;
                if (angle < .08f)
                {
                    angle += .005f;
                }
            }

            Velocity += newVelocity;
            Position += Velocity;
        }
Example #13
0
        public override void Update(GameTime gameTime)
        {
            player.Update(gameTime);
            sprite.Update(gameTime);

            if (InputHandler.KeyReleased(Keys.PageUp) ||
                InputHandler.ButtonReleased(Buttons.LeftShoulder, PlayerIndex.One))
            {
                player.Camera.ZoomIn();

                if (player.Camera.CameraMode == CameraMode.Follow)
                {
                    player.Camera.LockToSprite(sprite);
                }
            }
            else if (InputHandler.KeyReleased(Keys.PageDown) ||
                     InputHandler.ButtonReleased(Buttons.RightShoulder, PlayerIndex.One))
            {
                player.Camera.ZoomOut();

                if (player.Camera.CameraMode == CameraMode.Follow)
                {
                    player.Camera.LockToSprite(sprite);
                }
            }

            Vector2 motion = new Vector2();

            if (InputHandler.KeyDown(Keys.W) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickUp, PlayerIndex.One))
            {
                sprite.CurrentAnimation = AnimationKey.Up;
                motion.Y = -1;
            }
            else if (InputHandler.KeyDown(Keys.S) ||
                     InputHandler.ButtonDown(Buttons.LeftThumbstickDown, PlayerIndex.One))
            {
                sprite.CurrentAnimation = AnimationKey.Down;
                motion.Y = 1;
            }

            if (InputHandler.KeyDown(Keys.A) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickLeft, PlayerIndex.One))
            {
                sprite.CurrentAnimation = AnimationKey.Left;
                motion.X = -1;
            }
            else if (InputHandler.KeyDown(Keys.D) ||
                     InputHandler.ButtonDown(Buttons.LeftThumbstickRight, PlayerIndex.One))
            {
                sprite.CurrentAnimation = AnimationKey.Right;
                motion.X = 1;
            }

            if (motion != Vector2.Zero)
            {
                sprite.IsAnimating = true;

                motion.Normalize();

                sprite.Position += motion * sprite.Speed;
                sprite.LockToMap();

                if (player.Camera.CameraMode == CameraMode.Follow)
                {
                    player.Camera.LockToSprite(sprite);
                }
            }
            else
            {
                sprite.IsAnimating = false;
            }

            if (InputHandler.KeyReleased(Keys.F) ||
                InputHandler.ButtonReleased(Buttons.RightStick, PlayerIndex.One))
            {
                player.Camera.ToggleCameraMode();

                if (player.Camera.CameraMode == CameraMode.Follow)
                {
                    player.Camera.LockToSprite(sprite);
                }
            }

            if (player.Camera.CameraMode != CameraMode.Follow)
            {
                if (InputHandler.KeyReleased(Keys.C) ||
                    InputHandler.ButtonReleased(Buttons.LeftStick, PlayerIndex.One))
                {
                    player.Camera.LockToSprite(sprite);
                }
            }

            base.Update(gameTime);
        }
Example #14
0
        public void Update(GameTime gameTime)
        {
            camera.Update(gameTime);
            Sprite.Update(gameTime);

            if (InputHandler.KeyReleased(Keys.PageUp) ||
                InputHandler.ButtonReleased(Buttons.LeftShoulder, PlayerIndex.One))
            {
                camera.ZoomIn();

                if (camera.CameraMode == CameraMode.Follow)
                {
                    camera.LockToSprite(Sprite);
                }
            }
            else if (InputHandler.KeyReleased(Keys.PageDown) ||
                     InputHandler.ButtonReleased(Buttons.RightShoulder, PlayerIndex.One))
            {
                camera.ZoomOut();

                if (camera.CameraMode == CameraMode.Follow)
                {
                    camera.LockToSprite(Sprite);
                }
            }

            Vector2 motion = new Vector2();

            if (InputHandler.KeyDown(Keys.W) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickUp, PlayerIndex.One))
            {
                Sprite.CurrentAnimation = AnimationKey.Up;
                motion.Y = -1;
            }
            else if (InputHandler.KeyDown(Keys.S) ||
                     InputHandler.ButtonDown(Buttons.LeftThumbstickDown, PlayerIndex.One))
            {
                Sprite.CurrentAnimation = AnimationKey.Down;
                motion.Y = 1;
            }

            if (InputHandler.KeyDown(Keys.A) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickLeft, PlayerIndex.One))
            {
                Sprite.CurrentAnimation = AnimationKey.Left;
                motion.X = -1;
            }
            else if (InputHandler.KeyDown(Keys.D) ||
                     InputHandler.ButtonDown(Buttons.LeftThumbstickRight, PlayerIndex.One))
            {
                Sprite.CurrentAnimation = AnimationKey.Right;
                motion.X = 1;
            }

            if (motion != Vector2.Zero)
            {
                UpdatePosition(gameTime, motion);
            }
            else
            {
                Sprite.IsAnimating = false;
            }

            if (InputHandler.KeyReleased(Keys.F) ||
                InputHandler.ButtonReleased(Buttons.RightStick, PlayerIndex.One))
            {
                camera.ToggleCameraMode();

                if (camera.CameraMode == CameraMode.Follow)
                {
                    camera.LockToSprite(Sprite);
                }
            }

            if (camera.CameraMode != CameraMode.Follow)
            {
                if (InputHandler.KeyReleased(Keys.C) ||
                    InputHandler.ButtonReleased(Buttons.LeftStick, PlayerIndex.One))
                {
                    camera.LockToSprite(Sprite);
                }
            }
        }
Example #15
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (_lives <= 0)
            {
                IsAlive = false;
            }

            if (IsInvincible)
            {
                _invincibleTime -= gameTime.ElapsedGameTime;

                if (_invincibleTime.Milliseconds <= 0)
                {
                    _invincibleTime = Config.PlayerInvicibleTimer;
                    Position        = _originPosition;
                    IsInvincible    = false;
                }
            }
            else
            {
                float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

                _direction = Vector2.Zero;

                if (_controller == Config.Controller.Keyboard)
                {
                    // Keyboard
                    if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Up"]))
                    {
                        _direction.Y = -1;
                    }
                    if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Right"]))
                    {
                        _direction.X = 1;
                    }
                    if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Down"]))
                    {
                        _direction.Y = 1;
                    }
                    if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Left"]))
                    {
                        _direction.X = -1;
                    }

                    SlowMode   = (PlayerData.SlowModeEnabled && (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Slow"]))) ? true : false;
                    BulletTime = (PlayerData.BulletTimeEnabled && (!_bulletTimeReloading && InputHandler.MouseState.RightButton == ButtonState.Pressed)) ? true : false;

                    if (_direction != Vector2.Zero)
                    {
                        _velocitySlowMode = Config.PlayerMaxSlowVelocity / 2;
                        _velocity         = Config.PlayerMaxVelocity / 2;
                    }
                    else
                    {
                        _velocitySlowMode = Config.PlayerMaxSlowVelocity;
                        _velocity         = Config.PlayerMaxVelocity;
                    }

                    // Mouse
                    _distance.X = Position.X - InputHandler.MouseState.X;
                    _distance.Y = Position.Y - InputHandler.MouseState.Y;

                    Rotation = (float)Math.Atan2(_distance.Y, _distance.X) - MathHelper.PiOver2;

                    // Debug
                    if (InputHandler.KeyDown(Keys.R))
                    {
                        Rotation = 0;
                    }

                    if (InputHandler.MouseState.LeftButton == ButtonState.Pressed)
                    {
                        Fire(gameTime);
                    }
                }
                else if (_controller == Config.Controller.GamePad)
                {
                    _direction.X = InputHandler.GamePadStates[0].ThumbSticks.Left.X;
                    _direction.Y = (-1) * InputHandler.GamePadStates[0].ThumbSticks.Left.Y;

                    SlowMode   = (PlayerData.SlowModeEnabled && (InputHandler.ButtonDown(Config.PlayerGamepadInput[0], PlayerIndex.One))) ? true : false;
                    BulletTime = (PlayerData.BulletTimeEnabled && (!_bulletTimeReloading && InputHandler.ButtonDown(Config.PlayerGamepadInput[1], PlayerIndex.One))) ? true : false;

                    if (InputHandler.GamePadStates[0].ThumbSticks.Right.Length() > 0)
                    {
                        Rotation =
                            (float)
                            Math.Atan2(InputHandler.GamePadStates[0].ThumbSticks.Right.Y * (-1),
                                       InputHandler.GamePadStates[0].ThumbSticks.Right.X) + MathHelper.PiOver2;

                        Fire(gameTime);
                    }
                }

                if (BulletTime)
                {
                    _bulletTimeTimer -= gameTime.ElapsedGameTime;

                    if (_bulletTimeTimer <= TimeSpan.Zero)
                    {
                        _bulletTimeReloading = true;
                        _bulletTimeTimer     = TimeSpan.Zero;
                    }
                }

                if (_bulletTimeReloading)
                {
                    _bulletTimeTimer += gameTime.ElapsedGameTime;

                    if (_bulletTimeTimer >= Config.DefaultBulletTimeTimer)
                    {
                        _bulletTimeReloading = false;
                        _bulletTimeTimer     = Config.DefaultBulletTimeTimer;
                    }
                }

                UpdatePosition(dt);
            }

            _camera.Update();
        }
Example #16
0
        public void Update(GameTime gameTime, MapLayer tilemap)
        {
            createBullet = false;
            playerOrigin = new Vector2(animatedSprite.Position.X + animatedSprite.Width / 2, animatedSprite.Position.Y + animatedSprite.Height / 2);
            animatedSprite.Update(gameTime);

            if (InputHandler.KeyReleased(Keys.Z) || InputHandler.Scroll(Mouse.GetState()) == 1 ||
                InputHandler.ButtonReleased(Buttons.LeftShoulder, PlayerIndex.One))
            {
                camera.Zoom(0.25f);
                camera.LockToSprite(animatedSprite);
            }

            else if (InputHandler.KeyReleased(Keys.X) || InputHandler.Scroll(Mouse.GetState()) == -1 ||
                     InputHandler.ButtonReleased(Buttons.RightShoulder, PlayerIndex.One))
            {
                camera.Zoom(-0.25f);
                camera.LockToSprite(animatedSprite);
            }
            motion = new Vector2();
            if (InputHandler.KeyDown(Keys.W) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickUp, PlayerIndex.One))
            {
                animatedSprite.currentAnimation = Constants.Direction.Up;
                if (checkIfCanWalkOnTile(tilemap, 0, -3) == 2)
                {
                    completedLevel = true;
                }
                if (checkIfCanWalkOnTile(tilemap, 0, -3) == 1)
                {
                    motion.Y = -9;
                }
            }
            else if (InputHandler.KeyDown(Keys.S) ||
                     InputHandler.ButtonDown(Buttons.LeftThumbstickDown, PlayerIndex.One))
            {
                animatedSprite.currentAnimation = Constants.Direction.Down;
                if (checkIfCanWalkOnTile(tilemap, 0, 3) == 2)
                {
                    completedLevel = true;
                }
                if (checkIfCanWalkOnTile(tilemap, 0, 3) == 1)
                {
                    motion.Y = 9;
                }
            }
            if (InputHandler.KeyDown(Keys.A) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickLeft, PlayerIndex.One))
            {
                animatedSprite.currentAnimation = Constants.Direction.Left;
                if (checkIfCanWalkOnTile(tilemap, -3, 0) == 2)
                {
                    completedLevel = true;
                }
                if (checkIfCanWalkOnTile(tilemap, -3, 0) == 1)
                {
                    motion.X = -9;
                }
            }
            if (InputHandler.KeyDown(Keys.D) ||
                InputHandler.ButtonDown(Buttons.LeftThumbstickRight, PlayerIndex.One))
            {
                animatedSprite.currentAnimation = Constants.Direction.Right;
                if (checkIfCanWalkOnTile(tilemap, 3, 0) == 2)
                {
                    completedLevel = true;
                }
                if (checkIfCanWalkOnTile(tilemap, 3, 0) == 1)
                {
                    motion.X = 9;
                }
            }



            if (motion != Vector2.Zero)
            {
                animatedSprite.isAnimating = true;
                motion.Normalize();
                animatedSprite.Position += motion * animatedSprite.Speed;
                animatedSprite.LockToMap();
                camera.LockToSprite(animatedSprite);
            }
            else
            {
                animatedSprite.isAnimating = false;
            }
            if (InputHandler.KeyPressed(Keys.Space) ||
                InputHandler.ButtonDown(Buttons.A, PlayerIndex.One))
            {
                bullets.Add(new Bullet(bulletSprite, playerOrigin, animatedSprite.currentAnimation.ToString(), motion));
                createBullet = true;
            }

            foreach (Bullet bullet in bullets)
            {
                bullet.Update(gameTime, tilemap);
            }

            UpdateHealthBar();

            playerHealth.Update(gameTime);
        }
Example #17
0
        public override void Update(GameTime gameTime)
        {
            // If super speed is not enabled, max speed == 5, otherwise,
            // double that sucker!
            // --------------------------------------------------------
            maxspeed = !superSpeed ? 5f : 10f;

            // If you don't know what this is, don't touch.
            // --------------------------------------------
            var epsilon = .005f;

            // F**K! I have repeated, ugly code. What was I thinking?
            // ------------------------------------------------
            // TODO: Remove ugly code, add nice code
            // ------------------------------------------------
            if (!GamePad.GetState(playerIndex, GamePadDeadZone.Circular).IsConnected)
            {
                if (Math.Abs(0 - angle) > epsilon && ((!InputHandler.KeyDown(movingKeys[2]) && !InputHandler.KeyDown(movingKeys[3]))))
                {
                    if (angle < 0)
                    {
                        angle += .01f;
                    }
                    else if (angle > 0)
                    {
                        angle -= .01f;
                    }
                }
            }
            else
            {
                if (Math.Abs(0 - angle) > epsilon && ((!InputHandler.ButtonDown(Buttons.LeftThumbstickLeft, playerIndex) &&
                                                       !InputHandler.ButtonDown(Buttons.LeftThumbstickRight, playerIndex))))
                {
                    if (angle < 0)
                    {
                        angle += .01f;
                    }
                    else if (angle > 0)
                    {
                        angle -= .01f;
                    }
                }
            }

            // If the player is not moving, slow him the down to 0
            // ---------------------------------------------------
            if (Math.Abs(velocity.X) > epsilon && !InputHandler.KeyDown(movingKeys[2]) && !InputHandler.KeyDown(movingKeys[3]))
            {
                if (velocity.X > 0)
                {
                    velocity.X -= .1f;
                }
                else if (velocity.X < 0)
                {
                    velocity.X += .1f;
                }
            }

            HandleInput();

            // Each weapon may have it's own update method. More OOP == better code
            // --------------------------------------------------------------------
            equippedWeapon.Update(gameTime);

            // Loop through each players' bullets (except my own) and if any hit me,
            // hurt me and remove the bullet from the field
            // --------------------------------------------------------------------
            foreach (Player p in GameplayState.Players)
            {
                // Why would I check if my bullets hit me? Well... there's probably many
                //     resons but for now, keep it my way.
                if (p == this)
                {
                    continue;
                }

                // Laser guns are an exception to bullet checks... instead, they're just a
                // giant rectangle.  The laser isn't removed if it's hitting me
                if (p.equippedWeapon.GetType() == typeof(LaserGun))
                {
                    var weapon = (LaserGun)p.equippedWeapon;
                    if (weapon.Laser.Bounds.Intersects(Bounds) && weapon.Laser.IsActive)
                    {
                        Damage(p.equippedWeapon.Damage);
                    }
                }
                else
                {
                    // Here's the bullet foreach loop, it just uses a fancy lambda expression
                    p.equippedWeapon.Bullets.ForEach((b) =>
                    {
                        // If there's no forcefield then KILL ME
                        if (!forceField)
                        {
                            if (b.Bounds.Intersects(Bounds))
                            {
                                Damage(p.equippedWeapon.Damage);
                                b.DestroyMe = true;
                            }
                        }
                        // If there is a force field then don't kill me
                        else
                        {
                            if (b.Bounds.Intersects(ForceFieldBounds))
                            {
                                b.DestroyMe = true;
                            }
                        }
                    });
                }
            }
        }
Example #18
0
        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime, PlayerIndex.One);

            if (InputHandler.PressedCancel() && _passStep != 8)
            {
                Game.Exit();
            }

            if (InputHandler.PressedUp())
            {
                _menuIndex--;

                if (_menuIndex < 0)
                {
                    _menuIndex = _menuText.Length - 1;
                }

                GameRef.Select.Play();
            }

            if (InputHandler.PressedDown())
            {
                _menuIndex = (_menuIndex + 1) % _menuText.Length;
                GameRef.Select.Play();
            }

            if (InputHandler.PressedAction() && _passStep != 9)
            {
                GameRef.Choose.Play();

                // 1 Player
                if (_menuIndex == 0)
                {
                    Config.PlayersNumber = 1;
                    StateManager.ChangeState(GameRef.GameplayScreen);
                }
                // 2 Players
                else if (_menuIndex == 1)
                {
                    Config.PlayersNumber = 2;
                    StateManager.ChangeState(GameRef.GameplayScreen);
                }
                // Improvements
                else if (_menuIndex == 2)
                {
                    StateManager.ChangeState(GameRef.ImprovementScreen);
                }
                // Options
                else if (_menuIndex == 3)
                {
                    StateManager.ChangeState(GameRef.OptionsScreen);
                }
                // Exit
                else if (_menuIndex == 4)
                {
                    Game.Exit();
                }
            }

            if (_backgroundMainRectangle.X + Config.Resolution.X <= 0)
            {
                _backgroundMainRectangle.X = _backgroundRightRectangle.X + Config.Resolution.X;
            }

            if (_backgroundRightRectangle.X + Config.Resolution.X <= 0)
            {
                _backgroundRightRectangle.X = _backgroundMainRectangle.X + Config.Resolution.X;
            }

            /*
             * if (_backgroundMainRectangle.Y + _backgroundImage.Height <= 0)
             *  _backgroundMainRectangle.Y = _backgroundTopRectangle.Y - _backgroundImage.Height;
             *
             * if (_backgroundTopRectangle.Y + _backgroundImage.Height <= 0)
             *  _backgroundTopRectangle.Y = _backgroundMainRectangle.Y - _backgroundImage.Height;
             */


            _backgroundMainRectangle.X  -= (int)(70 * (float)gameTime.ElapsedGameTime.TotalSeconds);
            _backgroundRightRectangle.X -= (int)(70 * (float)gameTime.ElapsedGameTime.TotalSeconds);

            // Cheat and debug mode
            if (!Config.Cheat)
            {
                if ((_passStep == 0 || _passStep == 1) && InputHandler.PressedUp())
                {
                    _passStep++;
                }
                else if ((_passStep == 2 || _passStep == 3) && InputHandler.PressedDown())
                {
                    _passStep++;
                }
                else if ((_passStep == 4 || _passStep == 6) && InputHandler.PressedLeft())
                {
                    _passStep++;
                }
                else if ((_passStep == 5 || _passStep == 7) && InputHandler.PressedRight())
                {
                    _passStep++;
                }
                else if (_passStep == 8 && (InputHandler.PressedCancel() || InputHandler.KeyPressed(Keys.B)))
                {
                    _passStep++;
                }
                else if (_passStep == 9 && (InputHandler.PressedAction() || InputHandler.KeyPressed(Keys.A)))
                {
                    _passStep++;
                }

                if (_passStep == 10)
                {
                    Config.Cheat = true;
                    _passSound.Play();
                }
            }
            else if (InputHandler.KeyDown(Keys.F9))
            {
                Config.Debug = !Config.Debug;
            }

#if DEBUG
            if (InputHandler.KeyDown(Keys.F10))
            {
                StateManager.ChangeState(GameRef.DebugScreen);
            }
            else if (InputHandler.KeyDown(Keys.F12))
            {
                StateManager.ChangeState(GameRef.PatternTestScreen);
            }
#endif

            if (Config.Cheat)
            {
                if (InputHandler.KeyDown(Keys.A) || InputHandler.ButtonDown(Buttons.Y, PlayerIndex.One))
                {
                    PlayerData.Credits += 1000;
                }
            }

            base.Update(gameTime);
        }
Example #19
0
        /* The Update() function updates the player by updating its camera and character, and by handling movement. */
        public void Update(GameTime gameTime)
        {
            camera.Update(gameTime);
            character.Update(gameTime, camera);

            /* This commented out block of code allows the user to zoom in and out using the Camera.ZoomIn() and Camera.ZoomOut() functions.
             * It is not currently usable in the engine, since you can't zoom in or out in regular Pokemon games. */
            /* if (InputHandler.KeyReleased(Keys.O) || InputHandler.ButtonReleased(Buttons.LeftShoulder, PlayerIndex.One))
             * {
             *  camera.ZoomIn();
             *  if (camera.CameraMode == CameraMode.Follow)
             *      camera.LockToSprite(Sprite);
             * }
             * else if (InputHandler.KeyReleased(Keys.P) || InputHandler.ButtonReleased(Buttons.RightShoulder, PlayerIndex.One))
             * {
             *  camera.ZoomOut();
             *  if (camera.CameraMode == CameraMode.Follow)
             *      camera.LockToSprite(Sprite);
             * } */

            /* This block handles input relating to the movement of the player.
             * If the relevant W/A/S/D key (or gamepad stick) is down, then the character.Move() function is called with the direction and a reference to the map. */
            if (character.Dir == Movement.None)
            {
                if (InputHandler.KeyDown(Keys.W) || InputHandler.ButtonDown(Buttons.LeftThumbstickUp, PlayerIndex.One))
                {
                    character.Move('u', GameScreens.WorldScreen.World.CurrentMap);
                }
                else if (InputHandler.KeyDown(Keys.S) || InputHandler.ButtonDown(Buttons.LeftThumbstickDown, PlayerIndex.One))
                {
                    character.Move('d', GameScreens.WorldScreen.World.CurrentMap);
                }
                else if (InputHandler.KeyDown(Keys.A) || InputHandler.ButtonDown(Buttons.LeftThumbstickLeft, PlayerIndex.One))
                {
                    character.Move('l', GameScreens.WorldScreen.World.CurrentMap);
                }
                else if (InputHandler.KeyDown(Keys.D) || InputHandler.ButtonDown(Buttons.LeftThumbstickRight, PlayerIndex.One))
                {
                    character.Move('r', GameScreens.WorldScreen.World.CurrentMap);
                }
            }

            /* The window title is updated with the tile coordinates of the player for debugging purposes. */
            gameRef.Window.Title = "PlayerX: " + Sprite.TileLocation.X + " PlayerY: " + Sprite.TileLocation.Y;

            /* This commented out block allows the user to switch between "free" and "follow" camera modes.
             * At the moment, only "follow" is allowed, since the camera should follow the player. */
            /* if (InputHandler.KeyReleased(Keys.F) || InputHandler.ButtonReleased(Buttons.RightStick, PlayerIndex.One))
             * {
             *  camera.ToggleCameraMode();
             *  if (camera.CameraMode == CameraMode.Follow)
             *      camera.LockToSprite(Sprite);
             * }
             * if (camera.CameraMode != CameraMode.Follow)
             * {
             *  if (InputHandler.KeyReleased(Keys.C) || InputHandler.ButtonReleased(Buttons.LeftStick, PlayerIndex.One))
             *  {
             *      camera.LockToSprite(Sprite);
             *  }
             * }
             */
        }
Example #20
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (_lives <= 0)
            {
                IsAlive = false;
            }

            if (IsInvincible)
            {
                if (_timeBeforeRespawn.TotalMilliseconds > 0)
                {
                    _timeBeforeRespawn -= gameTime.ElapsedGameTime;

                    if (_timeBeforeRespawn.TotalMilliseconds <= 0)
                    {
                        Position = _originPosition;
                    }
                }
                else
                {
                    _invincibleTime -= gameTime.ElapsedGameTime;

                    if (_invincibleTime.TotalMilliseconds <= 0)
                    {
                        _invincibleTime = Config.PlayerInvicibleTime;
                        IsInvincible    = false;
                        CollisionBoxes.Remove(_shieldCollisionCircle);
                    }
                }
            }
            //else
            {
                float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

                _direction = Vector2.Zero;

                if (_controller == Config.Controller.Keyboard)
                {
                    // Keyboard
                    if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Up"]))
                    {
                        _direction.Y = -1;
                    }
                    if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Right"]))
                    {
                        _direction.X = 1;
                    }
                    if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Down"]))
                    {
                        _direction.Y = 1;
                    }
                    if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Left"]))
                    {
                        _direction.X = -1;
                    }

                    SlowMode   = (PlayerData.SlowModeEnabled && (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Slow"]))) ? true : false;
                    BulletTime = (PlayerData.BulletTimeEnabled && (!_bulletTimeReloading && InputHandler.MouseState.RightButton == ButtonState.Pressed)) ? true : false;

                    if (_direction != Vector2.Zero)
                    {
                        _velocitySlowMode = Config.PlayerMaxSlowVelocity / 2;
                        _velocity         = Config.PlayerMaxVelocity / 2;
                    }
                    else
                    {
                        _velocitySlowMode = Config.PlayerMaxSlowVelocity;
                        _velocity         = Config.PlayerMaxVelocity;
                    }

                    // Mouse
                    _distance.X = (_viewport.Width / 2f) - InputHandler.MouseState.X;
                    _distance.Y = (_viewport.Height / 2f) - InputHandler.MouseState.Y;

                    Rotation = (float)Math.Atan2(_distance.Y, _distance.X) - MathHelper.PiOver2;

                    // Debug
                    if (InputHandler.KeyDown(Keys.R))
                    {
                        Rotation = 0;
                    }
                    else if (InputHandler.KeyPressed(Keys.V))
                    {
                        Hit();
                    }

                    if (InputHandler.MouseState.LeftButton == ButtonState.Pressed)
                    {
                        Fire(gameTime);
                    }

                    if (InputHandler.MouseState.RightButton == ButtonState.Pressed)
                    {
                        _focusMode = true;
                        SlowMode   = true;
                    }
                    else if (_focusMode)
                    {
                        _focusMode = false;
                        SlowMode   = false;
                    }
                }
                else if (_controller == Config.Controller.GamePad)
                {
                    _direction.X = InputHandler.GamePadStates[0].ThumbSticks.Left.X;
                    _direction.Y = (-1) * InputHandler.GamePadStates[0].ThumbSticks.Left.Y;

                    SlowMode   = (PlayerData.SlowModeEnabled && (InputHandler.ButtonDown(Config.PlayerGamepadInput[0], PlayerIndex.One))) ? true : false;
                    BulletTime = (PlayerData.BulletTimeEnabled && (!_bulletTimeReloading && InputHandler.ButtonDown(Config.PlayerGamepadInput[1], PlayerIndex.One))) ? true : false;

                    if (InputHandler.GamePadStates[0].ThumbSticks.Right.Length() > 0)
                    {
                        Rotation =
                            (float)
                            Math.Atan2(InputHandler.GamePadStates[0].ThumbSticks.Right.Y * (-1),
                                       InputHandler.GamePadStates[0].ThumbSticks.Right.X) + MathHelper.PiOver2;

                        Fire(gameTime);
                    }
                }

                if (BulletTime)
                {
                    _bulletTimeTimer -= gameTime.ElapsedGameTime;

                    if (_bulletTimeTimer <= TimeSpan.Zero)
                    {
                        _bulletTimeReloading = true;
                        _bulletTimeTimer     = TimeSpan.Zero;
                    }
                }

                if (_bulletTimeReloading)
                {
                    _bulletTimeTimer += gameTime.ElapsedGameTime;

                    if (_bulletTimeTimer >= Config.DefaultBulletTimeTimer)
                    {
                        _bulletTimeReloading = false;
                        _bulletTimeTimer     = Config.DefaultBulletTimeTimer;
                    }
                }

                UpdatePosition(dt);
            }

            // Update camera position
            _cameraPosition.X = MathHelper.Lerp(
                _cameraPosition.X,
                Position.X - Config.CameraDistanceFromPlayer.X * (float)Math.Cos(Rotation + MathHelper.PiOver2),
                Config.CameraMotionInterpolationAmount
                );

            _cameraPosition.Y = MathHelper.Lerp(
                _cameraPosition.Y,
                Position.Y - Config.CameraDistanceFromPlayer.Y * (float)Math.Sin(Rotation + MathHelper.PiOver2),
                Config.CameraMotionInterpolationAmount
                );

            _camera.Update(_cameraPosition);

            if (!Config.DisableCameraZoom)
            {
                // Update camera zoom according to mouse distance from player
                var mouseWorldPosition = new Vector2(
                    _cameraPosition.X - GameRef.Graphics.GraphicsDevice.Viewport.Width / 2f + InputHandler.MouseState.X,
                    _cameraPosition.Y - GameRef.Graphics.GraphicsDevice.Viewport.Height / 2f + InputHandler.MouseState.Y
                    );

                var mouseDistanceFromPlayer =
                    (float)
                    Math.Sqrt(Math.Pow(Position.X - mouseWorldPosition.X, 2) +
                              Math.Pow(Position.Y - mouseWorldPosition.Y, 2));

                var cameraZoom = GameRef.Graphics.GraphicsDevice.Viewport.Width / mouseDistanceFromPlayer;

                if (_focusMode)
                {
                    cameraZoom = 1f;
                }
                else
                {
                    cameraZoom = cameraZoom > Config.CameraZoomLimit
                        ? 1f
                        : MathHelper.Clamp(cameraZoom / Config.CameraZoomLimit, Config.CameraZoomMin, Config.CameraZoomMax);
                }

                _camera.Zoom = MathHelper.Lerp(_camera.Zoom, cameraZoom, Config.CameraZoomInterpolationAmount);
            }
        }