2022-07-28 15:44:31 +03:00

81 lines
2.2 KiB
Plaintext

--@server
--@include ./engine.txt
--@include ./clutch.txt
--@include ./differential.txt
--@include ./factories/gearbox.txt
--@include /koptilnya/libs/table.txt
--@include /koptilnya/libs/constants.txt
require('./engine.txt')
require('./clutch.txt')
require('./differential.txt')
require('./factories/gearbox.txt')
require('/koptilnya/libs/table.txt')
require('/koptilnya/libs/constants.txt')
Vehicle = class('Vehicle')
function Vehicle:initialize(config)
if config == nil then
throw('Vehicle config not provided')
end
self.engine = Engine:new(config.Engine)
self.clutch = Clutch:new(config.Clutch)
self.gearbox = GearboxFactory.create(config.Gearbox)
self.axles = table.map(config.Axles, function(config, idx)
return Differential:new(config, idx)
end)
self.components = { self.engine, self.clutch, self.gearbox }
table.add(self.components, self.axles)
self:linkComponents()
self:createIO()
hook.add('tick', 'vehicle_update', function()
self:update()
end)
end
function Vehicle:createIO()
local inputs = {
Base = "entity"
}
local outputs = {}
for _, component in ipairs(self.components) do
inputs = table.merge(inputs, component.wireInputs)
outputs = table.merge(outputs, component.wireOutputs)
end
wire.adjustPorts(inputs, outputs)
end
function Vehicle:linkComponents()
self.engine.output = self.clutch
self.clutch.output = self.gearbox
self.gearbox.output = self.axles[1]
self.axles[1].input = self.gearbox
self.gearbox.input = self.clutch
self.clutch.input = self.engine
end
function Vehicle:update()
local base = wire.ports.Base
self.engine:integrateDownwards()
for _, component in pairs(self.components) do
component:updateWireOutputs()
end
if isValid(base) and (self.clutch:getPress() == 1 or self.gearbox.ratio == 0) then
local tiltForce = self.engine.torque * (-1 + self.engine.masterThrottle * 2)
base:applyForceOffset(base:getUp() * tiltForce, base:getMassCenter() + base:getRight() * UNITS_PER_METER)
base:applyForceOffset(-base:getUp() * tiltForce, base:getMassCenter() - base:getRight() * UNITS_PER_METER)
end
end