Skip to content

Commit

Permalink
staticify CustomCarManager, get loco audio working
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Aug 15, 2021
1 parent dfe7581 commit 4a9131f
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 62 deletions.
10 changes: 6 additions & 4 deletions CCL_GameScripts/ComponentInitSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public object CreateRealComponent( Func<string, Type> findTypeFunc, Action<strin
var proxies = sourceField.GetCustomAttributes().OfType<ProxyFieldAttribute>();
foreach( var proxy in proxies )
{
FieldInfo targetField = targetType.GetField(proxy.TargetName);
string targetName = proxy.TargetName ?? sourceField.Name;
FieldInfo targetField = targetType.GetField(targetName);

if( targetField != null )
{
Type assignValueType;
Expand Down Expand Up @@ -86,12 +88,12 @@ public object CreateRealComponent( Func<string, Type> findTypeFunc, Action<strin
}
else
{
logAction($"Proxy {targetType.Name}.{proxy.TargetName} is not assignable from {sourceType.Name}.{sourceField.Name}");
logAction($"Proxy {targetType.Name}.{targetName} is not assignable from {sourceType.Name}.{sourceField.Name}");
}
}
else
{
logAction($"From spec type {sourceType.Name} - target {proxy.TargetName} not found on {targetType.Name}");
logAction($"From spec type {sourceType.Name} - target {targetName} not found on {targetType.Name}");
}
}
}
Expand All @@ -110,7 +112,7 @@ public class ProxyFieldAttribute : Attribute
{
public string TargetName;

public ProxyFieldAttribute( string proxyField )
public ProxyFieldAttribute( string proxyField = null )
{
TargetName = proxyField;
}
Expand Down
11 changes: 11 additions & 0 deletions CCL_GameScripts/SimParamsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ public enum LocoRequiredLicense
Steam = 3,
}

public enum LocoAudioBasis
{
None = 0,
DE2 = 1,
DE6 = 2,
Steam = 3,
}

public abstract class SimParamsBase : MonoBehaviour
{
[HideInInspector]
public abstract LocoParamsType SimType { get; }

[HideInInspector]
public abstract LocoAudioBasis AudioType { get; }

// default values from diesel
[Header("Basic")]
public LocoRequiredLicense RequiredLicense = LocoRequiredLicense.None;
Expand Down
4 changes: 4 additions & 0 deletions CCL_GameScripts/SimParamsDiesel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class SimParamsDiesel : SimParamsBase
{
public override LocoParamsType SimType => LocoParamsType.DieselElectric;

[Header("Audio")]
public bool UseBigDieselAudio = false;
public override LocoAudioBasis AudioType => UseBigDieselAudio ? LocoAudioBasis.DE6 : LocoAudioBasis.DE2;

[Header("Throttle")]
public float ThrottleUpRate = 2f;
public float ThrottleDownRate = 2f;
Expand Down
111 changes: 111 additions & 0 deletions DVCustomCarLoader/CarTypeInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using CCL_GameScripts;
using DV.Logic.Job;
using DVCustomCarLoader.LocoComponents;
using HarmonyLib;
using UnityEngine;

Expand Down Expand Up @@ -114,6 +115,102 @@ private static void InjectCarTypesData( CustomCar car )

CarTypeToContainerType.Add(car.CarType, car.CargoClass);
}

#region Audio Pooling

private static GameObject GetPooledAudioPrefab( TrainComponentPool componentPool, TrainCarType carType )
{
foreach( var poolData in componentPool.audioPoolReferences.poolData )
{
if( poolData.trainCarType == carType )
{
return poolData.audioPrefab;
}
}

return null;
}

private static GameObject CopyAudioPrefab<TAudio>( GameObject sourcePrefab )
where TAudio : CustomLocoAudio
{
GameObject newFab = UnityEngine.Object.Instantiate(sourcePrefab, null);
newFab.SetActive(false);
UnityEngine.Object.DontDestroyOnLoad(newFab);

var origAudio = newFab.GetComponentInChildren<LocoTrainAudio>();
if( origAudio )
{
Main.Log($"Adding audio {typeof(TAudio).Name}");
TAudio newAudio = origAudio.gameObject.AddComponent<TAudio>();
newAudio.PullSettingsFromOtherAudio(origAudio);
UnityEngine.Object.DestroyImmediate(origAudio);

// grab extra components
newAudio.carFrictionSound = newFab.GetComponentInChildren<CarFrictionSound>(true);
newAudio.carCollisionSounds = newFab.GetComponentInChildren<CarCollisionSounds>(true);
newAudio.trainDerailAudio = newFab.GetComponentInChildren<TrainDerailAudio>(true);
}
else
{
Main.Warning($"Couldn't find LocoTrainAudio on prefab {sourcePrefab.name}");
}

return newFab;
}

public static void InjectLocoAudioToPool( CustomCar car, TrainComponentPool componentPool )
{
const int LOCO_POOL_SIZE = 10;

GameObject sourcePrefab;
GameObject newPrefab;

switch( car.LocoAudioType )
{
case LocoAudioBasis.DE2:
sourcePrefab = GetPooledAudioPrefab(componentPool, TrainCarType.LocoShunter);
if( sourcePrefab )
{
newPrefab = CopyAudioPrefab<CustomLocoAudioDiesel>(sourcePrefab);

var newPoolData = new AudioPoolReferences.AudioPoolData()
{
trainCarType = car.CarType,
audioPrefab = newPrefab,
poolSize = LOCO_POOL_SIZE
};

componentPool.audioPoolReferences.poolData.Add(newPoolData);
}
else Main.Warning("Couldn't find shunter pooled audio");
break;

case LocoAudioBasis.DE6:
sourcePrefab = GetPooledAudioPrefab(componentPool, TrainCarType.LocoDiesel);
if( sourcePrefab )
{
newPrefab = CopyAudioPrefab<CustomLocoAudioDiesel>(sourcePrefab);

var newPoolData = new AudioPoolReferences.AudioPoolData()
{
trainCarType = car.CarType,
audioPrefab = newPrefab,
poolSize = LOCO_POOL_SIZE
};

componentPool.audioPoolReferences.poolData.Add(newPoolData);
}
else Main.Warning("Couldn't find DE6 pooled audio");
break;

case LocoAudioBasis.Steam:
default:
break;
}
}

#endregion
}

