Skip to content

Commit

Permalink
double cab support, lamp fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Feb 17, 2022
1 parent d3509ba commit 723b700
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 16 deletions.
9 changes: 8 additions & 1 deletion CCL_GameScripts/CabControls/CopiedLamp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected static readonly (BaseTrainCarType, string)[] TargetObjects =
(BaseTrainCarType.LocoShunter, "C dashboard buttons controller/I power_fuse_lamp"),

(BaseTrainCarType.LocoDiesel, "offset/I Indicator lamps/I brake_aux_lamp"),
(BaseTrainCarType.LocoDiesel, "offset/I Indicator lamps/I bell_lamp"),
(BaseTrainCarType.LocoDiesel, "offset/I Indicator lamps/I headlights_lamp"),
(BaseTrainCarType.LocoDiesel, "offset/I Indicator lamps/I button_sand_lamp"),
(BaseTrainCarType.LocoDiesel, "offset/I Indicator lamps/I service_engine_lamp"),
(BaseTrainCarType.LocoDiesel, "offset/I Indicator lamps/I fan_lamp"),
Expand Down Expand Up @@ -59,6 +59,7 @@ protected static readonly (BaseTrainCarType, string)[] TargetObjects =
public CopiedLampType LampType;

[Header("Simulation Binding")]
public bool UseGaugeValue = false;
public SimThresholdDirection ThresholdDirection;
public SimAmount SolidThreshold;
public bool UseBlinkMode;
Expand All @@ -82,6 +83,12 @@ private void OnDrawGizmos()
LampGizmoInfo gizmo = GizmoData[(int)LampType];

Vector3 axis = gizmo.Flipped ? Vector3.back : Vector3.forward;
if (LampType == CopiedLampType.LargeRed)
{
// rotate gizmo 90 around X axis for this lamp
axis = Vector3.up;
}

Vector3 peak = transform.TransformPoint(axis * gizmo.Depth);

Vector3 lastVector = transform.position;
Expand Down
27 changes: 17 additions & 10 deletions DVCustomCarLoader/CustomCar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,26 @@ public bool FinalizePrefab()
//==============================================================================================================
#region Cab

var cabTform = newFab.transform.Find(CarPartNames.CAB_TELEPORT_ROOT);
if( cabTform )
CabinSnapshotSwitcher snapshotSwitcher = null;
for (int i = 0; i < newFab.transform.childCount; i++)
{
var teleportDest = cabTform.gameObject.AddComponent<CabTeleportDestination>();
teleportDest.hoverRenderer = cabTform.GetComponentInChildren<Renderer>();

if (InteriorPrefab)
var cabTform = newFab.transform.GetChild(i);
if (cabTform.name == CarPartNames.CAB_TELEPORT_ROOT)
{
var cabCollider = cabTform.gameObject.GetComponentInChildren<BoxCollider>();
if (cabCollider)
var teleportDest = cabTform.gameObject.AddComponent<CabTeleportDestination>();
teleportDest.hoverRenderer = cabTform.GetComponentInChildren<Renderer>();

if (InteriorPrefab)
{
var snapshotSwitcher = newFab.AddComponent<CabinSnapshotSwitcher>();
snapshotSwitcher.box = cabCollider;
var cabCollider = cabTform.gameObject.GetComponentInChildren<BoxCollider>();
if (cabCollider)
{
if (!snapshotSwitcher)
{
snapshotSwitcher = newFab.AddComponent<CabinSnapshotSwitcher>();
}
snapshotSwitcher.AddCabRegion(cabCollider);
}
}
}
}
Expand Down
62 changes: 59 additions & 3 deletions DVCustomCarLoader/LocoComponents/CabinSnapshotSwitcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Collections;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace DVCustomCarLoader.LocoComponents
Expand All @@ -8,9 +12,43 @@ public class CabinSnapshotSwitcher : InternalExternalSnapshotSwitcher
protected TrainCar AttachedTrainCar;
protected DoorAndWindowTracker OpeningTracker;

protected List<BoxCollider> interiorRegions = new List<BoxCollider>();

