using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnZone : MonoBehaviour
{
	public Transform Prefab;
	public Vector3 SpawnArea;

	public float SpawnInterval = 1f;
	public float SpawnCount = 1f;
	public float InitialDelay = 0f;
	private float nextSpawn;

	void FixedUpdate()
    {
		if (Scoreboard.instance.playerDied || Scoreboard.instance.noMoreBalloons)
		{
			return;
		}

		if (Time.fixedTime >= nextSpawn)
		{
			nextSpawn = Time.fixedTime + SpawnInterval;
			for (int i = 0; i < SpawnCount; i++)
			{
				SpawnOne();
			}
		}
    }

	private void SpawnOne()
	{
		Transform t = Instantiate(Prefab);
		t.position = RandomPosition();
	}

	private Vector3 RandomPosition()
	{
		float rx = Random.Range(-0.5f, 0.5f);
		float ry = Random.Range(-0.5f, 0.5f);

		return transform.position + new Vector3(rx * SpawnArea.x, ry * SpawnArea.y, 0);
	}

	void Start()
    {
		nextSpawn = Time.fixedTime + InitialDelay;
	}
}
