From 8dc9689601379d779c8b43fd92f9a6b62739dc3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=93=D1=80=D0=B0=D1=87=D1=91?= =?UTF-8?q?=D0=B2?= Date: Sun, 19 Sep 2021 17:31:20 +0500 Subject: [PATCH] Implemented new input system --- koptilnya/input_system/cl_input.txt | 68 +++++++++++++++ koptilnya/input_system/main.txt | 29 ++++++ koptilnya/input_system/sv_input.txt | 131 ++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 koptilnya/input_system/cl_input.txt create mode 100644 koptilnya/input_system/main.txt create mode 100644 koptilnya/input_system/sv_input.txt diff --git a/koptilnya/input_system/cl_input.txt b/koptilnya/input_system/cl_input.txt new file mode 100644 index 0000000..ef00bfa --- /dev/null +++ b/koptilnya/input_system/cl_input.txt @@ -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 diff --git a/koptilnya/input_system/main.txt b/koptilnya/input_system/main.txt new file mode 100644 index 0000000..7806b50 --- /dev/null +++ b/koptilnya/input_system/main.txt @@ -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) diff --git a/koptilnya/input_system/sv_input.txt b/koptilnya/input_system/sv_input.txt new file mode 100644 index 0000000..8629dca --- /dev/null +++ b/koptilnya/input_system/sv_input.txt @@ -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