using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class TBodyHit
{
	public TBodyHit()
	{
		this.spherelist = new List<THitSphere>();
	}

	public void SetSphere(Transform t, string SaveName)
	{
		this.tRoot = t;
		this.MakeList(t, "ROOT", t);
		Debug.Log("BodyHit=" + this.spherelist.Count);
		string text = "G_Assets/_DAT/hitcheck";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		BinaryWriter binaryWriter = new BinaryWriter(File.Create(text + "/" + SaveName + ".hitcheck"));
		int count = this.spherelist.Count;
		binaryWriter.Write("HitCheck");
		binaryWriter.Write(count);
		for (int i = 0; i < count; i++)
		{
			binaryWriter.Write(this.spherelist[i].type);
			binaryWriter.Write(this.spherelist[i].len);
			this.spherelist[i].len_ = this.spherelist[i].len;
			binaryWriter.Write(this.spherelist[i].lenxlen);
			this.spherelist[i].lenxlen_ = this.spherelist[i].lenxlen;
			binaryWriter.Write(this.spherelist[i].t.name);
			binaryWriter.Write(this.spherelist[i].t.parent.name);
			binaryWriter.Write(this.spherelist[i].t.localPosition.x);
			binaryWriter.Write(this.spherelist[i].t.localPosition.y);
			binaryWriter.Write(this.spherelist[i].t.localPosition.z);
			binaryWriter.Write(this.spherelist[i].SKRT);
			binaryWriter.Write(this.spherelist[i].RL);
		}
		binaryWriter.Close();
	}

	public void ScaleMune(string t, float fac)
	{
		if (t == "HARA")
		{
			this.HARA_FUTO = fac;
		}
		if (t == "MOMO")
		{
			this.MOMO_FUTO = fac;
		}
		if (t == "KOSHI_SCL")
		{
			this.KOSHI_SCL = fac;
		}
		if (t == "KOSHI_SVAL")
		{
			this.KOSHI_SVAL = fac;
		}
		if (t == "MUNE")
		{
			for (int i = 0; i < this.spherelist.Count; i++)
			{
				if (this.spherelist[i].SKRT == 99)
				{
					this.spherelist[i].len = this.spherelist[i].len_ * fac;
					this.spherelist[i].lenxlen = this.spherelist[i].len * this.spherelist[i].len;
				}
			}
		}
	}

	public void LoadSphereFromFile(Transform t, string FileName, string tag)
	{
		this.tRoot = t;
		this.BodySkinTAG = tag;
		MemoryStream input = null;
		using (AFileBase afileBase = GameUty.FileOpen(FileName, null))
		{
			input = new MemoryStream(afileBase.ReadAll());
		}
		BinaryReader binaryReader = new BinaryReader(input);
		if (binaryReader.ReadString() != "HitCheck")
		{
			binaryReader.Close();
			Debug.LogError("HitCheck header error!");
			return;
		}
		int num = binaryReader.ReadInt32();
		for (int i = 0; i < num; i++)
		{
			THitSphere thitSphere = new THitSphere();
			thitSphere.type = binaryReader.ReadInt32();
			thitSphere.len = binaryReader.ReadSingle();
			thitSphere.lenxlen = binaryReader.ReadSingle();
			thitSphere.lenxlen = thitSphere.len * thitSphere.len;
			thitSphere.lenxlen_ = thitSphere.lenxlen;
			thitSphere.len_ = thitSphere.len;
			thitSphere.name = binaryReader.ReadString();
			thitSphere.pname = binaryReader.ReadString();
			thitSphere.vs.x = binaryReader.ReadSingle();
			thitSphere.vs.y = binaryReader.ReadSingle();
			thitSphere.vs.z = binaryReader.ReadSingle();
			thitSphere.SKRT = binaryReader.ReadInt32();
			thitSphere.RL = binaryReader.ReadInt32();
			if (thitSphere.RL != -1 && thitSphere.SKRT == 1)
			{
				this.skrt_L1 = i;
			}
			if (thitSphere.RL != -1 && thitSphere.SKRT == 2)
			{
				this.skrt_L2 = i;
			}
			if (thitSphere.RL != -1 && thitSphere.SKRT == 3)
			{
				this.skrt_L3 = i;
			}
			if (thitSphere.RL == -1 && thitSphere.SKRT == 1)
			{
				this.skrt_R1 = i;
			}
			if (thitSphere.RL == -1 && thitSphere.SKRT == 2)
			{
				this.skrt_R2 = i;
			}
			if (thitSphere.RL == -1 && thitSphere.SKRT == 3)
			{
				this.skrt_R3 = i;
			}
			if (tag == THp.check_BONE)
			{
				Debug.LogWarning(string.Concat(new string[]
				{
					thitSphere.pname,
					":",
					thitSphere.name,
					" : cnt=",
					this.SearchCountObj(thitSphere.pname, this.tRoot, 0).ToString()
				}));
			}
			thitSphere.t = this.SearchObj(thitSphere.pname, this.tRoot);
			if (thitSphere.t == null)
			{
				Debug.LogError("Hit sphere t=null " + thitSphere.pname);
			}
			this.spherelist.Add(thitSphere);
			if (tag == THp.check_BONE)
			{
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(GameObject.Find("SphereHIT"), Vector3.one * 0.3f, Quaternion.identity);
				float num2 = 1f;
				gameObject.transform.localScale = new Vector3(thitSphere.len / num2, thitSphere.len / num2, thitSphere.len / num2);
				gameObject.transform.localPosition = thitSphere.vs;
				gameObject.transform.parent = thitSphere.t;
				gameObject.transform.localPosition = thitSphere.vs;
				if (thitSphere.pname == "Mune_L")
				{
				}
				if (thitSphere.RL != 0)
				{
					gameObject.name = "SphereHIT_L";
				}
				else
				{
					gameObject.name = "SphereHIT_R";
				}
				thitSphere.tPtr = gameObject.transform;
			}
		}
		if (GameMain.Instance.VRMode && Application.isPlaying && FileName.ToLower() != "ik.hitcheck")
		{
			this.OvrAddHitSphere();
		}
	}

	private void OvrAddHitSphere()
	{
		string[] array = new string[]
		{
			"Sphere_HandHitL_",
			"Sphere_HandHitR_"
		};
		for (int i = 0; i < array.Length; i++)
		{
			THitSphere thitSphere = new THitSphere();
			thitSphere.type = 0;
			thitSphere.len = 0.1f;
			thitSphere.lenxlen = 0.01f;
			thitSphere.lenxlen = thitSphere.len * thitSphere.len;
			thitSphere.lenxlen_ = thitSphere.lenxlen;
			thitSphere.len_ = thitSphere.len;
			thitSphere.name = array[i];
			thitSphere.pname = "Bip01";
			thitSphere.vs.x = 0f;
			thitSphere.vs.y = -10000f;
			thitSphere.vs.z = 0f;
			thitSphere.SKRT = 0;
			thitSphere.RL = 1;
			thitSphere.t = this.SearchObj(thitSphere.pname, this.tRoot);
			if (thitSphere.t == null)
			{
				Debug.LogError("Hit sphere t=null " + thitSphere.pname);
			}
			this.spherelist.Add(thitSphere);
			if (i == 0)
			{
				this.m_listHandHitL.Add(thitSphere);
			}
			else
			{
				this.m_listHandHitR.Add(thitSphere);
			}
		}
		array = new string[]
		{
			"Sphere_HandHitLeapL_",
			"Sphere_HandHitLeapR_"
		};
		for (int j = 0; j < array.Length; j++)
		{
			THitSphere thitSphere2 = new THitSphere();
			thitSphere2.type = 0;
			thitSphere2.len = 0.1f;
			thitSphere2.lenxlen = 0.01f;
			thitSphere2.lenxlen = thitSphere2.len * thitSphere2.len;
			thitSphere2.lenxlen_ = thitSphere2.lenxlen;
			thitSphere2.len_ = thitSphere2.len;
			thitSphere2.name = array[j];
			thitSphere2.pname = "Bip01";
			thitSphere2.vs.x = 0f;
			thitSphere2.vs.y = -10000f;
			thitSphere2.vs.z = 0f;
			thitSphere2.SKRT = 0;
			thitSphere2.RL = 1;
			thitSphere2.t = this.SearchObj(thitSphere2.pname, this.tRoot);
			if (thitSphere2.t == null)
			{
				Debug.LogError("Hit sphere t=null " + thitSphere2.pname);
			}
			this.spherelist.Add(thitSphere2);
			if (j == 0)
			{
				this.m_HandHitLeapL = thitSphere2;
			}
			else
			{
				this.m_HandHitLeapR = thitSphere2;
			}
		}
	}

	private Transform SearchObj(string name, Transform t)
	{
		string name2 = t.name;
		if (name2.Contains("_IA_"))
		{
			return null;
		}
		if (name2 == name)
		{
			return t;
		}
		for (int i = 0; i < t.childCount; i++)
		{
			Transform transform = this.SearchObj(name, t.GetChild(i));
			if (transform != null)
			{
				return transform;
			}
		}
		return null;
	}

	private int SearchCountObj(string name, Transform t, int cnt = 0)
	{
		string name2 = t.name;
		if (name2.Contains("_IA_"))
		{
			return 0;
		}
		if (name2 == name)
		{
			cnt++;
		}
		for (int i = 0; i < t.childCount; i++)
		{
			cnt += this.SearchCountObj(name, t.GetChild(i), cnt);
		}
		return cnt;
	}

	private void MakeList(Transform t, string oyax, Transform root)
	{
		if (t.name.Contains("Sphere") || t.name.Contains("_SKRT_"))
		{
			string name = t.parent.name;
			THitSphere thitSphere = new THitSphere();
			thitSphere.t = t;
			thitSphere.len = t.localScale.x * 0.5f;
			thitSphere.lenxlen = thitSphere.len * thitSphere.len;
			thitSphere.SKRT = 0;
			if (t.name.Contains("_1_"))
			{
				thitSphere.SKRT = 1;
			}
			if (t.name.Contains("_2_"))
			{
				thitSphere.SKRT = 2;
			}
			if (t.name.Contains("_3_"))
			{
				thitSphere.SKRT = 3;
			}
			if (t.name.Contains("_MUNE_"))
			{
				thitSphere.SKRT = 99;
			}
			if (t.name.Contains("_UDE_"))
			{
				thitSphere.SKRT = 98;
			}
			if (t.name.Contains("_HARA_"))
			{
				thitSphere.SKRT = 97;
			}
			if (t.name.Contains("_MOMO_"))
			{
				thitSphere.SKRT = 96;
			}
			thitSphere.RL = 1;
			thitSphere.type = 0;
			if (t.parent.name == "Bip01 Head")
			{
				thitSphere.type = 1;
			}
			this.spherelist.Add(thitSphere);
			if (name.Contains("R") || t.name.Contains("_R_"))
			{
				Transform transform;
				if (t.name.Contains("_R_"))
				{
					transform = t.parent;
				}
				else
				{
					string text = string.Empty;
					int length = name.Length;
					for (int i = 0; i < length; i++)
					{
						char c = name[i];
						if (c == 'R')
						{
							c = 'L';
						}
						text += c.ToString();
					}
					transform = this.SearchObj(text, this.tRoot);
				}
				if (transform != null)
				{
					Transform transform2 = UnityEngine.Object.Instantiate<Transform>(t);
					if (transform2.name.Contains("_R_"))
					{
						transform2.name = "hit_copy";
					}
					THitSphere thitSphere2 = new THitSphere();
					thitSphere2.t = transform2;
					thitSphere2.SKRT = thitSphere.SKRT;
					thitSphere.RL = -1;
					transform2.position = new Vector3(root.position.x * 2f - t.position.x, t.position.y, t.position.z);
					transform2.parent = transform;
					thitSphere2.len = transform2.localScale.x * 0.5f;
					thitSphere2.lenxlen = thitSphere.len * thitSphere.len;
					thitSphere2.type = 0;
					this.spherelist.Add(thitSphere2);
				}
			}
		}
		List<Transform> list = new List<Transform>();
		for (int j = 0; j < t.childCount; j++)
		{
			list.Add(t.GetChild(j));
		}
		for (int k = 0; k < list.Count; k++)
		{
			this.MakeList(list[k], t.name, root);
		}
	}

	public void PreUpdate()
	{
		for (int i = 0; i < this.spherelist.Count; i++)
		{
			this.spherelist[i].wv_old = this.spherelist[i].wv;
			Vector3 wv = this.spherelist[i].t.TransformPoint(this.spherelist[i].vs);
			this.spherelist[i].wv = wv;
			if (this.spherelist[i].tPtr != null)
			{
				this.spherelist[i].len = this.spherelist[i].tPtr.localScale.x;
				this.spherelist[i].lenxlen = this.spherelist[i].len * this.spherelist[i].len;
				this.spherelist[i].tPtr.rotation = Quaternion.identity;
				this.spherelist[i].wv = this.spherelist[i].tPtr.position;
			}
		}
	}

	public bool SphereCheck(Vector3 v)
	{
		for (int i = 0; i < this.spherelist.Count; i++)
		{
			Vector3 wv = this.spherelist[i].wv;
			if ((v - wv).sqrMagnitude < this.spherelist[i].lenxlen)
			{
				return true;
			}
		}
		return false;
	}

	public bool SphereMove(ref Vector3 v, ref Vector3 g, Vector3 v_old)
	{
		bool result = false;
		for (int i = 0; i < this.spherelist.Count; i++)
		{
			Vector3 wv = this.spherelist[i].wv;
			if ((v - wv).sqrMagnitude < this.spherelist[i].lenxlen)
			{
				Vector3 normalized = (v_old - this.spherelist[i].wv_old).normalized;
				Vector3 a = wv + normalized * this.spherelist[i].len;
				v = a * 0.5f + v * 0.5f;
				if (this.spherelist[i].type == 0)
				{
					g *= 1f - Mathf.Abs(Vector3.Dot(normalized, g.normalized));
					g.y = 0f;
				}
				else
				{
					g.x *= 0.1f;
					g.y *= 0.95f;
					g.z *= 0.1f;
				}
				result = true;
			}
		}
		return result;
	}

	public bool SphereMove_skrt(ref Vector3 v, ref Vector3 g)
	{
		bool result = false;
		Vector3 a = Vector3.zero;
		float num = 0f;
		float num2 = 0f;
		for (int i = 0; i < this.spherelist.Count; i++)
		{
			Vector3 wv = this.spherelist[i].wv;
			Vector3 a2 = v - wv;
			float num3 = this.spherelist[i].len * (1f + _TS.TestVal3 * 2f);
			if (this.spherelist[i].SKRT == 96)
			{
				num3 *= this.SkirtFT;
			}
			if (a2.sqrMagnitude < num3 * num3)
			{
				float magnitude = a2.magnitude;
				a2 /= magnitude;
				float num4 = 1f - magnitude / num3;
				num2 += num4;
				num += num4;
				a += a2 * this.spherelist[i].len * num4;
				result = true;
			}
		}
		this.MST = num;
		if (num2 > 0.8f)
		{
			num2 = 0.8f;
		}
		g *= 1f - num2;
		if (num > 0.5f)
		{
			num -= 0.5f;
			this.MST_v = a * num;
			v += this.MST_v;
		}
		else
		{
			this.MST_v = Vector3.zero;
		}
		return result;
	}

	public bool SphereMove_skrtVSHOT(ref Vector3 v, ref Vector3 g, Vector3 vshot)
	{
		bool result = false;
		Vector3 a = Vector3.zero;
		float num = 0f;
		float num2 = 0f;
		for (int i = 0; i < this.spherelist.Count; i++)
		{
			Vector3 wv = this.spherelist[i].wv;
			Vector3 vector = v - wv;
			float num3 = this.spherelist[i].len * (1f + _TS.TestVal3 * 2f);
			if (vector.sqrMagnitude < num3 * num3)
			{
				float magnitude = vector.magnitude;
				float num4 = 1f - magnitude / num3;
				num2 += num4;
				num += num4;
				a += vshot * this.spherelist[i].len * num4;
				result = true;
			}
		}
		if (num2 > 0.8f)
		{
			num2 = 0.8f;
		}
		g *= 1f - num2;
		if (num > 0.5f)
		{
			num -= 0.5f;
			v += a * num;
		}
		return result;
	}

	public bool SphereMove_hair(ref Vector3 v, ref Vector3 g, Vector3 v2)
	{
		bool result = false;
		Vector3 a = Vector3.zero;
		float num = 0f;
		float num2 = 0f;
		for (int i = 0; i < this.spherelist.Count; i++)
		{
			Vector3 wv = this.spherelist[i].wv;
			Vector3 vector = v - wv;
			if (vector.sqrMagnitude < this.spherelist[i].lenxlen)
			{
				float magnitude = vector.magnitude;
				vector /= magnitude;
				float num3 = 1f - magnitude / this.spherelist[i].len;
				num3 *= num3;
				num2 += num3;
				a += vector;
				num += num3 * this.spherelist[i].len;
				result = true;
			}
		}
		if (num2 > 0.8f)
		{
			num2 = 0.8f;
		}
		g *= 1f - num2;
		a.Normalize();
		v += a * num;
		return result;
	}

	public bool SphereMove_hair2(ref Vector3 v, ref Vector3 g, Vector3 v_old)
	{
		bool result = false;
		Vector3 a = Vector3.zero;
		Vector3 a2 = Vector3.zero;
		float num = 0f;
		float num2 = 0f;
		for (int i = 0; i < this.spherelist.Count; i++)
		{
			Vector3 wv = this.spherelist[i].wv;
			Vector3 a3 = v - wv;
			float num3 = this.spherelist[i].len * (1f + _TS.TestVal2 * 2f);
			if (a3.sqrMagnitude < num3 * num3)
			{
				float magnitude = a3.magnitude;
				a3 /= magnitude;
				float num4 = 1f - magnitude / num3;
				num2 += num4;
				num += num4;
				a += a3 * this.spherelist[i].len * num4;
				Vector3 a4 = this.spherelist[i].wv - this.spherelist[i].wv_old;
				a2 += a4 * num4;
				result = true;
			}
		}
		num2 *= _TS.TestVal;
		if (num2 > 0.8f)
		{
			num2 = 0.8f;
		}
		g *= 1f - num2;
		if (num > 0.5f)
		{
			num -= 0.5f;
			v += a * num;
		}
		return result;
	}

	public float RotOffset;

	public List<THitSphere> spherelist;

	public Transform tRoot;

	public int skrt_R1;

	public int skrt_R2;

	public int skrt_R3;

	public int skrt_L1;

	public int skrt_L2;

	public int skrt_L3;

	public float MOMO_FUTO = 1f;

	public float HARA_FUTO;

	public float KOSHI_SCL = 1f;

	public float KOSHI_SVAL = 0.5f;

	public string BodySkinTAG;

	public List<THitSphere> m_listHandHitL = new List<THitSphere>();

	public List<THitSphere> m_listHandHitR = new List<THitSphere>();

	public THitSphere m_HandHitLeapL;

	public THitSphere m_HandHitLeapR;

	public float SkirtFT = 1f;

	public float MST;

	public Vector3 MST_v;
}
