2021-03-28 18:30:08 +05:00

133 lines
3.6 KiB
Plaintext

-- @name Engine
-- @author DarkSupah, Opti1337, .hemp
-- @shared
-- @include ./constants.txt
-- @include ./engine.txt
-- @include ./gearbox.txt
-- @include ./input.txt
require("./constants.txt")
require("./engine.txt")
require("./gearbox.txt")
require("./input.txt")
local configPath = "engine/"
local inputConfigPath = "input.json"
local engineConfigPath = "engine.json"
local gearboxConfigPath = "gearbox.json"
Controller = class("Controller")
function Controller:initialize(inputController, engine, gearbox, axles)
self.inputController = inputController
self.engine = engine
self.gearbox = gearbox
self.axles = axles
if SERVER then
hook.add("PlayerEnteredVehicle", "enterHandler", function(ply, vehicle)
if (vehicle == wire.ports.Seat) then
self.inputController:setPlayer(ply)
hook.run("DriverChanged", ply)
end
end)
hook.add("PlayerLeaveVehicle", "exitHandler", function(ply, vehicle)
if (vehicle == wire.ports.Seat) then
self.inputController:setPlayer(NULL_ENTITY)
hook.run("DriverChanged", NULL_ENTITY)
end
end)
net.receive("serverConfigs", function()
local inputConfig = net.readTable()
local engineConfig = net.readTable()
local gearboxConfig = net.readTable()
self.inputController:setConfig(inputConfig)
self.engine:setConfig(engineConfig)
self.gearbox:setConfig(gearboxConfig)
net.start("clientConfigs")
net.writeTable(inputConfig)
net.writeTable(engineConfig)
net.writeTable(gearboxConfig)
net.send()
end)
end
if CLIENT then
net.receive("clientConfigs", function()
self.inputController:setConfig(net.readTable())
self.engine:setConfig(net.readTable())
self.gearbox:setConfig(net.readTable())
end)
end
end
function Controller:update()
self.inputController:update()
self.engine:update()
self.gearbox:update()
self.engine:setThrottle(self.inputController.throttle.value)
end
local inputController = InputController:new()
local engine = Engine:new()
local gearbox = Gearbox:new()
local controller = Controller:new(inputController, engine, gearbox)
if SERVER then
wire.adjustPorts({
Seat = "entity"
}, {
Input = "table",
Engine = "table"
})
if wire.ports.Seat:isValid() then
local possibleDriver = wire.ports.Seat:getDriver()
if possibleDriver:isValid() then
controller.inputController:setPlayer(possibleDriver)
wire.ports.Driver = possibleDriver
end
end
hook.add("DriverChanged", "driverChangeHandler", function(ply)
wire.ports.Driver = ply
end)
hook.add("tick", "update", function()
controller:update()
wire.ports.Engine = controller.engine
wire.ports.Input = controller.inputController
end)
end
if CLIENT then
if CURRENT_PLAYER == OWNER then
local inputConfigFile = file.read(configPath .. inputConfigPath)
local inputConfig = json.decode(inputConfigFile)
local engineConfigFile = file.read(configPath .. engineConfigPath)
local engineConfig = json.decode(engineConfigFile)
local gearboxConfigFile = file.read(configPath .. gearboxConfigPath)
local gearboxConfig = json.decode(gearboxConfigFile)
net.start("serverConfigs")
net.writeTable(inputConfig)
net.writeTable(engineConfig)
net.writeTable(gearboxConfig)
net.send()
end
end