Skip to content
Snippets Groups Projects
AnimateCircle.cs 3.99 KiB
Newer Older
rchia16's avatar
rchia16 committed
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using TMPro;

public class AnimateCircle : MonoBehaviour
{
    public GameObject block_obj;

    float inhale_period = 0f;
    float exhale_period = 0f;
    float max_runtime = 0f;
    float runtime = 0f;
rchia16's avatar
rchia16 committed

    private bool is_inhale; /* 1 if inhalation, 0 if exhaltion */
    private Vector3 scale_diff = new Vector3(0.9f, 0.9f, 0.0f);
    private Vector3 original_scale = new Vector3(0.1f, 0.1f, 0.0f);
    private Vector3 final_scale = new Vector3(0.98f, 0.98f, 0.0f);
    private Vector3 inhale_velocity = new Vector3(0.0f, 0.0f, 0.0f);
    private Vector3 exhale_velocity = new Vector3(0.0f, 0.0f, 0.0f);

    // Start is called before the first frame update
    void Start()
    {
        is_inhale = true;
        original_scale = this.transform.localScale;
        scale_diff = final_scale - original_scale;

        StateReset();
        this.enabled = false;
rchia16's avatar
rchia16 committed
    }

    // Update is called once per frame
    void Update()
    {
        if(is_inhale)
        {
            UpdateInhaleScale();
        }
        else
        {
            UpdateExhaleScale();
        }
        UpdateRespState();
        UpdateRuntimeState();
        runtime += Time.deltaTime;
    }

    public void ResetAndRun()
    {
        this.enabled = true;
        StateReset();
    }

    private void OnDisable()
    {
        StateReset();
    }

    void StateReset()
    {
        SetPeriod();
        SetMaxRuntime();
        inhale_velocity = scale_diff / inhale_period;
        exhale_velocity = scale_diff / exhale_period;
        this.transform.localScale = original_scale;
        runtime = 0f;
rchia16's avatar
rchia16 committed
    }

    void SetPeriod()
    {
        string name;
        GameObject go;
        TMP_InputField input_field;
        Transform[] transforms = block_obj.GetComponentsInChildren<Transform>();

        foreach (var transform in transforms)
        {
            go = transform.gameObject;
            name = go.name;
            if (name == "Inhale")
            {
                input_field = go.GetComponent<TMP_InputField>();
                inhale_period = (float) Convert.ToDouble(input_field.text);
rchia16's avatar
rchia16 committed
            }
            else if (name == "Exhale")
            {
                input_field = go.GetComponent<TMP_InputField>();
                exhale_period = (float) Convert.ToDouble(input_field.text);
rchia16's avatar
rchia16 committed
            }
        }
        Debug.Log("inhale: " + inhale_period);
        Debug.Log("exhale: " + exhale_period);
rchia16's avatar
rchia16 committed
    }
    void SetMaxRuntime()
    {
        string name;
        GameObject go;
        TMP_InputField input_field;
        Transform[] transforms = block_obj.GetComponentsInChildren<Transform>();
rchia16's avatar
rchia16 committed

        foreach (var transform in transforms)
        {
            go = transform.gameObject;
            name = go.name;
            if (name == "Runtime")
            {
                input_field = go.GetComponent<TMP_InputField>();
                max_runtime = (float) Convert.ToDouble(input_field.text) * 60;
            }
        }
        Debug.Log("runtime: " + max_runtime);
    }
rchia16's avatar
rchia16 committed

    void UpdateInhaleScale()
    {
        float dt = Time.deltaTime;
        Vector3 scale = this.transform.localScale;
        this.transform.localScale = inhale_velocity*dt + scale;
    }

    void UpdateExhaleScale()
    {
        float dt = Time.deltaTime;
        Vector3 scale = this.transform.localScale;
        this.transform.localScale = -exhale_velocity*dt + scale;
    }

    void UpdateRespState()
    {
        Vector3 scale = this.transform.localScale;
        if(scale.x >= final_scale.x | scale.x <= original_scale.x)
        {
            if (is_inhale)
            {
                this.transform.localScale = final_scale;
            }
            else
            {
                this.transform.localScale = original_scale;
            }
            is_inhale = !is_inhale;
        }
    }

    void UpdateRuntimeState()
    {
        if (runtime >= max_runtime)
        {
            StateReset();
            this.enabled = false;
        }
    }
rchia16's avatar
rchia16 committed
}