FieldSpeechPresentation.cs

Static helper that builds CSS opacity and transform strings for a floating field speech label. Holds three timing variants and smooths the enter and exit fades.

using System;

namespace Socer;

// Display-only clock. It never consumes football randomness or changes a decision.
public static class FieldSpeechPresentation
{
    public const int ActiveVariant=1;
    public static float ActiveDuration=>Duration(ActiveVariant);
    public static float Duration(int variant)=>variant switch{0=>1.25f,1=>1.55f,_=>1.4f};
    public static float Opacity(float age,float duration)
    {
        if(age<0 || age>=duration)return 0;
        return Smooth(Math.Min(age/.14f,(duration-age)/.2f));
    }
    static float Smooth(float t){t=Math.Clamp(t,0,1);return t*t*(3-2*t);}
    public static string Motion(float age,float duration,int variant=0,bool fade=true)
    {
        float enter=Smooth(age/.16f),exit=Smooth((age-duration+.2f)/.2f);
        float offset=variant==2?6*(1-enter)-4*exit:5*(1-enter)-3*exit;
        float scale=variant==1?.94f+.06f*enter-.03f*exit:1;
        return (fade?FormattableString.Invariant($"opacity:{Opacity(age,duration):0.###};"):"")+FormattableString.Invariant($"transform:translateY({offset:0.###}px) scale({scale:0.###});");
    }
}