[HarmonyPatch(typeof(CarTypes), nameof(CarTypes.GetCarPrefab))]
Expand Down Expand Up @@ -208,4 +305,18 @@ public static bool IsDieselLocomotive( TrainCarType carType, ref bool __result )
return true;
}
}

[HarmonyPatch(typeof(TrainComponentPool), "Awake")]
public static class TrainComponentPool_Awake_Patch
{
public static void Prefix( TrainComponentPool __instance )
{
Main.Log("Injecting custom cars into component pool");

foreach( CustomCar car in CustomCarManager.CustomCarTypes )
{
CarTypeInjector.InjectLocoAudioToPool(car, __instance);
}
}
}
}
2 changes: 1 addition & 1 deletion DVCustomCarLoader/CommsRadioController_Awake_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void Postfix(CommsRadioController __instance, ref List<ICommsRadioMode> _
try
{

if (Main.CustomCarManagerInstance.CustomCarsToSpawn.Count <= 0)
if( CustomCarManager.CustomCarTypes.Count <= 0 )
return;

//Add our spawner to the comms radio
Expand Down
10 changes: 5 additions & 5 deletions DVCustomCarLoader/CommsRadioCustomCarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void OnUse()
switch (state)
{
case State.EnterSpawnMode:
SetCarToSpawn(Main.CustomCarManagerInstance.CustomCarsToSpawn[selectedCarTypeIndex]);
SetCarToSpawn(CustomCarManager.CustomCarTypes[selectedCarTypeIndex]);
SetState(State.PickCar);
break;
case State.PickCar:
Expand Down Expand Up @@ -240,9 +240,9 @@ public bool ButtonACustomAction()
{
case State.PickCar:
selectedCarTypeIndex = selectedCarTypeIndex <= 0
? Main.CustomCarManagerInstance.CustomCarsToSpawn.Count - 1
? CustomCarManager.CustomCarTypes.Count - 1
: selectedCarTypeIndex - 1;
SetCarToSpawn(Main.CustomCarManagerInstance.CustomCarsToSpawn[selectedCarTypeIndex]);
SetCarToSpawn(CustomCarManager.CustomCarTypes[selectedCarTypeIndex]);
return true;
case State.PickDestination:
if (!canSpawnAtPoint) return false;
Expand All @@ -260,8 +260,8 @@ public bool ButtonBCustomAction()
switch (state)
{
case State.PickCar:
selectedCarTypeIndex = (selectedCarTypeIndex + 1) % Main.CustomCarManagerInstance.CustomCarsToSpawn.Count;
SetCarToSpawn(Main.CustomCarManagerInstance.CustomCarsToSpawn[selectedCarTypeIndex]);
selectedCarTypeIndex = (selectedCarTypeIndex + 1) % CustomCarManager.CustomCarTypes.Count;
SetCarToSpawn(CustomCarManager.CustomCarTypes[selectedCarTypeIndex]);
return true;
case State.PickDestination:
if (!canSpawnAtPoint) return false;
Expand Down
20 changes: 11 additions & 9 deletions DVCustomCarLoader/CustomCar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public class CustomCar
//Couplers
public Vector3 FrontCouplerPosition;
public Vector3 RearCouplerPosition;

public LocoParamsType LocoType { get; protected set; }
public LocoRequiredLicense RequiredLicense { get; protected set; }
public CargoContainerType CargoClass { get; protected set; }

public LocoParamsType LocoType { get; protected set; } = LocoParamsType.None;
public LocoAudioBasis LocoAudioType { get; protected set; } = LocoAudioBasis.None;
public LocoRequiredLicense RequiredLicense { get; protected set; } = LocoRequiredLicense.None;
public CargoContainerType CargoClass { get; protected set; } = CargoContainerType.None;

public bool FinalizePrefab()
{
Expand Down Expand Up @@ -362,13 +363,14 @@ public bool FinalizePrefab()
newCar.wheelRadius = baseCar.wheelRadius;
}

newCar.carType = BaseCarType;
newCar.carType = CarType;

var simParams = newFab.GetComponent<SimParamsBase>();
if( simParams )
{
LocoComponentManager.AddLocoSimulation(newFab, simParams);
LocoType = simParams.SimType;
LocoAudioType = simParams.AudioType;
RequiredLicense = simParams.RequiredLicense;

if( carSetup.InteriorPrefab )
Expand Down Expand Up @@ -428,8 +430,8 @@ public TrainCar SpawnCar( RailTrack track, Vector3 position, Vector3 forward, bo
spawnedCar.InitializeNewLogicCar();
spawnedCar.SetTrack(track, position, forward);

spawnedCar.OnDestroyCar += Main.CustomCarManagerInstance.DeregisterCar;
Main.CustomCarManagerInstance.RegisterSpawnedCar(spawnedCar, identifier);
spawnedCar.OnDestroyCar += CustomCarManager.DeregisterCar;
CustomCarManager.RegisterSpawnedCar(spawnedCar, identifier);

RaiseCarSpawned(spawnedCar);
return spawnedCar;
Expand Down Expand Up @@ -474,8 +476,8 @@ public TrainCar SpawnLoadedCar(
spawnedCar.rearCoupler.forceCoupleStateOnLoad = true;
spawnedCar.rearCoupler.loadedCoupledState = couplerRCoupled;

spawnedCar.OnDestroyCar += Main.CustomCarManagerInstance.DeregisterCar;
Main.CustomCarManagerInstance.RegisterSpawnedCar(spawnedCar, identifier);
spawnedCar.OnDestroyCar += CustomCarManager.DeregisterCar;
CustomCarManager.RegisterSpawnedCar(spawnedCar, identifier);

RaiseCarSpawned(spawnedCar);
return spawnedCar;
Expand Down
21 changes: 9 additions & 12 deletions DVCustomCarLoader/CustomCarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,34 @@

namespace DVCustomCarLoader
{
public class CustomCarManager : MonoBehaviour
public static class CustomCarManager
{
public List<CustomCar> CustomCarsToSpawn;
public static List<CustomCar> CustomCarTypes = new List<CustomCar>();

private readonly Dictionary<TrainCar, string> SpawnedCustomCarIds = new Dictionary<TrainCar, string>();
private static readonly Dictionary<TrainCar, string> SpawnedCustomCarIds = new Dictionary<TrainCar, string>();

public bool IsRegisteredCustomCar( TrainCar trainCar )
public static bool IsRegisteredCustomCar( TrainCar trainCar )
{
return SpawnedCustomCarIds.ContainsKey(trainCar);
}

public bool TryGetCustomCarId( TrainCar trainCar, out string id )
public static bool TryGetCustomCarId( TrainCar trainCar, out string id )
{
return SpawnedCustomCarIds.TryGetValue(trainCar, out id);
}

public void RegisterSpawnedCar( TrainCar car, string identifier )
public static void RegisterSpawnedCar( TrainCar car, string identifier )
{
SpawnedCustomCarIds[car] = identifier;
}

public void DeregisterCar( TrainCar car )
public static void DeregisterCar( TrainCar car )
{
SpawnedCustomCarIds.Remove(car);
}

public void Setup()
public static void Setup()
{

CustomCarsToSpawn = new List<CustomCar>();

//Load all json files
string bundlePath = Path.Combine(Main.ModEntry.Path, "Cars");

Expand Down Expand Up @@ -75,7 +72,7 @@ public void Setup()

if( newCar != null )
{
CustomCarsToSpawn.Add(newCar);
CustomCarTypes.Add(newCar);
Main.ModEntry.Logger.Log($"Successfully added new car to spawn list: {newCar.identifier}");
}
else
Expand Down
Loading

0 comments on commit 4a9131f

Please sign in to comment.