This commit is contained in:
Jérémie N'gadi
2017-09-02 12:15:18 +02:00
parent 1dfeee2a18
commit 58ab6be31e
5 changed files with 194 additions and 170 deletions
+50
View File
@@ -0,0 +1,50 @@
function CreateStatus(name, default, color, visible, tickCallback)
local self = {}
self.val = default
self.name = name
self.default = default
self.color = color
self.visible = visible
self.tickCallback = tickCallback
self._set = function(k, v)
self[k] = v
end
self._get = function(k)
return self[k]
end
self.onTick = function()
self.tickCallback(self)
end
self.set = function(val)
self.val = val
end
self.add = function(val)
if self.val + val > Config.StatusMax then
self.val = Config.StatusMax
else
self.val = self.val + val
end
end
self.remove = function(val)
if self.val - val < 0 then
self.val = 0
else
self.val = self.val - val
end
end
self.getPercent = function()
return (self.val / Config.StatusMax) * 100
end
return self
end
+126 -50
View File
@@ -1,21 +1,126 @@
local Status = {}
local loaded = false
RegisterNetEvent('esx_status:update')
AddEventHandler('esx_status:update', function(status)
function GetStatusData(minimal)
local status = {}
for i=1, #Status, 1 do
if minimal then
table.insert(status, {
name = Status[i].name,
val = Status[i].val,
})
else
table.insert(status, {
name = Status[i].name,
val = Status[i].val,
color = Status[i].color,
visible = Status[i].visible(Status[i]),
max = Status[i].max,
})
end
end
return status
end
AddEventHandler('esx_status:registerStatus', function(name, default, color, visible, tickCallback)
local s = CreateStatus(name, default, color, visible, tickCallback)
table.insert(Status, s)
end)
RegisterNetEvent('esx_status:load')
AddEventHandler('esx_status:load', function(status)
for i=1, #Status, 1 do
for j=1, #status, 1 do
if Status[i].name == status[j].name then
Status[i].set(status[j].val)
end
end
end
Citizen.CreateThread(function()
while true do
for i=1, #Status, 1 do
Status[i].onTick()
end
SendNUIMessage({
update = true,
status = GetStatusData()
})
Citizen.Wait(Config.TickTime)
end
end)
end)
RegisterNetEvent('esx_status:set')
AddEventHandler('esx_status:set', function(name, val)
Status = status
for i=1, #Status, 1 do
if Status[i].name == name then
Status[i].set(val)
break
end
end
SendNUIMessage({
update = true,
status = Status
status = GetStatusData()
})
if not loaded then
TriggerEvent('esx_status:loaded')
loaded = true
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
RegisterNetEvent('esx_status:add')
AddEventHandler('esx_status:add', function(name, val)
for i=1, #Status, 1 do
if Status[i].name == name then
Status[i].add(val)
break
end
end
SendNUIMessage({
update = true,
status = GetStatusData()
})
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
RegisterNetEvent('esx_status:remove')
AddEventHandler('esx_status:remove', function(name, val)
for i=1, #Status, 1 do
if Status[i].name == name then
Status[i].remove(val)
break
end
end
SendNUIMessage({
update = true,
status = GetStatusData()
})
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
AddEventHandler('esx_status:getStatus', function(name, cb)
@@ -38,48 +143,6 @@ AddEventHandler('esx_status:setDisplay', function(val)
end)
-- Calculate status
Citizen.CreateThread(function()
while true do
for i=1, #Status, 1 do
if Status[i].clientAction.add ~= nil then
local val = math.floor(Status[i].clientAction.add / (Config.TickTime / Config.ClientTickTime))
if Status[i].val + val > Config.StatusMax then
Status[i].val = StatusMax
else
Status[i].val = Status[i].val + val
end
end
if Status[i].clientAction.remove ~= nil then
local val = math.floor(Status[i].clientAction.remove / (Config.TickTime / Config.ClientTickTime))
if Status[i].val - val < 0 then
Status[i].val = 0
else
Status[i].val = Status[i].val - val
end
end
end
SendNUIMessage({
update = true,
status = Status
})
Citizen.Wait(Config.ClientqTickTime)
end
end)
-- Pause menu disable hud display
local isPaused = false
@@ -94,4 +157,17 @@ Citizen.CreateThread(function()
TriggerEvent('esx_status:setDisplay', 0.5)
end
end
end)
-- Loaded event
Citizen.CreateThread(function()
TriggerEvent('esx_status:loaded')
end)
-- Update server
Citizen.CreateThread(function()
while true do
Citizen.Wait(Config.UpdateInterval)
TriggerServerEvent('esx_status:update', GetStatusData(true))
end
end)