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

public class LDAsteroidSpawnComponent : MonoBehaviour
{
    public Vector2 MinMaxSpawnDelay;
    public GameObject Prefab_ThingToSpawn;

    private LDGameActor myOwningActor;
    private LDGameActor mySpawnedActor;
    private float fSpawnTimer = 0f;

    private GameManager theGame;

	// Use this for initialization
	void Start ()
	{
	    myOwningActor = GetComponent<LDGameActor>();
        theGame = GameManager.GetGame();
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (mySpawnedActor == null || mySpawnedActor.IsDead())
        {
            //count down to spawning a new one
            fSpawnTimer -= Time.deltaTime;

            if (fSpawnTimer <= 0f)
            {
                //is it time for me to go? :( 
                if( theGame.theAIManager.IsPositionNearLandmark( myOwningActor.vWorldPos, 10)) 
                {
                    Destroy(gameObject);
                    return;
                }

                DoSpawn();
                fSpawnTimer = Random.Range(MinMaxSpawnDelay.x, MinMaxSpawnDelay.y);
            }
        }
	}

    void DoSpawn()
    {
        mySpawnedActor = theGame.SpawnActorPrefab(myOwningActor.vWorldPos, Prefab_ThingToSpawn);
    }
}
