using System;
using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour
{
    public GameObject BasicEnemy;
    public GameObject StaticEnemy;
    public GameObject Toluen;

    private float _timer;
    private System.Random _rand;
    private float _rightBorder;

	void Start ()
	{
	    _timer = 0f;
        _rand = new System.Random();
	}
	
	void Update ()
	{
	    _timer += Time.deltaTime;
	    _rightBorder = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0)).x + 2;

	    if (_timer >= Math.Max((2 / (Time.time / 10)), .5f))
	    {
	        Spawn();
	        _timer = 0f;
	    }
	}

    private void Spawn()
    {
        var r = _rand.Next(100);

        if (r < 20)
            Instantiate(StaticEnemy, new Vector3(_rightBorder, _rand.Next(3, 12), 0), Quaternion.identity);
        else if (r < 45)
            Instantiate(Toluen, new Vector3(_rightBorder, _rand.Next(1, 9), 0), Quaternion.identity);
        r = _rand.Next(100);
        if (r < 50)
            Instantiate(BasicEnemy, new Vector3(_rightBorder, _rand.Next(1, 12), 0), Quaternion.identity);
    }
}
