コード例 #1
0
ファイル: ChargeBar.cs プロジェクト: Kvinster/LD48Project
        public void DrawBar(FpsController fpsController, float barRadius)
        {
            // get some data
            float barThickness            = fpsController.ammoBarThickness;
            float ammoBarOutlineThickness = fpsController.ammoBarOutlineThickness;
            float angRadMin     = -fpsController.ammoBarAngularSpanRad / 2;
            float angRadMax     = fpsController.ammoBarAngularSpanRad / 2;
            float angRadMinLeft = angRadMin + ShapesMath.TAU / 2;
            float angRadMaxLeft = angRadMax + ShapesMath.TAU / 2;
            float outerRadius   = barRadius + barThickness / 2;

            float chargeAnim = chargeFillCurve.Evaluate(charge);

            // charge bar shake:
            float   chargeMag    = animChargeShakeMagnitude.Evaluate(chargeAnim) * chargeShakeMagnitude;
            Vector2 origin       = fpsController.GetShake(chargeShakeSpeed, chargeMag);         // do shake here
            float   chargeAngRad = Mathf.Lerp(angRadMaxLeft, angRadMinLeft, chargeAnim);
            Color   chargeColor  = chargeFillGradient.Evaluate(chargeAnim);

            Draw.Arc(origin, fpsController.ammoBarRadius, barThickness, angRadMaxLeft, chargeAngRad, chargeColor);

            Vector2 movingLeftPos = origin + ShapesMath.AngToDir(chargeAngRad) * barRadius;
            Vector2 bottomLeftPos = origin + ShapesMath.AngToDir(angRadMaxLeft) * barRadius;

            // bottom fill
            Draw.Disc(bottomLeftPos, barThickness / 2f, chargeColor);

            // ticks
            const int tickCount = 7;

            Draw.LineEndCaps = LineEndCap.None;
            for (int i = 0; i < tickCount; i++)
            {
                float   t      = i / (tickCount - 1f);
                float   angRad = Mathf.Lerp(angRadMaxLeft, angRadMinLeft, t);
                Vector2 dir    = ShapesMath.AngToDir(angRad);
                Vector2 a      = origin + dir * outerRadius;
                bool    lorge  = i % 3 == 0;
                Vector2 b      = a + dir * (lorge ? tickSizeLorge : tickSizeSmol);
                Draw.Line(a, b, tickTickness, tickColor);

                // scale based on distance to real value
                float chargeDelta = t - chargeAnim;
                float growRange   = chargeDelta < 0 ? fontGrowRangePrev : fontGrowRangeNext;
                float tFontScale  = 1f - ShapesMath.SmoothCos01(Mathf.Clamp01(Mathf.Abs(chargeDelta) / growRange));
                float fontScale   = ShapesMath.Eerp(fontSize, fontSizeLorge, tFontScale);
                Draw.FontSize = fontScale;
                Vector2 labelPos = a + dir * percentLabelOffset;
                string  pct      = Mathf.RoundToInt(t * 100) + "%";
                Draw.Text(labelPos, angRad + ShapesMath.TAU / 2, pct, TextAlign.Right);
            }

            // moving dot
            Draw.Disc(movingLeftPos, barThickness / 2f + ammoBarOutlineThickness / 2f);
            Draw.Disc(movingLeftPos, barThickness / 2f - ammoBarOutlineThickness / 2f, chargeColor);

            FpsController.DrawRoundedArcOutline(origin, barRadius, barThickness, ammoBarOutlineThickness, angRadMinLeft, angRadMaxLeft);

            Draw.LineEndCaps = LineEndCap.Round;

            // glow
            Draw.BlendMode = ShapesBlendMode.Additive;
            Draw.DiscGradientRadial(movingLeftPos, barThickness * 2, chargeColor, Color.clear);
            Draw.BlendMode = ShapesBlendMode.Transparent;
        }
