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

public class DangerManager : MonoBehaviour {

	public DangerItem dangerItem;

	public float spawnRate = 1.0f;

	private float spawnTimer = 0;

	// Use this for initialization
	void Start () {
		
	}
	
	public void Restart()
	{
		Gameplay gameplay = gameObject.GetComponentInParent<Gameplay>();
		var oldItems = gameplay.GetComponentsInChildren<DangerItem>();

		for (int i=0; i < oldItems.Length; i++)
			Destroy(oldItems[i].gameObject);

	}

	public Vector3 CreateStartPos()
	{
		float maxD = GetComponentInParent<Gameplay>().playerShip.maxDistance;
		Vector3 direction = new Vector3(Random.value*2.0f - 1.0f, Random.value*2.0f - 1.0f, 0);
		direction.Normalize();
		return direction * maxD * 1.5f;
	}
	
	// Update is called once per frame
	void Update () {
		Gameplay gameplay = gameObject.GetComponentInParent<Gameplay>();

		spawnTimer += gameplay.timeMultipler * Time.deltaTime;

		if (spawnTimer > spawnRate)
		{
			Instantiate(dangerItem, CreateStartPos(), new Quaternion(), gameplay.transform);
			spawnTimer = 0;
		}
		
	}
}
