From e6194d624a471417850296237961bba82452dde4 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 14 Feb 2022 22:51:09 -0800 Subject: [PATCH] simplify a whole bunch --- QSB/Menus/MenuManager.cs | 8 +- QSB/Player/JoinLeaveSingularity.cs | 105 ++++++++++-------- .../Messages/JoinLeaveSingularityMessage.cs | 18 --- QSB/Player/Messages/PlayerJoinMessage.cs | 3 + .../TransformSync/PlayerTransformSync.cs | 13 +-- 5 files changed, 63 insertions(+), 84 deletions(-) delete mode 100644 QSB/Player/Messages/JoinLeaveSingularityMessage.cs diff --git a/QSB/Menus/MenuManager.cs b/QSB/Menus/MenuManager.cs index 10876c95..8b441ff9 100644 --- a/QSB/Menus/MenuManager.cs +++ b/QSB/Menus/MenuManager.cs @@ -2,7 +2,6 @@ using Mirror; using QSB.Messaging; using QSB.Player; -using QSB.Player.Messages; using QSB.Player.TransformSync; using QSB.SaveSync.Messages; using QSB.Utility; @@ -302,11 +301,6 @@ namespace QSB.Menus { _intentionalDisconnect = true; - if (!QSBCore.IsHost) - { - new JoinLeaveSingularityMessage(false).Send(); - } - QSBNetworkManager.singleton.StopHost(); SetButtonActive(DisconnectButton.gameObject, false); @@ -379,7 +373,7 @@ namespace QSB.Menus QSBNetworkManager.singleton.StartClient(); } - private static void OnConnected() + private void OnConnected() { if (QSBCore.IsHost || !QSBCore.IsInMultiplayer) { diff --git a/QSB/Player/JoinLeaveSingularity.cs b/QSB/Player/JoinLeaveSingularity.cs index cea1caa5..adcdcaa8 100644 --- a/QSB/Player/JoinLeaveSingularity.cs +++ b/QSB/Player/JoinLeaveSingularity.cs @@ -1,80 +1,87 @@ using QSB.Utility; -using QSB.WorldSync; -using System.Linq; +using System.Collections; using UnityEngine; namespace QSB.Player { - public static class JoinLeaveSingularity + public class JoinLeaveSingularity : MonoBehaviour { + private PlayerInfo _player; + private bool _joining; + public static void Create(PlayerInfo player, bool joining) { - DebugLog.DebugWrite($"{player.TransformSync} join/leave singularity: (joining = {joining})"); + var joinLeaveSingularity = new GameObject(nameof(JoinLeaveSingularity)) + .AddComponent(); + joinLeaveSingularity._player = player; + joinLeaveSingularity._joining = joining; + } - var go = new GameObject(nameof(JoinLeaveSingularity)); + private IEnumerator Start() + { + DebugLog.DebugWrite($"WARP {_player.TransformSync}"); - var playerGo = player.Body; - playerGo.SetActive(false); - go.transform.parent = playerGo.transform.parent; - go.transform.localPosition = playerGo.transform.localPosition; - go.transform.localRotation = playerGo.transform.localRotation; - go.transform.localScale = playerGo.transform.localScale; + transform.parent = _player.TransformSync.ReferenceTransform; + transform.localPosition = _player.Body.transform.localPosition; + transform.localRotation = _player.Body.transform.localRotation; + transform.localScale = _player.Body.transform.localScale; - var fakePlayerGo = playerGo.InstantiateInactive(); - fakePlayerGo.transform.parent = go.transform; - fakePlayerGo.transform.localPosition = Vector3.zero; - fakePlayerGo.transform.localRotation = Quaternion.identity; - fakePlayerGo.transform.localScale = Vector3.one; + var SingularityWarpEffect = _player.Body.transform.Find("SingularityWarpEffect").gameObject; - foreach (var component in fakePlayerGo.GetComponents()) - { - if (component is not (Transform or Renderer)) - { - Object.Destroy(component); - } - } - - fakePlayerGo.SetActive(true); - - var referenceEffect = joining ? - QSBWorldSync.GetUnityObjects() - .Select(x => x._probeRetrievalEffect) - .First(x => x) : - QSBWorldSync.GetUnityObjects() - .Select(x => x._warpEffect) - .First(x => x); - var effectGo = referenceEffect.gameObject.InstantiateInactive(); - effectGo.transform.parent = go.transform; + var effectGo = SingularityWarpEffect.InstantiateInactive(); + effectGo.transform.parent = transform; effectGo.transform.localPosition = Vector3.zero; effectGo.transform.localRotation = Quaternion.identity; effectGo.transform.localScale = Vector3.one; var effect = effectGo.GetComponent(); - effect._warpedObjectGeometry = fakePlayerGo; + var curve = AnimationCurve.EaseInOut(0, 0, .2f, 1); + effect._singularity._creationCurve = curve; + effect._singularity._destructionCurve = curve; + + var renderer = effectGo.GetComponent(); + renderer.SetMaterialProperty(Shader.PropertyToID("_DistortFadeDist"), 3); + renderer.SetMaterialProperty(Shader.PropertyToID("_MassScale"), _joining ? -1 : 1); + renderer.SetMaterialProperty(Shader.PropertyToID("_MaxDistortRadius"), 10); + renderer.SetMaterialProperty(Shader.PropertyToID("_Radius"), 1); + renderer.SetColor(_joining ? Color.white * 2 : Color.black); + + var warpedObjectGeometry = effect._warpedObjectGeometry.InstantiateInactive(); + warpedObjectGeometry.transform.parent = transform; + warpedObjectGeometry.transform.localPosition = Vector3.zero; + warpedObjectGeometry.transform.localRotation = Quaternion.identity; + warpedObjectGeometry.transform.localScale = Vector3.one; + effect._warpedObjectGeometry = warpedObjectGeometry; + + warpedObjectGeometry.SetActive(true); effectGo.SetActive(true); - effect.OnWarpComplete += () => - { - DebugLog.DebugWrite($"{player.TransformSync} warp complete"); + _player.SetVisible(false); - Object.Destroy(go); + effect.OnWarpComplete += OnWarpComplete; - if (playerGo) - { - playerGo.SetActive(true); - } - }; - const float length = 3; - if (joining) + yield return new WaitForSeconds(1); + + const float length = 1; + if (_joining) { - DebugLog.DebugWrite($"{player.TransformSync} warp in (white hole)"); + DebugLog.DebugWrite($"WARP IN {_player.TransformSync}"); effect.WarpObjectIn(length); } else { - DebugLog.DebugWrite($"{player.TransformSync} warp out (black hole)"); + DebugLog.DebugWrite($"WARP OUT {_player.TransformSync}"); effect.WarpObjectOut(length); } } + + private void OnWarpComplete() + { + DebugLog.DebugWrite($"WARP DONE {_player.TransformSync}"); + + Destroy(gameObject); + + _player.SetVisible(true); + } } } diff --git a/QSB/Player/Messages/JoinLeaveSingularityMessage.cs b/QSB/Player/Messages/JoinLeaveSingularityMessage.cs deleted file mode 100644 index 79348c28..00000000 --- a/QSB/Player/Messages/JoinLeaveSingularityMessage.cs +++ /dev/null @@ -1,18 +0,0 @@ -using QSB.Messaging; - -namespace QSB.Player.Messages -{ - /// - /// sent by non-hosts only - /// - public class JoinLeaveSingularityMessage : QSBMessage - { - public JoinLeaveSingularityMessage(bool joining) => Value = joining; - - public override void OnReceiveRemote() - { - var player = QSBPlayerManager.GetPlayer(From); - JoinLeaveSingularity.Create(player, Value); - } - } -} diff --git a/QSB/Player/Messages/PlayerJoinMessage.cs b/QSB/Player/Messages/PlayerJoinMessage.cs index dc9f5159..b86b7c26 100644 --- a/QSB/Player/Messages/PlayerJoinMessage.cs +++ b/QSB/Player/Messages/PlayerJoinMessage.cs @@ -76,6 +76,9 @@ namespace QSB.Player.Messages player.Name = PlayerName; DebugLog.ToAll($"{player.Name} joined!", MessageType.Info); DebugLog.DebugWrite($"{player.Name} joined. id:{player.PlayerId}, qsbVersion:{QSBVersion}, gameVersion:{GameVersion}, dlcInstalled:{DlcInstalled}", MessageType.Info); + + Delay.RunWhen(() => player.TransformSync.IsValid && player.TransformSync.ReferenceTransform, + () => JoinLeaveSingularity.Create(player, true)); } public override void OnReceiveLocal() diff --git a/QSB/Player/TransformSync/PlayerTransformSync.cs b/QSB/Player/TransformSync/PlayerTransformSync.cs index 94c5d3a8..20d65ef6 100644 --- a/QSB/Player/TransformSync/PlayerTransformSync.cs +++ b/QSB/Player/TransformSync/PlayerTransformSync.cs @@ -46,19 +46,12 @@ namespace QSB.Player.TransformSync DebugLog.DebugWrite($"Create Player : id<{Player.PlayerId}>", MessageType.Info); } - public override void OnStartLocalPlayer() - { - LocalInstance = this; - - if (!QSBCore.IsHost) - { - Delay.RunWhen(() => IsValid && ReferenceTransform, - () => new JoinLeaveSingularityMessage(true).Send()); - } - } + public override void OnStartLocalPlayer() => LocalInstance = this; public override void OnStopClient() { + JoinLeaveSingularity.Create(Player, false); + // TODO : Maybe move this to a leave event...? Would ensure everything could finish up before removing the player QSBPlayerManager.OnRemovePlayer?.Invoke(Player); base.OnStopClient();