quantum-space-buddies/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs

147 lines
4.4 KiB
C#
Raw Normal View History

using Mirror;
using OWML.Common;
2021-12-23 06:59:08 +00:00
using QSB.Messaging;
using QSB.MeteorSync.WorldObjects;
using QSB.Utility;
2021-11-14 11:29:58 +00:00
using UnityEngine;
2021-11-14 04:41:46 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.MeteorSync.Messages;
/// called when we request a resync on client join
/// pain
public class FragmentInitialStateMessage : QSBWorldObjectMessage<QSBFragment>
2021-11-14 04:41:46 +00:00
{
2022-03-03 03:46:33 +00:00
private float Integrity;
private float OrigIntegrity;
private float LeashLength;
private bool IsDetached;
private bool IsThruWhiteHole;
private Vector3 RelPos;
private Quaternion RelRot;
private Vector3 RelVel;
private Vector3 RelAngVel;
public FragmentInitialStateMessage(QSBFragment qsbFragment)
2021-11-14 04:41:46 +00:00
{
2022-03-03 03:46:33 +00:00
Integrity = qsbFragment.AttachedObject._integrity;
OrigIntegrity = qsbFragment.AttachedObject._origIntegrity;
LeashLength = qsbFragment.LeashLength;
IsDetached = qsbFragment.IsDetached;
if (IsDetached)
2021-11-14 04:41:46 +00:00
{
2022-03-03 03:46:33 +00:00
IsThruWhiteHole = qsbFragment.IsThruWhiteHole;
2021-11-14 04:41:46 +00:00
2022-03-03 03:46:33 +00:00
var body = qsbFragment.Body;
var refBody = qsbFragment.RefBody;
var pos = body.GetPosition();
RelPos = refBody.transform.ToRelPos(pos);
RelRot = refBody.transform.ToRelRot(body.GetRotation());
RelVel = refBody.ToRelVel(body.GetVelocity(), pos);
RelAngVel = refBody.ToRelAngVel(body.GetAngularVelocity());
2021-11-14 04:41:46 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-12-23 06:59:08 +00:00
2022-03-03 03:46:33 +00:00
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(Integrity);
writer.Write(OrigIntegrity);
writer.Write(LeashLength);
writer.Write(IsDetached);
if (IsDetached)
2021-12-23 06:59:08 +00:00
{
2022-03-03 03:46:33 +00:00
writer.Write(IsThruWhiteHole);
writer.Write(RelPos);
writer.Write(RelRot);
writer.Write(RelVel);
writer.Write(RelAngVel);
2021-12-23 06:59:08 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-12-23 06:59:08 +00:00
2022-03-03 03:46:33 +00:00
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
Integrity = reader.Read<float>();
OrigIntegrity = reader.Read<float>();
LeashLength = reader.Read<float>();
IsDetached = reader.Read<bool>();
if (IsDetached)
2021-12-23 06:59:08 +00:00
{
2022-03-03 03:46:33 +00:00
IsThruWhiteHole = reader.Read<bool>();
RelPos = reader.ReadVector3();
RelRot = reader.ReadQuaternion();
RelVel = reader.ReadVector3();
RelAngVel = reader.ReadVector3();
}
2022-03-03 03:46:33 +00:00
}
2021-12-23 06:59:08 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveRemote()
{
WorldObject.AttachedObject._origIntegrity = OrigIntegrity;
WorldObject.LeashLength = LeashLength;
if (!OWMath.ApproxEquals(WorldObject.AttachedObject._integrity, Integrity))
{
2022-03-03 03:46:33 +00:00
WorldObject.AttachedObject._integrity = Integrity;
WorldObject.AttachedObject.CallOnTakeDamage();
}
2021-12-23 06:59:08 +00:00
2022-03-03 03:46:33 +00:00
if (IsDetached && !WorldObject.IsDetached)
{
// the detach is delayed, so wait until that happens
Delay.RunWhen(() => WorldObject.IsDetached, () =>
{
2022-03-03 03:46:33 +00:00
var body = WorldObject.Body;
2022-03-03 03:46:33 +00:00
if (IsThruWhiteHole && !WorldObject.IsThruWhiteHole)
{
var whiteHoleVolume = MeteorManager.WhiteHoleVolume;
var attachedFluidDetector = body.GetAttachedFluidDetector();
var attachedForceDetector = body.GetAttachedForceDetector();
if (attachedFluidDetector is ConstantFluidDetector constantFluidDetector)
2021-12-23 06:59:08 +00:00
{
2022-03-03 03:46:33 +00:00
constantFluidDetector.SetDetectableFluid(whiteHoleVolume._fluidVolume);
2021-12-23 06:59:08 +00:00
}
2022-03-03 03:46:33 +00:00
if (attachedForceDetector is ConstantForceDetector constantForceDetector)
{
2022-03-03 03:46:33 +00:00
constantForceDetector.ClearAllFields();
}
2022-03-03 03:46:33 +00:00
WorldObject.DetachableFragment.ChangeFragmentSector(whiteHoleVolume._whiteHoleSector,
whiteHoleVolume._whiteHoleProxyShadowSuperGroup);
WorldObject.DetachableFragment.EndWarpScaling();
body.gameObject.AddComponent<DebrisLeash>().Init(whiteHoleVolume._whiteHoleBody, WorldObject.LeashLength);
whiteHoleVolume._ejectedBodyList.Add(body);
}
else if (!IsThruWhiteHole && WorldObject.IsThruWhiteHole)
{
// should only happen if client is way too far ahead and they try to connect. we fail here.
DebugLog.ToConsole($"{WorldObject} is thru white hole, but msg is not. fuck", MessageType.Error);
return;
}
if (WorldObject.IsThruWhiteHole)
{
var debrisLeash = body.GetComponent<DebrisLeash>();
debrisLeash._deccelerating = false;
debrisLeash.enabled = true;
}
var refBody = WorldObject.RefBody;
var pos = refBody.transform.FromRelPos(RelPos);
body.SetPosition(pos);
body.SetRotation(refBody.transform.FromRelRot(RelRot));
body.SetVelocity(refBody.FromRelVel(RelVel, pos));
body.SetAngularVelocity(refBody.FromRelAngVel(RelAngVel));
});
}
else if (!IsDetached && WorldObject.IsDetached)
{
// should only happen if client is way too far ahead and they try to connect. we fail here.
DebugLog.ToConsole($"{WorldObject} is detached, but msg is not. fuck", MessageType.Error);
2021-12-23 06:59:08 +00:00
}
2021-11-14 04:41:46 +00:00
}
}