Implemented new input system
This commit is contained in:
131
koptilnya/input_system/sv_input.txt
Normal file
131
koptilnya/input_system/sv_input.txt
Normal file
@@ -0,0 +1,131 @@
|
||||
-- @include ../libs/constants.txt
|
||||
require('../libs/constants.txt')
|
||||
|
||||
Input = class('Input')
|
||||
|
||||
-- Private here
|
||||
|
||||
function Input:_adjustPorts()
|
||||
local inputs = {
|
||||
Seat = 'entity'
|
||||
}
|
||||
local outputs = {
|
||||
Driver = 'entity',
|
||||
Axles = 'table'
|
||||
}
|
||||
|
||||
wire.adjustPorts(inputs, outputs)
|
||||
end
|
||||
|
||||
function Input:_syncDriver(ply)
|
||||
net.start('SyncDriver')
|
||||
net.writeEntity(ply)
|
||||
net.send()
|
||||
end
|
||||
|
||||
function Input:_setupHooks()
|
||||
hook.add('PlayerEnteredVehicle', 'VehicleEnter', function(ply, vehicle)
|
||||
if vehicle == self.seat then
|
||||
self.driver = ply
|
||||
self:_syncDriver(self.driver)
|
||||
end
|
||||
|
||||
end)
|
||||
|
||||
hook.add('PlayerLeaveVehicle', 'VehicleLeave', function(_, vehicle)
|
||||
if vehicle == self.seat then
|
||||
self.driver = NULL_ENTITY
|
||||
self:_syncDriver(self.driver)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function Input:_setTargetValue(target)
|
||||
local targetAxle = self.axles[target.Axle]
|
||||
targetAxle.Target = target.Target
|
||||
end
|
||||
|
||||
function Input:_setupNetListeners()
|
||||
net.receive('SetTargetAxle', function()
|
||||
local target = {
|
||||
Axle = net.readString(),
|
||||
Target = net.readInt(4)
|
||||
}
|
||||
|
||||
self:_setTargetValue(target)
|
||||
end)
|
||||
end
|
||||
|
||||
function Input:_updateOutputs()
|
||||
local driver = self.driver
|
||||
|
||||
if self.driver:isValid() == false then
|
||||
driver = NULL_ENTITY
|
||||
end
|
||||
|
||||
wire.ports.Driver = driver
|
||||
wire.ports.Axles = self.axles
|
||||
end
|
||||
|
||||
function Input:_setupAxles(axles)
|
||||
for k, v in pairs(axles) do
|
||||
self.axles[k] = {
|
||||
Value = 0,
|
||||
Target = 0,
|
||||
Gravity = v.Gravity,
|
||||
Sensitivity = v.Sensitivity
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
function Input:_getAxleValue(axle)
|
||||
local usedLerpCoeff = axle.Target ~= 0 and axle.Sensitivity or axle.Gravity
|
||||
|
||||
return math.lerp(usedLerpCoeff, axle.Value, axle.Target)
|
||||
end
|
||||
|
||||
function Input:_updateAxles()
|
||||
for k, v in pairs(self.axles) do
|
||||
v.Value = self:_getAxleValue(v)
|
||||
end
|
||||
end
|
||||
|
||||
-- Public here
|
||||
|
||||
function Input:initialize(options)
|
||||
options = options or {}
|
||||
local axles = options.Axles or {}
|
||||
|
||||
self.axles = {}
|
||||
|
||||
self:_adjustPorts()
|
||||
self:_setupHooks()
|
||||
self:_setupNetListeners()
|
||||
|
||||
self:_setupAxles(axles)
|
||||
|
||||
self.seat = wire.ports.Seat
|
||||
self.driver = self:getDriver()
|
||||
|
||||
-- in case chip was reset
|
||||
self:_syncDriver(self.driver)
|
||||
end
|
||||
|
||||
function Input:getDriver()
|
||||
if self.seat == nil or self.seat:isValid() == false then
|
||||
return NULL_ENTITY
|
||||
end
|
||||
|
||||
local driver = self.seat:getDriver()
|
||||
|
||||
if driver:isValid() == true then
|
||||
return driver
|
||||
else
|
||||
return NULL_ENTITY
|
||||
end
|
||||
end
|
||||
|
||||
function Input:update()
|
||||
self:_updateOutputs()
|
||||
self:_updateAxles()
|
||||
end
|
||||
Reference in New Issue
Block a user