﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameController : MonoBehaviour
{
    public GameObject[] Balls;
    public Vector2 ballValue;
    public int ballCount;
    public float ballWait;
    public float waveWait;

    public Text scoreText;
    private int score = 0;

    private IEnumerator BallWaves()
    {
        while (true)
        {
            for (int i = 0; i < ballCount; ++i)
            {
                GameObject Ball = Balls[Random.Range(0, Balls.Length)];
                Vector2 spawnPosition = new Vector2(Random.Range(-ballValue.x, ballValue.x), ballValue.y);
                Quaternion spawnRotion = Quaternion.identity;
                Instantiate(Ball, spawnPosition, spawnRotion);
                yield return new WaitForSeconds(ballWait);
            }
            yield return new WaitForSeconds(waveWait);

        }
    }

    private void Start()
    {
        UpdateScore();
        StartCoroutine(BallWaves());
    }

    void UpdateScore()
    {
        scoreText.text = "Score: " + score.ToString();
    }
    
    public void AddScore(int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }
}
