Progress bar speedometer

Started by PSL, Mar 18, 2023, 08:36 AM

Previous topic - Next topic

PSL

I'm going to bring you a simple, segmented speedometer.You can set the maximum value to adjust the speed limit.It has fade in, fade out, and shake effects.The code has been tested on a blank server and works perfectly.

// ====Server side====
enum StreamType
{
    EnterVehicle=0x01
    ExitVehicle=0x02
}

function onPlayerEnterVehicle(player,vehicle,door)
{
    SendDataToClient(player,StreamType.EnterVehicle,vehicle.ID);
}

function onPlayerExitVehicle(player,vehicle)
{
    SendDataToClient(player,StreamType.ExitVehicle);
}

function SendDataToClient(player,...)
{
    if(vargv[0])
    {
        local byte=vargv[0],len=vargv.len();
        if(1>len) devprint("ToClent <"+byte+"> No params specified.");
        else
        {
            Stream.StartWrite();
            Stream.WriteByte(byte);

            for(local i=1;i<len;i++)
            {
                switch(typeof(vargv[i]))
                {
                    case "integer": Stream.WriteInt(vargv[i]); break;
                    case "string": Stream.WriteString(vargv[i]); break;
                    case "float": Stream.WriteFloat(vargv[i]); break;
                }
            }
           
            if(player==null) Stream.SendStream(null);
            else if(typeof(player)=="instance") Stream.SendStream(player);
            else devprint("ToClient <"+byte+"> Player is not online.");
        }
    }
    else devprint("ToClient: Even the byte wasn't specified...");
}

// ====Client side====
local speedbar1=null;
local speedbar2=null;
local speedtext=null;
local speedveh=null;
local speedalpha=0;

function Script::ScriptLoad()
{
    speedbar1=GUIProgressBar();
    speedbar1.Pos=VectorScreen(GetProportion(1635,"x"),GetProportion(290,"y"));
    speedbar1.Size=VectorScreen(GetProportion(175,"x"),GetProportion(25,"y"));
    speedbar1.StartColour=Colour(255,0,0,255);
    speedbar1.EndColour=Colour(0,255,0,255);
    speedbar1.MaxValue=750;
    speedbar1.Value=750;
    speedbar1.Thickness=GetProportion(2,"y");
    speedbar1.BackgroundShade=0.75;
    speedbar1.Alpha=0;
    speedbar1.RemoveFlags(GUI_FLAG_VISIBLE|GUI_FLAG_BACKGROUND);

    speedbar2=GUIProgressBar();
    speedbar2.Pos=VectorScreen(GetProportion(1635,"x"),GetProportion(325,"y"));
    speedbar2.Size=VectorScreen(GetProportion(175,"x"),GetProportion(25,"y"));
    speedbar2.StartColour=Colour(0,128,255,255);
    speedbar2.EndColour=Colour(255,0,255,255);
    speedbar2.MaxValue=200;
    speedbar2.Value=1;
    speedbar2.Thickness=GetProportion(2,"y");
    speedbar1.BackgroundShade=0.75;
    speedbar2.Alpha=0;
    speedbar2.RemoveFlags(GUI_FLAG_VISIBLE|GUI_FLAG_BACKGROUND);

    speedtext=GUILabel();
    speedtext.Text="xxxxxxxx";
    speedtext.TextColour=Colour(255,255,255,255);
    speedtext.FontName="Helvetica";
    speedtext.FontSize=GetProportion(20,"y");
    speedtext.Pos=VectorScreen(0,GetProportion(25,"y"));
    speedtext.TextAlignment=GUI_ALIGN_LEFT|GUI_ALIGN_TOP;
    speedtext.FontFlags=GUI_FFLAG_BOLD;
    speedtext.Text=""+(speedbar2.Value-1)+" Km/h";
    speedtext.Alpha=0;
    speedtext.RemoveFlags(GUI_FLAG_VISIBLE);

    speedbar2.AddChild(speedtext);
}

