Navigation.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using SwipeSystems;
  4. public class Navigation : MonoBehaviour
  5. {
  6. public static Navigation Instance = null;
  7. [SerializeField] private Transform mainCamera;
  8. [SerializeField] private Transform cameraPositionTarget;
  9. [SerializeField] private float lLimit;
  10. [SerializeField] private float rLimit;
  11. [SerializeField] private float speed;
  12. [SerializeField] private float slerpSpeed;
  13. [SerializeField] private Vector2 offSet;
  14. public SwipeSystem swipeSystem;
  15. public Material mat;
  16. [SerializeField] private List<Transform> currentNpcs = new List<Transform>();
  17. [SerializeField] private float ZoomTarget = 2.5f;
  18. private bool ZoomCam;
  19. private bool isZoom = false;
  20. private float ZoomDefault
  21. {
  22. get
  23. {
  24. return 4.5f;
  25. }
  26. }
  27. private float currZoom;
  28. private float cameraZoom;
  29. private float time;
  30. private Vector3 startPos;
  31. [SerializeField] private float fadeBackGround = 0.001f;
  32. private float fadeDefault
  33. {
  34. get
  35. {
  36. return 0;
  37. }
  38. }
  39. private float currFade;
  40. private float offSetHeight;
  41. private float offSetWidth = 1;
  42. private float scale;
  43. public Vector3 StartPos
  44. {
  45. get
  46. {
  47. return startPos;
  48. }
  49. }
  50. public bool IsZoom
  51. {
  52. get
  53. {
  54. return isZoom;
  55. }
  56. }
  57. private void Awake()
  58. {
  59. Instance = this;
  60. float h = Screen.height;
  61. float w = Screen.width;
  62. scale = w / h;
  63. }
  64. private void Start()
  65. {
  66. ConversationManager.instance.onChangeDialog += GetDialogNpc;
  67. startPos = cameraPositionTarget.localPosition;
  68. }
  69. private void OnDisable()
  70. {
  71. ConversationManager.instance.onChangeDialog -= GetDialogNpc;
  72. }
  73. public void SetLimitLocation(Transform _L, Transform _R, float _startPostX)
  74. {
  75. if (scale < 1.4f)
  76. {
  77. offSetWidth = 2.8f / scale;
  78. }
  79. else if (scale > 1.8f)
  80. {
  81. offSetWidth = -2.3f / scale;
  82. _startPostX = 0;
  83. }
  84. else
  85. {
  86. offSetWidth = 0;
  87. }
  88. lLimit = _L.position.x - (offSetWidth);
  89. rLimit = _R.position.x + offSetWidth;
  90. if (lLimit > 0)
  91. {
  92. lLimit = 0;
  93. }
  94. if (rLimit < 0)
  95. {
  96. rLimit = 0;
  97. }
  98. cameraPositionTarget.position = new Vector3(_startPostX, cameraPositionTarget.position.y, cameraPositionTarget.position.z); ;
  99. }
  100. void Update ()
  101. {
  102. if (swipeSystem.Dragged())
  103. {
  104. cameraPositionTarget.Translate(new Vector3(-swipeSystem.SwipeDirection().x * speed, 0, 0));
  105. if (cameraPositionTarget.position.x < lLimit)
  106. {
  107. cameraPositionTarget.position = new Vector3(lLimit, cameraPositionTarget.position.y, cameraPositionTarget.position.z);
  108. }
  109. if (cameraPositionTarget.position.x > rLimit)
  110. {
  111. cameraPositionTarget.position = new Vector3(rLimit, cameraPositionTarget.position.y, cameraPositionTarget.position.z);
  112. }
  113. }
  114. ZoomCamera();
  115. SetMaterial();
  116. mainCamera.position = Vector3.Lerp(mainCamera.position, cameraPositionTarget.position, Time.deltaTime * slerpSpeed);
  117. }
  118. private void ZoomCamera()
  119. {
  120. if(ZoomCam)
  121. {
  122. time += Time.deltaTime / 0.5f;
  123. mainCamera.GetComponent<Camera>().orthographicSize = Mathf.SmoothStep(cameraZoom, currZoom, time);
  124. if(time > 1)
  125. {
  126. ZoomCam = false;
  127. }
  128. }
  129. }
  130. private void GetDialogNpc(DataTools.Dialog _dialog)
  131. {
  132. SetCameraPosition(Characters.Instance.GetCurrentNpc(_dialog.NpcId));
  133. }
  134. private void SetCameraPosition(Transform _posNpc)
  135. {
  136. if(!isZoom)
  137. {
  138. startPos = cameraPositionTarget.localPosition;
  139. }
  140. cameraZoom = ZoomDefault;
  141. if (_posNpc != null)
  142. {
  143. currZoom = ZoomTarget * _posNpc.localScale.y;
  144. cameraPositionTarget.position = new Vector3((_posNpc.position.x + (offSet.x * _posNpc.localScale.y)), (_posNpc.position.y + (offSet.y * _posNpc.localScale.y)), cameraPositionTarget.position.z);
  145. ZoomCam = true;
  146. isZoom = true;
  147. currFade = fadeBackGround;
  148. }
  149. else
  150. {
  151. FinishCam();
  152. }
  153. }
  154. void SetMaterial()
  155. {
  156. mat.SetFloat("_BlurDistance", currFade);
  157. }
  158. public void FinishCam()
  159. {
  160. time = 0;
  161. cameraPositionTarget.localPosition = startPos;
  162. cameraZoom = currZoom;
  163. currZoom = ZoomDefault;
  164. ZoomCam = true;
  165. isZoom = false;
  166. currFade = fadeDefault;
  167. }
  168. public void SetCurrentNpcs(Transform _Npc)
  169. {
  170. currentNpcs.Add(_Npc);
  171. }
  172. public void ClearCurrentNpcs()
  173. {
  174. currentNpcs.Clear();
  175. }
  176. }