Skip to content
Snippets Groups Projects
ProtocolHandler.cs 3.61 KiB
Newer Older
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class ProtocolHandler : MonoBehaviour
{
rchia16's avatar
rchia16 committed
    public static ProtocolHandler Instance;
    public GameObject numCyclesInput;
    public GameObject blockControl;
Raymond Chia's avatar
Raymond Chia committed
    public GameObject centreCircle;
    public Canvas canvas;
    public GameObject countdownObject;
    public float TotalRuntime { get; private set; }
Raymond Chia's avatar
Raymond Chia committed
    public bool runProtocol = false; // not running

    BlockHandler blockHandler;
    List<GameObject> blockStack = new List<GameObject>();
Raymond Chia's avatar
Raymond Chia committed
    AnimateCircle animateCircle;
    RunCountdown runCountdown;
    int nCycles = 1;
    int currentBlockIndex = 0;
Raymond Chia's avatar
Raymond Chia committed
    int previousBlockIndex = -1;
    int currentCycle = 0;

rchia16's avatar
rchia16 committed
    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()
    {
Raymond Chia's avatar
Raymond Chia committed
        blockHandler = blockControl.GetComponent<BlockHandler>();
        animateCircle = centreCircle.GetComponent<AnimateCircle>();
        runCountdown = countdownObject.GetComponent<RunCountdown>();
    }

    // Update is called once per frame
    void Update()
    {

Raymond Chia's avatar
Raymond Chia committed
        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()
    {
Raymond Chia's avatar
Raymond Chia committed
        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++;
        }
    }
rchia16's avatar
rchia16 committed

    public void PrintBlockStack()
    {
        int i = 0;
        foreach (var block in blockStack)
        {
            BlockObjectClass boc = block.GetComponent<BlockObjectClass>();
            Debug.Log("boc " + i + " inhale: " + boc.InhalePeriod);
            i++;
        }
    }

Raymond Chia's avatar
Raymond Chia committed
    void RunBlock(int blockIndex)
    {
        blockHandler.SetRunningBlockIndex(blockIndex);
        blockHandler.SetRunningBlock();
        blockHandler.UpdateRunningBlock();
        animateCircle.ResetAndRun();
    }