MathHelper.Clamp C# (CSharp) Method

Clamp() public static method

public static Clamp ( double v, double l, double h ) : double
v double
l double
h double
return double
    public static double Clamp(double v, double l, double h)
    {
        if (v < l) v = l;
        if (v > h) v = h;
        return v;
    }

Usage Example

Example #1
0
        internal override void Draw(SpriteBatch _, GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
            var lanIpSize = game.Fonts.Get(MonoGame_Engine.Font.DebugFont).MeasureString(lanIp);

            spriteBatch.DrawString(game.Fonts.Get(MonoGame_Engine.Font.DebugFont), lanIp, new Vector2((game.Screen.CanvasWidth - lanIpSize.X) / 2, game.Screen.CanvasHeight - 20), Color.White);

            for (int i = 0; i < 4; i++)
            {
                pos[i].X = i % 2 == 0 ? ScreenBorder : game.Screen.CanvasWidth - ScreenBorder - chars[i].Width;
                pos[i].Y = i / 2 == 0 ? ScreenBorder : game.Screen.CanvasHeight - ScreenBorder - chars[i].Height;

                if (game.MainScene.Players.Count > i)
                {
                    var player = game.MainScene.Players[i];

                    var fx    = i % 2 == 0 ? SpriteEffects.None : SpriteEffects.FlipHorizontally;
                    var alpha = (gameTime.TotalGameTime.TotalMilliseconds % 300 < 150) ? 0.2f : 1.0f;
                    var color = new Color(1f, 1f, 1f, player.IsInvincible ? alpha : 1f);
                    spriteBatch.Draw(chars[i], pos[i], null, color, 0, Vector2.Zero, Vector2.One, fx, 0);

                    if (!player.IsAlive)
                    {
                        var text           = "You died!\r\nPress Button to join";
                        var textPosXOffset = i % 2 == 0 ? pos[i].X + chars[i].Width : pos[i].X;
                        var textPosYOffset = i / 2 == 0 ? pos[i].Y : pos[i].Y + chars[i].Height - chars[2].Height;
                        DrawTextBox(i, spriteBatch, text, new Vector2(textPosXOffset, textPosYOffset), new Color(player.BaseColor, alpha));
                    }
                    else if (player.EventTextTime > TimeSpan.Zero)
                    {
                        player.EventTextTime -= gameTime.ElapsedGameTime;
                        var textPosXOffset = i % 2 == 0 ? pos[i].X + chars[i].Width : pos[i].X;
                        var textPosYOffset = i / 2 == 0 ? pos[i].Y : pos[i].Y + chars[i].Height - chars[2].Height;
                        DrawTextBox(i, spriteBatch, player.EventText, new Vector2(textPosXOffset, textPosYOffset), player.BaseColor);
                    }
                }
            }

            const float minDistance  = 2000;
            const float blendInMagic = 0.1f;
            float       offset       = MathHelper.Clamp((this.distanceToMotherShip - (minDistance)), -100 / blendInMagic, 0) * blendInMagic;

            //if (distanceToMotherShip > minDistance)
            {
                const float preMultiplier  = 0.0005f;
                const float postMultiplier = 200f;
                const float log            = 5;
                const float max            = 400;
                var         position       = (float)Math.Log(distanceToMotherShip * preMultiplier, log) * postMultiplier;
                position = MathHelper.Clamp(position, 0, max);
                var left      = game.Screen.CanvasWidth / 2 - max / 2;
                var rigth     = game.Screen.CanvasWidth / 2 + max / 2;
                var indicator = position + left;

                spriteBatch.Draw(distanceScale, new Vector2(left, offset));
                distanceMarker.Draw(spriteBatch, new Vector2(indicator, offset + distanceMarker.Height / 2), 0f);
            }

            if (game.MainScene.Players.Count == 0)
            {
                Title.Draw(spriteBatch, gameTime);
            }

            spriteBatch.End();
        }
All Usage Examples Of MathHelper::Clamp