Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed computer interface breaking stuff #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 136 additions & 50 deletions Behaviours/AirJump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -66,19 +67,22 @@ 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();
fileArray[1] = currentSizeIndex.ToString();
fileArray[2] = currentMaterialIndex.ToString();
}

PhotonNetwork.AddCallbackTarget(this);
PhotonNetwork.NetworkingClient.EventReceived += NetworkJump;
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<Collider>().enabled = toEnable;
}

foreach (var platform in rightJumpNetwork.Values) {
platform.GetComponent<Collider>().enabled = toEnable;
}

} catch { }
}

public void UpdateSize(int index)
{
leftJump.transform.localScale = sizes[index];
Expand Down Expand Up @@ -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<Renderer>().material.SetColor("_Color", Color.black);
else
rightJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId].GetComponent<Renderer>().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<Renderer>().material.SetColor("_Color", Color.black);
else
leftJumpNetwork[PhotonNetwork.CurrentRoom.GetPlayer(eventData.Sender).UserId].GetComponent<Renderer>().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;
Expand All @@ -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<Collider>().enabled = otherCollisions && isInModdedRoom && modEnabled;
SetJumpNetwork(ref platformObject, position, rotation, sizeIndex, matIndex);

if (matIndex == 0)
obj.GetComponent<Renderer>().material.SetColor("_Color", Color.black);
else
obj.GetComponent<Renderer>().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<Renderer>();
if (index == 0) {
renderer.material.SetColor("_Color", Color.black);

} else {
renderer.material = materials[index];
}
}

private void UpdateNetworkedPlatform(in object[] platformData, in int senderID, Dictionary<string, GameObject> 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
{
Expand Down
20 changes: 20 additions & 0 deletions Behaviours/PlayerLeft.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEngine;
using Photon.Pun;

namespace AirJump.Behaviours
{
public class PlayerLeft : MonoBehaviour
{
private string userID;

void Awake()
{
userID = GetComponent<PhotonView>()?.Owner?.UserId;
}

void OnDestroy()
{
AirJump.instance.PlayerLeftRoom(userID);
}
}
}
10 changes: 8 additions & 2 deletions ComputerInterface/AirJumpView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public AirJumpView()

selectionHandler = new UISelectionHandler(EKeyboardKey.Up, EKeyboardKey.Down, EKeyboardKey.Enter);

selectionHandler.MaxIdx = 2;
selectionHandler.MaxIdx = 3;

selectionHandler.OnSelected += OnEntrySelected;

Expand Down Expand Up @@ -52,6 +52,8 @@ public void UpdateScreen()
str.AppendLine(selectionHandler.GetIndicatedText(0, $"<color={(Behaviours.AirJump.instance.modEnabled ? string.Format("#{0}>[Enabled]", highlightColour) : "white>[Disabled]")}</color>"));
str.AppendLine(selectionHandler.GetIndicatedText(1, $"Material: <color=#{highlightColour}>{matNames[Behaviours.AirJump.instance.currentMaterialIndex]}</color>"));
str.AppendLine(selectionHandler.GetIndicatedText(2, $"Size: <color=#{highlightColour}>{sizeNames[Behaviours.AirJump.instance.currentSizeIndex]}</color>"));
//Not tested
str.AppendLine(selectionHandler.GetIndicatedText(3, $"Other collisions: <color={(Behaviours.AirJump.instance.otherCollisions ? string.Format("#{0}>[Enabled]", highlightColour) : "white>[Disabled]")}</color>"));

if (!Behaviours.AirJump.instance.isInModdedRoom)
{
Expand All @@ -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); }
Expand Down
12 changes: 12 additions & 0 deletions Patches/PlayerPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ private static void Postfix(GorillaLocomotion.Player __instance)
__instance.gameObject.AddComponent<Behaviours.AirJump>();
}
}

[HarmonyPatch(typeof(VRRig))]
[HarmonyPatch("Start", MethodType.Normal)]
class RigPatch
{
private static void Postfix(VRRig __instance)
{
if (!__instance.isOfflineVRRig && !__instance.photonView.IsMine) {
__instance.gameObject.AddComponent<Behaviours.PlayerLeft>();
}
}
}
}
Loading