Skip to content
Snippets Groups Projects
BlockObjectClass.cs 3.03 KiB
Newer Older
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Raymond Chia's avatar
Raymond Chia committed
using UnityEngine.Audio;
using TMPro;

public class BlockObjectClass : MonoBehaviour
{
    public float InhalePeriod { get; private set; }
    public float ExhalePeriod { get; private set; }
    public float MaxRuntime { get; private set; }

Raymond Chia's avatar
Raymond Chia committed
    public AudioSource InhaleAudio;
    public AudioSource ExhaleAudio;

Raymond Chia's avatar
Raymond Chia committed
    TMP_InputField inhalePeriodTextInput;
    TMP_InputField exhalePeriodTextInput;
    TMP_InputField runtimeTextInput;

    void Awake()
    {
        string name;
        GameObject go;
        Transform[] transforms = this.GetComponentsInChildren<Transform>();

        foreach (var transform in transforms)
        {
            go = transform.gameObject;
            name = go.name;
            if (name == "Inhale")
            {
Raymond Chia's avatar
Raymond Chia committed
                inhalePeriodTextInput = go.GetComponent<TMP_InputField>();
Raymond Chia's avatar
Raymond Chia committed
                InhaleAudio = go.GetComponentInChildren<AudioSource>();
                SetInhalePeriod();
            }
            else if (name == "Exhale")
            {
Raymond Chia's avatar
Raymond Chia committed
                exhalePeriodTextInput = go.GetComponent<TMP_InputField>();
Raymond Chia's avatar
Raymond Chia committed
                ExhaleAudio = go.GetComponentInChildren<AudioSource>();
                SetExhalePeriod();
            }
            else if (name == "Runtime")
            {
Raymond Chia's avatar
Raymond Chia committed
                runtimeTextInput = go.GetComponent<TMP_InputField>();
                SetRuntime();
            }
        }
    }

    // Start is called before the first frame update
    void Start()
    {

    }

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

Raymond Chia's avatar
Raymond Chia committed
    public void SetInhalePeriod(float inhalePeriod=-1f)
Raymond Chia's avatar
Raymond Chia committed
        if (inhalePeriod == -1)
        {
            InhalePeriod = (float)Convert.ToDouble(inhalePeriodTextInput.text);
        }
        else
        {
            InhalePeriod = inhalePeriod;
            inhalePeriodTextInput.text = inhalePeriod.ToString();
Raymond Chia's avatar
Raymond Chia committed
        }
Raymond Chia's avatar
Raymond Chia committed
        float period = (float) Math.Round(inhalePeriod, 1);
Raymond Chia's avatar
Raymond Chia committed
        String inhale_file = "inhale_" + period.ToString("0.0");
Raymond Chia's avatar
Raymond Chia committed

        AudioClip clip = Resources.Load(inhale_file) as AudioClip;
        InhaleAudio.clip = clip;
Raymond Chia's avatar
Raymond Chia committed

Raymond Chia's avatar
Raymond Chia committed
    public void SetExhalePeriod(float exhalePeriod=-1f)
Raymond Chia's avatar
Raymond Chia committed
        if (exhalePeriod == -1)
        {
            ExhalePeriod = (float)Convert.ToDouble(exhalePeriodTextInput.text);
        }
        else
        {
            ExhalePeriod = exhalePeriod;
            exhalePeriodTextInput.text = exhalePeriod.ToString();
Raymond Chia's avatar
Raymond Chia committed
        }
Raymond Chia's avatar
Raymond Chia committed
        float period = (float)Math.Round(exhalePeriod, 2);
        String exhale_file = "exhale_" + period.ToString("0.0");
        
        AudioClip clip = Resources.Load(exhale_file) as AudioClip;
        ExhaleAudio.clip = clip;
Raymond Chia's avatar
Raymond Chia committed
    public void SetRuntime(float runtimeInput=-1f)
Raymond Chia's avatar
Raymond Chia committed
        if (runtimeInput == -1)
        {
            MaxRuntime = (float)Convert.ToDouble(runtimeTextInput.text) * 60;
        }
        else
        {
            MaxRuntime = runtimeInput;
            runtimeTextInput.text = runtimeInput.ToString();
Raymond Chia's avatar
Raymond Chia committed
        }