81 lines
1.8 KiB
Plaintext
81 lines
1.8 KiB
Plaintext
-- @include ../libs/constants.txt
|
|
require('../libs/constants.txt')
|
|
|
|
Input = class('Input')
|
|
-- Private here
|
|
|
|
function Input:_setupHooks()
|
|
hook.add('inputPressed', 'KeyPress', function(key)
|
|
if self.driver == CURRENT_PLAYER then
|
|
self:_trySetTargetValue(key)
|
|
end
|
|
end)
|
|
|
|
hook.add('inputReleased', 'KeyRelease', function(key)
|
|
if self.driver == CURRENT_PLAYER then
|
|
self:_trySetTargetValue(key)
|
|
end
|
|
end)
|
|
end
|
|
|
|
function Input:bothKeysHolding(axle)
|
|
return (input.isKeyDown(axle.Positive) == true) and (input.isKeyDown(axle.Negative) == true)
|
|
end
|
|
|
|
function Input:noKeysHolding(axle)
|
|
return (input.isKeyDown(axle.Positive) == false) and (input.isKeyDown(axle.Negative) == false)
|
|
end
|
|
|
|
function Input:getAxleValue(axle)
|
|
return (input.isKeyDown(axle.Positive) and 1 or 0) - (input.isKeyDown(axle.Negative) and 1 or 0)
|
|
end
|
|
|
|
function Input:_trySetTargetValue(key)
|
|
local triggeredKey = self.keys[key]
|
|
|
|
if triggeredKey ~= nil then
|
|
local triggeredAxle = self.axles[triggeredKey.Axle]
|
|
local targetValue = self:getAxleValue(triggeredAxle)
|
|
|
|
net.start('SetTargetAxle')
|
|
net.writeString(triggeredKey.Axle)
|
|
net.writeInt(targetValue, 4)
|
|
net.send()
|
|
end
|
|
end
|
|
|
|
function Input:_setupNetListeners()
|
|
net.receive('SyncDriver', function()
|
|
self.driver = net.readEntity()
|
|
end)
|
|
end
|
|
|
|
function Input:_mapKeys(axles)
|
|
for k, v in pairs(axles) do
|
|
self.keys[v.Negative] = {
|
|
Axle = k
|
|
}
|
|
|
|
self.keys[v.Positive] = {
|
|
Axle = k
|
|
}
|
|
end
|
|
end
|
|
|
|
-- Public here
|
|
|
|
function Input:initialize(options)
|
|
options = options or {}
|
|
self.axles = options.Axles
|
|
self.keys = {}
|
|
self.driver = NULL_ENTITY
|
|
|
|
self:_setupHooks();
|
|
self:_setupNetListeners();
|
|
self:_mapKeys(options.Axles)
|
|
end
|
|
|
|
function Input:update()
|
|
|
|
end
|