using UnityEngine;
using System.Collections;

public class AstroidCtrl : MonoBehaviour {

	public float minForwardSpeed = 0;
	public float maxForwardSpeed = 0.02f;
	public int HP = 2;
	public GameObject explodeEffect;

	float forwardSpeed = 0;

	Transform _transform;
	Transform player;
	float disappearLength=0;
	// Use this for initialization
	void Start () {
		_transform = transform;
		player = GameObject.FindWithTag ("Player").transform;
		disappearLength = GameManager.instance.GetCameraArea ().magnitude * 2;
		forwardSpeed = Random.Range (minForwardSpeed, maxForwardSpeed);
	}
	
	// Update is called once per frame
	void Update () {
		transform.position += transform.right * forwardSpeed * -1 * Time.deltaTime;
		if(Vector3.Distance(player.position, _transform.position)>disappearLength){
			Destroy(gameObject);
		}
	}

	void OnCollisionEnter2D(Collision2D coll){
		if(coll.gameObject.tag=="bullet"){
			SoundFXCtrl.instance.PlaySound (7, 0.5f);
			Destroy(coll.gameObject);
			HP--;
			if(HP<=0){
				SoundFXCtrl.instance.PlaySound (8, 1f);
				Instantiate(explodeEffect, _transform.position, Quaternion.identity);
				Destroy(gameObject);
			}
		}
	}

}
