Skip to content

Commit

Permalink
lights, power, event fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Feb 15, 2022
1 parent d870a11 commit 0771014
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 46 deletions.
10 changes: 9 additions & 1 deletion CCL_GameScripts/Effects/ControllableLightSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace CCL_GameScripts.Effects
{
public class ControllableLightSetup : IndicatorSetupBase
public class ControllableLightSetup : ComponentInitSpec
{
public override string TargetTypeName => "DVCustomCarLoader.Effects.ControllableLight";
public override bool DestroyAfterCreation => true;
Expand All @@ -20,5 +20,13 @@ public class ControllableLightSetup : IndicatorSetupBase

[ProxyField]
public float Lag = 0.05f;

[Header("Binding")]
[ProxyField]
public SimEventType OutputBinding;
[ProxyField]
public float MinValue = 0;
[ProxyField]
public float MaxValue = 1;
}
}
50 changes: 31 additions & 19 deletions DVCustomCarLoader/Effects/ControllableLight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace DVCustomCarLoader.Effects
{
public class ControllableLight : Indicator
public class ControllableLight : MonoBehaviour, ILocoEventAcceptor
{
public Light[] Lights;
public float MinLevel = 0;
Expand All @@ -17,44 +17,56 @@ public class ControllableLight : Indicator
protected float SmoothingVelo = 0;
protected float TargetLevel = 0;

public SimEventType OutputBinding;
public SimEventType[] EventTypes => new[] { OutputBinding };
public float MinValue = 0;
public float MaxValue = 1;

private float MapInputValue(float input)
{
return (input - MinValue) / (MaxValue - MinValue);
}

public void ApplyNormalizedLevel(float newLevel)
{
float mapped = Mathf.Lerp(MinLevel, MaxLevel, newLevel);
float mapped = Mathf.LerpUnclamped(MinLevel, MaxLevel, newLevel);

foreach (Light l in Lights)
{
l.intensity = mapped;
SmoothedLevel = mapped;
}
}

protected override void Start()
protected void Start()
{
base.Start();
ApplyNormalizedLevel(MinLevel);
TargetLevel = 0;
ApplyNormalizedLevel(0);
}

protected override void OnValueSet()
protected void Update()
{
if (!Application.isPlaying)
if (Lag > 0)
{
return;
SmoothedLevel = Mathf.SmoothDamp(SmoothedLevel, TargetLevel, ref SmoothingVelo, Lag);
ApplyNormalizedLevel(SmoothedLevel);
}
}

TargetLevel = GetNormalizedValue(true);

if (Lag == 0)
public void HandleEvent(LocoEventInfo eventInfo)
{
if (eventInfo.NewValue is float value)
{
ApplyNormalizedLevel(TargetLevel);
TargetLevel = MapInputValue(value);
}
}
else if (eventInfo.NewValue is bool onOff)
{
TargetLevel = onOff ? 1 : 0;
}
else return;

protected void Update()
{
if (Lag > 0)
if (Lag == 0)
{
float smoothedLevel = Mathf.SmoothDamp(SmoothedLevel, TargetLevel, ref SmoothingVelo, Lag);
ApplyNormalizedLevel(smoothedLevel);
ApplyNormalizedLevel(TargetLevel);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion DVCustomCarLoader/Effects/SteamParticlesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected IEnumerator UpdateSmokeParticles(float period)

var main = chimneyParticles.main;
float burnRatePercent = controller.FuelConsumptionRate / controller.MaxFuelConsumptionRate;
Main.LogVerbose($"Chimney burn rate: {burnRatePercent} ({controller.FuelConsumptionRate}/{controller.MaxFuelConsumptionRate})");
//Main.LogVerbose($"Chimney burn rate: {burnRatePercent} ({controller.FuelConsumptionRate}/{controller.MaxFuelConsumptionRate})");

if ((burnRatePercent == 0) && chimneyParticles.isPlaying)
{
Expand Down
36 changes: 23 additions & 13 deletions DVCustomCarLoader/LocoComponents/CustomFuseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class CustomFuseController : MonoBehaviour, ILocoEventProvider, ICabContr
protected const float LATE_INIT_DELAY = 0.5f;
protected const float MAIN_BREAKER_DELAY = 0.2f;

protected bool StartupComplete = false;

protected bool AreAllSideFusesOn()
{
return SideFuses.TrueForAll(fuse => fuse.Value > SWITCH_THRESHOLD);
Expand All @@ -38,16 +40,7 @@ public void SetMasterPower( bool on )
}

MainFuse.Value = relayPos;

EventManager.Dispatch(this, SimEventType.PowerOn, on);
if( on )
{
TryStarter();
}
else
{
KillEngine();
}
SetLocoPowerState(on);
}

public void TryStarter()
Expand All @@ -66,6 +59,12 @@ public void KillEngine()
locoController.EngineRunning = false;
}

protected void SetLocoPowerState(bool newState)
{
EventManager.Dispatch(this, SimEventType.PowerOn, newState);
locoController.MasterPower = newState;
}

public void Start()
{
var car = TrainCar.Resolve(gameObject);
Expand All @@ -82,13 +81,15 @@ public void Start()
return;
}

StartupComplete = false;
StartCoroutine(DelayedEnable());
}

private IEnumerator DelayedEnable()
{
yield return WaitFor.SecondsRealtime(LATE_INIT_DELAY);
SetMasterPower(locoController.EngineRunning);
StartupComplete = true;
yield break;
}

Expand All @@ -97,7 +98,7 @@ private IEnumerator DelayedMainFuseOff()
yield return WaitFor.SecondsRealtime(MAIN_BREAKER_DELAY);
if( !AreAllSideFusesOn() )
{
EventManager.Dispatch(this, SimEventType.PowerOn, false);
SetLocoPowerState(false);
MainFuse.Value = 0;
}

Expand All @@ -110,6 +111,7 @@ private IEnumerator DelayedMainFuseOff()

protected void OnSideFuseChanged( float newVal )
{
if (!StartupComplete) return;
if( newVal <= SWITCH_THRESHOLD )
{
if( locoController.EngineRunning ) KillEngine();
Expand All @@ -122,6 +124,7 @@ protected void OnSideFuseChanged( float newVal )

protected void OnMainFuseChanged( float newVal )
{
if (!StartupComplete) return;
bool nowOn = (newVal > SWITCH_THRESHOLD);

if( nowOn )
Expand All @@ -134,18 +137,19 @@ protected void OnMainFuseChanged( float newVal )
}
return;
}
EventManager.Dispatch(this, SimEventType.PowerOn, nowOn);
SetLocoPowerState(nowOn);
}
else
{
// turned off
EventManager.Dispatch(this, SimEventType.PowerOn, nowOn);
SetLocoPowerState(nowOn);
KillEngine();
}
}

protected void OnStarterChanged( float newVal )
{
if (!StartupComplete) return;
if( (newVal > SWITCH_THRESHOLD) && !locoController.EngineRunning )
{
TryStarter();
Expand All @@ -159,6 +163,7 @@ protected void OnStarterChanged( float newVal )

protected void OnEStopChanged( float newVal )
{
if (!StartupComplete) return;
if( (newVal > SWITCH_THRESHOLD) && locoController.EngineRunning )
{
KillEngine();
Expand Down Expand Up @@ -200,5 +205,10 @@ public void RegisterControl( CabInputRelay inputRelay )
}

#endregion

public void ForceDispatchAll()
{
if (locoController != null) EventManager.Dispatch(this, SimEventType.PowerOn, locoController.MasterPower);
}
}
}
34 changes: 30 additions & 4 deletions DVCustomCarLoader/LocoComponents/CustomLocoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public void SetReverserFromCab( float position )
}
public float GetReverserCabPosition() => (reverser + 1f) / 2f;

protected abstract float AccessoryPowerLevel { get; }

// Headlights
protected float _HeadlightControlLevel;
protected float _Headlights;
public float Headlights => _Headlights;

Expand All @@ -48,20 +51,24 @@ public void SetReverserFromCab( float position )

public void SetHeadlight( float value )
{
if( value != Headlights )
if( value != _HeadlightControlLevel )
{
//headlights.SetActive(HeadlightsOn);
_HeadlightControlLevel = value;
value = _HeadlightControlLevel * AccessoryPowerLevel;
EventManager.UpdateValueDispatchOnChange(this, ref _Headlights, value, SimEventType.Headlights);
}
}

// Cab Lights
protected float _CabLightControlLevel;
protected float _CabLights;
public float CabLights => _CabLights;
public void SetCabLight( float value )
{
if (value != CabLights)
if (value != _CabLightControlLevel)
{
_CabLightControlLevel = value;
value = _CabLightControlLevel * AccessoryPowerLevel;
EventManager.UpdateValueDispatchOnChange(this, ref _CabLights, value, SimEventType.CabLights);
}
}
Expand All @@ -84,15 +91,34 @@ public override void Update()
EventManager.UpdateValueDispatchOnChange(this, ref _BrakeResPressure, GetBrakeResPressure(), SimEventType.BrakeReservoir);
EventManager.UpdateValueDispatchOnChange(this, ref _IndependentPressure, GetIndependentPressure(), SimEventType.IndependentPipe);

float light = _HeadlightControlLevel * AccessoryPowerLevel;
EventManager.UpdateValueDispatchOnChange(this, ref _Headlights, light, SimEventType.Headlights);

float cab = _CabLightControlLevel * AccessoryPowerLevel;
EventManager.UpdateValueDispatchOnChange(this, ref _CabLights, cab, SimEventType.CabLights);

float fwdLight = Mathf.Lerp(0, _Headlights, Mathf.InverseLerp(0, 1, reverser));
EventManager.UpdateValueDispatchOnChange(this, ref _ForwardLights, fwdLight, SimEventType.LightsForward);

float revLight = Mathf.Lerp(0, _RearLights, Mathf.InverseLerp(0, -1, reverser));
float revLight = Mathf.Lerp(0, _Headlights, Mathf.InverseLerp(0, -1, reverser));
EventManager.UpdateValueDispatchOnChange(this, ref _RearLights, revLight, SimEventType.LightsReverse);

base.Update();
}

