This commit is contained in:
Никита Круглицкий
2025-05-11 08:18:33 +06:00
parent 14ab069846
commit a6c89e2f21
13 changed files with 229 additions and 51 deletions

View File

@@ -10,6 +10,8 @@ local Clutch = class('Clutch', PowertrainComponent)
function Clutch:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT then return end
self.wireInputs = {
Clutch = 'number'
}
@@ -22,6 +24,8 @@ function Clutch:initialize(vehicle, name, config)
end
function Clutch:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self)
wire.ports.Clutch_Torque = self.torque
end

View File

@@ -15,6 +15,8 @@ end
function Differential:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT then return end
self.wireOutputs = {
Diff_Torque = 'number'
}
@@ -48,6 +50,8 @@ function Differential:linkComponent(component)
end
function Differential:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self)
wire.ports.Diff_Torque = self.torque
if self.outputA ~= nil then

View File

@@ -3,6 +3,8 @@
local PowertrainComponent = require('./powertrain_component.txt')
require('/koptilnya/libs/constants.txt')
local Engine = class('Engine', PowertrainComponent)
@@ -38,9 +40,18 @@ function Engine:initialize(vehicle, name, config)
self.reactTorque = 0
self.returnedTorque = 0
if CLIENT then
--@include /koptilnya/engine_sound_2.txt
local Sound = require('/koptilnya/engine_sound_2.txt')
Sound(config.MaxRPM or 7000, chip())
end
end
function Engine:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self)
wire.ports.Engine_RPM = self:getRPM()
wire.ports.Engine_Torque = self.torque
wire.ports.Engine_MasterThrottle = self.masterThrottle
@@ -97,7 +108,7 @@ function Engine:forwardStep()
local generatedTorque = self:_generateTorque()
local reactTorque = (targetW - self.angularVelocity) * self.inertia / TICK_INTERVAL
local returnedTorque = self.output:forwardStep(generatedTorque - reactTorque, 0) -- ??? 0 -> self.inertia
local returnedTorque = self.output:forwardStep(generatedTorque - reactTorque, self.inertia) -- ??? 0 -> self.inertia
self.reactTorque = reactTorque
self.returnedTorque = returnedTorque
@@ -108,6 +119,11 @@ function Engine:forwardStep()
self.angularVelocity = self.angularVelocity + finalTorque / inertiaSum * TICK_INTERVAL
self.angularVelocity = math.max(self.angularVelocity, 0)
net.start("ENGINE_FULLRPM")
net.writeUInt(self:getRPM(), 16)
net.writeFloat(self.masterThrottle)
net.send(nil, true)
end
return Engine

View File

@@ -7,6 +7,8 @@ local Gearbox = class('Gearbox', PowertrainComponent)
function Gearbox:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT then return end
self.wireInputs = {
Upshift = 'number',
Downshift = 'number'
@@ -24,6 +26,8 @@ function Gearbox:initialize(vehicle, name, config)
end
function Gearbox:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self)
wire.ports.Gearbox_RPM = self:getRPM()
wire.ports.Gearbox_Torque = self.torque
wire.ports.Gearbox_Ratio = self.ratio

View File

@@ -10,6 +10,8 @@ local ManualGearbox = class('ManualGearbox', BaseGearbox)
function ManualGearbox:initialize(vehicle, name, config)
BaseGearbox.initialize(self, vehicle, name, config)
if CLIENT then return end
table.merge(self.wireOutputs, {
Gearbox_Gear = 'number'
})

View File

@@ -1,9 +1,11 @@
--@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)
@@ -15,12 +17,29 @@ function PowertrainComponent:initialize(vehicle, name, config)
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()
@@ -67,4 +86,12 @@ function PowertrainComponent:forwardStep(torque, inertia)
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

View File

