ILinkedNetworkBehaviour doesnt need property

This commit is contained in:
JohnCorby 2022-03-14 03:34:00 -07:00
parent 611ecec4a3
commit 7b47211166
10 changed files with 53 additions and 52 deletions

View File

@ -7,16 +7,16 @@ using UnityEngine;
namespace QSB.Anglerfish.TransformSync; namespace QSB.Anglerfish.TransformSync;
public class AnglerTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour<QSBAngler> public class AnglerTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour
{ {
protected override bool UseInterpolation => false; protected override bool UseInterpolation => false;
protected override bool AllowInactiveAttachedObject => true; // since they deactivate when suspended protected override bool AllowInactiveAttachedObject => true; // since they deactivate when suspended
public QSBAngler WorldObject { get; private set; } private QSBAngler _qsbAngler;
public void LinkTo(IWorldObject worldObject) => WorldObject = (QSBAngler)worldObject; public void SetWorldObject(IWorldObject worldObject) => _qsbAngler = (QSBAngler)worldObject;
protected override OWRigidbody InitAttachedRigidbody() protected override OWRigidbody InitAttachedRigidbody()
=> WorldObject.AttachedObject._anglerBody; => _qsbAngler.AttachedObject._anglerBody;
public override void OnStartClient() public override void OnStartClient()
{ {
@ -44,7 +44,7 @@ public class AnglerTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehavi
protected override void Init() protected override void Init()
{ {
base.Init(); base.Init();
SetReferenceTransform(WorldObject.AttachedObject._brambleBody.transform); SetReferenceTransform(_qsbAngler.AttachedObject._brambleBody.transform);
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend; AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend; AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
@ -74,18 +74,18 @@ public class AnglerTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehavi
base.OnRenderObject(); base.OnRenderObject();
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), WorldObject.AttachedObject._arrivalDistance, Color.blue); Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._arrivalDistance, Color.blue);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), WorldObject.AttachedObject._pursueDistance, Color.red); Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._pursueDistance, Color.red);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), WorldObject.AttachedObject._escapeDistance, Color.yellow); Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._escapeDistance, Color.yellow);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition() Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition()
+ AttachedRigidbody.transform.TransformDirection(WorldObject.AttachedObject._mouthOffset), 3, Color.grey); + AttachedRigidbody.transform.TransformDirection(_qsbAngler.AttachedObject._mouthOffset), 3, Color.grey);
if (WorldObject.TargetTransform) if (_qsbAngler.TargetTransform)
{ {
Popcron.Gizmos.Line(WorldObject.TargetTransform.position, AttachedRigidbody.GetPosition(), Color.gray); Popcron.Gizmos.Line(_qsbAngler.TargetTransform.position, AttachedRigidbody.GetPosition(), Color.gray);
Popcron.Gizmos.Line(WorldObject.TargetTransform.position, WorldObject.TargetTransform.position + WorldObject.TargetVelocity, Color.green); Popcron.Gizmos.Line(_qsbAngler.TargetTransform.position, _qsbAngler.TargetTransform.position + _qsbAngler.TargetVelocity, Color.green);
Popcron.Gizmos.Line(AttachedRigidbody.GetPosition(), WorldObject.AttachedObject._targetPos, Color.red); Popcron.Gizmos.Line(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._targetPos, Color.red);
Popcron.Gizmos.Sphere(WorldObject.AttachedObject._targetPos, 5, Color.red); Popcron.Gizmos.Sphere(_qsbAngler.AttachedObject._targetPos, 5, Color.red);
} }
// Popcron.Gizmos.Line(AttachedObject.GetPosition(), _qsbAngler.AttachedObject.GetTargetPosition(), Color.white); // Popcron.Gizmos.Line(AttachedObject.GetPosition(), _qsbAngler.AttachedObject.GetTargetPosition(), Color.white);

View File

