From 0840510f9c3377e430976872d5014ebe32cbba54 Mon Sep 17 00:00:00 2001 From: Abbas Jafari Date: Tue, 19 Dec 2023 11:48:57 +0100 Subject: [PATCH] Circuit checking in all steps --- .../Android/addressables_content_state.bin | Bin 3418 -> 3418 bytes .../Scripts/BitsBehaviourController.cs | 64 +++++++++++++----- .../eROBSON/Scripts/ErobsonItemManager.cs | 18 +++-- .../Player/Prefabs/eROBSON/Scripts/Port.cs | 9 ++- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/Assets/AddressableAssetsData/Android/addressables_content_state.bin b/Assets/AddressableAssetsData/Android/addressables_content_state.bin index 341bd65ba45a94861cf67c57386fb7277f879a4b..1d33387e0494b2d5cbc352bd643ca1cb41ca55d2 100644 GIT binary patch delta 115 zcmca5bxUeP2P2oUo{@o`v4x(I#pX$jcUcuu%`K8ljgykn%uNi^EG^QEl1wZNQq5Bg m%o5E_6D>_P_p?W{st{7u-4w_e0R$Ks7`OqB1|9eS delta 115 zcmca5bxUeP2P2n(o)Hk4=oy)Ap2T>URUswKz#`4mG9}eAE!8N++#ty$HOVqHEzvN^ l&@3q}#bR?mdo-&GAys9|{iaUl<8|N(c2;Bn0Y(M}ZU73NAjbdz diff --git a/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/BitsBehaviourController.cs b/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/BitsBehaviourController.cs index e233d1843..98c56b959 100644 --- a/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/BitsBehaviourController.cs +++ b/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/BitsBehaviourController.cs @@ -102,7 +102,7 @@ public void BitActivatingToggle() { _circuitControlling = false; _eRobsonItem.IsActive = !_eRobsonItem.IsActive; - ControlCircuit(); + _ = ControlCircuit(); } @@ -110,7 +110,7 @@ public void BitActivatingToggle() /// /// Control every bit in this circuit and activate/deactivate it if it is not connected to power source /// - public async void ControlCircuit(bool checkConnections = false) + public async Task ControlCircuit(bool checkConnections = false) { try { @@ -179,7 +179,7 @@ public async void ControlCircuit(bool checkConnections = false) { if (ErobsonItemManager.ERobsonConnectedItemsListByPlayer.Count == ErobsonItemManager.ERobsonConnectedItemsListByTeacher.Count) { - ComparePlayerCircuit(); + await ComparePlayerCircuit(); } } @@ -237,23 +237,38 @@ private eROBSONItems FindUsbPowerBit() /// The next connected bit if found, otherwise null. private eROBSONItems FindNextBit(eROBSONItems currentBit) { - // Assuming each bit has a list of ports and each port knows what it's connected to + if (!currentBit) + { + return null; + } + + // Check each port of the current bit foreach (var port in currentBit.Ports) { - if ((port.Pole == Pole.NEGATIVE || port.DetectedPortPole.Pole == Pole.USB) && port.Connected) + // Skip if the port is not negative or USB, or if it's not connected + if ((port.Pole != Pole.NEGATIVE && port.Pole != Pole.USB) || !port.Connected) { - // Assuming the connected port has a reference to the bit it's part of - var connectedBit = port.DetectedPortPole.ERobsonItem; + continue; + } - // Verify that the connected port is positive - if (connectedBit != null && (port.DetectedPortPole.Pole == Pole.POSITIVE || port.DetectedPortPole.Pole == Pole.USB)) - { - return connectedBit; - } + // Check if the DetectedPortPole is null to avoid null reference error + if (port.DetectedPortPole == null) + { + continue; + } + + // Get the bit connected to this port + var connectedBit = port.DetectedPortPole.ERobsonItem; + + // Verify that the connected bit's port is either positive or USB + if (connectedBit != null && (port.DetectedPortPole.Pole == Pole.POSITIVE || port.DetectedPortPole.Pole == Pole.USB)) + { + return connectedBit; } } - return null; // No next bit found + // If no next bit found or if all next bits have null DetectedPortPole + return null; } @@ -262,7 +277,7 @@ private eROBSONItems FindNextBit(eROBSONItems currentBit) /// /// Control if the user in playmode has connected the bits same as the editor /// - private void ComparePlayerCircuit() + private async Task ComparePlayerCircuit() { bool allConnectedCorrectly = true; @@ -283,6 +298,7 @@ private void ComparePlayerCircuit() } if (ErobsonItemManager.Instance.PromptMessageIsOpen) { + await Task.CompletedTask; return; } @@ -291,12 +307,19 @@ private void ComparePlayerCircuit() // Display result if (allConnectedCorrectly) { - DialogWindow.Instance.Show("Success!", "Circuit connected correctly", new DialogButtonContent("Close")); + RootView_v2.Instance.dialog.ShowMiddle( + "Success!", + "Circuit connected correctly", + "OK", () => Debug.Log("Left - click!"), + "OK", () => Debug.Log("Left - click!"), + true); } else { DialogWindow.Instance.Show("Warning!", "You connected the bits wrong", new DialogButtonContent("OK")); } + + await Task.CompletedTask; } @@ -447,7 +470,7 @@ private void OnItemConnected(eROBSONItems bit) } } - ControlCircuit(true); + _ = ControlCircuit(true); } @@ -489,7 +512,7 @@ private void OnItemDisconnected(eROBSONItems bit) } } - ControlCircuit(true); + _ = ControlCircuit(true); } @@ -507,7 +530,7 @@ public async void SetValue() _eRobsonItem.Value = pinchSlider.SliderValue; - ControlCircuit(); + await ControlCircuit(); await Task.Delay(100); } @@ -587,6 +610,11 @@ private async void BitActionToggle(eROBSONItems bit, bool status) /// private async Task ControlLedLight(eROBSONItems bit, bool status, float averageValue) { + if (!bit) + { + return; + } + var light = bit.GetComponentInChildren(); light.enabled = status; light.intensity = averageValue; diff --git a/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/ErobsonItemManager.cs b/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/ErobsonItemManager.cs index 15ddddb3a..0c89c81c9 100644 --- a/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/ErobsonItemManager.cs +++ b/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/ErobsonItemManager.cs @@ -108,6 +108,7 @@ public static ErobsonItemManager Instance private string _eRobsonDataFolder; + /// /// Save the circuit data into a json file /// @@ -276,7 +277,7 @@ public void CutCircuitPower() } - private void Start() + private void Awake() { //Singleton if (Instance == null) @@ -292,7 +293,11 @@ private void Start() ERobsonActiveConnectedItemsList = new List(); ERobsonConnectedItemsListByTeacher = new List(); ERobsonConnectedItemsListByPlayer = new List(); + } + + private void Start() + { Subscribe(); StartCoroutine(Init()); @@ -334,9 +339,6 @@ private void Unsubscribe() /// private async void LoadERobsonCircuit() { - //wait a bit for Mirage XR to load anything in the step - await Task.Delay(500); - //Load json file ERobsonCircuit circuit = null; var jsonPath = $"{_eRobsonDataFolder}/eRobsonCircuit.json"; @@ -627,6 +629,14 @@ private IEnumerator Init() } _eRobsonDataFolder = Path.Combine(ActivityManager.ActivityPath, $"eRobson/{ActivityManager.ActiveAction.id}"); + foreach (var bit in ERobsonActiveConnectedItemsList) + { + foreach (var port in bit.Ports) + { + port.Disconnect(); + } + } + ERobsonItemsList.Clear(); ERobsonActiveConnectedItemsList.Clear(); ERobsonConnectedItemsListByTeacher.Clear(); diff --git a/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/Port.cs b/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/Port.cs index f0f3b8b75..a7b6a0abc 100644 --- a/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/Port.cs +++ b/Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/Port.cs @@ -385,7 +385,7 @@ private void Connect(Port detectedPort) /// /// When the port is disconnected /// - public void Disconnect() + public void Disconnect(bool withoutEventTrigger = false) { if (!DetectedPortPole) { @@ -403,8 +403,11 @@ public void Disconnect() ERobsonItem.ConnectedBits.Remove(DetectedPortPole.ERobsonItem); } - ErobsonItemManager.BitDisconnected(ERobsonItem); - ErobsonItemManager.BitDisconnected(DetectedPortPole.ERobsonItem); + if (!withoutEventTrigger) + { + ErobsonItemManager.BitDisconnected(ERobsonItem); + ErobsonItemManager.BitDisconnected(DetectedPortPole.ERobsonItem); + } DetectedPortPole = null;