using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace CameraTest
{
	public class Camera
	{
		float cameraPitch, cameraYaw, cameraRoll;
		Vector3 cameraPos = Vector3.Zero, cameraTarget, cameraUpVector;
		Vector3 move = Vector3.Zero;

		public Vector3 Position { get { return cameraPos; } set { cameraPos = value; } }

		public Matrix Matrix
		{
			get
			{
				Matrix t = Matrix.CreateFromYawPitchRoll ( cameraYaw, 0, 0 );
				cameraPos += Vector3.Transform ( move, t );
				t = Matrix.CreateFromYawPitchRoll ( cameraYaw, cameraPitch, cameraRoll );

				cameraTarget = Vector3.Transform ( new Vector3 ( 0, 0, -1 ), t );
				cameraUpVector = Vector3.Transform ( new Vector3 ( 0, 1, 0 ), t );

				t = Matrix.CreateLookAt ( cameraPos, cameraTarget + cameraPos, cameraUpVector );
				move = Vector3.Zero;

				return t;
			}
		}

		public void Strafe ( float u )
		{
			move.X += u;
		}

		public void Fly ( float u )
		{
		}

		public void Walk ( float u )
		{
			move.Z -= u;
		}

		public float Yaw
		{
			get { return cameraYaw; }
			set
			{
				cameraYaw = value;
				if ( cameraYaw > 3.141592f * 2 ) cameraYaw -= 3.141592f * 2;
				if ( cameraYaw < -3.141592f * 2 ) cameraYaw += 3.141592f * 2;
			}
		}

		public float Pitch
		{
			get { return cameraPitch; }
			set
			{
				cameraPitch = value;
				if ( cameraPitch > 3.141592f / 2 ) cameraPitch = 3.141592f / 2;
				if ( cameraPitch < -3.141592f / 2 ) cameraPitch = -3.141592f / 2;
			}
		}

		public float Roll
		{
			get { return cameraRoll; }
			set { cameraRoll = value; }
		}
	}
}