@ -7,14 +7,14 @@ using QSB.WorldSync;
namespace QSB.EchoesOfTheEye.RaftSync.TransformSync; namespace QSB.EchoesOfTheEye.RaftSync.TransformSync;
public class RaftTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour<QSBRaft> public class RaftTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour
{ {
protected override bool UseInterpolation => false; protected override bool UseInterpolation => false;
public QSBRaft WorldObject { get; private set; } private QSBRaft _qsbRaft;
public void LinkTo(IWorldObject worldObject) => WorldObject = (QSBRaft)worldObject; public void SetWorldObject(IWorldObject worldObject) => _qsbRaft = (QSBRaft)worldObject;
protected override OWRigidbody InitAttachedRigidbody() => WorldObject.AttachedObject._raftBody; protected override OWRigidbody InitAttachedRigidbody() => _qsbRaft.AttachedObject._raftBody;
public override void OnStartClient() public override void OnStartClient()
{ {

View File

@ -9,16 +9,16 @@ using UnityEngine;
namespace QSB.JellyfishSync.TransformSync; namespace QSB.JellyfishSync.TransformSync;
public class JellyfishTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour<QSBJellyfish> public class JellyfishTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour
{ {
protected override bool UseInterpolation => false; protected override bool UseInterpolation => false;
protected override bool AllowInactiveAttachedObject => true; // since they deactivate when suspended protected override bool AllowInactiveAttachedObject => true; // since they deactivate when suspended
public QSBJellyfish WorldObject { get; private set; } private QSBJellyfish _qsbJellyfish;
public void LinkTo(IWorldObject worldObject) => WorldObject = (QSBJellyfish)worldObject; public void SetWorldObject(IWorldObject worldObject) => _qsbJellyfish = (QSBJellyfish)worldObject;
protected override OWRigidbody InitAttachedRigidbody() protected override OWRigidbody InitAttachedRigidbody()
=> WorldObject.AttachedObject._jellyfishBody; => _qsbJellyfish.AttachedObject._jellyfishBody;
public override void OnStartClient() public override void OnStartClient()
{ {
@ -46,7 +46,7 @@ public class JellyfishTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBeh
protected override void Init() protected override void Init()
{ {
base.Init(); base.Init();
SetReferenceTransform(WorldObject.AttachedObject._planetBody.transform); SetReferenceTransform(_qsbJellyfish.AttachedObject._planetBody.transform);
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend; AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend; AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
@ -66,13 +66,13 @@ public class JellyfishTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBeh
protected override void Serialize(NetworkWriter writer) protected override void Serialize(NetworkWriter writer)
{ {
writer.Write(WorldObject.AttachedObject._isRising); writer.Write(_qsbJellyfish.AttachedObject._isRising);
base.Serialize(writer); base.Serialize(writer);
} }
protected override void Deserialize(NetworkReader reader) protected override void Deserialize(NetworkReader reader)
{ {
WorldObject.SetIsRising(reader.Read<bool>()); _qsbJellyfish.SetIsRising(reader.Read<bool>());
base.Deserialize(reader); base.Deserialize(reader);
} }
@ -98,7 +98,7 @@ public class JellyfishTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBeh
base.OnRenderObject(); base.OnRenderObject();
var jellyfish = WorldObject.AttachedObject; var jellyfish = _qsbJellyfish.AttachedObject;
var position = ReferenceTransform.position; var position = ReferenceTransform.position;
var dir = Vector3.Normalize(jellyfish.transform.position - position); var dir = Vector3.Normalize(jellyfish.transform.position - position);
// Popcron.Gizmos.Line(position + dir * jellyfish._lowerLimit, position + dir * jellyfish._upperLimit, Color.magenta); // Popcron.Gizmos.Line(position + dir * jellyfish._lowerLimit, position + dir * jellyfish._upperLimit, Color.magenta);

View File

@ -8,7 +8,7 @@ using UnityEngine;
namespace QSB.OrbSync.TransformSync; namespace QSB.OrbSync.TransformSync;
public class NomaiOrbTransformSync : UnsectoredTransformSync, ILinkedNetworkBehaviour<QSBOrb> public class NomaiOrbTransformSync : UnsectoredTransformSync, ILinkedNetworkBehaviour
{ {
/// <summary> /// <summary>
/// normally prints error when attached object is null. /// normally prints error when attached object is null.
@ -19,11 +19,11 @@ public class NomaiOrbTransformSync : UnsectoredTransformSync, ILinkedNetworkBeha
protected override bool UseInterpolation => true; protected override bool UseInterpolation => true;
protected override float DistanceChangeThreshold => 1f; protected override float DistanceChangeThreshold => 1f;
public QSBOrb WorldObject { get; private set; } private QSBOrb _qsbOrb;
public void LinkTo(IWorldObject worldObject) => WorldObject = (QSBOrb)worldObject; public void SetWorldObject(IWorldObject worldObject) => _qsbOrb = (QSBOrb)worldObject;
protected override Transform InitLocalTransform() => WorldObject.AttachedObject.transform; protected override Transform InitLocalTransform() => _qsbOrb.AttachedObject.transform;
protected override Transform InitRemoteTransform() => WorldObject.AttachedObject.transform; protected override Transform InitRemoteTransform() => _qsbOrb.AttachedObject.transform;
private static readonly List<NomaiOrbTransformSync> _instances = new(); private static readonly List<NomaiOrbTransformSync> _instances = new();
@ -83,6 +83,6 @@ public class NomaiOrbTransformSync : UnsectoredTransformSync, ILinkedNetworkBeha
{ {
base.ApplyToAttached(); base.ApplyToAttached();
WorldObject.AttachedObject.SetTargetPosition(AttachedTransform.position); _qsbOrb.AttachedObject.SetTargetPosition(AttachedTransform.position);
} }
} }

View File

@ -1,7 +1,5 @@
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;
using Mirror; using Mirror;
using QSB.WorldSync;
using System.Linq;
using System.Threading; using System.Threading;
using UnityEngine; using UnityEngine;
@ -9,6 +7,15 @@ namespace QSB.Utility.LinkedWorldObject;
public static class Extensions public static class Extensions
{ {
/// <summary>
/// link a world object and a network behaviour
/// </summary>
public static void LinkTo(this ILinkedWorldObject<NetworkBehaviour> worldObject, ILinkedNetworkBehaviour networkBehaviour)
{
worldObject.SetNetworkBehaviour((NetworkBehaviour)networkBehaviour);
networkBehaviour.SetWorldObject(worldObject);
}
/// <summary> /// <summary>
/// link a world object and network object, then spawn it. /// link a world object and network object, then spawn it.
/// (host only) /// (host only)
@ -16,11 +23,9 @@ public static class Extensions
public static void SpawnLinked(this ILinkedWorldObject<NetworkBehaviour> worldObject, GameObject prefab) public static void SpawnLinked(this ILinkedWorldObject<NetworkBehaviour> worldObject, GameObject prefab)
{ {
var go = Object.Instantiate(prefab); var go = Object.Instantiate(prefab);
var networkIdentity = go.GetComponent<NetworkIdentity>(); var networkBehaviour = go.GetComponent<ILinkedNetworkBehaviour>();
var networkBehaviour = networkIdentity.NetworkBehaviours.OfType<ILinkedNetworkBehaviour<IWorldObject>>().First();
worldObject.LinkTo((NetworkBehaviour)networkBehaviour); worldObject.LinkTo(networkBehaviour);
networkBehaviour.LinkTo(worldObject);
NetworkServer.Spawn(go); NetworkServer.Spawn(go);
} }

View File

@ -5,9 +5,7 @@ namespace QSB.Utility.LinkedWorldObject;
/// <summary> /// <summary>
/// a network behaviour that is linked to a world object /// a network behaviour that is linked to a world object
/// </summary> /// </summary>
public interface ILinkedNetworkBehaviour<out TWorldObject> public interface ILinkedNetworkBehaviour
where TWorldObject : IWorldObject
{ {
TWorldObject WorldObject { get; } void SetWorldObject(IWorldObject worldObject);
void LinkTo(IWorldObject worldObject);
} }

View File

@ -10,5 +10,5 @@ public interface ILinkedWorldObject<out TNetworkBehaviour> : IWorldObject
where TNetworkBehaviour : NetworkBehaviour where TNetworkBehaviour : NetworkBehaviour
{ {
TNetworkBehaviour NetworkBehaviour { get; } TNetworkBehaviour NetworkBehaviour { get; }
void LinkTo(NetworkBehaviour networkBehaviour); void SetNetworkBehaviour(NetworkBehaviour networkBehaviour);
} }

View File

@ -1,7 +1,6 @@
using Mirror; using Mirror;
using QSB.Messaging; using QSB.Messaging;
using QSB.WorldSync; using QSB.WorldSync;
using System.Linq;
namespace QSB.Utility.LinkedWorldObject; namespace QSB.Utility.LinkedWorldObject;
@ -17,10 +16,9 @@ public class LinkMessage : QSBMessage<(int ObjectId, uint NetId)>
public override void OnReceiveRemote() public override void OnReceiveRemote()
{ {
var worldObject = Data.ObjectId.GetWorldObject<ILinkedWorldObject<NetworkBehaviour>>(); var worldObject = Data.ObjectId.GetWorldObject<ILinkedWorldObject<NetworkBehaviour>>();
var networkIdentity = NetworkClient.spawned[Data.NetId]; var identity = NetworkClient.spawned[Data.NetId];
var networkBehaviour = networkIdentity.NetworkBehaviours.OfType<ILinkedNetworkBehaviour<IWorldObject>>().First(); var networkBehaviour = identity.GetComponent<ILinkedNetworkBehaviour>();
worldObject.LinkTo((NetworkBehaviour)networkBehaviour); worldObject.LinkTo(networkBehaviour);
networkBehaviour.LinkTo(worldObject);
} }
} }

View File

@ -6,9 +6,9 @@ namespace QSB.Utility.LinkedWorldObject;
/// <summary> /// <summary>
/// helper implementation of the interface /// helper implementation of the interface
/// </summary> /// </summary>
public abstract class LinkedVariableSyncer<T, TWorldObject> : BaseVariableSyncer<T>, ILinkedNetworkBehaviour<TWorldObject> public abstract class LinkedVariableSyncer<T, TWorldObject> : BaseVariableSyncer<T>, ILinkedNetworkBehaviour
where TWorldObject : IWorldObject where TWorldObject : IWorldObject
{ {
public TWorldObject WorldObject { get; private set; } protected TWorldObject WorldObject { get; private set; }
public void LinkTo(IWorldObject worldObject) => WorldObject = (TWorldObject)worldObject; public void SetWorldObject(IWorldObject worldObject) => WorldObject = (TWorldObject)worldObject;
} }

View File

@ -14,7 +14,7 @@ public abstract class LinkedWorldObject<T, TNetworkBehaviour> : WorldObject<T>,
where TNetworkBehaviour : NetworkBehaviour where TNetworkBehaviour : NetworkBehaviour
{ {
public TNetworkBehaviour NetworkBehaviour { get; private set; } public TNetworkBehaviour NetworkBehaviour { get; private set; }
public void LinkTo(NetworkBehaviour networkBehaviour) => NetworkBehaviour = (TNetworkBehaviour)networkBehaviour; public void SetNetworkBehaviour(NetworkBehaviour networkBehaviour) => NetworkBehaviour = (TNetworkBehaviour)networkBehaviour;
protected abstract GameObject NetworkObjectPrefab { get; } protected abstract GameObject NetworkObjectPrefab { get; }