using UnityEngine;
using System.Collections.Generic;

public class ObstacleController : MonoBehaviour {

    public List<GameObject> obstacles;
    public List<GameObject> activeObstacles;
    private int ticks = 0;
    public int nextObjectSpawn = 20;
    public float speed;
    public float xPos;
    public float yPos;

	// Use this for initialization
	void Start () {
        activeObstacles = new List<GameObject>();
	}
	
	// Update is called once per frame
	void Update () {
        if (((++ticks) % nextObjectSpawn) == 0)
        {
            CreateSpawnObject();
        }

        for (int i = 0; i < activeObstacles.Count; ++i)
        {
            var obstacle = activeObstacles[i];
            obstacle.transform.Translate(new Vector3(-speed * Time.deltaTime, 0, 0));
        }
        
	}

    void CreateSpawnObject()
    {
        var randomIndex = Random.Range(0, obstacles.Count);
        var obstacle = Instantiate(obstacles[randomIndex]);
        obstacle.transform.position = new Vector3(xPos, yPos, 0);
        obstacle.transform.parent = gameObject.transform;
        activeObstacles.Add(obstacle);
    }
}