function Script::ScriptProcess()
{
    if(speedveh!=null)
    {
        if(speedalpha<255)
        {
            speedalpha+=5;
            speedbar1.Alpha=speedalpha;
            speedbar2.Alpha=speedalpha;
            speedtext.Alpha=speedalpha;
        }

        local speed=((speedveh.Speed.X*speedveh.Speed.X)+(speedveh.Speed.Y*speedveh.Speed.Y)+(speedveh.Speed.Z*speedveh.Speed.Z))*100;
        local a=speed-speedbar2.Value.tointeger();
        if(speedbar2.Value!=speed)
        {   speedbar2.Value=speedbar2.Value+a*0.1;
            speedtext.Text=""+speedbar2.Value.tointeger()+" Km/h";
        }

        local hp=speedveh.Health.tointeger()-250;
        local a=speedbar1.Value.tointeger()-hp;
        if(speedbar1.Value!=speedveh.Health.tointeger()-250) speedbar1.Value=speedbar1.Value-a*0.1;

        if(speed>speedbar2.MaxValue) speedtext.TextColour=Colour(255,0,0,255);
        else speedtext.TextColour=Colour(255,255,255,255);

        if(speed>=25)
        {
            local a=random(-2,2);
            local b=random(-2,2);
            local c=5;
           
            if(speedbar1.Pos.X+a>=1635-c&&speedbar1.Pos.X+a<=1635+c) speedbar1.Pos.X+=a;
            if(speedbar1.Pos.Y+b>=290-c&&speedbar1.Pos.Y+b<=290+c) speedbar1.Pos.Y+=b;
            if(speedbar2.Pos.X+a>=1635-c&&speedbar2.Pos.X+a<=1635+c) speedbar2.Pos.X+=a;
            if(speedbar2.Pos.Y+b>=325-c&&speedbar2.Pos.Y+b<=325+c) speedbar2.Pos.Y+=b;
        }
    }
    else
    {
        if(speedalpha>0)
        {
            speedalpha-=5;
            speedbar1.Alpha=speedalpha;
            speedbar2.Alpha=speedalpha;
            speedtext.Alpha=speedalpha;
        }
        else
        {
            speedbar1.RemoveFlags(GUI_FLAG_VISIBLE);
            speedbar2.RemoveFlags(GUI_FLAG_VISIBLE);
            speedtext.RemoveFlags(GUI_FLAG_VISIBLE);
        }
    }
}

function Server::ServerData(stream)
{
    local type=stream.ReadByte();
    switch(type)
    {
        case 0x01:
        {
            local id=stream.ReadInt();
            speedveh=World.FindVehicle(id);
            speedbar1.AddFlags(GUI_FLAG_VISIBLE);
            speedbar2.AddFlags(GUI_FLAG_VISIBLE);
            speedtext.AddFlags(GUI_FLAG_VISIBLE);
        }
        break;

        case 0x02:
        {
            speedveh=null;
        }
        break;
    }
}

function GUI::GameResize(width,height)
{
    speedbar1.Pos=VectorScreen(GetProportion(1635,"x"),GetProportion(290,"y"));
    speedbar1.Size=VectorScreen(GetProportion(175,"x"),GetProportion(25,"y"));
    speedbar1.Thickness=GetProportion(2,"y");
    speedbar2.Pos=VectorScreen(GetProportion(1635,"x"),GetProportion(325,"y"));
    speedbar2.Size=VectorScreen(GetProportion(175,"x"),GetProportion(25,"y"));
    speedbar2.Thickness=GetProportion(2,"y");
    speedtext.FontSize=GetProportion(20,"y");
    speedtext.Pos=VectorScreen(0,GetProportion(25,"y"));
}

function GetProportion(a,b)
{
    local x=GUI.GetScreenSize().X,y=GUI.GetScreenSize().Y;
    if(b=="x")
    {
        a=(a.tofloat()*x.tofloat())/1920;
        local c=x.tofloat()*(a.tofloat()/x.tofloat());
        return c.tointeger();
    }

    if(b=="y")
    {
        a=(a.tofloat()*y.tofloat())/1080;
        local c=y.tofloat()*(a.tofloat()/y.tofloat());
        return c.tointeger();
    }
}

function random(start,finish)
{
    local r=((rand() % (finish-start))+start);
    return r;
}

drtpolice


Sebastian

Wow, long time no see a speedometer being released for public.
Can you show us a photo/video?

PSL