using UnityEngine;
using System.Collections;

public class ChildFalling : MonoBehaviour {

	Vector2 origPos;
	public float fallDist;
	public float speed = 4;
	public float delay = 2;
	bool fall = false;
	Room room;
	GameController gc;

	// Use this for initialization
	void Start () {
		gc = GameController.instance;
		origPos = transform.position;
		room = FindObjectOfType<Room>();
		StartCoroutine (StartFall ());
	}
	
	// Update is called once per frame
	void Update () {
		if(fall){
			if(transform.position.y>=origPos.y-fallDist){
				transform.position-=Time.deltaTime*speed*Vector3.up;
			} else{
				transform.position = origPos;
			}
		}
	}

	IEnumerator StartFall(){
		yield return new WaitForSeconds(delay);
		fall = true;
	}

	void OnTriggerEnter2D(){
		StartCoroutine (Ouch ());
	}

	IEnumerator Ouch(){
		Destroy (collider2D);
		if(room.player){
			room.player.rigidbody2D.velocity = Vector2.zero;
			Destroy(room.player);
		}
		room.SendMessage ("ShowMessage","Ouch!! Sharp objects hurt!!!");
		yield return new WaitForSeconds(room.msgTime +room.msgFadeTime);
		gc.SendMessage ("ClearChildItems");
		Application.LoadLevel("ChildLiving");
	}
}
