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

public class Castle : Building
{
    public float PopulationGiven => GlobalConfig.Instance.Current.populationGivenPerHouse;

    [SerializeField]
    private Root _root;

    public override void Start()
    {
        base.Start();
    }

    public override void Update()
    {
        base.Update();
        if (IsEnemyDetected)
        {
            AttackEnemy();
        }

    }

    public override void LevelStart()
    {
        ResourceManager.Instance.MaximumPopulation += PopulationGiven * 2;
        base.LevelStart();
    }
    public override void LevelDestroy()
    {
        ResourceManager.Instance.MaximumPopulation -= PopulationGiven * 2;
        base.LevelDestroy();
    }

    void AttackEnemy()
    {
        //Attack Cooldown
        if (AttackInterval < AttackSpeedWait)
        {
            
            for(int i = 0; i < 4 && IsEnemyDetected; i++)
            {
                Invoke("ShootInterval", 0.1f * i);
                
                if (Enemy.Hp <= 0) IsEnemyDetected = false;
            }

            AttackSpeedWait = 0f;

        }

        AttackSpeedWait += Time.deltaTime;

    }

    void ShootInterval()
    {
        _root.ShotProjectile();
    }
}
