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

53 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;
public override void SetupListener()
{
GlobalMessenger<int>.AddListener(EventNames.QSBOrbUser, Handler);
}
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 12:24:51 +00:00
var fromPlayer = (NetworkServer.connections.First(x => x.playerControllers[0].gameObject.GetComponent<PlayerTransformSync>().netId.Value == message.FromId));
DebugLog.DebugWrite($"[S] Setting orb {message.ObjectId} to auth id {message.FromId}");
var orbIdentity = WorldRegistry.OrbList
.First(x => x.AttachedOrb == WorldRegistry.OldOrbList[message.ObjectId])
.GetComponent<NetworkIdentity>();
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);
}
}
}
}