diff --git a/Behaviours/AirJump.cs b/Behaviours/AirJump.cs index c0f58e0..3ead887 100644 --- a/Behaviours/AirJump.cs +++ b/Behaviours/AirJump.cs @@ -19,10 +19,11 @@ class AirJump : MonoBehaviour public static AirJump instance; string fileLocation = string.Format("{0}/SaveData", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); - string[] fileArray = new string[3]; + string[] fileArray = new string[4]; public bool modEnabled; public bool isInModdedRoom; + public bool otherCollisions; bool isLeftPressed; bool isRightPressed; @@ -66,12 +67,14 @@ void Awake() { fileArray = File.ReadAllText(fileLocation).Split(','); modEnabled = bool.Parse(fileArray[0]); + otherCollisions = bool.Parse(fileArray[3]); UpdateSize(int.Parse(fileArray[1])); UpdateMat(int.Parse(fileArray[2])); } else { modEnabled = false; + otherCollisions = true; currentSizeIndex = 0; currentMaterialIndex = 0; fileArray[0] = modEnabled.ToString(); @@ -79,6 +82,7 @@ void Awake() fileArray[2] = currentMaterialIndex.ToString(); } + PhotonNetwork.AddCallbackTarget(this); PhotonNetwork.NetworkingClient.EventReceived += NetworkJump; } @@ -119,7 +123,7 @@ void Update() { if (!onceRight) { - rightJump.transform.position = new Vector3(0, (float)-0.0075, 0) + player.rightHandTransform.position; + rightJump.transform.position = new Vector3(0, -0.0075f, 0) + player.rightHandTransform.position; //rightJump.transform.rotation = Quaternion.Euler(0, 45, 0) * player.rightHandTransform.rotation; rightJump.transform.rotation = player.rightHandTransform.rotation; @@ -154,19 +158,46 @@ public void LeaveModded() GameObject.Destroy(obj); } - public void UpdateEnabled() + public void UpdateEnabled(bool? toEnable = null) { - modEnabled = !modEnabled; - if (!modEnabled) - { + modEnabled = (toEnable.HasValue ? toEnable.Value : !modEnabled); + + if (!modEnabled) { leftJump.transform.position = new Vector3(0, -999, 0); rightJump.transform.position = new Vector3(0, -999, 0); + UpdateCollisions(false); + + } else { + UpdateCollisions(modEnabled && isInModdedRoom && otherCollisions); } fileArray[0] = modEnabled.ToString(); File.WriteAllText(fileLocation, string.Join(",", fileArray)); } + public void ToggleCollisions(bool? toEnable = null) + { + otherCollisions = toEnable ?? !otherCollisions; + UpdateCollisions(otherCollisions); + + fileArray[3] = otherCollisions.ToString(); + File.WriteAllText(fileLocation, string.Join(",", fileArray)); + } + + public void UpdateCollisions(bool toEnable) + { + try { + foreach (var platform in leftJumpNetwork.Values) { + platform.GetComponent().enabled = toEnable; + } + + foreach (var platform in rightJumpNetwork.Values) { + platform.GetComponent().enabled = toEnable; + } + + } catch { } + } + public void UpdateSize(int index) { leftJump.transform.localScale = sizes[index]; @@ -217,51 +248,53 @@ void NetworkJump(EventData eventData) { object[] data = null; - if (eventData.CustomData != null) + if (eventData.CustomData != null) { data = (object[])eventData.CustomData; + + } switch (eventCode) { - case (byte)PhotonEventCodes.LeftJump: - leftJumpNetwork.Add(PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId, CreateJumpNetwork((Vector3)data[0], (Quaternion)data[1], (int)data[2], (int)data[3])); + case (byte)PhotonEventCodes.LeftJump: { + UpdateNetworkedPlatform(data, eventData.Sender, leftJumpNetwork); break; - case (byte)PhotonEventCodes.RightJump: - rightJumpNetwork.Add(PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId, CreateJumpNetwork((Vector3)data[0], (Quaternion)data[1], (int)data[2], (int)data[3])); + } + + case (byte)PhotonEventCodes.RightJump: { + UpdateNetworkedPlatform(data, eventData.Sender, rightJumpNetwork); break; - case (byte)PhotonEventCodes.LeftJumpDeletion: - GameObject.Destroy(leftJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId]); - leftJumpNetwork.Remove(PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId); + } + + case (byte)PhotonEventCodes.LeftJumpDeletion: { + if (leftJumpNetwork.TryGetValue(PhotonNetwork.CurrentRoom?.GetPlayer(eventData.Sender)?.UserId, out var platform)) { + platform?.SetActive(false); + } break; - case (byte)PhotonEventCodes.RightJumpDeletion: - GameObject.Destroy(rightJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId]); - rightJumpNetwork.Remove(PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId); + } + + case (byte)PhotonEventCodes.RightJumpDeletion: { + if (rightJumpNetwork.TryGetValue(PhotonNetwork.CurrentRoom?.GetPlayer(eventData.Sender)?.UserId, out var platform)) { + platform?.SetActive(false); + } break; - case (byte)PhotonEventCodes.UpdateJump: - if ((bool)data[0]) - { - if(rightJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId] != null) - { - if((int)data[1] == 0) - rightJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId].GetComponent().material.SetColor("_Color", Color.black); - else - rightJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId].GetComponent().material = materials[(int)data[1]]; - } - if (leftJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId] != null) - { - if ((int)data[1] == 0) - leftJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId].GetComponent().material.SetColor("_Color", Color.black); - else - leftJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId].GetComponent().material = materials[(int)data[1]]; + } + + case (byte)PhotonEventCodes.UpdateJump: { + string userKey = PhotonNetwork.CurrentRoom?.GetPlayer(eventData.Sender)?.UserId; + + if (data[1] is int index && leftJumpNetwork.TryGetValue(userKey, out var leftPlatform) && rightJumpNetwork.TryGetValue(userKey, out var rightPlatform)) { + if ((bool)data[0]) { + SetPlatformMaterial(ref rightPlatform, index); + SetPlatformMaterial(ref leftPlatform, index); + + } else { + rightPlatform.transform.localScale = sizes[index]; + leftPlatform.transform.localScale = sizes[index]; } } - else - { - if (rightJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId] != null) - rightJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId].transform.localScale = sizes[(int)data[1]]; - if (leftJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId] != null) - leftJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId].transform.localScale = sizes[(int)data[1]]; - } break; + } + default: //just incase break; @@ -279,19 +312,72 @@ GameObject CreateJump() GameObject CreateJumpNetwork(Vector3 position, Quaternion rotation, int sizeIndex, int matIndex) { - GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube); - obj.transform.localScale = sizes[sizeIndex]; - obj.transform.position = position; - obj.transform.rotation = rotation; + GameObject platformObject = GameObject.CreatePrimitive(PrimitiveType.Cube); + platformObject.GetComponent().enabled = otherCollisions && isInModdedRoom && modEnabled; + SetJumpNetwork(ref platformObject, position, rotation, sizeIndex, matIndex); - if (matIndex == 0) - obj.GetComponent().material.SetColor("_Color", Color.black); - else - obj.GetComponent().material = materials[matIndex]; + return platformObject; + } - return obj; + void SetJumpNetwork(ref GameObject platformObject, Vector3 position, Quaternion rotation, int sizeIndex, int matIndex) + { + //GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube); + platformObject.transform.localScale = sizes[sizeIndex]; + platformObject.transform.position = position; + platformObject.transform.rotation = rotation; + + SetPlatformMaterial(ref platformObject, matIndex); } - } + + void SetPlatformMaterial(ref GameObject platformObject, in int index) + { + var renderer = platformObject?.GetComponent(); + if (index == 0) { + renderer.material.SetColor("_Color", Color.black); + + } else { + renderer.material = materials[index]; + } + } + + private void UpdateNetworkedPlatform(in object[] platformData, in int senderID, Dictionary platformMap) + { + try { + if (platformData?.Length < 4) { + Debug.Log("Platform information being sent is missing data"); + } + + string userKey = PhotonNetwork.CurrentRoom?.GetPlayer(senderID)?.UserId; + + if (platformData[0] is Vector3 pos && platformData[1] is Quaternion rot && platformData[2] is int platSize && platformData[3] is int platMat) { + if (platformMap.TryGetValue(userKey, out var platform)) { + SetJumpNetwork(ref platform, pos, rot, platSize, platMat); + if(!platform.activeSelf) { + platform.SetActive(true); + } + + } else { + platformMap.Add(userKey, CreateJumpNetwork(pos, rot, platSize, platMat)); + } + } + + } catch (Exception e) { + Debug.Log(e.ToString()); + } + } + public void PlayerLeftRoom(string userID) + { + if (rightJumpNetwork.TryGetValue(userID, out var rightPlatform)) { + GameObject.Destroy(rightPlatform); + rightJumpNetwork.Remove(userID); + } + + if (leftJumpNetwork.TryGetValue(userID, out var leftplatform)) { + GameObject.Destroy(leftplatform); + leftJumpNetwork.Remove(userID); + } + } + } public enum PhotonEventCodes { diff --git a/Behaviours/PlayerLeft.cs b/Behaviours/PlayerLeft.cs new file mode 100644 index 0000000..b74232e --- /dev/null +++ b/Behaviours/PlayerLeft.cs @@ -0,0 +1,20 @@ +using UnityEngine; +using Photon.Pun; + +namespace AirJump.Behaviours +{ + public class PlayerLeft : MonoBehaviour + { + private string userID; + + void Awake() + { + userID = GetComponent()?.Owner?.UserId; + } + + void OnDestroy() + { + AirJump.instance.PlayerLeftRoom(userID); + } + } +} diff --git a/ComputerInterface/AirJumpView.cs b/ComputerInterface/AirJumpView.cs index 47342d5..749d603 100644 --- a/ComputerInterface/AirJumpView.cs +++ b/ComputerInterface/AirJumpView.cs @@ -23,7 +23,7 @@ public AirJumpView() selectionHandler = new UISelectionHandler(EKeyboardKey.Up, EKeyboardKey.Down, EKeyboardKey.Enter); - selectionHandler.MaxIdx = 2; + selectionHandler.MaxIdx = 3; selectionHandler.OnSelected += OnEntrySelected; @@ -52,6 +52,8 @@ public void UpdateScreen() str.AppendLine(selectionHandler.GetIndicatedText(0, $"[Enabled]", highlightColour) : "white>[Disabled]")}")); str.AppendLine(selectionHandler.GetIndicatedText(1, $"Material: {matNames[Behaviours.AirJump.instance.currentMaterialIndex]}")); str.AppendLine(selectionHandler.GetIndicatedText(2, $"Size: {sizeNames[Behaviours.AirJump.instance.currentSizeIndex]}")); + //Not tested + str.AppendLine(selectionHandler.GetIndicatedText(3, $"Other collisions: [Enabled]", highlightColour) : "white>[Disabled]")}")); if (!Behaviours.AirJump.instance.isInModdedRoom) { @@ -68,9 +70,13 @@ private void OnEntrySelected(int index) switch (index) { case 0: - Behaviours.AirJump.instance.UpdateEnabled(); + Behaviours.AirJump.instance.UpdateEnabled(); UpdateScreen(); break; + + case 3: + Behaviours.AirJump.instance.ToggleCollisions(); + break; } } catch (Exception e) { Console.WriteLine(e); } diff --git a/Patches/PlayerPatch.cs b/Patches/PlayerPatch.cs index 73817c1..c6ad190 100644 --- a/Patches/PlayerPatch.cs +++ b/Patches/PlayerPatch.cs @@ -17,4 +17,16 @@ private static void Postfix(GorillaLocomotion.Player __instance) __instance.gameObject.AddComponent(); } } + + [HarmonyPatch(typeof(VRRig))] + [HarmonyPatch("Start", MethodType.Normal)] + class RigPatch + { + private static void Postfix(VRRig __instance) + { + if (!__instance.isOfflineVRRig && !__instance.photonView.IsMine) { + __instance.gameObject.AddComponent(); + } + } + } } diff --git a/Plugin.cs b/Plugin.cs index 3b566fa..086c974 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -1,11 +1,15 @@ using HarmonyLib; using BepInEx; using Bepinject; +using System; using System.Reflection; +using System.ComponentModel; using Utilla; +using UnityEngine; namespace AirJump { + [Description("HauntedModMenu")] [BepInPlugin(PluginInfo.GUID, PluginInfo.Name, PluginInfo.Version)] [BepInDependency("org.legoandmars.gorillatag.utilla", "1.5.0")] [ModdedGamemode] @@ -14,16 +18,53 @@ public class Plugin : BaseUnityPlugin public void Awake() { new Harmony(PluginInfo.GUID).PatchAll(Assembly.GetExecutingAssembly()); - Zenjector.Install().OnProject(); + try { + Zenjector.Install().OnProject(); + + } catch (Exception e) { + Debug.LogError(e.ToString()); + } } + void OnEnable() + { + if (Behaviours.AirJump.instance != null && Behaviours.AirJump.instance.modEnabled == false) { + Behaviours.AirJump.instance.UpdateEnabled(true); + + try { + UpdateScreen(); + + } catch (Exception e) { + Debug.LogWarning(e.ToString()); + } + } + } + + void OnDisable() + { + if (Behaviours.AirJump.instance != null && Behaviours.AirJump.instance.modEnabled == true) { + Behaviours.AirJump.instance.UpdateEnabled(false); + + try { + UpdateScreen(); + + } catch (Exception e) { + Debug.LogWarning(e.ToString()); + } + } + } + [ModdedGamemodeJoin] void JoinModded() { Behaviours.AirJump.instance.isInModdedRoom = true; - if (ComputerInterface.AirJumpView.instance != null) - ComputerInterface.AirJumpView.instance.UpdateScreen(); + try { + UpdateScreen(); + + } catch (Exception e) { + Debug.LogWarning(e.ToString()); + } } [ModdedGamemodeLeave] @@ -32,6 +73,16 @@ void LeaveModded() Behaviours.AirJump.instance.isInModdedRoom = false; Behaviours.AirJump.instance.LeaveModded(); + try { + UpdateScreen(); + + } catch (Exception e) { + Debug.LogWarning(e.ToString()); + } + } + + void UpdateScreen() + { if (ComputerInterface.AirJumpView.instance != null) ComputerInterface.AirJumpView.instance.UpdateScreen(); }