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

public class CandelScript : FreqReactorScript {

    public GameObject[] flames;
    
    FreqOriginScript freq_source;

    public bool lit;

    // Use this for initialization
    void Start() {
        lit = false;
        //Light();
        resonance_note = GetComponent<FreqFallScript>().resonance_note-1; //Random.Range(15f, 25f);
        resonance_frequency = Tools.Note_To_Freq(resonance_frequency);
        freq_source = GameObject.FindGameObjectWithTag("Freq").GetComponent<FreqOriginScript>();
    }

    // Update is called once per frame
    void Update() {
        if (freq_source == null) return;
        if (freq_source.Amplitude() > 0.3f && 
            Mathf.Abs(Tools.Freq_To_Note(freq_source.Freq()) - resonance_note) < 2) {
            if (Random.Range(0, 15) == 0) Light();
        }
        if (lit && Mathf.Abs(GetComponent<Rigidbody2D>().rotation) > 45) {
            character_killer[] cs = GameObject.FindObjectsOfType<character_killer>();
            
            for (int i =0; i <cs.Length; i++) {
                if (Vector3.Distance(cs[i].gameObject.transform.position, transform.position) < 3) {
                    cs[i].GetDamage(6);
                }
            }
            Destroy(this);
        }
    }
    void Light() {
        lit = true;
        for (int i = 0; i < flames.Length;i++) {
            flames[i].GetComponent<SpriteRenderer>().enabled = true;
        }
    }
}
