quantum-space-buddies/QSB/SatelliteSync/SatelliteProjectorManager.cs

115 lines
3.0 KiB
C#
Raw Normal View History

2022-02-01 08:58:52 +00:00
using QSB.Utility;
using QSB.WorldSync;
2021-11-25 15:32:34 +00:00
using System.Linq;
2021-11-03 09:18:52 +00:00
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.SatelliteSync;
2023-07-28 18:30:57 +00:00
public class SatelliteProjectorManager : MonoBehaviour, IAddComponentOnStart
2021-11-03 09:18:52 +00:00
{
2022-03-03 03:46:33 +00:00
public static SatelliteProjectorManager Instance { get; private set; }
2022-03-03 03:46:33 +00:00
public SatelliteSnapshotController Projector { get; private set; }
public RenderTexture SatelliteCameraSnapshot
{
get
2021-11-30 19:17:26 +00:00
{
2022-03-03 03:46:33 +00:00
if (_satelliteCameraSnapshot == null)
2021-11-30 19:17:26 +00:00
{
2022-03-03 03:46:33 +00:00
_satelliteCameraSnapshot = new RenderTexture(512, 512, 16)
2021-11-30 19:17:26 +00:00
{
2022-03-03 03:46:33 +00:00
name = "SatelliteCameraSnapshot",
hideFlags = HideFlags.HideAndDontSave
};
_satelliteCameraSnapshot.Create();
2021-11-30 19:17:26 +00:00
}
2022-03-03 03:46:33 +00:00
return _satelliteCameraSnapshot;
2021-11-30 19:17:26 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-11-30 19:17:26 +00:00
2022-03-03 03:46:33 +00:00
private static RenderTexture _satelliteCameraSnapshot;
2021-11-03 09:18:52 +00:00
2022-03-03 03:46:33 +00:00
private void Awake()
{
Instance = this;
QSBSceneManager.OnUniverseSceneLoaded += OnSceneLoaded;
QSBNetworkManager.singleton.OnClientConnected += OnConnected;
}
2021-11-03 09:18:52 +00:00
2022-03-03 03:46:33 +00:00
public void OnDestroy() => QSBSceneManager.OnUniverseSceneLoaded -= OnSceneLoaded;
2021-11-03 09:18:52 +00:00
2022-03-03 03:46:33 +00:00
public void OnConnected()
{
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
2021-11-30 19:17:26 +00:00
{
2022-03-03 03:46:33 +00:00
Projector._snapshotTexture = SatelliteCameraSnapshot;
Projector._satelliteCamera.targetTexture = Projector._snapshotTexture;
2021-11-30 19:17:26 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-11-30 19:17:26 +00:00
2022-03-03 03:46:33 +00:00
private void OnSceneLoaded(OWScene oldScene, OWScene newScene)
{
if (newScene == OWScene.SolarSystem)
2021-11-03 09:18:52 +00:00
{
2022-05-03 07:48:24 +00:00
Projector = QSBWorldSync.GetUnityObject<SatelliteSnapshotController>();
2022-03-03 03:46:33 +00:00
Projector._loopingSource.spatialBlend = 1f;
Projector._oneShotSource.spatialBlend = 1f;
2021-11-30 19:17:26 +00:00
2022-03-03 03:46:33 +00:00
Projector._snapshotTexture = SatelliteCameraSnapshot;
Projector._satelliteCamera.targetTexture = Projector._snapshotTexture;
2021-11-03 09:18:52 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-11-03 09:18:52 +00:00
2022-03-03 03:46:33 +00:00
public void RemoteEnter()
{
Projector.enabled = true;
Projector._interactVolume.DisableInteraction();
2022-03-03 03:46:33 +00:00
if (Projector._showSplashTexture)
{
Projector._splashObject.SetActive(false);
Projector._diagramObject.SetActive(true);
Projector._projectionScreen.gameObject.SetActive(false);
2021-11-03 09:18:52 +00:00
}
2022-03-03 03:46:33 +00:00
if (Projector._fadeLight != null)
2021-11-03 09:18:52 +00:00
{
2022-03-03 03:46:33 +00:00
Projector._fadeLight.StartFade(0f, 2f);
}
2021-11-03 09:18:52 +00:00
2022-03-03 03:46:33 +00:00
var audioClip = Projector._oneShotSource.PlayOneShot(AudioType.TH_ProjectorActivate);
Projector._loopingSource.FadeIn(audioClip.length);
}
2021-11-03 09:18:52 +00:00
2022-03-03 03:46:33 +00:00
public void RemoteExit()
{
Projector.enabled = false;
Projector._interactVolume.EnableInteraction();
Projector._interactVolume.ResetInteraction();
2021-11-03 09:18:52 +00:00
2022-03-03 03:46:33 +00:00
if (Projector._showSplashTexture)
{
Projector._splashObject.SetActive(true);
Projector._diagramObject.SetActive(false);
Projector._projectionScreen.gameObject.SetActive(false);
2021-11-03 09:18:52 +00:00
}
2022-03-03 03:46:33 +00:00
if (Projector._fadeLight != null)
2021-11-03 09:18:52 +00:00
{
2022-03-03 03:46:33 +00:00
Projector._fadeLight.StartFade(Projector._initLightIntensity, 2f);
}
2022-03-03 03:46:33 +00:00
var audioClip = Projector._oneShotSource.PlayOneShot(AudioType.TH_ProjectorStop);
Projector._loopingSource.FadeOut(audioClip.length);
}
public void RemoteTakeSnapshot(bool forward)
{
Projector._satelliteCamera.transform.localEulerAngles = forward
? Projector._initCamLocalRot
: Projector._initCamLocalRot + new Vector3(0f, 180f, 0f);
Projector.RenderSnapshot();
2021-11-03 09:18:52 +00:00
}
}