Implemented new input system
This commit is contained in:
parent
9ff9b08a42
commit
8dc9689601
68
koptilnya/input_system/cl_input.txt
Normal file
68
koptilnya/input_system/cl_input.txt
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
-- @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, 'press')
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
hook.add('inputReleased', 'KeyRelease', function(key)
|
||||||
|
if self.driver == CURRENT_PLAYER then
|
||||||
|
self:_trySetTargetValue(key, 'release')
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Input:_trySetTargetValue(key, evt)
|
||||||
|
local triggeredKey = self.keys[key]
|
||||||
|
|
||||||
|
if triggeredKey ~= nil then
|
||||||
|
local targetValue = evt == 'press' and triggeredKey.Value or 0
|
||||||
|
|
||||||
|
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,
|
||||||
|
Value = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
self.keys[v.Positive] = {
|
||||||
|
Axle = k,
|
||||||
|
Value = 1
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Public here
|
||||||
|
|
||||||
|
function Input:initialize(options)
|
||||||
|
options = options or {}
|
||||||
|
self.keys = {}
|
||||||
|
self.driver = NULL_ENTITY
|
||||||
|
|
||||||
|
self:_setupHooks();
|
||||||
|
self:_setupNetListeners();
|
||||||
|
self:_mapKeys(options.Axles)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Input:update()
|
||||||
|
|
||||||
|
end
|
||||||
29
koptilnya/input_system/main.txt
Normal file
29
koptilnya/input_system/main.txt
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
-- @name Input System
|
||||||
|
-- @author DarkSupah
|
||||||
|
-- @shared
|
||||||
|
-- @include ./cl_input.txt
|
||||||
|
-- @include ./sv_input.txt
|
||||||
|
local Axles = {
|
||||||
|
Horizontal = {
|
||||||
|
Negative = KEY.A,
|
||||||
|
Positive = KEY.D,
|
||||||
|
Gravity = 0.05,
|
||||||
|
Sensitivity = 0.1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
local options = {
|
||||||
|
Axles = Axles
|
||||||
|
}
|
||||||
|
|
||||||
|
if SERVER then
|
||||||
|
require('./sv_input.txt')
|
||||||
|
else
|
||||||
|
require('./cl_input.txt')
|
||||||
|
end
|
||||||
|
|
||||||
|
local input = Input:new(options)
|
||||||
|
|
||||||
|
hook.add('tick', 'InputUpdate', function()
|
||||||
|
input:update()
|
||||||
|
end)
|
||||||
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
|
||||||
Loading…
x
Reference in New Issue
Block a user