Skip to content

Commit

Permalink
feat(lib/server): add Lua vehicle bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Feb 21, 2024
1 parent 6ba40fb commit c9a3b49
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions lib/server/vehicle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local OxVehicle = lib.class('OxVehicle')

function OxVehicle:__index(index)
local value = OxVehicle[index] --[[@as any]]

if type(value) == 'function' then
self[index] = value == OxVehicle.__call and function(...)
return value(self, index, ...)
end or function(...)
return value(self, ...)
end

return self[index]
end

return value
end

function OxVehicle:__call(...)
return exports.ox_core:CallVehicle(self.entity, ...)
end

function OxVehicle:__tostring()
return json.encode(self)
end

function OxVehicle:getCoords()
return GetEntityCoords(self.entity);
end

function OxVehicle:getState()
return Entity(self.entity).state;
end

for method in pairs(exports.ox_core:GetVehicleCalls() or {}) do
if not OxVehicle[method] then OxVehicle[method] = OxVehicle.__call end
end

local function CreateVehicleInstance(vehicle)
if not vehicle then return end;

return OxVehicle:new(vehicle)
end

---@class OxServer
local Ox = Ox

function Ox.GetVehicle(entityId)
return CreateVehicleInstance(exports.ox_core:GetVehicle(entityId))
end

function Ox.GetVehicleFromNetId(netId)
return CreateVehicleInstance(exports.ox_core:GetVehicleFromNetId(netId))
end

function Ox.GetVehicleFromVin(vin)
return CreateVehicleInstance(exports.ox_core:GetVehicleFromVin(vin))
end

function Ox.CreateVehicle(data, coords, heading)
return CreateVehicleInstance(exports.ox_core:CreateVehicle(data, coords, heading));
end

function Ox.SpawnVehicle(dbId, coords, heading)
return CreateVehicleInstance(exports.ox_core:SpawnVehicle(dbId, coords, heading));
end

0 comments on commit c9a3b49

Please sign in to comment.