using UnityEngine;
using System.Collections;

public class FallingPlatform : MonoBehaviour {

    Rigidbody2D body;
	public float fallDelay = .5f;
    public float restartDelay = .5f;

    Vector2 startPos = Vector2.zero;
    GameObject originalObject;
    void Awake()
    {
        startPos = transform.position;
        originalObject = this.gameObject;
        body=GetComponent<Rigidbody2D>();
        body.isKinematic = true;        
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Player")
        {
            StartCoroutine(fall());
        }
    }
  
    IEnumerator fall()
    {
        yield return new WaitForSeconds(fallDelay);
        body.isKinematic = false;
        yield return new WaitForSeconds(fallDelay);
        StartCoroutine(Return());
    }

    IEnumerator Return()
    {
        yield return new WaitForSeconds(restartDelay);
        Instantiate(originalObject, startPos, this.transform.rotation);
        //FadeColor fade = newPlatform.AddComponent<FadeColor>();
        //fade.setInicialTransparentColor();
        //fade.speedTransition = 1.8f;
        //fade.play = true;
        StopAllCoroutines();
        Destroy(gameObject);

    }
}
