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

[Serializable]
public class JuiceDropper : MonoBehaviour {

	public List<Transform> JuicePrefabs;
	public float DropTimeSpan;
	public float DropInterval;
	public Vector2 PositionDelta;
	public Vector2 PositionOffset;


	private float startTime;
	private float lastDropTime;

	private GameController gc;
	
	
	// Use this for initialization
	void Start () {
		GameObject go = GameObject.Find ("GameController");
		gc = go.GetComponent<GameController>();
	}

	public void StartDropping()
	{
		startTime = Time.time;
		lastDropTime = 0;
	}
	
	
	void FixedUpdate () {
		if(Time.time - startTime > DropTimeSpan)
			return;

		if(Time.time - lastDropTime > DropInterval)
		{
			lastDropTime = Time.time;
			int jnum = gc.rand.Next(0, JuicePrefabs.Count);
			Transform juice = (Transform) GameObject.Instantiate(JuicePrefabs[jnum]);
			juice.position = transform.position + (Vector3)PositionOffset + (Vector3)(PositionDelta*(float)gc.rand.NextDouble() - PositionDelta*0.5f);
		}
	}
}
