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

public class BlastScript : MonoBehaviour {
    SpriteRenderer spr;
    Vector3 opos;
 
    // Use this for initialization
    void Start () {
        opos = this.transform.position;

        // check for damage

        // erase old hearts
        GameObject[] gameObjects;
        gameObjects = GameObject.FindGameObjectsWithTag("Guy");

        Vector3 gp = gameObjects[0].transform.position;

        float d = Mathf.Abs(gp.x - opos.x);
//        Debug.Log("BlastDist = " + d);

        if (d < 0.8f)
        {
            Debug.Log("Close enough X");

            d = Mathf.Abs(gp.y - opos.y);
            if (d < 1.1f)
            {
                Debug.Log("Close enough Y" + d);

                int h = PlayerPrefs.GetInt("Health");
                h--;
                PlayerPrefs.SetInt("NeedOuch", 1);
                PlayerPrefs.SetInt("Health", h);
                if (h < 1) // game over
                {
                    PlayerPrefs.SetInt("Dying",1);
                }

                gameObjects = GameObject.FindGameObjectsWithTag("Sky");

                gameObjects[0].GetComponent<LandMaker>().DoHealth();
            }
            else Debug.Log("NOT Close enough Y" + d);

        }
        else Debug.Log("MISS X "+d);


    }

    // Update is called once per frame
    void Update () {

        spr = GetComponent<SpriteRenderer>();
        Color cl;

        cl = new Color(Random.Range(0.6f, 1f), Random.Range(0.5f, 0.8f), 0.5f);
        spr.color = cl;

        Vector3 v = this.transform.localScale;
        v.x *= 1f+4*Time.deltaTime;
        v.y = v.x;
        this.transform.localScale = v;
        if (v.x > 0.6f) Destroy(this.gameObject);

        /*        Vector3 p = this.transform.position;
                p.y = opos.y + 0.1f*(v.x / 0.2f);
                this.transform.position = p;
        */

        this.transform.rotation = Quaternion.identity;
        this.transform.Rotate(new Vector3(0, 0, Random.Range(-180f,180f))); 


    }
}