コード例 #2
0
ファイル: Compass.cs プロジェクト: the-pixelgrapher/Gubble
        public void DrawCompass(Vector3 worldDir)
        {
            Vector2 compArcOrigin = position + Vector2.down * bendRadius;

            float   angUiMin    = ShapesMath.TAU * 0.25f - (width / 2) / bendRadius;
            float   angUiMax    = ShapesMath.TAU * 0.25f + (width / 2) / bendRadius;
            Vector2 dirWorld    = new Vector2(worldDir.x, worldDir.z).normalized;
            float   lookAng     = ShapesMath.DirToAng(dirWorld);
            float   angWorldMin = lookAng + fieldOfView / 2;
            float   angWorldMax = lookAng - fieldOfView / 2;

            Draw.Arc(compArcOrigin, bendRadius, lineThickness, angUiMin, angUiMax, ArcEndCap.Round);

            void CompassArcNoot(float worldAng, float size, string label = null)
            {
                float   tCompass = ShapesMath.InverseLerpAngleRad(angWorldMax, angWorldMin, worldAng);
                float   uiAng    = Mathf.Lerp(angUiMin, angUiMax, tCompass);
                Vector2 uiDir    = ShapesMath.AngToDir(uiAng);
                Vector2 a        = compArcOrigin + uiDir * bendRadius;
                Vector2 b        = compArcOrigin + uiDir * (bendRadius - size * tickSize);
                float   fade     = Mathf.InverseLerp(0, tickEdgeFadeFraction, (1f - Mathf.Abs(tCompass * 2 - 1)));

                Draw.Line(a, b, LineEndCap.None, new Color(1, 1, 1, fade));
                if (label != null)
                {
                    Draw.FontSize = fontSizeTickLabel;
                    Draw.Text(b - uiDir * tickLabelOffset, uiAng - ShapesMath.TAU / 4f, label, TextAlign.Center, new Color(1, 1, 1, fade));
                }
            }

            Draw.LineEndCaps   = LineEndCap.Square;
            Draw.LineThickness = lineThickness;


            Vector2 trianglePos = compArcOrigin + Vector2.up * (bendRadius + 0.01f);
            Vector2 labelPos    = compArcOrigin + Vector2.up * (bendRadius) + lookAngLabelOffset * 0.1f;
            string  lookLabel   = Mathf.RoundToInt(-lookAng * Mathf.Rad2Deg + 180f) + "°";

            Draw.FontSize = fontSizeLookLabel;
            Draw.Text(labelPos, 0f, lookLabel, TextAlign.Center);
            Vector2 triA = trianglePos + ShapesMath.AngToDir(-ShapesMath.TAU / 4) * triangleNootSize;
            Vector2 triB = trianglePos + ShapesMath.AngToDir(-ShapesMath.TAU / 4 + ShapesMath.TAU / 3) * triangleNootSize;
            Vector2 triC = trianglePos + ShapesMath.AngToDir(-ShapesMath.TAU / 4 + 2 * ShapesMath.TAU / 3) * triangleNootSize;

            Draw.Triangle(triA, triB, triC);

            int tickCount = (ticksPerQuarterTurn - 1) * 4;

            for (int i = 0; i < tickCount; i++)
            {
                float t        = i / ((float)tickCount);
                float ang      = ShapesMath.TAU * t;
                bool  cardinal = i % (tickCount / 4) == 0;

                string label = null;
                if (cardinal)
                {
                    int angInt = Mathf.RoundToInt((1f - t) * 4);
                    switch (angInt)
                    {
                    case 0:
                    case 4:
                        label = "S";
                        break;

                    case 1:
                        label = "W";
                        break;

                    case 2:
                        label = "N";
                        break;

                    case 3:
                        label = "E";
                        break;
                    }
                }

                float tCompass = ShapesMath.InverseLerpAngleRad(angWorldMax, angWorldMin, ang);
                if (tCompass < 1f && tCompass > 0f)
                {
                    CompassArcNoot(ang, cardinal ? 0.8f : 0.5f, label);
                }
            }
        }
コード例 #3
0
        public void DrawCompass(Vector3 worldDir)
        {
            // prepare all variables
            Vector2 compArcOrigin = position + Vector2.down * bendRadius;
            float   angUiMin      = ShapesMath.TAU * 0.25f - (width / 2) / bendRadius;
            float   angUiMax      = ShapesMath.TAU * 0.25f + (width / 2) / bendRadius;
            Vector2 dirWorld      = new Vector2(worldDir.x, worldDir.z).normalized;
            float   lookAng       = ShapesMath.DirToAng(dirWorld);
            float   angWorldMin   = lookAng + fieldOfView / 2;
            float   angWorldMax   = lookAng - fieldOfView / 2;
            Vector2 labelPos      = compArcOrigin + Vector2.up * (bendRadius) + lookAngLabelOffset * 0.1f;
            string  lookLabel     = Mathf.RoundToInt(-lookAng * Mathf.Rad2Deg + 180f) + "°";

            // prepare draw state
            Draw.LineEndCaps = LineEndCap.Square;
            Draw.Thickness   = lineThickness;

            // draw the horizontal line/arc of the compass
            Draw.Arc(compArcOrigin, bendRadius, lineThickness, angUiMin, angUiMax, ArcEndCap.Round);

            // draw the look angle label
            Draw.FontSize = fontSizeLookLabel;
            Draw.Text(labelPos, 0f, lookLabel, TextAlign.Center);

            // triangle arrow
            Vector2 trianglePos = compArcOrigin + Vector2.up * (bendRadius + 0.01f);

            Draw.RegularPolygon(trianglePos, 3, triangleNootSize, -ShapesMath.TAU / 4);

            // draw ticks
            int tickCount = (ticksPerQuarterTurn - 1) * 4;

            for (int i = 0; i < tickCount; i++)
            {
                float t        = i / ((float)tickCount);
                float ang      = ShapesMath.TAU * t;
                bool  cardinal = i % (tickCount / 4) == 0;

                string label = null;
                if (cardinal)
                {
                    int angInt = Mathf.RoundToInt((1f - t) * 4);
                    label = directionLabels[angInt % 4];
                }

                float tCompass = ShapesMath.InverseLerpAngleRad(angWorldMax, angWorldMin, ang);
                if (tCompass < 1f && tCompass > 0f)
                {
                    DrawTick(ang, cardinal ? 0.8f : 0.5f, label);
                }
            }

            void DrawTick(float worldAng, float size, string label = null)
            {
                float   tCompass = ShapesMath.InverseLerpAngleRad(angWorldMax, angWorldMin, worldAng);
                float   uiAng    = Mathf.Lerp(angUiMin, angUiMax, tCompass);
                Vector2 uiDir    = ShapesMath.AngToDir(uiAng);
                Vector2 a        = compArcOrigin + uiDir * bendRadius;
                Vector2 b        = compArcOrigin + uiDir * (bendRadius - size * tickSize);
                float   fade     = Mathf.InverseLerp(0, tickEdgeFadeFraction, (1f - Mathf.Abs(tCompass * 2 - 1)));

                Draw.Line(a, b, LineEndCap.None, new Color(1, 1, 1, fade));
                if (label != null)
                {
                    Draw.FontSize = fontSizeTickLabel;
                    Draw.Text(b - uiDir * tickLabelOffset, uiAng - ShapesMath.TAU / 4f, label, TextAlign.Center, new Color(1, 1, 1, fade));
                }
            }
        }