quantum-space-buddies/QSB/QuantumSync/Messages/MoonStateChangeMessage.cs

86 lines
2.5 KiB
C#
Raw Normal View History

using Mirror;
using QSB.Messaging;
2021-12-25 06:13:00 +00:00
using QSB.WorldSync;
using System.Linq;
2021-01-03 09:52:34 +00:00
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.QuantumSync.Messages;
public class MoonStateChangeMessage : QSBMessage
2021-01-03 09:52:34 +00:00
{
2022-03-03 03:46:33 +00:00
private int StateIndex;
private Vector3 OnUnitSphere;
private int OrbitAngle;
public MoonStateChangeMessage(int stateIndex, Vector3 onUnitSphere, int orbitAngle)
{
StateIndex = stateIndex;
OnUnitSphere = onUnitSphere;
OrbitAngle = orbitAngle;
}
public override void Serialize(NetworkWriter writer)
2021-01-03 09:52:34 +00:00
{
2022-03-03 03:46:33 +00:00
base.Serialize(writer);
writer.Write(StateIndex);
writer.Write(OnUnitSphere);
writer.Write(OrbitAngle);
}
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
StateIndex = reader.Read<int>();
OnUnitSphere = reader.ReadVector3();
OrbitAngle = reader.Read<int>();
}
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
2021-01-03 09:52:34 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveRemote()
{
var moon = Locator.GetQuantumMoon();
var wasPlayerEntangled = moon.IsPlayerEntangled();
var location = new RelativeLocationData(Locator.GetPlayerTransform().GetComponent<OWRigidbody>(), moon.transform);
var moonBody = moon._moonBody;
var constantForceDetector = (ConstantForceDetector)moonBody.GetAttachedForceDetector();
var orbits = moon._orbits;
var orbit = orbits.First(x => x.GetStateIndex() == StateIndex);
var orbitRadius = orbit.GetOrbitRadius();
var owRigidbody = orbit.GetAttachedOWRigidbody();
var position = OnUnitSphere * orbitRadius + owRigidbody.GetWorldCenterOfMass();
moonBody.transform.position = position;
if (!Physics.autoSyncTransforms)
2021-01-03 09:52:34 +00:00
{
2022-03-03 03:46:33 +00:00
Physics.SyncTransforms();
2021-01-03 09:52:34 +00:00
}
2022-03-03 03:46:33 +00:00
constantForceDetector.AddConstantVolume(owRigidbody.GetAttachedGravityVolume(), true, true);
moonBody.SetVelocity(OWPhysics.CalculateOrbitVelocity(owRigidbody, moonBody, OrbitAngle) + owRigidbody.GetVelocity());
moon._stateIndex = StateIndex;
if (moon.IsPlayerInside())
2021-01-03 09:52:34 +00:00
{
2022-03-03 03:46:33 +00:00
moon.SetSurfaceState(StateIndex);
2021-12-25 06:13:00 +00:00
}
2022-03-03 03:46:33 +00:00
else
2021-12-25 06:13:00 +00:00
{
2022-03-03 03:46:33 +00:00
moon.SetSurfaceState(-1);
moon._quantumSignal.SetSignalActivation(StateIndex != 5);
}
2021-12-25 06:13:00 +00:00
2022-03-03 03:46:33 +00:00
moon._referenceFrameVolume.gameObject.SetActive(StateIndex != 5);
moonBody.SetIsTargetable(StateIndex != 5);
foreach (var obj in moon._deactivateAtEye)
{
2022-03-03 03:46:33 +00:00
obj.SetActive(StateIndex != 5);
}
2022-03-03 03:46:33 +00:00
GlobalMessenger<OWRigidbody>.FireEvent("QuantumMoonChangeState", moonBody);
2022-03-03 03:46:33 +00:00
if (wasPlayerEntangled)
{
Locator.GetPlayerTransform().GetComponent<OWRigidbody>().MoveToRelativeLocation(location, moon.transform);
2021-12-25 06:13:00 +00:00
}
2021-01-03 09:52:34 +00:00
}
}