muzika-gromche/MuzikaGromche/PoweredLights.cs

80 lines
2.5 KiB
C#

using HarmonyLib;
using System.Collections.Generic;
using UnityEngine;
namespace MuzikaGromche
{
[HarmonyPatch(typeof(RoundManager))]
static class AllPoweredLightsPatch
{
// Vanilla method assumes that GameObjects with tag "PoweredLight" only contain a single Light component each.
// This is, however, not true for certains double-light setups, such as:
// - double PointLight (even though one of them is 'Point' another is 'Spot') inside CeilingFanAnimContainer in BedroomTile/BedroomTileB;
// - MineshaftSpotlight when it has not only `Point Light` but also `IndirectLight` in BirthdayRoomTile;
// - (maybe more?)
// In order to fix that, replace singular GetComponentInChildren<Light> with plural GetComponentsInChildren<Light> version.
[HarmonyPatch(nameof(RoundManager.RefreshLightsList))]
[HarmonyPostfix]
static void OnRefreshLightsList(RoundManager __instance)
{
var behaviour = __instance.gameObject.GetComponent<PoweredLightsBehaviour>() ?? __instance.gameObject.AddComponent<PoweredLightsBehaviour>();
behaviour.Refresh();
}
}
internal class PoweredLightsBehaviour : MonoBehaviour
{
private struct LightData
{
public Light Light;
public Color InitialColor;
}
private readonly List<LightData> AllPoweredLights = [];
public static PoweredLightsBehaviour Instance { get; private set; } = null!;
void Awake()
{
if (Instance == null)
{
Instance = this;
}
}
public void Refresh()
{
AllPoweredLights.Clear();
foreach (var light in RoundManager.Instance.allPoweredLights)
{
AllPoweredLights.Add(new LightData
{
Light = light,
InitialColor = light.color,
});
}
}
public void SetLightColor(SetLightsColorEvent e)
{
foreach (var data in AllPoweredLights)
{
var color = e.GetColor(data.InitialColor);
data.Light.color = color;
}
}
public void ResetLightColor()
{
foreach (var data in AllPoweredLights)
{
var light = data.Light;
if (light != null)
{
light.color = data.InitialColor;
}
}
}
}
}