using UnityEngine;
using System.Collections;

public class SoulOrb : MonoBehaviour {

	public Transform target;
	float speed = 7f;

	bool isDeathMode = false;

	void Start() {
		SoundManager.current.PlaySound("Soul Suck");
	}

	void Update () {
		if(target == null) {
			Destroy (gameObject);
			return;
		}


		Vector2 dir = target.position - this.transform.position;
		
		if(dir.magnitude <= 0.2f) {
			//Destroy(gameObject);
			if(isDeathMode == false) {
				StartDeathMode();
			}
			else {
				target.GetComponent<Portal>().hps--;
				SoundManager.current.PlaySound("Portal Damage");
				Destroy(gameObject);
			}
		}
		else {
			this.transform.Translate( dir.normalized * speed * Time.deltaTime );
		}

	}

	void StartDeathMode() {
		isDeathMode = true;
		target = GameObject.Find ("Portal").transform;
		SetColor( new Color32(0, 0, 0, 255) );
	}

	public void SetColor(Color32 c) {
		GetComponent<SpriteRenderer>().color = c;

		if( c.r + c.g + c.g < 100 ) 
			c = new Color32(55, 55, 55, 255);

		GetComponentInChildren<TrailRenderer>().material.SetColor("_TintColor", c);
	}
}
