using System.Collections;
using UnityEngine;
using UnityEngine.Serialization;


/*
WICHTIG
Für dieses Script muss ein Object namens "RespawnProjectile" vorhanden sein an dem man wieder spawnt
Collider Disabled Seconds muss so gesetzt werden das das Projectil aus der Quelle rauskommt ohne zerstört zu werden
*/

public class KitchenGun : MonoBehaviour
{
    // The shot projectiles
    public GameObject projectilePrefab;
    // Time between shots, game needs to be restarted to take effect
    public float shotIntervalSeconds = 1f;
    // Time alive for the projectiles or until they hit something
    public float shotAliveSeconds = 1f;
    // Time until collider gets enabled
    public float colliderDisabledSeconds = .2f;
    // Speed of projectiles
    public Vector2 shotSpeed = new Vector2(1f, 0f);
    // Smallest step size for shot speed
    private short shotSpeedStepSize = 50;
    [SerializeField] private bool dim1;
    private string dim;

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("RecurringBang",0f,shotIntervalSeconds);
        if(dim1){dim="Dim1";} else {dim="Dim2";}
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void RecurringBang()
    {
        StartCoroutine("Bang");
    }

    private IEnumerator Bang()
    {
        Transform t = transform;
        Vector3 position = t.position;
        
        // create projectile
        GameObject projectile = Instantiate(projectilePrefab, 
                                            position, 
                                            t.rotation);
        projectile.transform.parent = GameObject.Find(dim).transform;

        // add force to projectile
        projectile.GetComponent<Rigidbody2D>().velocity = shotSpeed;

        // wait until enabling collider
        yield return new WaitForSeconds(colliderDisabledSeconds);
        projectile.GetComponent<Collider2D>().enabled = true;

        // wait given time before destroying
        yield return new WaitForSeconds(shotAliveSeconds);
        
        Destroy(projectile);

    }
}