using UnityEngine;
using System.Collections;

public class RaisePillar : MonoBehaviour {
	//Public
	[Header("Control")]
	public bool			raise		= false;
	public int			type		= 0;
	public Generator	generator	= null;
	
	[Header("Data")]
	public float	height		= 0f;
	
	//Private
	private bool	fall		= false;
	private float	fallTimer	= 0f;
	
	void Start() {
	
	}
	
	void Update() {
		fallTimer += Time.deltaTime;
		
		if (raise && fall == false) {
			transform.position	= new Vector3(
				transform.position.x,
				Mathf.Lerp(transform.position.y, height * 0.5f - 0.5f, Time.deltaTime * 2f),
				transform.position.z
			);
		} else if (fall == true && fallTimer > 0.001f) {
			transform.position	= new Vector3(
				transform.position.x,
				transform.position.y - 0.2f,
				transform.position.z
			);
			fallTimer	= 0f;
		}
		
		if (transform.position.y < -5f) {
			Destroy(gameObject);
		}
	}

	void Bump(GameObject other) {
		audio.Play();
		fall = true;
		generator.doSpawn	+=	20 + (int)(Random.value * 5f);
		other.rigidbody.AddForce(
			0f,
			150f +
				Mathf.Abs(other.rigidbody.velocity.x * other.rigidbody.velocity.z) + Mathf.Abs(other.rigidbody.velocity.z) +
				height * 10f,
			0f
		);
		other.renderer.material.color = renderer.material.color;
		other.GetComponent<Movement>().pulse();
	}
	
	void OnCollisionEnter(Collision other) {
		if (other.gameObject.tag == "Player") {
			Bump(other.gameObject);
		}
	}
	void OnTriggerEnter(Collider other) {
		if (other.gameObject.tag == "Player") {
			Bump(other.gameObject);
		}
	}
}
