quantum-space-buddies/QSB/OrbSync/OrbUserEvent.cs

48 lines
1.7 KiB
C#
Raw Normal View History

using QSB.Events;
using QSB.Messaging;
2020-09-05 12:24:51 +00:00
using QSB.TransformSync;
using QSB.Utility;
using QSB.WorldSync;
2020-09-05 12:24:51 +00:00
using System.Linq;
using UnityEngine.Networking;
namespace QSB.OrbSync
{
public class OrbUserEvent : QSBEvent<WorldObjectMessage>
{
public override EventType Type => EventType.OrbUser;
2020-09-05 14:46:20 +00:00
public override void SetupListener() => GlobalMessenger<int>.AddListener(EventNames.QSBOrbUser, Handler);
2020-09-05 14:46:20 +00:00
public override void CloseListener() => GlobalMessenger<int>.RemoveListener(EventNames.QSBOrbUser, Handler);
private void Handler(int id) => SendEvent(CreateMessage(id));
private WorldObjectMessage CreateMessage(int id) => new WorldObjectMessage
{
AboutId = LocalPlayerId,
ObjectId = id
};
2020-09-05 12:24:51 +00:00
public override void OnServerReceive(WorldObjectMessage message)
{
2020-09-05 14:46:20 +00:00
var fromPlayer = NetworkServer.connections
.First(x => x.playerControllers[0].gameObject.GetComponent<PlayerTransformSync>().netId.Value == message.FromId);
2020-09-05 12:24:51 +00:00
DebugLog.DebugWrite($"[S] Setting orb {message.ObjectId} to auth id {message.FromId}");
2020-09-05 14:46:20 +00:00
var orb = WorldRegistry.OrbList
.First(x => x.AttachedOrb == WorldRegistry.OldOrbList[message.ObjectId]);
2020-09-05 14:31:09 +00:00
var orbIdentity = orb.GetComponent<NetworkIdentity>();
2020-09-05 12:24:51 +00:00
orbIdentity.RemoveClientAuthority(orbIdentity.clientAuthorityOwner);
orbIdentity.AssignClientAuthority(fromPlayer);
}
2020-09-05 12:24:51 +00:00
public override void OnReceiveLocal(WorldObjectMessage message)
{
if (NetworkServer.active)
{
OnServerReceive(message);
}
}
}
}