using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class arasuzi : MonoBehaviour {
    [SerializeField]
    GameObject BaseObj;
    GameObject[] Objcts = new GameObject[17];
    string Message = "<size=50>　　　　　　　　　  世界的に　　巨大な　　正義</size>\n海水浴で賑わう「Globally Giant Justice」\n通称\"GGJ\"ビーチにある日突然津波が襲う！！\n\n溺れた人たちを救助するため一人の<color=red>サーファー</color>と\n一枚の<color=yellow>サーフボード</color>が立ち上がる";
    [SerializeField]
    float Yoffset = 10;
    [SerializeField]
    GameObject Fade;
    int touchflag = 0;
    void Start() {
        Fade.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        Fade.GetComponent<ColorFade>().Fadeout(4.0f);
        GetComponent<await>().start(15f);
        Objcts[0] = Instantiate(BaseObj);
        Objcts[0].transform.position += new Vector3(0.0f, -Yoffset, 0.0f);
        Objcts[0].GetComponent<TextMesh>().text = Message;
        Message = Message.Replace("<color=yellow>", "");
        Message = Message.Replace("<color=red>", "");
        Message = Message.Replace("</color>", "");
        for (int i = 1; i < Objcts.Length; i++) {

            Objcts[i] = Instantiate(BaseObj);
            Objcts[i].GetComponent<TextMesh>().text = Message;
            Objcts[i].GetComponent<TextMesh>().color = Color.black;

            Objcts[i].transform.position = new Vector3(
                Objcts[0].transform.position.x + Mathf.Sin((float)(i - 1) / 16 * Mathf.Deg2Rad * 360) * 0.1f,
                Objcts[0].transform.position.y + Mathf.Cos((float)(i - 1) / 16 * Mathf.Deg2Rad * 360) * 0.1f,
                Objcts[0].transform.position.z + 0.01f);
            //
        }

    }

    // Update is called once per frame]
    void Update() {
        for (int i = 0; i < Objcts.Length; i++) {
            Objcts[i].transform.position += new Vector3(0.0f, 1.0f * Time.deltaTime, 0.0f);
        }
        if(touchflag == 0) {
            if (Input.anyKeyDown) {
                touchflag = 1;
            }
            if (GetComponent<await>().await_finish()) {
                touchflag = 1;
            }
        }
        if (touchflag == 1) {
            Fade.GetComponent<ColorFade>().Fadein(4.0f);
            GetComponent<await>().start(4.0f);
            touchflag = 2;
        }
        if(touchflag == 2) {
            if (GetComponent<await>().await_finish()) {
                SceneManager.LoadScene("Title");
                Debug.Log("タイトル読み込み");
                touchflag = 3;
            }
        }
    }


}
