0.0.1
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
CC = {
|
||||
cruiseActive = false,
|
||||
increasePressure = false,
|
||||
lastEngineHealth = 0,
|
||||
currentCCSpeed = 0,
|
||||
lastIdealPedalPressure = 0.0,
|
||||
speedDiffTolerance = (0.5 / 3.6),
|
||||
Reset = function(self)
|
||||
if not Utils:DriverCheck() then return end
|
||||
if not self.cruiseActive then return end
|
||||
|
||||
self.cruiseActive = false
|
||||
self.currentCCSpeed = 0
|
||||
self.lastEngineHealth = 0
|
||||
self.lastIdealPedalPressure = 0.0
|
||||
|
||||
Utils:SetHudState(false)
|
||||
end,
|
||||
ApplyIdealPedalPressure = function(self)
|
||||
if not Utils:DriverCheck() then return end
|
||||
if not self.cruiseActive then return end
|
||||
|
||||
if not self.increasePressure then
|
||||
self.increasePressure = true
|
||||
end
|
||||
SetControlNormal(0, 71, self.lastIdealPedalPressure)
|
||||
end,
|
||||
ChangeSpeed = function(self, state)
|
||||
if not Utils:DriverCheck() then return end
|
||||
if not self.cruiseActive then return end
|
||||
|
||||
local currentSpeed = Utils:GetSpeed()
|
||||
|
||||
if state then
|
||||
if currentSpeed >= 120 then return end
|
||||
currentSpeed += 10
|
||||
self.currentCCSpeed = currentSpeed
|
||||
else
|
||||
if currentSpeed == 0 then return end
|
||||
currentSpeed += -10
|
||||
if currentSpeed < 0 then currentSpeed = 0 end
|
||||
self.currentCCSpeed = currentSpeed
|
||||
end
|
||||
end,
|
||||
Enable = function(self)
|
||||
if not Config.Cruise.Enable then return end
|
||||
if not Utils:DriverCheck() then return end
|
||||
|
||||
self.cruiseActive = true
|
||||
self.lastEngineHealth = GetVehicleEngineHealth(Utils.vehicle)
|
||||
|
||||
CreateThread(function()
|
||||
self.currentCCSpeed = Utils:GetSpeed() or 0
|
||||
if self.currentCCSpeed == 0 then self.cruiseActive = false return end
|
||||
|
||||
Utils:SetHudState(true)
|
||||
|
||||
while self.cruiseActive do
|
||||
local engineHealth = GetVehicleEngineHealth(Utils.vehicle)
|
||||
|
||||
if IsControlJustPressed(0, 72) or IsControlJustPressed(0, 76) then
|
||||
self:Reset()
|
||||
end
|
||||
|
||||
if self.lastEngineHealth and ((self.lastEngineHealth - engineHealth) > 10) then
|
||||
self:Reset()
|
||||
else
|
||||
self.lastEngineHealth = engineHealth
|
||||
end
|
||||
|
||||
local curSpeed = Utils:GetSpeed() or 0
|
||||
if curSpeed == 0 then self:Reset() return end
|
||||
|
||||
local diff = self.currentCCSpeed - curSpeed
|
||||
|
||||
if diff > self.speedDiffTolerance then
|
||||
local pedalPressure = 0.8
|
||||
|
||||
if Utils:IsSteering() then
|
||||
pedalPressure = 0.4
|
||||
end
|
||||
|
||||
if self.increasePressure then
|
||||
self.lastIdealPedalPressure = self.lastIdealPedalPressure + 0.025
|
||||
self.increasePressure = false
|
||||
end
|
||||
|
||||
SetControlNormal(0, 71, pedalPressure)
|
||||
elseif diff > - (4 * self.speedDiffTolerance) then
|
||||
self:ApplyIdealPedalPressure()
|
||||
else
|
||||
self.lastIdealPedalPressure = 0.2
|
||||
end
|
||||
|
||||
Wait(0)
|
||||
end
|
||||
end)
|
||||
end
|
||||
}
|
||||
|
||||
--Handlers
|
||||
AddEventHandler('esx:playerPedChanged', function(newPed)
|
||||
Utils.ped = newPed
|
||||
end)
|
||||
|
||||
RegisterNetEvent('esx:playerLoaded')
|
||||
AddEventHandler('esx:playerLoaded',function(xPlayer, isNew, skin)
|
||||
Utils.ped = PlayerPedId()
|
||||
end)
|
||||
|
||||
AddEventHandler('esx:enteredVehicle', function(vehicle, plate, seat, displayName, netId)
|
||||
Utils.vehicle = vehicle
|
||||
if not Config.Seatbelt.Enable then return end
|
||||
SB:Thread()
|
||||
end)
|
||||
|
||||
AddEventHandler('esx:exitedVehicle', function(vehicle, plate, seat, displayName, netId)
|
||||
Utils.vehicle = nil
|
||||
end)
|
||||
|
||||
AddEventHandler('onResourceStart', function(resourceName)
|
||||
if (GetCurrentResourceName() ~= resourceName) then
|
||||
return
|
||||
end
|
||||
Utils.ped = PlayerPedId()
|
||||
end)
|
||||
31
resources/[esx_addons]/esx_cruisecontrol/client/keybind.lua
Normal file
31
resources/[esx_addons]/esx_cruisecontrol/client/keybind.lua
Normal file
@@ -0,0 +1,31 @@
|
||||
if Config.Cruise.Enable then
|
||||
ESX.RegisterInput('esx_cruisecontrol:Enable', Translate('cruiseControl'), "keyboard", Config.Cruise.Key, function()
|
||||
if not Utils.vehicle then return end
|
||||
|
||||
if CC.cruiseActive then
|
||||
CC:Reset()
|
||||
return
|
||||
end
|
||||
CC:Enable()
|
||||
end)
|
||||
|
||||
ESX.RegisterInput('esx_cruisecontrol:IncreaseSpeed', Translate('increaseSpeed'), "keyboard", "ADD", function()
|
||||
if not Utils.vehicle then return end
|
||||
CC:ChangeSpeed(true)
|
||||
end)
|
||||
|
||||
ESX.RegisterInput('esx_cruisecontrol:DecreaseSpeed', Translate('decreaseSpeed'), "keyboard", "SUBTRACT", function()
|
||||
if not Utils.vehicle then return end
|
||||
CC:ChangeSpeed(false)
|
||||
end)
|
||||
end
|
||||
|
||||
if Config.Seatbelt.Enable then
|
||||
ESX.RegisterInput('esx_cruisecontrol:ToggleSeatbelt', Translate('toggleSeatbelt'), "keyboard", Config.Seatbelt.Key, function()
|
||||
if not Utils.vehicle then return end
|
||||
SB.seatbelt = not SB.seatbelt
|
||||
SB:SetState(SB.seatbelt)
|
||||
ESX.ShowNotification(Translate(SB.seatbelt and 'seatbeltOn' or 'seatbeltOff', 5000, 'info'))
|
||||
end)
|
||||
end
|
||||
|
||||
92
resources/[esx_addons]/esx_cruisecontrol/client/seatbelt.lua
Normal file
92
resources/[esx_addons]/esx_cruisecontrol/client/seatbelt.lua
Normal file
@@ -0,0 +1,92 @@
|
||||
SB = {
|
||||
cacheVeh = nil,
|
||||
isRagdoll = false,
|
||||
carArray = {0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 17, 18, 19, 20},
|
||||
seatbelt = false,
|
||||
vehSpeed = 0.0,
|
||||
previousVelocity = vector3(0,0,0),
|
||||
Reset = function(self)
|
||||
self.cacheVeh = nil
|
||||
self.carArray = {0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 17, 18, 19, 20}
|
||||
self.seatbelt = false
|
||||
self.vehSpeed = 0.0
|
||||
self.isRagdoll = false
|
||||
end,
|
||||
EjectSeat = function(self)
|
||||
local position = GetEntityCoords(Utils.ped)
|
||||
SetEntityCoords(Utils.ped, position.x, position.y, position.z - 0.47, true, true, true, false)
|
||||
SetEntityVelocity(Utils.ped, self.previousVelocity.x, self.previousVelocity.y, self.previousVelocity.z)
|
||||
self:SetRagdoll()
|
||||
Citizen.SetTimeout((Config.Seatbelt.RagdollTime or 1) * 1000, function()
|
||||
self:Reset()
|
||||
end)
|
||||
end,
|
||||
Logic = function(self)
|
||||
local prevSpeed = self.vehSpeed
|
||||
self.vehSpeed = GetEntitySpeed(self.cacheVeh)
|
||||
SetPedConfigFlag(Utils.ped, 32, true)
|
||||
|
||||
if self.seatbelt then
|
||||
return DisableControlAction(0, 75, false)
|
||||
end
|
||||
|
||||
local isVehMovingFwd = GetEntitySpeedVector(self.cacheVeh, true).y > 1.0
|
||||
local vehAcceleration = (prevSpeed - self.vehSpeed) / GetFrameTime()
|
||||
local speedIsBiggerThanConfigMPH = prevSpeed > ((Config.Seatbelt.EjectCheckSpeed or 45)/2.237)
|
||||
local reallyFast = vehAcceleration > (100*9.81)
|
||||
|
||||
if (isVehMovingFwd and speedIsBiggerThanConfigMPH and reallyFast) then
|
||||
self:EjectSeat()
|
||||
else
|
||||
self.previousVelocity = GetEntityVelocity(self.cacheVeh)
|
||||
end
|
||||
end,
|
||||
Thread = function(self)
|
||||
CreateThread(function ()
|
||||
while true do
|
||||
if not Utils.vehicle then break end
|
||||
if not self.cacheVeh or self.cacheVeh ~= Utils.vehicle then
|
||||
local vehClass = GetVehicleClass(Utils.vehicle)
|
||||
for i = 1, #self.carArray do
|
||||
if self.carArray[i] == vehClass then
|
||||
self.cacheVeh = Utils.vehicle
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
self:Logic()
|
||||
Wait(200)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
DisableExit = function(self)
|
||||
CreateThread(function ()
|
||||
while self.seatbelt do
|
||||
DisableControlAction(0, 75, true)
|
||||
Wait(0)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
SetState = function(self, state)
|
||||
if not Config.Seatbelt.Enable then return end
|
||||
Utils:SetHudState(state, true)
|
||||
if not state then
|
||||
self:Reset()
|
||||
return
|
||||
end
|
||||
self:DisableExit()
|
||||
end,
|
||||
SetRagdoll = function(self)
|
||||
self.isRagdoll = true
|
||||
CreateThread(function()
|
||||
while self.isRagdoll do
|
||||
Wait(0)
|
||||
SetPedToRagdoll(Utils.ped, 1000, 1000, 0, false, false, false)
|
||||
end
|
||||
end)
|
||||
end
|
||||
}
|
||||
|
||||
exports('isSeatbeltOn', function()
|
||||
return SB.seatbelt
|
||||
end)
|
||||
38
resources/[esx_addons]/esx_cruisecontrol/client/utils.lua
Normal file
38
resources/[esx_addons]/esx_cruisecontrol/client/utils.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
Utils = {
|
||||
ped = 0,
|
||||
vehicle = nil,
|
||||
driver = false,
|
||||
DriverCheck = function(self)
|
||||
if not self.vehicle then return false end
|
||||
if not DoesEntityExist(self.vehicle) then return false end
|
||||
if GetPedInVehicleSeat(self.vehicle, -1) == self.ped then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
HudCheck = function(self)
|
||||
return GetResourceState(Config.HudResource) == 'started' and true or false
|
||||
end,
|
||||
SetHudState = function(self, state, seatbelt)
|
||||
if not self:HudCheck() then return end
|
||||
local settings = seatbelt and Config.Seatbelt or Config.Cruise
|
||||
if not settings.Enable then return end
|
||||
local success, result_or_error = pcall(function()
|
||||
return settings.Export(state)
|
||||
end)
|
||||
if not success and result_or_error then
|
||||
print(("^5[^3ERROR^5] ^1%s"):format(result_or_error))
|
||||
print(("^5[^4INFO^5] ^0The two exports(CruiseControlState,SeatbeltState) in config.lua do not exist in your ^2%s^0 script."):format(Config.HudResource))
|
||||
end
|
||||
if seatbelt then return end
|
||||
ESX.ShowNotification(Translate(state and 'activated' or 'deactivated', 5000, 'info'))
|
||||
end,
|
||||
IsSteering = function(self)
|
||||
return GetVehicleSteeringAngle(self.vehicle) > 10.0
|
||||
end,
|
||||
GetSpeed = function(self)
|
||||
if not self:DriverCheck() then return end
|
||||
local curSpeed = GetEntitySpeed(self.vehicle)
|
||||
return math.floor(curSpeed * 2.236936)
|
||||
end
|
||||
}
|
||||
Reference in New Issue
Block a user