quantum-space-buddies/QSB/QuantumSync/Events/MoonStateChangeEvent.cs

75 lines
3.1 KiB
C#
Raw Normal View History

2021-01-03 09:52:34 +00:00
using OWML.Utils;
using QSB.Events;
using System.Linq;
2021-01-18 12:33:07 +00:00
using System.Reflection;
2021-01-03 09:52:34 +00:00
using UnityEngine;
namespace QSB.QuantumSync.Events
{
public class MoonStateChangeEvent : QSBEvent<MoonStateChangeMessage>
{
public override QSB.Events.EventType Type => QSB.Events.EventType.MoonStateChange;
public override void SetupListener() => GlobalMessenger<int, Vector3, int>.AddListener(EventNames.QSBMoonStateChange, Handler);
public override void CloseListener() => GlobalMessenger<int, Vector3, int>.RemoveListener(EventNames.QSBMoonStateChange, Handler);
private void Handler(int stateIndex, Vector3 onUnitSphere, int orbitAngle) => SendEvent(CreateMessage(stateIndex, onUnitSphere, orbitAngle));
private MoonStateChangeMessage CreateMessage(int stateIndex, Vector3 onUnitSphere, int orbitAngle) => new MoonStateChangeMessage
{
AboutId = LocalPlayerId,
StateIndex = stateIndex,
OnUnitSphere = onUnitSphere,
OrbitAngle = orbitAngle
};
public override void OnReceiveRemote(bool server, MoonStateChangeMessage message)
{
if (!QSBCore.HasWokenUp)
{
return;
}
var moon = Locator.GetQuantumMoon();
2021-01-18 12:33:07 +00:00
var wasPlayerEntangled = moon.IsPlayerEntangled();
var location = new RelativeLocationData(Locator.GetPlayerTransform().GetComponent<OWRigidbody>(), moon.transform);
2021-01-03 09:52:34 +00:00
var moonBody = moon.GetValue<OWRigidbody>("_moonBody");
var constantFoceDetector = (ConstantForceDetector)moonBody.GetAttachedForceDetector();
var orbits = moon.GetValue<QuantumOrbit[]>("_orbits");
var orbit = orbits.First(x => x.GetStateIndex() == message.StateIndex);
var orbitRadius = orbit.GetOrbitRadius();
var owRigidbody = orbit.GetAttachedOWRigidbody();
var position = (message.OnUnitSphere * orbitRadius) + owRigidbody.GetWorldCenterOfMass();
moonBody.transform.position = position;
if (!Physics.autoSyncTransforms)
{
Physics.SyncTransforms();
}
constantFoceDetector.AddConstantVolume(owRigidbody.GetAttachedGravityVolume(), true, true);
moonBody.SetVelocity(OWPhysics.CalculateOrbitVelocity(owRigidbody, moonBody, message.OrbitAngle) + owRigidbody.GetVelocity());
moon.SetValue("_stateIndex", message.StateIndex);
2021-01-18 12:33:07 +00:00
2021-02-14 20:39:40 +00:00
if (moon.IsPlayerInside())
{
moon.GetType().GetMethod("SetSurfaceState", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(moon, new object[] { message.StateIndex });
}
else
{
moon.GetType().GetMethod("SetSurfaceState", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(moon, new object[] { -1 });
moon.GetValue<AudioSignal>("_quantumSignal").SetSignalActivation(message.StateIndex != 5, 2f);
}
moon.GetValue<ReferenceFrameVolume>("_referenceFrameVolume").gameObject.SetActive(message.StateIndex != 5);
moonBody.SetIsTargetable(message.StateIndex != 5);
foreach (var obj in moon.GetValue<GameObject[]>("_deactivateAtEye"))
{
obj.SetActive(message.StateIndex != 5);
}
GlobalMessenger<OWRigidbody>.FireEvent("QuantumMoonChangeState", moonBody);
2021-01-18 12:33:07 +00:00
if (wasPlayerEntangled)
{
Locator.GetPlayerTransform().GetComponent<OWRigidbody>().MoveToRelativeLocation(location, moon.transform);
}
2021-01-03 09:52:34 +00:00
}
}
}