50 lines
984 B
Plaintext
50 lines
984 B
Plaintext
Gearbox = class('Gearbox')
|
|
|
|
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:recalcRatio()
|
|
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 axlesRPM = table.map(self.axles, function(diff)
|
|
return diff.avgRPM
|
|
end)
|
|
|
|
local maxAxlesRPM = math.max(unpack(axlesRPM))
|
|
|
|
self.rpm = maxAxlesRPM * self.ratio
|
|
end
|
|
|
|
function Gearbox:shift()
|
|
end
|