Drift counter / Дрифт счетчик

NozmaD

Администратор
Команда форума
Администратор
Сообщения
166
Баллы
208

Из названия понятно, что будет представлено в этом посте. Скрипт предельно прост, то есть в нём реализован только счетчик. Остальное (Сохранение/Выгрузка и т.д.) нужно доделывать самим. Вернее встраивать в свой мод

JavaScript:
var res = API.getScreenResolutionMaintainRatio();
var gShowMeter = true;
var bIfDrifting = false;
var iStartDriftTick = 0;
var iCurrentTick = 0;
var iDriftTime = 0;
var iEndDriftTick = 0;
var iBackToDriftMS = 2000;
var iLastFrameTick = 0;
var iMinDriftAngle = 25;
var iMaxDriftAngle = 105;
var sDriftDir = "";
var bContinuosDrift = false;
var iDriftCombo = 1;
var iMaxCombo = 20;
var bIsDrifting = false;
var iDriftVelocity =15;
var iVehicleLastDamage = 0;
API.onUpdate.connect(function ()
{
    iCurrentTick = Date.now();
    var sDriftDir = "";
    var cr = 255;
    var cg = 0;
    if(API.isPlayerInAnyVehicle(API.getLocalPlayer()))
    {
        var vehicle = API.getPlayerVehicle(API.getLocalPlayer());
        
        var on4Wheels = API.returnNative('IS_VEHICLE_ON_ALL_WHEELS', 8, vehicle);
        var forwardVec = API.returnNative('GET_ENTITY_FORWARD_VECTOR', 5, vehicle);
        //API.sendChatMessage(forwardVec.X + " " + forwardVec.Y + " "+ forwardVec.Z);
        if(isAllowedVehicleType(vehicle))
        {
            var vVel = API.getEntityVelocity(vehicle);
            var vVel2 = new Vector3(vVel.X*2,vVel.Y*2,vVel.Z*2);
            var fDriveDir = ToDeg(Math.atan2(vVel.X, vVel.Y));
            var vRot = API.getEntityRotation(vehicle);
            var fRot = ClampAngle(vRot.Z);
            var fVelocity = Math.sqrt( (vVel.X*vVel.X) + (vVel.Y*vVel.Y) );
            var vVehPos = API.getEntityPosition(vehicle);
            var vOffset = API.getOffsetInWorldCoords(vehicle, new Vector3( 0, 5, 0 ));
                
            vOffset = new Vector3(vOffset.X-vVehPos.X,vOffset.Y-vVehPos.Y,vOffset.Z-vVehPos.Z);
            
            var vCross = crossProduct(vOffset, vVel2);
            var fNormal = dotProduct(vCross, new Vector3(0, 0, 4));
            if(fNormal > 0)
                sDriftDir = "right";
            else if(fNormal < 0)
                sDriftDir = "left";
            //API.sendChatMessage("Direction: "+sDriftDir);
            if(fDriveDir < 0)                       
                fDriveDir = fDriveDir * -1; // NORTH -> TO WEST -> TO SOUTH
            else if(fDriveDir > 0)
                fDriveDir = 360 - fDriveDir; // NORTH -> TO EAST -> TO SOUTH
            //if(fDriveDir >= 180)fDriveDir = fDriveDir / 2;
            //API.sendChatMessage("frot "+fRot);
            var fDriftAng = Math.abs( fRot - fDriveDir );
            if(fDriftAng > 140)
            {
                if (fRot < 360 && fRot > 180 && fDriveDir > 0 && fDriveDir < 180)
                    fDriftAng = 360 - fRot + fDriveDir;
                else if(fRot < 180 && fRot > 0 && fDriveDir > 180 && fDriveDir < 360)
                    fDriftAng = ( 360 - fDriveDir ) + fRot;
            }
            if(fVelocity < 1 || fDriftAng < 1)
            {
                sDriftDir = "";
                fDriftAng = 0;
            }
            var fAngPercent = ( fDriftAng / iMaxDriftAngle ) * 4;
            if(fAngPercent > 0 && fAngPercent < 1)
            {
                cr = 255;
                cg = 255 * fAngPercent;
            }
            else if (fAngPercent > 1 && fAngPercent < 2)
            {
                cr = 255 * (2 - fAngPercent);
                cg = 255;
            }
            else if (fAngPercent > 2 && fAngPercent < 3)
            {
                cr = 255 * (1 - (4 - fAngPercent));
                cg = 255;
            }
            else if (fAngPercent > 3 && fAngPercent < 4)
            {
                cr = 255;
                cg = 255 * (5 - fAngPercent);
            }
            if(cr < 0)cr = 0;
            if(cr > 255)cr = 255;
            if(cg < 0)cg = 0;
            if(cg > 255)cg = 255;
            cg = Math.floor(cg);
            cr = Math.floor(cr);
            if(API.getVehicleHealth(vehicle) < iVehicleLastDamage)
            {
                bIsDrifting = false;
                bContinuosDrift = false;
                iDriftCombo = 0;
                iDriftTime = 0;
            }
            if(fVelocity > iDriftVelocity && fDriftAng > iMinDriftAngle && fDriftAng < iMaxDriftAngle && !bContinuosDrift && !bIsDrifting && on4Wheels) //start DRIFT
            {
                bIsDrifting = true;
                bContinuosDrift = true;
                iStartDriftTick = iCurrentTick;
                iDriftTime = 0;
            }
            else if(((fVelocity < iDriftVelocity && fVelocity != 0 ) || fDriftAng < iMinDriftAngle || fDriftAng > iMaxDriftAngle) && bIsDrifting || !on4Wheels)//valid when stopped meeting requirements (still in drift)
            {
                bIsDrifting = false;
                iEndDriftTick = iCurrentTick;
            }
            else if(fVelocity > iDriftVelocity && fDriftAng > iMinDriftAngle && fDriftAng < iMaxDriftAngle && bContinuosDrift && !bIsDrifting && on4Wheels)//triggered to back on drift (+1 combo)
            {
                bIsDrifting = true;
                iStartDriftTick = iCurrentTick;
                if(iDriftCombo < iMaxCombo)
                {
                    iDriftCombo = iDriftCombo + 1;
                }
            }
            else if(!bIsDrifting && bContinuosDrift && on4Wheels)
            {
                if(iCurrentTick - iEndDriftTick >= iBackToDriftMS)
                {
                    bContinuosDrift = false;
                    bIsDrifting = false;
                    iEndDriftTick = 0;
                    iDriftTime = 0;
                    iDriftCombo = 1;
                }
            }
            else if(bIsDrifting && bContinuosDrift)
            {
                iDriftTime = iDriftTime + (iCurrentTick - iLastFrameTick);
            }
            if(iDriftTime > 0)API.drawText(""+iDriftCombo*iDriftTime, Math.round(res.Width/2), Math.round(res.Height/2), 1.05, cr, cg, 0, 200, 2, 1, false, true, 0);
            iVehicleLastDamage = API.getVehicleHealth(vehicle);
        }
    }
    iLastFrameTick = iCurrentTick;
});
function isAllowedVehicleType(veh)
{
    var vType = API.getVehicleClass(API.getEntityModel(veh));
    return (vType != 8 && vType != 10 && vType != 11 && vType != 12 && vType != 13 && vType != 14 && vType != 15 && vType != 16 && vType != 17 && vType != 18 && vType != 19 && vType != 20 && vType != 21);
}
function ClampAngle(angle)
{
    return (angle + Math.ceil(-angle / 360) * 360);
}
function dotProduct(vec31, vec32)
{
    var mul = new Vector3(vec31.X * vec32.X,vec31.Y * vec32.Y,vec31.Z * vec32.Z);
    return mul.X + mul.Y + mul.Z;
}
function crossProduct(vec31, vec32 )
{
    var fX = (vec31.Y * vec32.Z) - (vec31.Z * vec32.Y);
    var fY = (vec31.Z * vec32.X) - (vec31.X * vec32.Z);
    var fZ = (vec31.X * vec32.Y) - (vec31.Y * vec32.X);
    return new Vector3( fX, fY, fZ);
}
function ToRad(degrees) {
  return degrees * Math.PI / 180;
}
function ToDeg(radians) {
  return radians * 180 / Math.PI;
}
function HeadingToDirection(Heading)
{
    Heading = ToRad(Heading);
    return new Vector3(-Math.sin(Heading), Math.cos(Heading), 0.0);
}
function RotationToDirection(Rotation)
{
    var rotZ = DegreesToRadians(Rotation.Z);
    var rotX = DegreesToRadians(Rotation.X);
    var multXY = Math.abs(Math.cos(rotX));
    return new Vector3(-Math.sin(rotZ) * multXY, Math.cos(rotZ) * multXY, Math.sin(rotX));
}
function DirectionToHeading(dir)
{
    dir.Z = 0.0;
    dir.Normalize();
    return RadiansToDegrees(Math.atan2(dir.X, dir.Y));
}
 
Автор
Vektor42O
Верх