using UnityEngine;

/// <summary>
/// Want something to spin? Attach this script to it. Works equally well with rigidbodies as without.
/// </summary>

[AddComponentMenu("NGUI/Examples/Spin")]
public class Spin : MonoBehaviour
{
	public Vector3 rotationsPerSecond = new Vector3(0f, 0.1f, 0f);

	Rigidbody mRb;
	Transform mTrans;

	void Start ()
	{
		mTrans = transform;
		mRb = GetComponent<Rigidbody>();
	}

	void Update ()
	{
		if (mRb == null)
		{
			ApplyDelta (Frontiers.WorldClock.RTDeltaTime);//Time.deltaTime);
		}
	}
	void FixedUpdate ()
	{
		if (mRb != null)
		{
			ApplyDelta (Frontiers.WorldClock.RTDeltaTime);
		}
	}

	public void ApplyDelta (double delta)
	{
		delta *= Mathf.Rad2Deg * Mathf.PI * 2;
		Quaternion offset = Quaternion.Euler (rotationsPerSecond * (float) delta);

		if (mRb == null)
		{
			mTrans.rotation = mTrans.rotation * offset;
		}
		else
		{
			mRb.MoveRotation(mRb.rotation * offset);
		}
	}
}