71 lines
1.5 KiB
Plaintext
71 lines
1.5 KiB
Plaintext
--@include /koptilnya/libs/watcher.txt
|
|
--@include ./base.txt
|
|
|
|
require('/koptilnya/libs/watcher.txt')
|
|
require('./base.txt')
|
|
|
|
ManualGearbox = class('ManualGearbox', Gearbox)
|
|
|
|
function ManualGearbox:initialize(config)
|
|
Gearbox.initialize(self, config)
|
|
|
|
table.merge(self.wireOutputs, {
|
|
Gearbox_Gear = 'number'
|
|
})
|
|
|
|
self.ratios = config.Ratios or { 3.6, 2.2, 1.5, 1.2, 1.0, 0.8}
|
|
self.reverse = config.Reverse or 3.4
|
|
|
|
self.gear = 0
|
|
|
|
function shiftFunc()
|
|
local upshift = wire.ports.Upshift or 0
|
|
local downshift = wire.ports.Downshift or 0
|
|
|
|
return upshift - downshift
|
|
end
|
|
|
|
self.shiftWatcher = watcher(shiftFunc, function(val)
|
|
if val ~= 0 then
|
|
self:shift(val)
|
|
end
|
|
end)
|
|
|
|
self:recalcRatio()
|
|
end
|
|
|
|
function ManualGearbox:updateWireOutputs()
|
|
Gearbox.updateWireOutputs(self)
|
|
|
|
wire.ports.Gearbox_Gear = self.gear
|
|
end
|
|
|
|
function ManualGearbox:setGear(gear)
|
|
if gear >= -1 and gear <= #self.ratios then
|
|
self.gear = gear
|
|
self:recalcRatio()
|
|
end
|
|
end
|
|
|
|
function ManualGearbox: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 ManualGearbox:shift(dir)
|
|
self:setGear(self.gear + dir)
|
|
end
|
|
|
|
function ManualGearbox:forwardStep(torque, inertia)
|
|
local result = Gearbox.forwardStep(self, torque, inertia)
|
|
|
|
self.shiftWatcher()
|
|
|
|
return result
|
|
end
|