82 lines
1.7 KiB
Plaintext
82 lines
1.7 KiB
Plaintext
-- @include /koptilnya/libs/wire_component.txt
|
|
require('/koptilnya/libs/wire_component.txt')
|
|
|
|
Gearbox = class('Gearbox', WireComponent)
|
|
|
|
function Gearbox:initialize(config, clutch, axles)
|
|
self.ratios = config.Ratios
|
|
self.reverse = config.Reverse
|
|
|
|
self.rpm = 0
|
|
self.gear = 0
|
|
self.torque = 0
|
|
|
|
self.axles = axles or {}
|
|
self.clutch = clutch
|
|
|
|
self.type = config.Type
|
|
|
|
clutch:linkGearbox(self)
|
|
|
|
for _, axle in pairs(axles) do
|
|
axle:linkGearbox(self)
|
|
end
|
|
|
|
self:recalcRatio()
|
|
end
|
|
|
|
function Gearbox:getInputs()
|
|
return {
|
|
Upshift = 'number',
|
|
Downshift = 'number'
|
|
}
|
|
end
|
|
|
|
function Gearbox:getOutputs()
|
|
return {
|
|
Gearbox_RPM = 'number',
|
|
Gearbox_Torque = 'number',
|
|
Gearbox_Gear = 'number',
|
|
Gearbox_Ratio = 'number'
|
|
}
|
|
end
|
|
|
|
function Gearbox:updateOutputs()
|
|
wire.ports.Gearbox_RPM = self.rpm
|
|
wire.ports.Gearbox_Torque = self.torque
|
|
wire.ports.Gearbox_Gear = self.gear
|
|
wire.ports.Gearbox_Ratio = self.ratio
|
|
end
|
|
|
|
function Gearbox:setGear(gear)
|
|
if gear >= -1 and gear <= #self.ratios then
|
|
self.gear = gear
|
|
self:recalcRatio()
|
|
end
|
|
end
|
|
|
|
function Gearbox:recalcRatio()
|
|
if self.gear == -1 then
|
|
self.ratio = -self.reverse
|
|
elseif self.gear == 0 then
|
|
self.ratio = 0
|
|
else
|
|
self.ratio = self.ratios[self.gear]
|
|
end
|
|
end
|
|
|
|
function Gearbox:update()
|
|
if self.clutch ~= nil then
|
|
self.torque = self.clutch.torque * self.ratio
|
|
end
|
|
|
|
local maxAxlesRPM = 0
|
|
|
|
if #self.axles > 0 then
|
|
maxAxlesRPM = math.max(unpack(table.map(self.axles, function(diff)
|
|
return diff.avgRPM
|
|
end)))
|
|
end
|
|
|
|
self.rpm = maxAxlesRPM * self.ratio
|
|
end |