67 lines
1.3 KiB
Lua
67 lines
1.3 KiB
Lua
-- @include ../../libs/table.txt
|
|
require('../../libs/table.txt')
|
|
|
|
Gearbox = class('Gearbox')
|
|
|
|
function Gearbox:initialize(options)
|
|
options = options or {}
|
|
|
|
self.ratios = options.Ratios
|
|
self.finalDrive = options.FinalDrive
|
|
self.reverse = options.Reverse
|
|
|
|
self.rpm = 0
|
|
self.gear = 0
|
|
|
|
self:recalcRatio()
|
|
|
|
self.torque = 0
|
|
|
|
self._linkedDiffs = {}
|
|
self._clutch = nil
|
|
end
|
|
|
|
function Gearbox:linkDiff(diff)
|
|
table.insert(self._linkedDiffs, diff)
|
|
end
|
|
|
|
function Gearbox:shift(dir)
|
|
self:setGear(self.gear + dir)
|
|
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:linkClutch(clutch)
|
|
self._clutch = clutch
|
|
clutch:linkGearbox(self)
|
|
end
|
|
|
|
function Gearbox:update()
|
|
if self._clutch ~= nil then
|
|
self.torque = self._clutch.torque * self.ratio * self.finalDrive
|
|
end
|
|
|
|
local axlesRPM = table.map(self._linkedDiffs, function(diff)
|
|
return diff.avgRPM
|
|
end)
|
|
|
|
local maxAxlesRPM = math.max(unpack(axlesRPM))
|
|
|
|
self.rpm = maxAxlesRPM * self.finalDrive * self.ratio
|
|
end
|