Beispiel #1
0
            public override void Update(GameTime gameTime)
            {
                #region Prend le focus de la caméra
                if (FocusCamera)
                {
                    Camera cam = MainGame.Camera;
                    if (cam.Position.Y < 0 && Position.Y - BoundingBox.Height > 0)
                    {
                        cam.SetCameraOnActor(this, HAlign.Center, VAlign.Bottom);
                    }
                    else
                    {
                        cam.SetCameraOnActor(this, true, Position.Y - BoundingBox.Height < 0 || cam.Position.Y < 0);
                    }
                }
                #endregion

                Gameplay g = Sender.Parent.Parent;

                #region Application de la gravité
                float vx = Velocity.X;
                float vy = Velocity.Y;

                vy      += GRAVITY / 10;
                Velocity = new Vector2(vx, vy);

                // Dans l'eau
                if (Position.Y > g.WaterLevel)
                {
                    Velocity.Normalize();
                    if (Position.Y > g.WaterLevel + g.WaterHeight)
                    {
                        Die(false);
                    }
                }
                #endregion

                #region Récupération de l'ancienne position pour vérifier les collisions
                Vector2 previousPosition = Position;
                #endregion

                base.Update(gameTime);

                #region Collision avec le sol
                bool    collision         = false;
                Vector2 normalised        = Vector2.Normalize(Velocity);
                Vector2 collisionPosition = previousPosition;
                Vector2 mapSize           = g.MapSize;
                do
                {
                    collisionPosition += normalised;
                    if (g.IsSolid(collisionPosition))
                    {
                        collision          = true;
                        collisionPosition -= normalised;
                    }
                } while (!collision && Math.Abs((collisionPosition - Position).X) >= Math.Abs(normalised.X) && Math.Abs((collisionPosition - Position).Y) >= Math.Abs(normalised.Y) && collisionPosition.X > 0 && collisionPosition.X < mapSize.X && collisionPosition.Y > 0 && collisionPosition.Y < mapSize.Y);

                if (collision)
                {
                    #region Calcul de l'emplacement de la nouvelle position
                    Vector2 center;
                    Vector2 before;
                    Vector2 after;
                    int     delta = 2;
                    if (collisionPosition.Y > previousPosition.Y)
                    {
                        center = g.FindHighestPoint(collisionPosition, 0);
                        before = g.FindHighestPoint(collisionPosition, -delta);
                        after  = g.FindHighestPoint(collisionPosition, delta);
                    }
                    else
                    {
                        center = g.FindHighestPoint(previousPosition, 0);
                        before = g.FindHighestPoint(previousPosition, -delta);
                        after  = g.FindHighestPoint(previousPosition, delta);
                    }
                    #endregion

                    #region Rebond sur le sol
                    float hyp = (float)utils.MathDist(Vector2.Zero, Velocity) - FRICTION / 3;
                    // Limite le rebond.
                    hyp = MathHelper.Clamp(hyp, 0, BounceLimit);

                    float angleFloor     = (float)utils.MathAngle(after - before);
                    float angleDirection = (float)utils.MathAngle(Velocity);
                    float normAngle      = angleDirection - angleFloor;
                    vx              = (float)Math.Cos(normAngle) * hyp;
                    vy              = -(float)Math.Sin(normAngle) * hyp;
                    angleDirection  = (float)utils.MathAngle(new Vector2(vx, vy));
                    angleDirection += angleFloor;
                    vx              = (float)Math.Cos(angleDirection) * hyp;
                    vy              = (float)Math.Sin(angleDirection) * hyp;
                    Velocity        = new Vector2(vx, vy);
                    Position        = collisionPosition + center;
                    #endregion
                }
                #endregion

                #region Positionnement de la GUI
                _textBox.Position = new Vector2(BoundingBox.Left, BoundingBox.Top - BoundingBox.Height);
                #endregion

                #region Sortie de map
                if (Sender.Parent.Parent.OutOfMap(this))
                {
                    Die(false);
                }
                #endregion
            }
Beispiel #2
0
            public override void Update(GameTime gameTime)
            {
                Gameplay g = Sender.Parent.Parent;

                #region Gestion de la gravité
                float vx = Velocity.X;
                float vy = Velocity.Y;

                if (!_onFloor)
                {
                    vy = GRAVITY;
                }

                // Dans l'eau
                if (Position.Y > g.WaterLevel)
                {
                    Velocity.Normalize();
                    if (Position.Y > g.WaterLevel + g.WaterHeight)
                    {
                        Die(false);
                    }
                }
                #endregion

                #region Limitations de la vélocité
                vx       = MathHelper.Clamp(vx, -SPEED_MAX, SPEED_MAX);
                vy       = MathHelper.Clamp(vy, -SPEED_MAX, SPEED_MAX);
                Velocity = new Vector2(vx, vy);
                #endregion

                #region Récupération de l'ancienne position en 3 points en bas (Gauche / Centre / Droite) pour vérifier les collisions
                Vector2 previousPosMiddle = new Vector2(BoundingBox.Location.X, BoundingBox.Size.Y);
                Vector2 previousPosLeft   = new Vector2(BoundingBox.Left, BoundingBox.Bottom);
                Vector2 previousPosRight  = new Vector2(BoundingBox.Right, BoundingBox.Bottom);
                #endregion

                base.Update(gameTime);

                #region Collision avec le sol
                Vector2 newPosLeft   = new Vector2(Position.X - ImgBox.Value.Width / 2, Position.Y);
                Vector2 newPosMiddle = Position;
                Vector2 newPosRight  = new Vector2(Position.X + ImgBox.Value.Width / 2, Position.Y);

                if (g.IsSolid(newPosLeft, previousPosLeft) ||
                    g.IsSolid(newPosMiddle, previousPosMiddle) ||
                    g.IsSolid(newPosRight, previousPosRight))
                {
                    _onFloor = true;

                    // Récupère l'altitude en Y à position.X -20 et +20 afin d'en déterminer l'angle à partir d'un vecteur tracé entre ces deux points.
                    Vector2 center = g.FindHighestPoint(Position, 0);
                    Vector2 before = g.FindHighestPoint(Position, -BoundingBox.Width / 2);
                    Vector2 after  = g.FindHighestPoint(Position, BoundingBox.Width / 2);
                    Angle     = (float)utils.MathAngle(after - before);
                    Position += center;
                }
                else
                {
                    _onFloor = false;
                }
                RefreshBoundingBox();
                #endregion
            }