public virtual void ForceDispatchAll()
{
EventManager.Dispatch(this, SimEventType.Speed, Speed);
EventManager.Dispatch(this, SimEventType.BrakePipe, _BrakePipePressure);
EventManager.Dispatch(this, SimEventType.BrakeReservoir, _BrakeResPressure);
EventManager.Dispatch(this, SimEventType.IndependentPipe, _IndependentPressure);

EventManager.Dispatch(this, SimEventType.Headlights, _Headlights);
EventManager.Dispatch(this, SimEventType.CabLights, _CabLights);
EventManager.Dispatch(this, SimEventType.LightsForward, _ForwardLights);
EventManager.Dispatch(this, SimEventType.LightsReverse, _RearLights);
}

#region ICabControlAcceptor

public virtual void RegisterControl( CabInputRelay inputRelay )
Expand Down
7 changes: 7 additions & 0 deletions DVCustomCarLoader/LocoComponents/CustomLocoSimEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ private IEnumerator CheckTankDamageStateRoutine()
}

protected abstract void CheckTankAndDamageLevels();

public virtual void ForceDispatchAll()
{
SandEvent.ForceDispatch();
WheelslipEvent.ForceDispatch();
CoupleEvent.ForceDispatch();
}
}

public abstract class CustomLocoSimEvents<TSim,TDmg> : CustomLocoSimEvents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,16 @@ protected override void CheckTankAndDamageLevels()
EngineDamageChanged.Invoke(newDamageLevel);
}
}

