using System;
using OpenTK;

namespace SharpQuake
{
	internal static class chase
	{
		public static bool IsActive
		{
			get
			{
				return chase._Active.Value != 0f;
			}
		}

		public static void Init()
		{
			if (chase._Back == null)
			{
				chase._Back = new cvar("chase_back", "100");
				chase._Up = new cvar("chase_up", "16");
				chase._Right = new cvar("chase_right", "0");
				chase._Active = new cvar("chase_active", "0");
			}
		}

		public static void Reset()
		{
		}

		public static void Update()
		{
			Vector3 vec;
			Vector3 vec2;
			Vector3 vector;
			mathlib.AngleVectors(ref client.cl.viewangles, out vec, out vec2, out vector);
			chase._Dest = render.RefDef.vieworg - vec * chase._Back.Value - vec2 * chase._Right.Value;
			chase._Dest.Z = render.RefDef.vieworg.Z + chase._Up.Value;
			Vector3 vector2 = render.RefDef.vieworg + vec * 4096f;
			Vector3 vector3;
			chase.TraceLine(ref render.RefDef.vieworg, ref vector2, out vector3);
			vector3 -= render.RefDef.vieworg;
			float num;
			Vector3.Dot(ref vector3, ref vec, out num);
			if (num < 1f)
			{
				num = 1f;
			}
			render.RefDef.viewangles.X = (float)(-(float)Math.Atan((double)(vector3.Z / num)) / 3.1415926535897931 * 180.0);
			render.RefDef.vieworg = chase._Dest;
		}

		private static void TraceLine(ref Vector3 start, ref Vector3 end, out Vector3 impact)
		{
			trace_t trace_t = new trace_t();
			server.RecursiveHullCheck(client.cl.worldmodel.hulls[0], 0, 0f, 1f, ref start, ref end, trace_t);
			impact = trace_t.endpos;
		}

		private static cvar _Back;

		private static cvar _Up;

		private static cvar _Right;

		private static cvar _Active;

		private static Vector3 _Dest;
	}
}