@@ -16,12 +16,40 @@ local DEBUG = false
function Wheel:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT and self.DEBUG then
local scale = 0.1
local font = render.createFont("Roboto", 256, 400, true)
local mat = render.createMaterial('models/debug/debugwhite')
hook.add("PostDrawTranslucentRenderables", "DEBUG_RENDER_" .. self.name, function()
if next(self.DEBUG_DATA) == nil then return end
if not isValid(self.DEBUG_DATA.entity) then return end
local pos = self.DEBUG_DATA.entity:getPos()
render.setMaterial(mat)
render.setColor(Color(255, 0, 0, 200))
render.draw3DBeam(pos, pos + (16 * self.DEBUG_DATA.forward), 1, 0, 0)
render.setColor(Color(0, 255, 0, 255))
render.draw3DBeam(pos, pos + (16 * self.DEBUG_DATA.right), 1, 0, 0)
render.setColor(Color(0, 0, 255, 200))
render.draw3DBeam(pos, pos + (16 * self.DEBUG_DATA.up), 1, 0, 0)
end)
if player() == owner() then
enableHud(nil, true)
end
return
end
self.steerLock = config.SteerLock or 0
self.brakePower = config.BrakePower or 0
self.handbrakePower = config.HandbrakePower or 0
self.rotationAxle = config.RotationAxle or Angle(0, 0, 1)
self.wireInputs = {
[self.name] = 'entity'
}
@@ -50,14 +78,8 @@ function Wheel:initialize(vehicle, name, config)
self.holo = self:createHolo(self.entity)
if DEBUG then
self.debugHoloF:setPos(self.entity:getPos())
self.debugHoloR:setPos(self.entity:getPos())
self.debugHoloU:setPos(self.entity:getPos())
self.debugHoloF:setParent(self.entity)
self.debugHoloR:setParent(self.entity)
self.debugHoloU:setParent(self.entity)
if self.DEBUG then
self.DEBUG_DATA.entity = self.entity
end
if not config.Radius then
@@ -66,20 +88,6 @@ function Wheel:initialize(vehicle, name, config)
end
end)
if DEBUG then
self.debugHoloF = holograms.create(Vector(), Angle(), 'models/sprops/rectangles_thin/size_0/rect_1_5x48x1_5.mdl')
self.debugHoloR = holograms.create(Vector(), Angle(), 'models/sprops/rectangles_thin/size_0/rect_1_5x48x1_5.mdl')
self.debugHoloU = holograms.create(Vector(), Angle(), 'models/sprops/rectangles_thin/size_0/rect_1_5x48x1_5.mdl')
self.debugHoloF:setMaterial('models/debug/debugwhite')
self.debugHoloR:setMaterial('models/debug/debugwhite')
self.debugHoloU:setMaterial('models/debug/debugwhite')
self.debugHoloF:setColor(Color(255, 0, 0))
self.debugHoloR:setColor(Color(0, 255, 0))
self.debugHoloU:setColor(Color(0, 0, 255))
end
self.steerVelocity = 0
end
@@ -110,6 +118,8 @@ function Wheel:createHolo(entity)
end
function Wheel:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self)
wire.ports[string.format('%s_RPM', self.name)] = self.customWheel:getRPM()
wire.ports[string.format('%s_Mz', self.name)] = self.customWheel.mz
wire.ports[string.format('%s_Fz', self.name)] = self.customWheel.load
@@ -145,11 +155,11 @@ function Wheel:forwardStep(torque, inertia)
end
if isValid(self.holo) then
if DEBUG then
self.debugHoloF:setAngles(self.customWheel.forward:getAngle())
self.debugHoloR:setAngles(self.customWheel.right:getAngle())
self.debugHoloU:setAngles(self.customWheel.up:getAngle())
end
if self.DEBUG then
self.DEBUG_DATA.forward = self.customWheel.forward
self.DEBUG_DATA.right = self.customWheel.right
self.DEBUG_DATA.up = self.customWheel.up
end
local entityAngles = self.entity:getAngles()