simplify a whole bunch

This commit is contained in:
JohnCorby 2022-02-14 22:51:09 -08:00
parent 01e1acfc0f
commit e6194d624a
5 changed files with 63 additions and 84 deletions

View File

@ -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)
{

View File

@ -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>();
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<Component>())
{
if (component is not (Transform or Renderer))
{
Object.Destroy(component);
}
}
fakePlayerGo.SetActive(true);
var referenceEffect = joining ?
QSBWorldSync.GetUnityObjects<ProbeLauncher>()
.Select(x => x._probeRetrievalEffect)
.First(x => x) :
QSBWorldSync.GetUnityObjects<SurveyorProbe>()
.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<SingularityWarpEffect>();
effect._warpedObjectGeometry = fakePlayerGo;
var curve = AnimationCurve.EaseInOut(0, 0, .2f, 1);
effect._singularity._creationCurve = curve;
effect._singularity._destructionCurve = curve;
var renderer = effectGo.GetComponent<OWRenderer>();
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);
}
}
}

View File

@ -1,18 +0,0 @@
using QSB.Messaging;
namespace QSB.Player.Messages
{
/// <summary>
/// sent by non-hosts only
/// </summary>
public class JoinLeaveSingularityMessage : QSBMessage<bool>
{
public JoinLeaveSingularityMessage(bool joining) => Value = joining;
public override void OnReceiveRemote()
{
var player = QSBPlayerManager.GetPlayer(From);
JoinLeaveSingularity.Create(player, Value);
}
}
}

View File

@ -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()

View File

@ -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();