protected static readonly MethodInfo PointInOABBMethod;
protected readonly Func<Vector3, BoxCollider, bool> PointInOABB;

static CabinSnapshotSwitcher()
{
PointInOABBMethod = AccessTools.Method(typeof(InternalExternalSnapshotSwitcher), "PointInOABB");
if (PointInOABBMethod == null)
{
Main.Error("CabinSnapshotSwitcher - PointInOABB not found");
}
}

public CabinSnapshotSwitcher()
{
PointInOABB = AccessTools.MethodDelegate<Func<Vector3, BoxCollider, bool>>(PointInOABBMethod, this);
}

public void AddCabRegion(BoxCollider box)
{
if (interiorRegions.Count == 0)
{
this.box = box;
}
interiorRegions.Add(box);
}

protected override void Start()
{
base.Start();
if (!AudioManager.e)
{
Main.Error("AudioManager instance not found. This should not happen. Disabling self.");
enabled = false;
return;
}

AttachedTrainCar = GetComponent<TrainCar>();
if( !AttachedTrainCar )
{
Expand Down Expand Up @@ -47,7 +85,25 @@ protected virtual void OnInteriorLoaded(GameObject interior)

protected override bool InInternalSpace(Transform playerCameraTransform)
{
return base.InInternalSpace(playerCameraTransform) && OpeningTracker && !OpeningTracker.IsAnythingOpen();
return OpeningTracker && !OpeningTracker.IsAnythingOpen() && interiorRegions.Any(r => PointInOABB(playerCameraTransform.position, r));
}
}

[HarmonyPatch(typeof(CabTeleportDestination))]
public static class CabTeleportDestinationPatches
{
[HarmonyPrefix]
[HarmonyPatch("OnEnable")]
public static bool OnEnable(CabTeleportDestination __instance)
{
return __instance.hoverRenderer;
}

[HarmonyPrefix]
[HarmonyPatch("OnDisable")]
public static bool OnDisable(CabTeleportDestination __instance)
{
return __instance.hoverRenderer;
}
}
}
12 changes: 10 additions & 2 deletions DVCustomCarLoader/LocoComponents/DashboardLampRelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class DashboardLampRelay : MonoBehaviour, ILocoEventAcceptor
public SimEventType[] EventTypes => new[] { EventType };

public LampControl Lamp;

public bool UseGaugeValue = false;
public SimThresholdDirection ThresholdDirection;
public LocoSimulationEvents.Amount SolidThreshold;
public bool UseBlinkMode;
Expand All @@ -28,6 +30,7 @@ public static void FinalizeLampSetup(CopiedLamp spec, GameObject newObject)
lampRelay.EventType = spec.SimBinding;
lampRelay.Lamp = realLamp;

lampRelay.UseGaugeValue = spec.UseGaugeValue;
lampRelay.ThresholdDirection = spec.ThresholdDirection;
lampRelay.SolidThreshold = (LocoSimulationEvents.Amount)spec.SolidThreshold;
lampRelay.UseBlinkMode = spec.UseBlinkMode;
Expand All @@ -36,7 +39,11 @@ public static void FinalizeLampSetup(CopiedLamp spec, GameObject newObject)

public void HandleEvent(LocoEventInfo eventInfo)
{
if (eventInfo.NewValue is LocoSimulationEvents.Amount newAmount)
if (UseGaugeValue && eventInfo.NewValue is float newFloat)
{
HandleChange(newFloat > 0);
}
else if (eventInfo.NewValue is LocoSimulationEvents.Amount newAmount)
{
HandleChange(newAmount);
}
Expand Down Expand Up @@ -94,7 +101,8 @@ public void HandleChange(bool newValue)

if( (ThresholdDirection == SimThresholdDirection.Below) ^ newValue )
{
UpdateLampState(UseBlinkMode ? LampControl.LampState.Blinking : LampControl.LampState.On, true);
LampControl.LampState newState = UseBlinkMode ? LampControl.LampState.Blinking : LampControl.LampState.On;
UpdateLampState(newState, true);
}
else
{
Expand Down

0 comments on commit 723b700

Please sign in to comment.