using Microsoft.Unity.VisualStudio.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallScripts : MonoBehaviour
{
    private GameObject Gamemanager;

    // Start is called before the first frame update
    public int nowBall = 1;

    //nʂ{[ɐGꂽ
    public bool isPut = false;

    //Ƃ
    public bool isfall = false;

    [SerializeField]
    private Sprite[] texture;

    private SpriteRenderer renderer;

    private float timecount;

    void Start()
    {
        renderer = GetComponent<SpriteRenderer>();

        Gamemanager = GameObject.Find("GameManager");

        this.gameObject.transform.localScale = new Vector2(0.5f * (nowBall * 0.7f), 0.5f * (nowBall * 0.7f));

        renderer.sprite = texture[nowBall - 1];
    }

    // Update is called once per frame
    void Update()
    {
        timecount += Time.deltaTime;

        this.gameObject.transform.localScale = new Vector2(0.5f * (nowBall * 0.7f), 0.5f * (nowBall * 0.7f));

        renderer.sprite = texture[nowBall - 1];

    }

    private void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Ball")
        {
            if (collision.gameObject.GetComponent<BallScripts>().nowBall == nowBall)
            {
                if (nowBall != 12)
                {
                    nowBall++;
                    Gamemanager.GetComponent<GameManager>().Score += (6 + ((4 * (nowBall - 1)) + (nowBall - 1))) / 2;
                    Destroy(collision.gameObject);
                }
                if (nowBall == 12)
                {
                    Destroy(collision.gameObject);
                    Destroy(this.gameObject);
                }
            }
        }
        if (!isPut && isfall && timecount > 2f)
        {
            if (collision.gameObject.tag == "Ball")
            {
                isPut = true;
                this.gameObject.tag = "Ball";
                timecount = 0f;
            }
            if (collision.gameObject.tag == "Wall")
            {
                isPut = true;
                this.gameObject.tag = "Ball";
                timecount = 0f;
            }
        }
    
    }

    private void OnValidate()
    {
        Start();
    }
}
