Skip to content

Commit

Permalink
Circuit checking in all steps
Browse files Browse the repository at this point in the history
  • Loading branch information
jafarigit committed Dec 19, 2023
1 parent 17e4029 commit 0840510
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 25 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ public void BitActivatingToggle()
{
_circuitControlling = false;
_eRobsonItem.IsActive = !_eRobsonItem.IsActive;
ControlCircuit();
_ = ControlCircuit();
}



/// <summary>
/// Control every bit in this circuit and activate/deactivate it if it is not connected to power source
/// </summary>
public async void ControlCircuit(bool checkConnections = false)
public async Task ControlCircuit(bool checkConnections = false)
{
try
{
Expand Down Expand Up @@ -179,7 +179,7 @@ public async void ControlCircuit(bool checkConnections = false)
{
if (ErobsonItemManager.ERobsonConnectedItemsListByPlayer.Count == ErobsonItemManager.ERobsonConnectedItemsListByTeacher.Count)
{
ComparePlayerCircuit();
await ComparePlayerCircuit();
}
}

Expand Down Expand Up @@ -237,23 +237,38 @@ private eROBSONItems FindUsbPowerBit()
/// <returns>The next connected bit if found, otherwise null.</returns>
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;
}


Expand All @@ -262,7 +277,7 @@ private eROBSONItems FindNextBit(eROBSONItems currentBit)
/// <summary>
/// Control if the user in playmode has connected the bits same as the editor
/// </summary>
private void ComparePlayerCircuit()
private async Task ComparePlayerCircuit()
{
bool allConnectedCorrectly = true;

Expand All @@ -283,6 +298,7 @@ private void ComparePlayerCircuit()
}
if (ErobsonItemManager.Instance.PromptMessageIsOpen)
{
await Task.CompletedTask;
return;
}

Expand All @@ -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;
}


Expand Down Expand Up @@ -447,7 +470,7 @@ private void OnItemConnected(eROBSONItems bit)
}
}

ControlCircuit(true);
_ = ControlCircuit(true);
}


Expand Down Expand Up @@ -489,7 +512,7 @@ private void OnItemDisconnected(eROBSONItems bit)
}
}

ControlCircuit(true);
_ = ControlCircuit(true);
}


Expand All @@ -507,7 +530,7 @@ public async void SetValue()

_eRobsonItem.Value = pinchSlider.SliderValue;

ControlCircuit();
await ControlCircuit();

await Task.Delay(100);
}
Expand Down Expand Up @@ -587,6 +610,11 @@ private async void BitActionToggle(eROBSONItems bit, bool status)
/// <returns></returns>
private async Task ControlLedLight(eROBSONItems bit, bool status, float averageValue)
{
if (!bit)
{
return;
}

var light = bit.GetComponentInChildren<Light>();
light.enabled = status;
light.intensity = averageValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static ErobsonItemManager Instance
private string _eRobsonDataFolder;



/// <summary>
/// Save the circuit data into a json file
/// </summary>
Expand Down Expand Up @@ -276,7 +277,7 @@ public void CutCircuitPower()
}


private void Start()
private void Awake()
{
//Singleton
if (Instance == null)
Expand All @@ -292,7 +293,11 @@ private void Start()
ERobsonActiveConnectedItemsList = new List<eROBSONItems>();
ERobsonConnectedItemsListByTeacher = new List<eROBSONItems>();
ERobsonConnectedItemsListByPlayer = new List<eROBSONItems>();
}


private void Start()
{
Subscribe();

StartCoroutine(Init());
Expand Down Expand Up @@ -334,9 +339,6 @@ private void Unsubscribe()
/// </summary>
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";
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 6 additions & 3 deletions Assets/MirageXR/Player/Prefabs/eROBSON/Scripts/Port.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ private void Connect(Port detectedPort)
/// <summary>
/// When the port is disconnected
/// </summary>
public void Disconnect()
public void Disconnect(bool withoutEventTrigger = false)
{
if (!DetectedPortPole)
{
Expand All @@ -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;

Expand Down

0 comments on commit 0840510

Please sign in to comment.