2022-08-26 18:21:13 -04:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using Mirror;
|
|
|
|
|
using QSB.AuthoritySync;
|
|
|
|
|
using QSB.Messaging;
|
|
|
|
|
using QSB.Player;
|
2022-08-27 11:11:49 -07:00
|
|
|
|
using QSB.Tools.ProbeLauncherTool.Messages;
|
|
|
|
|
using QSB.Tools.ProbeLauncherTool.VariableSync;
|
2022-08-26 18:21:13 -04:00
|
|
|
|
using QSB.Utility.LinkedWorldObject;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2022-08-27 11:11:49 -07:00
|
|
|
|
namespace QSB.Tools.ProbeLauncherTool.WorldObjects;
|
2022-08-26 18:21:13 -04:00
|
|
|
|
|
2022-08-27 12:42:29 -07:00
|
|
|
|
// TODO John - i think this can be simplified, i might be able to remove the variable syncer
|
2022-08-27 11:21:09 -07:00
|
|
|
|
public class QSBStationaryProbeLauncher : QSBProbeLauncher, ILinkedWorldObject<StationaryProbeLauncherVariableSyncer>
|
2022-08-26 18:21:13 -04:00
|
|
|
|
{
|
2022-08-27 02:28:10 -04:00
|
|
|
|
private uint _currentUser = uint.MaxValue;
|
|
|
|
|
|
2022-08-27 11:21:09 -07:00
|
|
|
|
public StationaryProbeLauncherVariableSyncer NetworkBehaviour { get; private set; }
|
|
|
|
|
public void SetNetworkBehaviour(NetworkBehaviour networkBehaviour) => NetworkBehaviour = (StationaryProbeLauncherVariableSyncer)networkBehaviour;
|
2022-08-26 18:21:13 -04:00
|
|
|
|
|
|
|
|
|
private bool _isInUse;
|
|
|
|
|
private StationaryProbeLauncher _stationaryProbeLauncher;
|
|
|
|
|
|
|
|
|
|
public override async UniTask Init(CancellationToken ct)
|
|
|
|
|
{
|
2022-08-27 02:28:10 -04:00
|
|
|
|
// This is implemented by inheriting LinkedWorldObject normally,
|
|
|
|
|
// However I want to inherit from QSBProbeLauncher or else we'd have to redo the sync for the VFX/SFX
|
2022-08-26 18:21:13 -04:00
|
|
|
|
if (QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
this.SpawnLinked(QSBNetworkManager.singleton.StationaryProbeLauncherPrefab, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await this.WaitForLink(ct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await base.Init(ct);
|
|
|
|
|
|
2022-08-26 16:10:35 -07:00
|
|
|
|
_stationaryProbeLauncher = (StationaryProbeLauncher)AttachedObject;
|
2022-08-26 18:21:13 -04:00
|
|
|
|
_stationaryProbeLauncher._interactVolume.OnPressInteract += OnPressInteract;
|
|
|
|
|
|
2022-08-27 14:06:28 -04:00
|
|
|
|
// Fix spatial blend of sound effects
|
|
|
|
|
_stationaryProbeLauncher._effects._owAudioSource.spatialBlend = 1;
|
|
|
|
|
_stationaryProbeLauncher._audioSource.spatialBlend = 1;
|
|
|
|
|
|
2022-08-26 18:21:13 -04:00
|
|
|
|
UpdateUse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnRemoval()
|
|
|
|
|
{
|
|
|
|
|
if (QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
NetworkServer.Destroy(NetworkBehaviour.gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.OnRemoval();
|
|
|
|
|
|
|
|
|
|
_stationaryProbeLauncher._interactVolume.OnPressInteract -= OnPressInteract;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 02:28:10 -04:00
|
|
|
|
private void OnPressInteract() => OnLocalUseStateChanged(true);
|
2022-08-26 18:21:13 -04:00
|
|
|
|
|
|
|
|
|
public override void SendInitialState(uint to)
|
|
|
|
|
{
|
|
|
|
|
base.SendInitialState(to);
|
|
|
|
|
|
2022-08-27 02:28:10 -04:00
|
|
|
|
this.SendMessage(new StationaryProbeLauncherMessage(_isInUse, _currentUser) { To = to });
|
2022-08-26 18:21:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 16:15:50 -07:00
|
|
|
|
public void OnRemoteUseStateChanged(bool isInUse, uint user)
|
2022-08-26 18:52:20 -04:00
|
|
|
|
{
|
2022-08-27 02:28:10 -04:00
|
|
|
|
_isInUse = isInUse;
|
|
|
|
|
|
|
|
|
|
_currentUser = isInUse ? user : uint.MaxValue;
|
|
|
|
|
|
2022-08-26 18:52:20 -04:00
|
|
|
|
// Whoever is using it needs authority to be able to rotate it
|
2022-08-26 16:11:29 -07:00
|
|
|
|
if (QSBCore.IsHost)
|
|
|
|
|
{
|
2022-08-27 02:28:10 -04:00
|
|
|
|
NetworkBehaviour.netIdentity.SetAuthority(_currentUser);
|
2022-08-26 16:11:29 -07:00
|
|
|
|
}
|
2022-08-26 18:52:20 -04:00
|
|
|
|
|
2022-08-27 02:28:10 -04:00
|
|
|
|
UpdateUse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnLocalUseStateChanged(bool isInUse)
|
|
|
|
|
{
|
2022-08-26 18:52:20 -04:00
|
|
|
|
_isInUse = isInUse;
|
|
|
|
|
|
2022-08-27 02:28:10 -04:00
|
|
|
|
_currentUser = isInUse ? QSBPlayerManager.LocalPlayerId : uint.MaxValue;
|
|
|
|
|
|
|
|
|
|
// Whoever is using it needs authority to be able to rotate it
|
|
|
|
|
if (QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
NetworkBehaviour.netIdentity.SetAuthority(_currentUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.SendMessage(new StationaryProbeLauncherMessage(isInUse, _currentUser));
|
2022-08-26 18:52:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 18:21:13 -04:00
|
|
|
|
private void UpdateUse()
|
|
|
|
|
{
|
|
|
|
|
// If somebody is using this we disable the interaction shape
|
2022-08-26 16:10:35 -07:00
|
|
|
|
_stationaryProbeLauncher._interactVolume.SetInteractionEnabled(!_isInUse);
|
2022-08-26 18:52:20 -04:00
|
|
|
|
|
2022-08-26 18:21:13 -04:00
|
|
|
|
if (_isInUse)
|
|
|
|
|
{
|
|
|
|
|
_stationaryProbeLauncher._audioSource.SetLocalVolume(0f);
|
2022-08-26 18:52:20 -04:00
|
|
|
|
_stationaryProbeLauncher._audioSource.Play();
|
2022-08-26 18:21:13 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_stationaryProbeLauncher._audioSource.Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|