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

//moves slowly with lots of fun pets around it
public class LDAISwarmMaster : LDGameActor
{

    public Vector2 MinMaxTimeBetweenHops;
    private float fTimeTillNextHop;

    public GameObject Prefab_SwarmBabby;
    public Vector2 MinMaxBabbies;
    List<LDAIMovementFlyingHunter> babbyFlies;


    private GameManager theGame;

    protected override void Start()
    {
        base.Start();

        theGame = GameManager.GetGame();

        //never draw me!
        var renderer = GetComponent<SpriteRenderer>();
        if (renderer != null)
        {
            renderer.enabled = false;
        }

        babbyFlies = new List<LDAIMovementFlyingHunter>();
        int iBabbyNum = Random.Range((int) MinMaxBabbies.x, (int) MinMaxBabbies.y + 1);
        for (int t = 0; t < iBabbyNum; t++)
        {
            LDGameActor hun = theGame.SpawnActorPrefab(vWorldPos, Prefab_SwarmBabby);
            LDAIMovementFlyingHunter hunMover = hun.GetComponent<LDAIMovementFlyingHunter>();
            if (hunMover != null)
            {
                babbyFlies.Add(hunMover);
            }

            hun.SetAnchorActor(this);
        }
    }

    public override bool CanBeTargeted()
    {
        return false;
    }

    protected override void Update()
    {
        base.Update();

        fTimeTillNextHop -= Time.deltaTime;
        if (fTimeTillNextHop < 0f)
        {
            DoHop();
        }

        if (babbyFlies.Count == 0)
        {
            //oh well
            Destroy(gameObject,0f);
        }

        babbyFlies.RemoveAll( a => a == null);
        babbyFlies.RemoveAll(a => a.myOwningActor.IsDead());
    }

    void DoHop()
    {
        fTimeTillNextHop = Random.Range(MinMaxTimeBetweenHops.x, MinMaxTimeBetweenHops.y);
    }

}
