using UnityEngine;
using System.Collections;

public class TEST_MobCube : MonoBehaviour {
	//public GameObject Unicorn;
	//private GameObject UnicornSpawn;
	public int score_value_of_this_unit=10;
	public float aAngle = 360.0F;
	public float minBAngle = 5.0F;
	public float maxBAngle = 15.0F;
	private float randomAAngle = 0.0F;
	private float randomBAngle = 0.0F;
	public int spawnDistance = 100;
	public float delay = 5.0f;
	
	private Vector3 randomPosition = Vector3.zero;
	private Vector3 UnicornDirection;
	private Quaternion UnicornRotation;
	
	//private float UnicornVelocity = 0.0F;
	private Vector3 UnicornVelocityVector = Vector3.zero;
	
	GameObject finish;
	public float speed=8;
	[HideInInspector]
	public HUD hud;
	[HideInInspector]
	public SpawnPoint spawnPoint;
	[HideInInspector]
	public ExplosionPool explosionPool;

	void Start() {
		finish = GameObject.FindGameObjectWithTag("Finish");
	}
	
	void Update() {
		transform.LookAt(finish.transform.position);
		transform.position += transform.forward*speed*Time.deltaTime;
	}
	
	void OnCollisionEnter(Collision other ) {
		if ( other.gameObject.CompareTag("Bullet") ) {
			gameObject.SetActive(false);
			hud.score.text =   (int.Parse(hud.score.text) + score_value_of_this_unit).ToString();
			spawnPoint.spawn_nb --;
		}

		if ( other.gameObject.CompareTag("Base") ) {
			gameObject.SetActive(false);
			other.gameObject.GetComponent<Base>().DecreaseHp();
			spawnPoint.spawn_nb --;
			GameObject j = explosionPool.Get ();
			j.particleSystem.Play();
		}
	}

	public void CollideWithParticle() {
		gameObject.SetActive(false);
		hud.score.text =   (int.Parse(hud.score.text) + score_value_of_this_unit).ToString();
		spawnPoint.spawn_nb --;
	}
	
	public void Spawn (){
		randomAAngle = Random.value * aAngle;
		randomBAngle = Random.value * (maxBAngle - minBAngle) + minBAngle;
		randomPosition.x = spawnDistance * Mathf.Cos (randomBAngle * Mathf.Deg2Rad) * Mathf.Cos (randomAAngle * Mathf.Deg2Rad);
		randomPosition.z = spawnDistance * Mathf.Cos (randomBAngle * Mathf.Deg2Rad) * Mathf.Sin (randomAAngle * Mathf.Deg2Rad);
		randomPosition.y = spawnDistance * Mathf.Sin (randomBAngle * Mathf.Deg2Rad);
		//UnicornDirection = finish.transform.position - randomPosition;
		//UnicornRotation = Quaternion.LookRotation (UnicornDirection);
		
		transform.position = randomPosition;
		
		
		//UnicornSpawn = (GameObject)Instantiate(Unicorn, randomPosition, UnicornRotation); 
		
		/*UnicornVelocity = Mathf.Sqrt (Mathf.Pow (randomPosition.x, 2) + Mathf.Pow (randomPosition.z, 2)) * Mathf.Sqrt ((0.0981F) / (2 * (randomPosition.y - 1)));
		UnicornVelocityVector.x = -randomPosition.x;
		UnicornVelocityVector.z = -randomPosition.z;
		UnicornVelocityVector.y = 0;
		UnicornSpawn.rigidbody.velocity = UnicornVelocityVector;*/
		
		
	}
}
