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

public class Obstacle : MonoBehaviour {

	private float bounceTime = 0;

	private Rigidbody2D rg;
	private bool isPlaying = true;
	[SerializeField] private float upwardsSpeed = .1f;
	[SerializeField] private float fallSpeed = .1f;
	[SerializeField] private float timeOfBouncing = .5f;

	private bool TouchFloor = false;
	private bool bounced = false;

	void Start () {
		rg = GetComponent<Rigidbody2D> ();
	}

	void Update () {
		if (!isPlaying) {
			if (TouchFloor) {
				//Bounce and fall

				if (bounced) {
					//fall
					rg.velocity = (Vector3.up * -fallSpeed);
				} else {
					//goUp (inBounce)
					rg.velocity = (Vector3.up * upwardsSpeed);
					bounceTime += Time.deltaTime;
					if (bounceTime >= timeOfBouncing)
						bounced = true;

				}

			} else {
				//Fall till touch floor
				rg.velocity = (Vector3.up * -fallSpeed);

			}
		} else {
			//GoUptillanimEnds
			rg.velocity = (Vector3.up * upwardsSpeed);

		}
	}
		

	void OnTriggerEnter2D(Collider2D col){

		if (col.CompareTag ("Floor")) {
			TouchFloor = true;
		}else if(col.CompareTag ("Player")){
			//kill TODO
		}

	}

	void StartToFall(){
		isPlaying = false;
	}

}
