98 lines
2.3 KiB
Plaintext
98 lines
2.3 KiB
Plaintext
--@include /koptilnya/libs/constants.txt
|
|
--@include /koptilnya/libs/utils.txt
|
|
--@include ../wire_component.txt
|
|
|
|
local WireComponent = require('../wire_component.txt')
|
|
|
|
require('/koptilnya/libs/constants.txt')
|
|
require('/koptilnya/libs/utils.txt')
|
|
|
|
local PowertrainComponent = class('PowertrainComponent', WireComponent)
|
|
|
|
function PowertrainComponent:initialize(vehicle, name, config)
|
|
config = config or {}
|
|
|
|
WireComponent.initialize(self)
|
|
|
|
self.vehicle = vehicle
|
|
self.name = name or 'PowertrainComponent'
|
|
self.CONFIG = config
|
|
self.DEBUG = config.DEBUG or false
|
|
self.input = nil
|
|
self.output = nil
|
|
|
|
self.inertia = 0.02
|
|
self.angularVelocity = 0
|
|
self.torque = 0
|
|
|
|
self.DEBUG_DATA = {}
|
|
|
|
if self.DEBUG then
|
|
if CLIENT then
|
|
net.receive('DEBUG_' .. self.name, function()
|
|
self.DEBUG_DATA = net.readTable()
|
|
end)
|
|
end
|
|
|
|
self.DEBUG_SEND_DATA_DEBOUNCED = debounce(function()
|
|
net.start("DEBUG_" .. self.name)
|
|
net.writeTable(self.DEBUG_DATA)
|
|
net.send(nil, true)
|
|
end, TICK_INTERVAL * 20)
|
|
end
|
|
end
|
|
|
|
function PowertrainComponent:start()
|
|
end
|
|
|
|
function PowertrainComponent:linkComponent(component)
|
|
if not component:isInstanceOf(PowertrainComponent) then
|
|
return
|
|
end
|
|
|
|
if self.output == nil then
|
|
self.output = component
|
|
component.input = self
|
|
end
|
|
end
|
|
|
|
function PowertrainComponent:getRPM()
|
|
return self.angularVelocity * RAD_TO_RPM
|
|
end
|
|
|
|
function PowertrainComponent:queryInertia()
|
|
if self.output == nil then
|
|
return self.inertia
|
|
end
|
|
|
|
return self.inertia + self.output:queryInertia()
|
|
end
|
|
|
|
function PowertrainComponent:queryAngularVelocity(angularVelocity)
|
|
self.angularVelocity = angularVelocity
|
|
|
|
if self.output == nil then
|
|
return 0
|
|
end
|
|
|
|
return self.output:queryAngularVelocity(angularVelocity)
|
|
end
|
|
|
|
function PowertrainComponent:forwardStep(torque, inertia)
|
|
if self.output == nil then
|
|
return self.torque
|
|
end
|
|
|
|
return self.output:forwardStep(self.torque, self.inertia + inertia)
|
|
end
|
|
|
|
function PowertrainComponent:updateWireOutputs()
|
|
WireComponent.updateWireOutputs(self)
|
|
|
|
if self.DEBUG then
|
|
self.DEBUG_SEND_DATA_DEBOUNCED()
|
|
end
|
|
end
|
|
|
|
return PowertrainComponent
|