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

public class BubbleScript : MonoBehaviour {

    float dir;

    private void Start()
    {
        dir = Random.Range(0, 360);
    }

    // Update is called once per frame
    void Update () {
        GetComponent<Rigidbody2D>().AddForce(new Vector2(Mathf.Cos(dir*Mathf.PI/180), Mathf.Sin(dir * Mathf.PI / 180))*10);
        dir += Random.Range(-3f, 3f);
        Vector2 v = Random.insideUnitSphere;
        RaycastHit2D r2h = Physics2D.Raycast(transform.position, v, 200, LayerMask.GetMask(new string[] { "enemy" }));
        if (r2h.rigidbody == null || r2h.distance > 40) return;
        if (Random.Range(0, 40) < r2h.distance) return;
        if (r2h.rigidbody.gameObject.GetComponent<BubbleScript>()) { 
            dir = Mathf.Atan2(v.y, v.x)/Mathf.PI*180;
        }
	}

    private void OnDestroy()
    {
        if (GameObject.Find("Keuhkoback") == null) return;
        GameObject.Find("Keuhkoback").GetComponent<KeuhkoScript>().poppedbubbles++;
    }
}
