отрефактори

script_name("g-meter")
script_author("plasma")
script_version("2.0")
local mad = require 'MoonAdditions'
local m = require "memory"


--variaveis--
local gColor = 0x00000000 -- cor do centro do g-meter (alpha, red, green, blue)
local gBorderColor = 0xCCFF5544  -- cor da borda do g-meter
local gPointColor = 0xFF1100CC -- cor do ponto medidor do g-meter
local gPointBorderColor = 0xFFFFFFFF -- cor da borda do ponto
local gBorderSize = 4 -- tamanho da borda
local gx = 20  -- posição horizontal do g-meter
local gy = 320 -- posição vertical do g-meter
local gSizeX = 150 -- tamanho horizontal do g-meter
local gSizeY = 130 -- tamanho vertical do g-meter
local maxLongG = 3.0 -- maximo Gs longitudinal (frente/traseira)
local maxLatG = 6.0 -- maximo Gs lateral (direita/esquerda)


local sp1 = 0
local sp2 = 0
local acel = 0
local acelL = 0
local t1 = 0
local t2 = 0
local deltaTimeime = 0
local car = 0
local angZ = 0
local angZ2 = 0
local radian = 0


local function getTimeScale()
	gameSpeed = m.getfloat(0xB7CB64, false)
	return gameSpeed
end


function updateDeltaTime()
	while true do
		local time1 = gameClock()
		wait(0)
		deltaTime = gameClock() - time1
	end
end


local function testSpeedDiff()
	if sp1 ~= sp2 then
		sp1 = sp2
	end
end


local function acelLong()
	sp2 = getCharSpeed(PLAYER_PED) -- velocidade atual do player
	acel = (sp2 - sp1)/deltaTime
	acel = acel/getTimeScale()
end


local function getZAngle(car)
	return getCarHeading(car)
end


local function acelLat()
	angZ = getZAngle(car)
	radian = ((angZ-angZ2) / (deltaTime)) * 0.01745 -- transforma a variação de deg/s em rad/s
	acelL = (radian * sp2) --  ao multiplicar rad/s por m/s temos m/s^2 = aceleração lateral
	acelL = acelL/getTimeScale()
end
	
	
local function testAngleDiff()
	if angZ2 ~= angZ then
		angZ2 = angZ
	end
end


function draw_text(str, x, y, r, g, b)
	mad.draw_text(str, x, y, mad.font_style.MENU, 0.3, 0.6, mad.font_align.LEFT, 2000, true, false, r, g, b, 255, 1, 0, 30, 30, 30, 120)
end


local function drawGraphics()
	local x, y = 0, 0
	local maxX, maxY = gx+gSizeX, gy+gSizeY
	local halfX, halfY = gSizeX/2, gSizeY/2
	
	local pointCenterX = gx+halfX+gBorderSize/2
	local pointCenterY = gy+halfY-gBorderSize
	
	x = pointCenterX + (acelL/9.8) * ((maxX - pointCenterX) / maxLatG)
	y = pointCenterY + (acel/9.8) * ((maxY - pointCenterY) / maxLongG)
	
	renderDrawBoxWithBorder(gx, gy, gSizeX, gSizeY, gColor, gBorderSize, gBorderColor)
	renderDrawLine(gx+1, gy+halfY-gBorderSize, gx+gSizeX, gy+halfY-gBorderSize, gBorderSize/2, gBorderColor) -- horizontal line
	renderDrawLine(gx+halfX+gBorderSize, gy+1, gx+halfX+gBorderSize, gy+gSizeY, gBorderSize/2, gBorderColor)
	
	x = x >= gx+gBorderSize and x or gx+gBorderSize
	x = x <= maxX-gBorderSize and x or maxX-gBorderSize
	y = y >= gy+gBorderSize and y or gy+gBorderSize
	y = y <= maxY-gBorderSize and y or maxY-gBorderSize
	
	renderDrawPolygon(x, y, 7, 7, 16, 0.0, gPointBorderColor) -- border
	renderDrawPolygon(x, y, 5, 5, 16, 0.0, gPointColor)
	
	draw_text("Lat: " .. string.format("%.2f", acelL/9.8) .. " g", gx+halfX/2, gy+gSizeY+gBorderSize, 255, 255, 255)
	draw_text("Long: " .. string.format("%.2f", acel/9.8) .. " g", gx+gSizeX+gBorderSize*2, gy+halfY, 255, 255, 255)
end


function main()
	local threadTime = lua_thread.create(updateDeltaTime)
	while true do
		wait(0)
		if isPlayerPlaying(PLAYER_HANDLE) then
			if not isCharOnFoot(PLAYER_PED) then --calculos secudarios sao feitos apenas quando dentro de um veiculo
				car = getCarCharIsUsing(PLAYER_PED)
				testSpeedDiff()	
				testAngleDiff()
				acelLong()
				acelLat()
				drawGraphics()
			end
			
		end
	end
	return
end