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

public class EnemyBody : MonoBehaviour {

	Rigidbody RB;
	Transform Trail;

	// Use this for initialization
	void Start () {
		RB = GetComponent<Rigidbody>();
		Trail = transform.FindChild("Trail");
	}
	
	// Update is called once per frame
	void Update () {
		if((Player.Instance.transform.position - transform.position).sqrMagnitude<1.0f) {
			if(!Player.Instance.LetSpawnText) {
				Player.Instance.CurrentLife = Mathf.Max(0.0f, Player.Instance.CurrentLife-1.0f);
			}
			Player.Instance.HurtTimer = 1.0f;
		}
		if(Random.value<0.1f) {
			Vector3 RVec = new Vector3(Random.value * 2 - 1,Random.value * 0.7f,Random.value * 2 - 1);
			Vector3 PlayerDir = Vector3.Normalize(Player.Instance.transform.position - transform.position);
			RB.AddForce(RVec * 100.0f + PlayerDir * (Random.value-0.0f) * 80.0f);
		}
		float Len = RB.velocity.magnitude;
		if(Len>0.01f) {
			Trail.rotation = Quaternion.FromToRotation(Vector3.up, -RB.velocity / Len);
		}
	}

	public void AddRandomPulse(Transform Ref) {
		if(Random.value<0.5f) {
			Vector3 RefDir = Vector3.Normalize(transform.position - Ref.position);
			RB.AddForce(RefDir * (Random.value) * 150.0f);
		}
	}

	public void AddRandomCone(Transform Ref) {
		Vector3 RefDir = Vector3.Normalize(transform.position - Ref.position);
		Vector3 RefDir2 = Vector3.Normalize(RefDir - Ref.right * Vector3.Dot(RefDir, Ref.right));
		RB.AddForce((RefDir + RefDir2) * (Random.value) * 150.0f);
	}
}
