using UnityEngine;
using System.Collections;

[RequireComponent(typeof (BoxCollider2D))]
public class Bullet : MonoBehaviour {

	private bool bactive;

	private Vector3 endPosition;

	private float covered;
	private float distance;
	private float startTime;

	void FixedUpdate(){
		if (bactive) {
			covered = (Time.time - startTime) * Constants.BULLET_SPEED;
			transform.position = Vector3.Lerp (transform.position, endPosition, covered / distance);
			if(transform.position == endPosition)
				Destroy(this.gameObject);
		}
	}

	public void Activate(Transform p){
		float factor = Constants.OUT_CIRCLE_RADIUS / Constants.ORBIT_RADIUS;
		endPosition = new Vector3 (p.position.x * factor, p.position.y * factor, p.position.z);
		covered = 0.0f;
		distance = Vector3.Distance (transform.position, endPosition);
		startTime = Time.time;
		transform.eulerAngles = p.eulerAngles;
		bactive = true;
	}

	void OnCollisionEnter2D(Collision2D coll){
		DestroyBullet();
	}

	public void DestroyBullet(){
		Destroy (this.gameObject);
	}
}