using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class ProtocolHandler : MonoBehaviour { public static ProtocolHandler Instance; public GameObject numCyclesInput; public GameObject blockControl; public GameObject centreCircle; public Canvas canvas; public GameObject countdownObject; public float TotalRuntime { get; private set; } public bool runProtocol = false; // not running BlockHandler blockHandler; List<GameObject> blockStack = new List<GameObject>(); AnimateCircle animateCircle; RunCountdown runCountdown; int nCycles = 1; int currentBlockIndex = 0; int previousBlockIndex = -1; int currentCycle = 0; static float timeBetweenBlocks = 3f; // seconds, not implemented private void Awake() { if (Instance != null) { Destroy(gameObject); return; } Instance = this; DontDestroyOnLoad(gameObject); } // Start is called before the first frame update void Start() { blockHandler = blockControl.GetComponent<BlockHandler>(); animateCircle = centreCircle.GetComponent<AnimateCircle>(); runCountdown = countdownObject.GetComponent<RunCountdown>(); } // Update is called once per frame void Update() { Debug.Log("is protocol running " + runProtocol); Debug.Log("is circle animating " + animateCircle.enabled); if (runProtocol && !animateCircle.enabled) { // if it's time to run and animation not running, then set to run and iterate RunBlock(currentBlockIndex); previousBlockIndex = currentBlockIndex; currentBlockIndex++; Debug.Log("Finished " + currentBlockIndex + " / " + blockStack.Count.ToString()); runProtocol = false; } else if (previousBlockIndex >= 0 && !animateCircle.enabled && currentBlockIndex < blockStack.Count) { runCountdown.enabled = true; } if (currentBlockIndex == blockStack.Count && !animateCircle.enabled) { // export data currentBlockIndex = 0; previousBlockIndex = -1; currentCycle = 0; runProtocol = false; animateCircle.enabled = false; animateCircle.MoveToOffset(); canvas.enabled = true; } } // Update runtime by adding each block runtime, adding 3s for each block, // and multiplying by cycles public void UpdateTotalRuntime() { TotalRuntime = blockHandler.blockStackRuntime / 60; TotalRuntime *= nCycles; } public void UpdateNumCycles() { TMP_InputField inputField = numCyclesInput.GetComponent<TMP_InputField>(); nCycles = (int)Convert.ToInt32(inputField.text); } public void SetBlockStack() { blockStack = blockHandler.GetBlockStack(); int i = 0; foreach (var block in blockStack) { BlockObjectClass boc = block.GetComponent<BlockObjectClass>(); i++; } } public void PrintBlockStack() { int i = 0; foreach (var block in blockStack) { BlockObjectClass boc = block.GetComponent<BlockObjectClass>(); Debug.Log("boc " + i + " inhale: " + boc.InhalePeriod); i++; } } void RunBlock(int blockIndex) { blockHandler.SetRunningBlockIndex(blockIndex); blockHandler.SetRunningBlock(); blockHandler.UpdateRunningBlock(); animateCircle.ResetAndRun(); } }