quantum-space-buddies/QSB/Player/JoinLeaveSingularity.cs

153 lines
4.2 KiB
C#
Raw Normal View History

using Cysharp.Threading.Tasks;
2022-02-21 12:16:42 +00:00
using QSB.Animation.Player;
2022-02-17 07:45:54 +00:00
using QSB.Player.TransformSync;
using QSB.Utility;
using System.Threading;
2022-02-15 02:07:36 +00:00
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.Player;
public static class JoinLeaveSingularity
2022-02-15 02:07:36 +00:00
{
2022-03-03 03:46:33 +00:00
public static void Create(PlayerInfo player, bool joining)
2022-02-15 02:07:36 +00:00
{
2022-03-03 03:46:33 +00:00
if (player.IsLocalPlayer)
2022-02-15 02:07:36 +00:00
{
2022-03-03 03:46:33 +00:00
return;
}
2022-03-03 03:46:33 +00:00
if (joining)
{
if (PlayerTransformSync.LocalInstance == null ||
player.PlayerId < QSBPlayerManager.LocalPlayerId)
2022-02-18 12:25:14 +00:00
{
2022-03-03 03:46:33 +00:00
// player was here before we joined
return;
}
2022-03-03 03:46:33 +00:00
}
else
{
if (!player.Visible)
{
2022-03-03 03:46:33 +00:00
return;
}
if (!player.Body)
{
// player was kicked (i.e. player body doesn't exist cuz they're still in title screen)
return;
}
2022-03-03 03:46:33 +00:00
}
2022-02-17 12:43:57 +00:00
2022-03-06 09:01:02 +00:00
var go = new GameObject($"player {player} JoinLeaveSingularity");
2022-05-30 01:25:30 +00:00
// yes, it throws sometimes, but i cant destroy when the transform sync destroy or else the black hole doesn't happen
var ct = go.GetCancellationTokenOnDestroy();
2022-03-03 03:46:33 +00:00
UniTask.Create(async () =>
{
DebugLog.DebugWrite($"{go.name}: WARP TASK");
2022-02-17 07:45:54 +00:00
2022-03-03 03:46:33 +00:00
await go.name.Try("running warp task",
() => Run(go.transform, player, joining, ct));
Object.Destroy(go);
2022-03-03 03:46:33 +00:00
DebugLog.DebugWrite($"{go.name}: WARP TASK DONE");
});
}
2022-02-15 09:29:25 +00:00
2022-03-03 03:46:33 +00:00
private static async UniTask Run(Transform transform, PlayerInfo player, bool joining, CancellationToken ct)
{
if (joining)
{
2022-03-03 03:46:33 +00:00
await UniTask.WaitUntil(() => player.Body, cancellationToken: ct);
player.Body.SetActive(false);
await UniTask.WaitUntil(() => player.TransformSync.ReferenceTransform, cancellationToken: ct);
}
2022-02-15 07:35:23 +00:00
2022-03-03 03:46:33 +00:00
transform.parent = player.TransformSync.ReferenceTransform;
transform.localPosition = player.TransformSync.transform.position;
transform.localRotation = player.TransformSync.transform.rotation;
2022-02-15 09:29:25 +00:00
2022-03-03 03:46:33 +00:00
#region fake player
2022-02-15 09:29:25 +00:00
2022-03-03 03:46:33 +00:00
GameObject fakePlayer = null;
if (!joining)
{
player.Body.SetActive(false);
fakePlayer = player.Body.transform.Find("REMOTE_Traveller_HEA_Player_v2").gameObject.InstantiateInactive();
fakePlayer.transform.SetParent(transform, false);
Object.Destroy(fakePlayer.GetComponent<Animator>());
Object.Destroy(fakePlayer.GetComponent<PlayerHeadRotationSync>());
var REMOTE_ItemCarryTool = fakePlayer.transform.Find(
// TODO : kill me for my sins
"Traveller_Rig_v01:Traveller_Trajectory_Jnt/" +
"Traveller_Rig_v01:Traveller_ROOT_Jnt/" +
"Traveller_Rig_v01:Traveller_Spine_01_Jnt/" +
"Traveller_Rig_v01:Traveller_Spine_02_Jnt/" +
"Traveller_Rig_v01:Traveller_Spine_Top_Jnt/" +
"Traveller_Rig_v01:Traveller_RT_Arm_Clavicle_Jnt/" +
"Traveller_Rig_v01:Traveller_RT_Arm_Shoulder_Jnt/" +
"Traveller_Rig_v01:Traveller_RT_Arm_Elbow_Jnt/" +
"Traveller_Rig_v01:Traveller_RT_Arm_Wrist_Jnt/" +
"REMOTE_ItemCarryTool"
).gameObject;
Object.Destroy(REMOTE_ItemCarryTool);
fakePlayer.SetActive(true);
}
2022-02-15 02:07:36 +00:00
2022-03-03 03:46:33 +00:00
#endregion
2022-02-15 07:35:23 +00:00
2022-03-03 03:46:33 +00:00
#region effect
2022-02-15 06:51:09 +00:00
2022-03-03 03:46:33 +00:00
var effectGo = player.Body.transform.Find("JoinLeaveSingularity").gameObject.InstantiateInactive();
effectGo.transform.SetParent(transform, false);
2022-02-15 06:51:09 +00:00
2022-03-03 03:46:33 +00:00
var effect = effectGo.GetComponent<SingularityWarpEffect>();
effect._warpedObjectGeometry = joining ? player.Body : fakePlayer;
2022-02-15 08:28:24 +00:00
2022-03-03 03:46:33 +00:00
var singularity = effect._singularity;
singularity._creationCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);
singularity._destructionCurve = AnimationCurve.EaseInOut(0, 1, 1, 0);
2022-02-15 08:28:24 +00:00
2022-03-03 03:46:33 +00:00
var renderer = effectGo.GetComponent<Renderer>();
renderer.material.SetFloat("_DistortFadeDist", 3);
renderer.material.SetFloat("_MassScale", joining ? -1 : 1);
renderer.material.SetFloat("_MaxDistortRadius", 10);
renderer.transform.localScale = Vector3.one * 10;
renderer.material.SetFloat("_Radius", 1);
renderer.material.SetColor("_Color", joining ? Color.white : Color.black);
2022-02-15 06:51:09 +00:00
2022-03-03 03:46:33 +00:00
effectGo.SetActive(true);
2022-02-17 10:15:43 +00:00
2022-03-03 03:46:33 +00:00
#endregion
2022-03-03 03:46:33 +00:00
await UniTask.WaitForEndOfFrame();
2022-03-03 03:46:33 +00:00
if (joining)
{
player.Body.SetActive(true);
DebugLog.DebugWrite($"{transform.name}: WARP IN");
effect.WarpObjectIn(0);
}
else
{
DebugLog.DebugWrite($"{transform.name}: WARP OUT");
effect.WarpObjectOut(0);
}
2022-03-03 03:46:33 +00:00
effect.OnWarpComplete += () =>
{
DebugLog.DebugWrite($"{transform.name}: WARP DONE");
if (!joining)
{
2022-03-03 03:46:33 +00:00
Object.Destroy(fakePlayer);
}
};
await UniTask.WaitUntil(() => !effect.enabled && !singularity._owOneShotSource.isPlaying, cancellationToken: ct);
2022-02-15 02:07:36 +00:00
}
}