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

public class dropBombsScript : MonoBehaviour
{
    public GameObject bombPrefab;
    private GameObject[] invaders;

    public float bombFrequency = 2f; // one bomb every two seconds
    private float bombDropTimer = 0f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        bombDropTimer += Time.deltaTime;
        if (bombDropTimer > bombFrequency)
        {
            invaders = GameObject.FindGameObjectsWithTag("Enemy");
            int numVaders = invaders.Length;
            if (numVaders > 0)
            {
                GameObject theChosenDropper = invaders[Random.Range(0, numVaders)];
                var bomb = Instantiate(bombPrefab, theChosenDropper.transform.position - (transform.up / 2), theChosenDropper.transform.rotation);
                bomb.GetComponent<Rigidbody2D>().linearVelocity = transform.up * (-3);
                Destroy(bomb, 5f);
            }
           
            bombDropTimer = 0f;
        }
    }
}
