MemoryObject.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. using DG.Tweening;
  7. public class MemoryObject : MonoBehaviour
  8. {
  9. [SerializeField] GameObject CurrentMemoryObject;
  10. public List<MemoryTab> memoryTabs = new List<MemoryTab>();
  11. [SerializeField] MemoryPredictionLine memoryPredictionLine;
  12. int oldMemory = 0;
  13. public void Init()
  14. {
  15. AssignMemoryTab();
  16. memoryPredictionLine.Init();
  17. foreach (MemoryTab memoryTab in memoryTabs)
  18. {
  19. memoryTab.Init();
  20. }
  21. }
  22. public void AssignMemoryTab()
  23. {
  24. bool isMasterClient = GManager.instance.You.PlayerID == 0;
  25. for (int i = 0; i < memoryTabs.Count; i++)
  26. {
  27. if (isMasterClient)
  28. {
  29. memoryTabs[i].Memory = i - 10;
  30. }
  31. else
  32. {
  33. memoryTabs[i].Memory = 10 - i;
  34. }
  35. }
  36. }
  37. public IEnumerator SetMemory()
  38. {
  39. if (oldMemory != GManager.instance.turnStateMachine.gameContext.Memory)
  40. {
  41. bool isMasterClient = GManager.instance.You.PlayerID == 0;
  42. MemoryTab targetMemoryTab = null;
  43. MemoryTab currentMemoryTab = null;
  44. foreach (MemoryTab memoryTab in memoryTabs)
  45. {
  46. if (memoryTab.Memory == GManager.instance.turnStateMachine.gameContext.Memory)
  47. {
  48. targetMemoryTab = memoryTab;
  49. }
  50. if (memoryTab.Memory == oldMemory)
  51. {
  52. currentMemoryTab = memoryTab;
  53. }
  54. }
  55. if (targetMemoryTab != null)
  56. {
  57. List<MemoryTab> lightMemoryTabs = new List<MemoryTab>();
  58. int startIndex = memoryTabs.IndexOf(currentMemoryTab);
  59. int endIndex = memoryTabs.IndexOf(targetMemoryTab);
  60. if (startIndex < endIndex)
  61. {
  62. for (int i = 0; i < memoryTabs.Count; i++)
  63. {
  64. if (startIndex <= i && i <= endIndex)
  65. {
  66. lightMemoryTabs.Add(memoryTabs[i]);
  67. }
  68. }
  69. }
  70. else
  71. {
  72. for (int i = 0; i < memoryTabs.Count; i++)
  73. {
  74. if (endIndex <= i && i <= startIndex)
  75. {
  76. lightMemoryTabs.Add(memoryTabs[i]);
  77. }
  78. }
  79. }
  80. foreach (MemoryTab memoryTab in lightMemoryTabs)
  81. {
  82. if (memoryTab.Light != null)
  83. {
  84. memoryTab.Light.SetActive(true);
  85. }
  86. }
  87. #region ƒAƒjƒ��[ƒVƒ‡ƒ“
  88. bool end = false;
  89. var sequence = DOTween.Sequence();
  90. Vector3 TargetPosition = targetMemoryTab.tabObject.transform.localPosition;
  91. float GoTime = 0.2f;
  92. sequence
  93. .Append(CurrentMemoryObject.transform.DOLocalMove(TargetPosition, GoTime).SetEase(Ease.OutCubic))
  94. .AppendCallback(() =>
  95. {
  96. end = true;
  97. });
  98. sequence.Play();
  99. yield return new WaitWhile(() => !end);
  100. end = false;
  101. #endregion
  102. }
  103. }
  104. foreach (MemoryTab memoryTab in memoryTabs)
  105. {
  106. if (memoryTab.Light != null)
  107. {
  108. memoryTab.Light.SetActive(false);
  109. }
  110. }
  111. oldMemory = GManager.instance.turnStateMachine.gameContext.Memory;
  112. }
  113. public void ShowMemoryPredictionLine(int nextMemory)
  114. {
  115. if (nextMemory >= 10)
  116. {
  117. nextMemory = 10;
  118. }
  119. else if (nextMemory <= -10)
  120. {
  121. nextMemory = -10;
  122. }
  123. MemoryTab currentMemoryTab = null;
  124. MemoryTab nextMemoryTab = null;
  125. foreach (MemoryTab memoryTab in memoryTabs)
  126. {
  127. if (memoryTab.Memory == GManager.instance.turnStateMachine.gameContext.Memory)
  128. {
  129. currentMemoryTab = memoryTab;
  130. }
  131. if (memoryTab.Memory == nextMemory)
  132. {
  133. nextMemoryTab = memoryTab;
  134. }
  135. }
  136. if (currentMemoryTab != null && nextMemoryTab != null)
  137. {
  138. memoryPredictionLine.SetMemoryPredictionLine(currentMemoryTab, nextMemoryTab);
  139. }
  140. else
  141. {
  142. OffMemoryPredictionLine();
  143. }
  144. }
  145. public void OffMemoryPredictionLine()
  146. {
  147. memoryPredictionLine.gameObject.SetActive(false);
  148. }
  149. }
  150. [Serializable]
  151. public class MemoryTab
  152. {
  153. public int Memory { get; set; }
  154. public GameObject tabObject;
  155. public GameObject Light
  156. {
  157. get
  158. {
  159. if (tabObject != null)
  160. {
  161. if (tabObject.transform.childCount >= 2)
  162. {
  163. return tabObject.transform.GetChild(tabObject.transform.childCount - 2).gameObject;
  164. }
  165. }
  166. return null;
  167. }
  168. }
  169. public void Init()
  170. {
  171. if (Light != null)
  172. {
  173. Light.SetActive(false);
  174. }
  175. }
  176. }