public override void ForceDispatchAll()
{
base.ForceDispatchAll();

FuelEvent.ForceDispatch();
OilEvent.ForceDispatch();
EngineRunningEvent.ForceDispatch();
EngineTempEvent.ForceDispatch();
EngineDamageEvent.ForceDispatch();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public bool EngineRunning
}
}
}
public bool CanEngineStart => !EngineRunning && (FuelLevel > 0);
public bool CanEngineStart => (FuelLevel > 0);
public bool AutoStart => autostart;
public bool MasterPower { get; set; }

protected override float AccessoryPowerLevel =>
MasterPower ? (0.6f + sim.engineRPM.value * 0.4f) : 0;

private float GetFuel() => sim.fuel.value;
private float GetOil() => sim.oil.value;
Expand Down Expand Up @@ -85,6 +88,20 @@ public void SetFanControl(float value)
EventManager.UpdateValueDispatchOnChange(this, ref FanOn, value > 0.5f, SimEventType.Fan);
}

public override void ForceDispatchAll()
{
base.ForceDispatchAll();

EventManager.Dispatch(this, SimEventType.Fuel, _FuelLevel);
EventManager.Dispatch(this, SimEventType.Oil, _OilLevel);
EventManager.Dispatch(this, SimEventType.Sand, _SandLevel);
EventManager.Dispatch(this, SimEventType.EngineTemp, _EngineTemp);
EventManager.Dispatch(this, SimEventType.EngineRPMGauge, _EngineRPMGauge);
EventManager.Dispatch(this, SimEventType.Amperage, _Amperage);

EventManager.Dispatch(this, SimEventType.Fan, FanOn);
}

private void UpdateWatchables()
{
EventManager.UpdateValueDispatchOnChange(this, ref _FuelLevel, GetFuel(), SimEventType.Fuel);
Expand Down Expand Up @@ -177,7 +194,13 @@ public override void SetReverser( float position )
base.SetReverser(position);
}

public override float GetTractionForce()
public override void UpdateHorn(float value)
{
if (BrakeResPressure < 2) value = 0;
base.UpdateHorn(value);
}

public override float GetTractionForce()
{
float num = (sim.engineRPM.value > 0f) ? tractionTorqueCurve.Evaluate(GetSpeedKmH() / sim.engineRPM.value) : 0f;
float num2 = (Mathf.Sign(GetForwardSpeed() * reverser) > 0f) ? num : 1f;
Expand Down
Loading

0 comments on commit 0771014

Please sign in to comment.