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);
            }
        }
Beispiel #2
0
        private void Split(CollisionConvexPolygon box)
        {
            var newPolygonShape = _structure.Split(box);
            var center          = box.GetCenterInWorldSpace();

            // TODO: Part is dead?
            // A boss part is dead when its center is dead?
            // or when the number of sub-parts is less than a number?
            if (center.X > (Size.X / 2f - 2 * _step) + Position.X - Origin.X &&
                center.X < (Size.X / 2f + 2 * _step) + Position.X - Origin.X)
            {
                TakeDamage(99999);
            }
            else
            {
                var boxLocalPosition = box.GetLocalPosition();

                // Left (1) or right (-1) part?
                var factor = (boxLocalPosition.X > Origin.X) ? 1 : -1;

                // If the break out part is not large enough => we don't create another part
                if (newPolygonShape.Vertices != null && newPolygonShape.GetArea() > Config.MinBossPartArea)
                {
                    var bossPart = new BossPart(
                        GameRef, _bossRef, _players, _moverManager, null, _color,
                        _health, 0, 25f, null, newPolygonShape
                        );

                    bossPart.Initialize();

                    var bossPartSize     = newPolygonShape.GetSize();
                    var boxWorldPosition = box.GetWorldPosition();

                    // Compute new boss part's world position
                    var worldPosition = Vector2.Zero;
                    worldPosition.Y = boxWorldPosition.Y - boxLocalPosition.Y;

                    // Left part
                    if (factor == 1)
                    {
                        worldPosition.X = boxWorldPosition.X - bossPartSize.X;
                    }
                    // Right part
                    else if (factor == -1)
                    {
                        worldPosition.X = boxWorldPosition.X + _step;
                    }

                    // Update world position according to parent rotation
                    bossPart.Scale    = Scale;
                    bossPart.Rotation = Rotation;

                    var newLocalPosition = Vector2.Zero;
                    newLocalPosition.X = (Position.X - Origin.X) + (boxLocalPosition.X);
                    newLocalPosition.Y = (Position.Y - Origin.Y);

                    if (factor == -1)
                    {
                        newLocalPosition.X -= bossPartSize.X;
                    }
                    else
                    {
                        newLocalPosition.X += _step;
                    }

                    var newOrigin = newPolygonShape.GetCenter();
                    newLocalPosition += newOrigin;

                    var rotationOrigin = Position;
                    newLocalPosition = Vector2.Transform(newLocalPosition - rotationOrigin, Matrix.CreateRotationZ(Rotation)) + rotationOrigin;

                    bossPart.Origin   = newOrigin;
                    bossPart.Position = newLocalPosition;
                    _bossRef.Parts.Add(bossPart);

                    // Give to this new BossPart an impulsion to (pseudo) random direction due to explosion
                    var random = (float)(GameRef.Rand.NextDouble() * (1f - 0.75f)) + 0.75f;
                    bossPart.ApplyImpulse(new Vector2(factor, random * factor), new Vector2(random / 5f));
                    ApplyImpulse(new Vector2(-factor, random * -factor), new Vector2(random / 5f));
                }

                // Remove destroyed bounding boxes
                if (factor == -1)
                {
                    CollisionBoxes.RemoveAll(bb => bb.GetCenter().X < boxLocalPosition.X);
                }
                else
                {
                    CollisionBoxes.RemoveAll(bb => bb.GetCenter().X > boxLocalPosition.X);
                }

                // This is the bounding that the player has destroyed, so we remove it from the list
                CollisionBoxes.Remove(box);
            }
        }