new powertrain смерть чуркам

This commit is contained in:
Valera
2025-06-13 21:16:20 +07:00
parent ba9afba4d1
commit 964b46e1c5
15 changed files with 868 additions and 416 deletions

View File

@@ -0,0 +1,45 @@
using Sandbox;
using System;
namespace VeloX.Powertrain;
[Category( "VeloX/Powertrain/Gearbox" )]
public abstract class BaseGearbox : PowertrainComponent
{
[Property] public override float Inertia { get; set; } = 0.01f;
protected float ratio;
public override float QueryInertia()
{
if ( !HasOutput || ratio == 0 )
return Inertia;
return Inertia + Output.QueryInertia() / MathF.Pow( ratio, 2 );
}
public override float QueryAngularVelocity( float angularVelocity )
{
this.angularVelocity = angularVelocity;
if ( !HasOutput || ratio == 0 )
return angularVelocity;
return Output.QueryAngularVelocity( angularVelocity ) * ratio;
}
public override float ForwardStep( float torque, float inertia )
{
Torque = torque * ratio;
if ( !HasOutput )
return torque;
if ( ratio == 0 )
{
Output.ForwardStep( 0, Inertia * 0.5f );
return torque;
}
return Output.ForwardStep( Torque, (inertia + Inertia) * MathF.Pow( ratio, 2 ) ) / ratio;
}
}

View File

@@ -0,0 +1,52 @@
using Sandbox;
namespace VeloX.Powertrain;
public class ManualGearbox : BaseGearbox
{
[Property] public float[] Ratios { get; set; } = [3.626f, 2.200f, 1.541f, 1.213f, 1.000f, 0.767f];
[Property] public float Reverse { get; set; } = 3.4f;
[Property, InputAction] public string ForwardAction { get; set; } = "Attack1";
[Property, InputAction] public string BackwardAction { get; set; } = "Attack2";
private int gear;
protected void SetGear( int gear )
{
if ( gear < -1 || gear >= Ratios.Length )
return;
this.gear = gear;
RecalcRatio();
}
private void RecalcRatio()
{
if ( gear == -1 )
ratio = -Reverse;
else if ( gear == 0 )
ratio = 0;
else
ratio = Ratios[gear - 1];
}
public void Shift( int dir )
{
SetGear( gear + dir );
}
private void InputResolve()
{
if ( Sandbox.Input.Pressed( ForwardAction ) )
Shift( 1 );
else if ( Sandbox.Input.Pressed( BackwardAction ) )
Shift( -1 );
}
public override float ForwardStep( float torque, float inertia )
{
InputResolve();
return base.ForwardStep( torque, inertia );
}
}