87 lines
2.1 KiB
C#
Raw Normal View History

2021-11-09 20:35:55 -08:00
using System;
using System.Linq;
2021-11-09 18:38:19 -08:00
using OWML.Common;
using QSB.Anglerfish.TransformSync;
using QSB.Events;
2021-11-09 20:35:55 -08:00
using QSB.Player;
2021-11-09 17:56:45 -08:00
using QSB.Utility;
using QSB.WorldSync;
using QuantumUNET;
using UnityEngine;
2021-11-09 20:35:55 -08:00
using Object = UnityEngine.Object;
2021-11-09 17:56:45 -08:00
namespace QSB.Anglerfish.WorldObjects
{
public class QSBAngler : WorldObject<AnglerfishController>
{
public AnglerTransformSync transformSync;
public override void Init(AnglerfishController attachedObject, int id)
{
ObjectId = id;
AttachedObject = attachedObject;
if (QSBCore.IsHost)
{
Object.Instantiate(QSBNetworkManager.Instance.AnglerPrefab).SpawnWithServerAuthority();
}
2021-11-09 18:38:19 -08:00
AttachedObject.OnChangeAnglerState += OnChangeState;
2021-11-09 17:56:45 -08:00
}
public override void OnRemoval()
{
if (QSBCore.IsHost)
{
QNetworkServer.Destroy(transformSync.gameObject);
}
2021-11-09 18:38:19 -08:00
AttachedObject.OnChangeAnglerState -= OnChangeState;
2021-11-09 18:38:19 -08:00
}
private void OnChangeState(AnglerfishController.AnglerState state) =>
2021-11-09 20:35:55 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, this);
2021-11-09 18:38:19 -08:00
public void TransferAuthority(uint id)
{
if (!QSBCore.IsHost)
{
DebugLog.ToConsole("Error - non-host trying to transfer angler authority", MessageType.Error);
return;
}
var conn = QNetworkServer.connections.First(x => x.GetPlayerId() == id);
var identity = transformSync.NetIdentity;
if (identity.ClientAuthorityOwner == conn)
{
return;
}
if (identity.ClientAuthorityOwner != null)
{
identity.RemoveClientAuthority(identity.ClientAuthorityOwner);
}
identity.AssignClientAuthority(conn);
DebugLog.DebugWrite($"angler {ObjectId} - transferred authority to {id}");
2021-11-09 17:56:45 -08:00
}
2021-11-09 20:35:55 -08:00
public Transform target;
public uint TargetToId()
{
var body = AttachedObject._targetBody;
if (body == null) return uint.MaxValue;
if (body == Locator.GetShipBody()) return uint.MaxValue - 1;
return QSBPlayerManager.LocalPlayerId;
}
public static Transform IdToTarget(uint id)
{
if (id == uint.MaxValue) return null;
if (id == uint.MaxValue - 1) return Locator.GetShipTransform();
return QSBPlayerManager.GetPlayer(id).Body.transform;
}
2021-11-09 17:56:45 -08:00
}
}