OneNote.cs 4.7 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//싱글노트
public class OneNote : MonoBehaviour
{
    public float speed; //노트 떨어지는 속도
    public bool isActive = false;//노트가 떨어질지 말지 결정
    private bool isinitial=true; //active 될때마다 위치초기화하기위해서.

    public int channel = 10;
    //public int  noteTime;   

    //굳이 생성될때마다 계산해서 넣을필요 없고 고정값 넣어줘도 되는애들
    public float[] PGB_standard = new float[3]; //perfect, good, bad 's standard
    public float destroyPositionX;
    public float destroyPositionY;
    public float sizeDif;


    private int PGB_result;
    private Vector3 waiting_loc = new Vector3(0.0f, 400.0f, 0.0f);

    public GameObject Button; //노트가 떨어질 버튼
    public GameObject controller;

    void Start()
    {
        controller = GameObject.Find("NoteControler");

        //고정값
        float r = GetComponent<SphereCollider>().bounds.size.y;
        PGB_standard[0] = r/3; 
        PGB_standard[1] = r/3*2; 
        PGB_standard[2] = r;
        speed = 0.6f;

        StartCoroutine(Move());

    }


    IEnumerator Move()
    {
        while (true)
        {
            if (isActive)
            {
                //처음 루프에 들어 왔으면
                if (isinitial)
                {
                    
                    //채널에 의해 초기위치로 이동
                    Button = GameObject.Find("Button_" + channel);
                    destroyPositionX = Button.GetComponent<SphereCollider>().bounds.center.x - GetComponent<SphereCollider>().bounds.center.x;
                    destroyPositionY = Button.GetComponent<SphereCollider>().bounds.center.y - GetComponent<SphereCollider>().bounds.center.y;
                    sizeDif = Button.GetComponent<RectTransform>().rect.width * Button.transform.localScale.x;

                    isinitial = false;
                }

                if (transform.position.y > Button.transform.position.y - 0.5)
                {
                    //Delta time : 1초/현재프레임 (보통 1초 60frame가정하지만 프레임 드랍 일어날수있으므로..) 
                    transform.Translate(new Vector3(0, destroyPositionY, 0) * speed * Time.smoothDeltaTime);
                    transform.Translate(new Vector3(destroyPositionX, 0, 0) * speed * Time.smoothDeltaTime);

                    /* 사이즈 크게하기
                    Vector3 temp = transform.localScale;
                    temp = temp * sizeDif * speed * Time.smoothDeltaTime;
                    transform.localScale += temp; */

                    //그냥 rectTranform의 rect.width * scale 해서 내가 구해야겠다.. 
                    if (GetComponent<RectTransform>().rect.width * transform.localScale.x < sizeDif)
                    { transform.localScale += new Vector3(0.1f, 0.1f, 0); }
                }
                else
                {
                    PGB_result = 3;
                    controller.GetComponent<NoteControl>().PGBM_count[PGB_result] += 1; //미스카운트 업! 
                    isActive = false;
                }

            }
            else
            {
                if (!isinitial) {
                    //위치, 크기, initial flag 초기화.
                    transform.localPosition = waiting_loc;
                    transform.localScale = new Vector3(1, 1, 1);
                    isinitial = true;
                }
            }
            yield return null;

        }




    }


    public void TouchOccur()
    {
        //노트 좌표와 col좌표의 거리 계산
        float distance = transform.position.y - Button.transform.position.y + transform.position.z - Button.transform.position.z;
        if (distance < 0) { distance = -distance; }

        if (distance < PGB_standard[1])
        {
            if (distance < PGB_standard[0])
            {
                //perfect
                transform.GetComponent<Image>().color = Color.red;
                Debug.Log(Time.time + "Perfect!");
                PGB_result = 0;
            }
            else
            {
                //good
                transform.GetComponent<Image>().color = Color.blue;
                Debug.Log(Time.time + "Good!");
                PGB_result = 1;

            }
        }
        else
        {
            //bad
            transform.GetComponent<Image>().color = Color.black;
            Debug.Log(Time.time + "Bad!");
            PGB_result = 2;
        }


        //Perfect/Good/Bad 카운트 업!
        controller.GetComponent<NoteControl>().PGBM_count[PGB_result] += 1;

        //해당쿨 종료!
        isActive = false;

    }


}