AutoKupaCtrl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. using System;
  2. using System.Collections.Generic;
  3. using kt.Utility;
  4. using UnityEngine;
  5. [Serializable]
  6. public class AutoKupaCtrl
  7. {
  8. public AutoKupaCtrl(TBody tgt_body, AutoKupaCtrl.MorphType morph_type)
  9. {
  10. this.body = tgt_body;
  11. this.morphType = morph_type;
  12. switch (this.morphType)
  13. {
  14. case AutoKupaCtrl.MorphType.Vagina:
  15. this.morphName = "openvagina";
  16. this.MorphInsert.bone = tgt_body.GetBone("_IK_vagina");
  17. break;
  18. case AutoKupaCtrl.MorphType.Anal:
  19. this.morphName = "openanal";
  20. this.MorphInsert.bone = tgt_body.GetBone("_IK_anal");
  21. break;
  22. case AutoKupaCtrl.MorphType.Kupa:
  23. this.morphName = "openkupa";
  24. this.MorphInsert.bone = tgt_body.GetBone("_IK_vagina");
  25. break;
  26. }
  27. this.isNotKupa = true;
  28. }
  29. public int morphIndex
  30. {
  31. get
  32. {
  33. return (this.BodySkin.morph == null) ? -1 : ((int)this.BodySkin.morph.hash[this.morphName]);
  34. }
  35. }
  36. private TBodySkin BodySkin
  37. {
  38. get
  39. {
  40. return this.body.GetSlot(0);
  41. }
  42. }
  43. public AutoKupaCtrl.MovementType movementType { get; private set; }
  44. public AutoKupaCtrl.KupaType kupaType { get; private set; }
  45. public bool isNotKupa { get; private set; }
  46. public float morphValue
  47. {
  48. get
  49. {
  50. return this.BodySkin.morph.GetBlendValues(this.morphIndex);
  51. }
  52. set
  53. {
  54. int num = MathUtility.RatioToPercentage(this.BodySkin.morph.GetBlendValues(this.morphIndex), false);
  55. int num2 = MathUtility.RatioToPercentage(value * this.morphValueMax, true);
  56. bool flag = num != num2;
  57. if (flag)
  58. {
  59. this.BodySkin.morph.FixBlendValues();
  60. }
  61. }
  62. }
  63. public float morphValueMax
  64. {
  65. get
  66. {
  67. return this.MorphValueMax;
  68. }
  69. set
  70. {
  71. this.MorphValueMax = Mathf.Clamp01(value);
  72. }
  73. }
  74. public AutoKupaCtrl.TimerParam morphTime
  75. {
  76. get
  77. {
  78. return this.MorphTime;
  79. }
  80. }
  81. public AutoKupaCtrl.InsertParam morphInsert
  82. {
  83. get
  84. {
  85. return this.MorphInsert;
  86. }
  87. }
  88. public void KupaUpdate()
  89. {
  90. if (this.isNotKupa)
  91. {
  92. return;
  93. }
  94. if (!this.IsDoneSetUp)
  95. {
  96. this.SetKupaTimer(AutoKupaCtrl.KupaType.Close, this.body.motionBlendTime, 0f);
  97. }
  98. AutoKupaCtrl.MovementType movementType = this.movementType;
  99. if (movementType != AutoKupaCtrl.MovementType.Insert)
  100. {
  101. if (movementType == AutoKupaCtrl.MovementType.Timer)
  102. {
  103. this.MorphTime.curTime += Time.deltaTime;
  104. float morphValue = (this.kupaType != AutoKupaCtrl.KupaType.Open) ? (1f - this.MorphTime.ratio) : this.MorphTime.ratio;
  105. this.morphValue = morphValue;
  106. }
  107. }
  108. else
  109. {
  110. int chain_count = this.MorphInsert.chainBones.Length;
  111. Transform chainEndBone = this.MorphInsert.chainEndBone;
  112. if (!chainEndBone)
  113. {
  114. this.Finish();
  115. return;
  116. }
  117. Vector3 lhs = chainEndBone.position - this.MorphInsert.bone.position;
  118. float end_length = Vector3.Dot(lhs, this.MorphInsert.worldInsertAxis);
  119. bool isInsert = this.MorphInsert.isInsert;
  120. if (this.MorphInsert.isInsert && this.kupaType == AutoKupaCtrl.KupaType.Open)
  121. {
  122. this.MorphInsert.isInsert = (end_length <= 0f);
  123. }
  124. else
  125. {
  126. this.MorphInsert.isInsert = (end_length <= 0f && this.IsKupaRangeIn(chainEndBone.position));
  127. }
  128. if (!this.MorphInsert.isInsert)
  129. {
  130. this.kupaSpeed = this.MorphInsert.kupaSpeedIn;
  131. this.MorphInsert.lastInsertValue = 0f;
  132. this.MorphInsert.adjustVal = 0f;
  133. if (!this.MorphInsert.isTimeClose)
  134. {
  135. this.morphValue = 0f;
  136. if (this.kupaType == AutoKupaCtrl.KupaType.Close)
  137. {
  138. this.isNotKupa = true;
  139. this.kupaSpeed = -1f;
  140. }
  141. }
  142. else
  143. {
  144. AutoKupaCtrl.TimerParam timeCloseParam = this.MorphInsert.timeCloseParam;
  145. this.SetKupaTimer(AutoKupaCtrl.KupaType.Close, timeCloseParam.totalTime, timeCloseParam.waitTime);
  146. }
  147. return;
  148. }
  149. DebugUtility.DrawSquare(this.MorphInsert.bone.position, this.MorphInsert.bone.rotation, this.MorphInsert.checkInsertAxis, 0.05f);
  150. DebugUtility.DrawObjAxis(chainEndBone, 0.0625f);
  151. Func<Vector3, float> func = delegate(Vector3 pos)
  152. {
  153. float num5 = Vector3.Dot(pos - this.MorphInsert.bone.position, this.MorphInsert.worldInsertAxis);
  154. return (num5 <= 0f) ? 1f : (1f - num5 / (num5 + Mathf.Abs(end_length)));
  155. };
  156. float num = func(this.MorphInsert.chainStartBone.position);
  157. float num2 = num - this.MorphInsert.lastInsertValue;
  158. bool flag = false;
  159. if (Mathf.Abs(num2) > 0.001f)
  160. {
  161. float num3 = (num2 <= 0f) ? this.MorphInsert.kupaSpeedOut : this.MorphInsert.kupaSpeedIn;
  162. flag = (num3 != this.kupaSpeed);
  163. this.kupaSpeed = num3;
  164. }
  165. Func<float, Vector3> func2 = delegate(float speed)
  166. {
  167. float num5 = Mathf.Floor(speed);
  168. float f = (float)chain_count * Mathf.Clamp01(num5 / 10f);
  169. int num6 = Mathf.Clamp(Mathf.FloorToInt(f) - 1, 0, chain_count - 1);
  170. int num7 = Mathf.Clamp(num6 + 1, 0, chain_count - 1);
  171. Vector3 position = this.MorphInsert.chainBones[num6].position;
  172. Vector3 position2 = this.MorphInsert.chainBones[num7].position;
  173. float t = speed - num5;
  174. return Vector3.Lerp(position, position2, t);
  175. };
  176. this.MorphInsert.lastInsertValue = num;
  177. Vector3 vector = func2(this.kupaSpeed);
  178. float num4 = func(vector);
  179. if (flag)
  180. {
  181. this.morphInsert.adjustVal = this.morphValue - num4;
  182. }
  183. DebugUtility.DrawAxis(vector, chainEndBone.rotation, Color.cyan, Color.cyan, Color.cyan, 0.0625f);
  184. if (this.morphValue >= 1f)
  185. {
  186. if (num4 >= 1f)
  187. {
  188. this.morphInsert.adjustVal = 0f;
  189. }
  190. }
  191. else if (this.morphValue <= 0f && num4 <= 0f)
  192. {
  193. this.morphInsert.adjustVal = 0f;
  194. }
  195. if (this.MorphInsert.isValueFixed)
  196. {
  197. this.morphValue = this.MorphInsert.fixedValue;
  198. }
  199. else
  200. {
  201. if (this.MorphInsert.isTimeClose && this.morphValue > num4 + this.morphInsert.adjustVal)
  202. {
  203. return;
  204. }
  205. this.morphValue = num4 + this.morphInsert.adjustVal;
  206. }
  207. }
  208. if (this.kupaType == AutoKupaCtrl.KupaType.Close)
  209. {
  210. this.isNotKupa = (this.morphValue <= 0f);
  211. if (this.isNotKupa)
  212. {
  213. this.kupaSpeed = -1f;
  214. }
  215. }
  216. }
  217. public bool IsKupaRangeIn(Vector3 position)
  218. {
  219. Vector3 lhs = position - this.MorphInsert.bone.position;
  220. Quaternion rotation = Quaternion.FromToRotation(Vector3.forward, this.MorphInsert.worldInsertAxis);
  221. Vector3 rhs = rotation * Vector3.up;
  222. Vector3 rhs2 = rotation * Vector3.right;
  223. float num = Mathf.Abs(Vector3.Dot(lhs, rhs));
  224. float num2 = Mathf.Abs(Vector3.Dot(lhs, rhs2));
  225. return num <= 0.05f && num2 <= 0.05f;
  226. }
  227. public void SetInsertParamFromScript(KagTagSupport tag_data)
  228. {
  229. this.MorphInsert.isValueFixed = tag_data.IsValid("kupafixed");
  230. if (this.MorphInsert.isValueFixed)
  231. {
  232. this.MorphInsert.fixedValue = MathUtility.PercentageToRatio(tag_data.GetTagProperty("kupafixed").AsInteger(), false);
  233. }
  234. else
  235. {
  236. if (tag_data.IsValid("kupaspeed"))
  237. {
  238. this.MorphInsert.kupaSpeedIn = (this.MorphInsert.kupaSpeedOut = MathUtility.PercentageToRatio(tag_data.GetTagProperty("kupaspeed").AsInteger(), false));
  239. }
  240. else
  241. {
  242. if (tag_data.IsValid("kupaspeedin"))
  243. {
  244. this.MorphInsert.kupaSpeedIn = MathUtility.PercentageToRatio(tag_data.GetTagProperty("kupaspeedin").AsInteger(), false);
  245. }
  246. if (tag_data.IsValid("kupaspeedout"))
  247. {
  248. this.MorphInsert.kupaSpeedOut = MathUtility.PercentageToRatio(tag_data.GetTagProperty("kupaspeedout").AsInteger(), false);
  249. }
  250. }
  251. this.morphValueMax = ((!tag_data.IsValid("kupamax")) ? 1f : MathUtility.PercentageToRatio(tag_data.GetTagProperty("kupamax").AsInteger(), false));
  252. }
  253. this.MorphInsert.isTimeClose = tag_data.IsValid("timeclose");
  254. if (this.MorphInsert.isTimeClose)
  255. {
  256. string[] array = tag_data.GetTagProperty("timeclose").AsString().Split(new char[]
  257. {
  258. ' '
  259. });
  260. string[] array2 = array;
  261. int i = 0;
  262. while (i < array2.Length)
  263. {
  264. string text = array2[i];
  265. string text2 = text.Split(new char[]
  266. {
  267. '='
  268. })[0];
  269. float num = MathUtility.MillisecondToSecond(int.Parse(text.Split(new char[]
  270. {
  271. '='
  272. })[1]));
  273. if (text2 != null)
  274. {
  275. if (!(text2 == "time"))
  276. {
  277. if (text2 == "wait")
  278. {
  279. this.MorphInsert.timeCloseParam.waitTime = num;
  280. }
  281. }
  282. else
  283. {
  284. this.MorphInsert.timeCloseParam.totalTime = num;
  285. }
  286. }
  287. IL_213:
  288. i++;
  289. continue;
  290. goto IL_213;
  291. }
  292. }
  293. else
  294. {
  295. this.MorphInsert.timeCloseParam.Reset();
  296. }
  297. }
  298. public void SetKupaInsert(AutoKupaCtrl.InsertObjType insert, TBody tgt_body, Vector3 insert_axis)
  299. {
  300. Transform[] kupaInsertObjs = tgt_body.GetKupaInsertObjs(insert);
  301. if (kupaInsertObjs != null)
  302. {
  303. this.MorphInsert.boneBody = tgt_body;
  304. this.isUpdateTBody = (this.MorphInsert.boneBody == this.body);
  305. if (!this.isUpdateTBody)
  306. {
  307. if (tgt_body.fullBodyIK.isIKExec)
  308. {
  309. tgt_body.fullBodyIK.onPostSolverUpdate.Add(new Action(this.KupaUpdate), false);
  310. }
  311. else
  312. {
  313. tgt_body.onLateUpdateEnd.Add(new Action(this.KupaUpdate), false);
  314. }
  315. }
  316. this.SetKupaInsert(kupaInsertObjs, insert_axis);
  317. }
  318. }
  319. public void SetKupaInsertFromBgItem(string item_name, Vector3 insert_axis)
  320. {
  321. GameObject prefabFromBg = GameMain.Instance.BgMgr.GetPrefabFromBg(item_name);
  322. if (!prefabFromBg)
  323. {
  324. return;
  325. }
  326. Transform transform = prefabFromBg.transform.Find("Bone_insert1");
  327. Transform[] chain_bones = UnityUtility.GetAllChildren(transform, true).ToArray();
  328. this.SetKupaInsert(chain_bones, insert_axis);
  329. }
  330. public void SetKupaInsert(Transform[] chain_bones, Vector3 insert_axis)
  331. {
  332. this.SetKupaInsert(AutoKupaCtrl.KupaType.Open, chain_bones, insert_axis);
  333. }
  334. public void SetKupaInsert(AutoKupaCtrl.KupaType kupa_type, Transform[] chain_bones, Vector3 insert_axis)
  335. {
  336. this.movementType = AutoKupaCtrl.MovementType.Insert;
  337. this.isNotKupa = false;
  338. this.IsDoneSetUp = true;
  339. this.kupaType = kupa_type;
  340. this.MorphInsert.chainBones = chain_bones;
  341. this.MorphInsert.checkInsertAxis = insert_axis;
  342. this.MorphInsert.isInsert = false;
  343. if (this.kupaSpeed < 0f)
  344. {
  345. this.kupaSpeed = ((this.kupaType != AutoKupaCtrl.KupaType.Open) ? this.MorphInsert.kupaSpeedOut : this.MorphInsert.kupaSpeedIn);
  346. }
  347. }
  348. public void SetKupaTimer(AutoKupaCtrl.KupaType kupa_type, float kupa_time, float wait = 0f)
  349. {
  350. this.movementType = AutoKupaCtrl.MovementType.Timer;
  351. this.isNotKupa = false;
  352. this.IsDoneSetUp = true;
  353. this.kupaType = kupa_type;
  354. this.MorphTime.Reset();
  355. this.MorphTime.waitTime = wait;
  356. this.MorphTime.totalTime = kupa_time;
  357. }
  358. public void StopKupa()
  359. {
  360. this.isNotKupa = true;
  361. }
  362. public void DoneStateReset()
  363. {
  364. this.IsDoneSetUp = false;
  365. this.isUpdateTBody = true;
  366. this.MorphInsert.isValueFixed = false;
  367. if (this.MorphInsert.boneBody)
  368. {
  369. this.MorphInsert.boneBody.fullBodyIK.onPostSolverUpdate.Remove(new Action(this.KupaUpdate));
  370. this.MorphInsert.boneBody.onLateUpdateEnd.Remove(new Action(this.KupaUpdate));
  371. }
  372. this.MorphInsert.boneBody = null;
  373. }
  374. public void Finish()
  375. {
  376. this.isNotKupa = true;
  377. this.IsDoneSetUp = false;
  378. this.MorphTime.Reset();
  379. this.MorphInsert.isInsert = false;
  380. if (this.BodySkin != null && this.BodySkin.morph != null)
  381. {
  382. this.BodySkin.morph.FixBlendValues();
  383. }
  384. }
  385. private const float KUPA_RANGE_MAX = 0.05f;
  386. public static readonly Dictionary<string, AutoKupaCtrl.MorphType> InsertBoneMorphDic = new Dictionary<string, AutoKupaCtrl.MorphType>
  387. {
  388. {
  389. "_IK_vagina",
  390. AutoKupaCtrl.MorphType.Vagina
  391. },
  392. {
  393. "_IK_anal",
  394. AutoKupaCtrl.MorphType.Anal
  395. }
  396. };
  397. public readonly TBody body;
  398. public readonly AutoKupaCtrl.MorphType morphType;
  399. public readonly string morphName;
  400. private bool IsDoneSetUp;
  401. [SerializeField]
  402. [Range(0f, 1f)]
  403. private float MorphValueMax = 1f;
  404. [Range(1f, 10f)]
  405. public float kupaSpeed = -1f;
  406. [SerializeField]
  407. private AutoKupaCtrl.TimerParam MorphTime = new AutoKupaCtrl.TimerParam();
  408. [SerializeField]
  409. private AutoKupaCtrl.InsertParam MorphInsert = new AutoKupaCtrl.InsertParam();
  410. public bool isUpdateTBody = true;
  411. public enum MorphType
  412. {
  413. Vagina,
  414. Anal,
  415. Kupa
  416. }
  417. public enum MovementType
  418. {
  419. Insert,
  420. Timer
  421. }
  422. public enum KupaType
  423. {
  424. Open,
  425. Close
  426. }
  427. public enum InsertObjType
  428. {
  429. Penis,
  430. Finger_R0,
  431. Finger_R1,
  432. Finger_R2,
  433. Finger_R3,
  434. Finger_R4,
  435. Finger_L0,
  436. Finger_L1,
  437. Finger_L2,
  438. Finger_L3,
  439. Finger_L4,
  440. HandItem_R,
  441. HandItem_L,
  442. BgItem
  443. }
  444. [Serializable]
  445. public class TimerParam
  446. {
  447. public float ratio
  448. {
  449. get
  450. {
  451. return (this.totalTime <= 0f) ? 1f : Mathf.Clamp01(Mathf.Max(this.curTime - this.waitTime, 0f) / this.totalTime);
  452. }
  453. }
  454. public void Reset()
  455. {
  456. this.curTime = 0f;
  457. this.waitTime = 0f;
  458. }
  459. public float curTime;
  460. public float totalTime;
  461. public float waitTime;
  462. }
  463. [Serializable]
  464. public class InsertParam
  465. {
  466. public Transform chainStartBone
  467. {
  468. get
  469. {
  470. return (this.chainBones == null || this.chainBones.Length == 0) ? null : this.chainBones[0];
  471. }
  472. }
  473. public Transform chainEndBone
  474. {
  475. get
  476. {
  477. return (this.chainBones == null || this.chainBones.Length == 0) ? null : this.chainBones[this.chainBones.Length - 1];
  478. }
  479. }
  480. public Vector3 worldInsertAxis
  481. {
  482. get
  483. {
  484. return this.bone.rotation * this.checkInsertAxis;
  485. }
  486. }
  487. public TBody boneBody;
  488. public Transform[] chainBones;
  489. public Transform bone;
  490. public Vector3 checkInsertAxis;
  491. [ReadOnly]
  492. public bool isInsert;
  493. [Range(1f, 10f)]
  494. public float kupaSpeedIn = 1f;
  495. [Range(1f, 10f)]
  496. public float kupaSpeedOut = 1f;
  497. [ReadOnly]
  498. public float adjustVal;
  499. [HideInInspector]
  500. public float lastInsertValue;
  501. public bool isValueFixed;
  502. public float fixedValue = 1f;
  503. public bool isTimeClose;
  504. public AutoKupaCtrl.TimerParam timeCloseParam = new AutoKupaCtrl.TimerParam();
  505. }
  506. }