using UnityEngine;
using System.Collections;

public class DragControl : MonoBehaviour {

	#region Private Variables
	private GameObject grabObject;
	private Rigidbody2D _rigidbody;

	[SerializeField]
	private Transform _world;
	#endregion


	#region Events
	void Awake () {

		_rigidbody = GetComponent <Rigidbody2D> ();
		
	}

	void OnMouseDown () {
		
		grabObject = new GameObject ();
		grabObject.transform.position = (Vector2) Camera.main.ScreenToWorldPoint (Input.mousePosition);

		transform.parent = grabObject.transform;

		if (_rigidbody != null)
			_rigidbody.gravityScale = 0f;
		
	}


	void OnMouseDrag () {

		grabObject.transform.position = (Vector2) Camera.main.ScreenToWorldPoint (Input.mousePosition);
		
	}


	void OnMouseUp () {

		transform.parent = _world;
		Destroy (grabObject);

		if (_rigidbody != null)
			_rigidbody.gravityScale = 1f;

	}
	#endregion

}
