63 lines
1.6 KiB
Plaintext
63 lines
1.6 KiB
Plaintext
--@include ./powertrain_component.txt
|
|
--@include /koptilnya/libs/constants.txt
|
|
--@include /koptilnya/libs/entity.txt
|
|
|
|
require('./powertrain_component.txt')
|
|
require('/koptilnya/libs/constants.txt')
|
|
require('/koptilnya/libs/entity.txt')
|
|
|
|
Wheel = class('Wheel', PowertrainComponent)
|
|
|
|
function Wheel:initialize(config, prefix)
|
|
PowertrainComponent.initialize(self)
|
|
|
|
self.prefix = prefix
|
|
self.direction = config.Direction or 1
|
|
|
|
self.wireInputs = {
|
|
[prefix .. 'Wheel'] = 'entity'
|
|
}
|
|
self.wireOutputs = {
|
|
[prefix .. 'WheelRPM'] = 'number'
|
|
}
|
|
|
|
self.entity = NULL_ENTITY
|
|
|
|
hook.add('input', 'vehicle_wheel_update' .. prefix, function(key, val)
|
|
if key == prefix .. 'Wheel' then
|
|
self.entity = val
|
|
|
|
if not isValid(val) then
|
|
return
|
|
end
|
|
|
|
if config.CalculateInertia then
|
|
--self.entity:setInertia(calculateWheelInertia(val))
|
|
--print(calculateWheelInertia(val))
|
|
end
|
|
|
|
self.inertia = self.entity:getInertia().y
|
|
end
|
|
end)
|
|
end
|
|
|
|
function Wheel:updateWireOutputs()
|
|
wire.ports[self.prefix .. 'WheelRPM'] = self:getRPM()
|
|
end
|
|
|
|
function Wheel:queryAngularVelocity()
|
|
return self.angularVelocity
|
|
end
|
|
|
|
function Wheel:forwardStep(torque, inertia)
|
|
if not isValid(self.entity) then
|
|
return 0
|
|
end
|
|
|
|
--self.entity:setInertia(self.entity:getInertia():setY(inertia))
|
|
self.entity:applyTorque(torque * self.direction * self.entity:getRight())
|
|
self.angularVelocity = self.entity:getAngleVelocity().y * self.direction * TICK_INTERVAL
|
|
|
|
return self.angularVelocity * self.inertia
|
|
end
|