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

public class TextEffectController : MonoBehaviour {
	[SerializeField]private TextMesh textMesh = null; // set from editor

	private const float fullLifeTime = 2f;
	private const float hideLifeTime = 1f;
	private float lifeTime = 0;

	private Vector3 startPosition;
	private Color color = Color.white;
	private float startAlpha = 1f;

	private Transform selfTransform = null;

	public void Init(Vector3 position, string text, Color textColor) {
		color = textColor;
		startAlpha = textColor.a;
		startPosition = position;
		selfTransform.localPosition = position;
		lifeTime = fullLifeTime;
		textMesh.text = text;
        textMesh.color = color;
        gameObject.SetActive(true);
	}

	private void Awake() {
		selfTransform = transform;
		lifeTime = 2;
	}

	private void Update () {
		if (lifeTime > 0) {
			lifeTime -= Time.deltaTime;
			selfTransform.localPosition = startPosition + Vector3.up * (1f - lifeTime / fullLifeTime);
			float alpha = Mathf.Min(1f, lifeTime / (fullLifeTime - hideLifeTime));
			color.a = startAlpha * alpha;
			textMesh.color = color;
		} else {
			gameObject.SetActive (false);
		}
	}
}
