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

public class CollectableScript : MonoBehaviour
{
    //public static int collectables; // globals are cool <- no

    Color color;
    SpriteRenderer sRenderer;

    float alphaTarget;
    bool taken;
    public AudioClip myclip;
    public void Awake()
    {
        // if (myclip == null) { myclip = Resources.Load<AudioClip>()}
        taken = false;
        //collectables++;
        sRenderer = GetComponent<SpriteRenderer>();
        color = sRenderer.color;
        alphaTarget = 0;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!taken && collision.gameObject.GetComponent<PlayerScript2>())
        {
            //if (collectables > 0) collectables--;
            //Destroy(g);
            GetComponent<SpriteRenderer>().enabled = false;
            transform.GetChild(0).GetComponent<SpriteRenderer>().enabled = false;
            gameObject.tag = "Untagged";
            //GetComponent<AudioSource>().PlayOneShot(myclip);
            taken = true;
        }
    }

    private void Update()
    {
        sRenderer.color = new Color(color.r, color.g, color.b, Mathf.Lerp(sRenderer.color.a, alphaTarget, Time.deltaTime));
        if (Mathf.Abs(alphaTarget - sRenderer.color.a) < 0.1)
        {
            alphaTarget = 1 - alphaTarget;
        }
    }
}
