CameraController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using System;
  2. using System.Collections;
  3. using DG.Tweening;
  4. using DG.Tweening.Core;
  5. using DG.Tweening.Plugins.Core.PathCore;
  6. using DG.Tweening.Plugins.Options;
  7. using ExtensionMethods;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. using UnityStandardAssets.ImageEffects;
  11. [RequireComponent(typeof(Camera))]
  12. public class CameraController : SingletonMono<CameraController>
  13. {
  14. private void Awake()
  15. {
  16. this._camera = base.GetComponent<Camera>();
  17. this._blur = base.GetComponent<CameraMotionBlur>();
  18. this._globalBloom = base.GetComponent<Bloom>();
  19. }
  20. private void OnEnable()
  21. {
  22. SceneManager.sceneLoaded += this.OnSceneLoaded;
  23. }
  24. private void LateUpdate()
  25. {
  26. if (this.IsFollowPivot && this.Pivot != null)
  27. {
  28. this.UpdateCamera(Time.deltaTime);
  29. }
  30. }
  31. private void OnDisable()
  32. {
  33. SceneManager.sceneLoaded -= this.OnSceneLoaded;
  34. }
  35. private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
  36. {
  37. this.ManualOffsetY = 0f;
  38. this._fieldOfView = 52f;
  39. this.MovableCamera.position = this.MovableCamera.position.SetZ(-10f);
  40. }
  41. public Transform Pivot
  42. {
  43. get
  44. {
  45. return this._pivot ?? R.Player.Transform;
  46. }
  47. set
  48. {
  49. this._pivot = value;
  50. }
  51. }
  52. public Transform MovableCamera
  53. {
  54. get
  55. {
  56. return base.transform.parent;
  57. }
  58. }
  59. public float FieldOfView
  60. {
  61. get
  62. {
  63. return this._fieldOfView;
  64. }
  65. set
  66. {
  67. this._fieldOfView = Mathf.Clamp(value, 0f, 179f);
  68. }
  69. }
  70. private float Fv2DeltaZ()
  71. {
  72. float num = Mathf.Abs(-10f);
  73. return num * Mathf.Tan(0.4537856f) / Mathf.Tan(this.FieldOfView / 2f * 0.0174532924f) - num;
  74. }
  75. private float MaxVisibleZ
  76. {
  77. get
  78. {
  79. return -12f + this.Fv2DeltaZ();
  80. }
  81. }
  82. private float MaxDetectZ
  83. {
  84. get
  85. {
  86. return this.MaxVisibleZ - -2f + this.Fv2DeltaZ();
  87. }
  88. }
  89. private float MinVisibleZ
  90. {
  91. get
  92. {
  93. return -8f + this.Fv2DeltaZ();
  94. }
  95. }
  96. private float MinDetectZ
  97. {
  98. get
  99. {
  100. return this.MinVisibleZ - -2f + this.Fv2DeltaZ();
  101. }
  102. }
  103. private Rect MaxVisibleRect
  104. {
  105. get
  106. {
  107. return this.CameraRect(this.MaxVisibleZ);
  108. }
  109. }
  110. private Rect MaxDetectRect
  111. {
  112. get
  113. {
  114. return this.CameraRect(this.MaxDetectZ);
  115. }
  116. }
  117. private Rect MinVisibleRect
  118. {
  119. get
  120. {
  121. return this.CameraRect(this.MinVisibleZ);
  122. }
  123. }
  124. private Rect MinDetectRect
  125. {
  126. get
  127. {
  128. return this.CameraRect(this.MinDetectZ);
  129. }
  130. }
  131. public YieldInstruction CameraMoveTo(Vector3 pos, float second, Ease ease = Ease.Linear)
  132. {
  133. if (!this._isLock)
  134. {
  135. this.IsFollowPivot = false;
  136. this._isLock = true;
  137. pos.z = this.MovableCamera.position.z;
  138. this.KillTweening();
  139. return this.MovableCamera.DOMove(this.CameraPositionClamp(pos), second, false).SetEase(ease).OnComplete(delegate
  140. {
  141. this.CamereMoveFinished(false);
  142. }).WaitForCompletion();
  143. }
  144. return null;
  145. }
  146. public void CameraMoveToBySpeed(Vector3 pos, float speed, bool canReturn = false, Ease type = Ease.Linear)
  147. {
  148. if (!this._isLock)
  149. {
  150. this.IsFollowPivot = false;
  151. this._isLock = true;
  152. pos.z = this.MovableCamera.position.z;
  153. if (canReturn)
  154. {
  155. Vector3[] path = new Vector3[]
  156. {
  157. this.CameraPositionClamp(pos),
  158. this.MovableCamera.position
  159. };
  160. this.MovableCamera.DOPath(path, speed, PathType.Linear, PathMode.Full3D, 10, null).SetSpeedBased(true).SetEase(type).OnComplete(delegate
  161. {
  162. this.CamereMoveFinished(true);
  163. });
  164. }
  165. else
  166. {
  167. this.MovableCamera.DOMove(this.CameraPositionClamp(pos), speed, false).SetSpeedBased(true).SetEase(type).OnComplete(delegate
  168. {
  169. this.CamereMoveFinished(false);
  170. });
  171. }
  172. }
  173. }
  174. private void CamereMoveFinished(bool follow)
  175. {
  176. this._isLock = false;
  177. this.IsFollowPivot = follow;
  178. }
  179. public void CameraZoom(Vector3 pos, float second, float deltaZ = 3f)
  180. {
  181. if (!this._isLock)
  182. {
  183. this._isLock = true;
  184. this.IsFollowPivot = false;
  185. float num = (1f - Mathf.Tan(0.4537856f) / Mathf.Tan(0.0174532924f * this.FieldOfView / 2f)) * 7f;
  186. pos.z = -10f + deltaZ + num;
  187. this.MovableCamera.DOMove(this.CameraPositionClamp(pos), second, false).SetEase(Ease.Linear).OnComplete(new TweenCallback(this.ZoomFinished));
  188. }
  189. }
  190. public void ZoomFinished()
  191. {
  192. this._isLock = false;
  193. }
  194. public void CameraZoomFinished()
  195. {
  196. this._pivot = R.Player.Transform;
  197. this.IsFollowPivot = true;
  198. }
  199. public void CameraShake(float second, float strength = 0.2f, CameraController.ShakeTypeEnum type = CameraController.ShakeTypeEnum.Rect, bool isLoop = false)
  200. {
  201. if (Math.Abs(second) < 0.01f)
  202. {
  203. return;
  204. }
  205. Vector3 strength2 = new Vector3(1f, 1f, 0f) * strength;
  206. if (type == CameraController.ShakeTypeEnum.Vertical)
  207. {
  208. strength2.x = 0f;
  209. }
  210. if (type == CameraController.ShakeTypeEnum.Horizon)
  211. {
  212. strength2.y = 0f;
  213. }
  214. this.KillTweening();
  215. base.transform.DOShakePosition(second, strength2, 100, 90f, false, true).SetLoops((!isLoop) ? 1 : -1).OnKill(new TweenCallback(this.OnShakeFinished)).OnComplete(new TweenCallback(this.OnShakeFinished));
  216. }
  217. private void OnShakeFinished()
  218. {
  219. base.transform.localPosition = Vector3.zero;
  220. }
  221. public void KillTweening()
  222. {
  223. base.transform.DOKill(false);
  224. }
  225. public void OpenMotionBlur(float second, float scale, Vector3 pos)
  226. {
  227. }
  228. private IEnumerator CameraMotionBlur(float second, float scale, Vector3 pos)
  229. {
  230. float startTime = Time.time;
  231. float calTime = 0f;
  232. this._blur.preview = true;
  233. do
  234. {
  235. this._blur.velocityScale = scale * calTime / second;
  236. Vector2 camPos = this._camera.WorldToViewportPoint(pos);
  237. Vector2 blurPos = camPos * -2f + new Vector2(1f, 1f / this._camera.aspect);
  238. Vector3 realBlurScale = blurPos;
  239. realBlurScale.z = 1f;
  240. realBlurScale *= 13f;
  241. this._blur.previewScale = realBlurScale;
  242. calTime += Time.deltaTime;
  243. yield return null;
  244. }
  245. while (Time.time - startTime < second);
  246. this._blur.enabled = false;
  247. this._isMotionBlur = false;
  248. yield break;
  249. }
  250. public void CloseMotionBlur()
  251. {
  252. }
  253. public void EnableGlobalBloom()
  254. {
  255. this._globalBloom.enabled = true;
  256. }
  257. public void DisableGlobalBloom()
  258. {
  259. this._globalBloom.enabled = false;
  260. }
  261. public void CameraBloom(float recoveryTime, float waitingTime)
  262. {
  263. if (this._bloom == null)
  264. {
  265. base.StartCoroutine(this.BloomCoroutine(recoveryTime, waitingTime));
  266. }
  267. }
  268. private IEnumerator BloomCoroutine(float recoveryTime, float waitingTime)
  269. {
  270. this._bloom = R.Camera.AddComponent<BloomOptimized>();
  271. this._bloom.fastBloomShader = (this._bloom.fastBloomShader ?? Shader.Find("Hidden/FastBloom"));
  272. if (!this._bloom.enabled)
  273. {
  274. this._bloom.enabled = true;
  275. }
  276. this._bloom.intensity = 2.5f;
  277. this._bloom.threshold = 0.3f;
  278. yield return new WaitForSeconds(waitingTime);
  279. float calTime = 0f;
  280. float startTime = Time.time;
  281. while (Time.time - startTime < recoveryTime)
  282. {
  283. this._bloom.intensity = Mathf.Lerp(2.5f, 0.38f, Mathf.Clamp(calTime, 0f, recoveryTime) / recoveryTime);
  284. this._bloom.threshold = Mathf.Lerp(0.3f, 0.4f, Mathf.Clamp(calTime, 0f, recoveryTime) / recoveryTime);
  285. yield return null;
  286. calTime += Time.deltaTime;
  287. }
  288. this._bloom.enabled = false;
  289. UnityEngine.Object.Destroy(this._bloom);
  290. this._bloom = null;
  291. yield break;
  292. }
  293. private Vector3 CameraPositionClamp(Vector3 pos)
  294. {
  295. float num = -pos.z * Mathf.Tan(0.4537856f);
  296. Vector2 vector = new Vector2(num * this._camera.aspect, num);
  297. Rect cameraRange = GameArea.CameraRange;
  298. float num2 = cameraRange.width / (-2f * Mathf.Tan(this.FieldOfView / 2f) * this._camera.aspect);
  299. cameraRange.xMin += vector.x;
  300. cameraRange.xMax -= vector.x;
  301. cameraRange.yMin += vector.y;
  302. cameraRange.yMax -= vector.y;
  303. Vector3 result;
  304. result.x = ((cameraRange.xMin >= cameraRange.xMax) ? cameraRange.center.x : Mathf.Clamp(pos.x, cameraRange.xMin, cameraRange.xMax));
  305. result.y = ((cameraRange.yMin >= cameraRange.yMax) ? cameraRange.center.y : Mathf.Clamp(pos.y, cameraRange.yMin, cameraRange.yMax));
  306. result.z = pos.z;
  307. return result;
  308. }
  309. private Vector3 CalculateFollowCameraPos()
  310. {
  311. if (this.Pivot == null)
  312. {
  313. return Vector3.zero;
  314. }
  315. Vector3 position = this.Pivot.position;
  316. Vector3? farestEnemyPosition = R.Enemy.GetFarestEnemyPosition(position, new Rect?(this.MaxDetectRect));
  317. Vector3 vector;
  318. if (farestEnemyPosition == null)
  319. {
  320. vector = position;
  321. vector.z = -10f;
  322. }
  323. else
  324. {
  325. vector = (position + farestEnemyPosition.Value) / 2f;
  326. float num;
  327. if (Mathf.Abs(((Vector2)position - (Vector2)farestEnemyPosition.Value).Slope()) > new Vector2(16f, 9f).Slope())
  328. {
  329. num = Mathf.Abs(position.y - farestEnemyPosition.Value.y);
  330. }
  331. else
  332. {
  333. num = Mathf.Abs(position.x - farestEnemyPosition.Value.x) * 9f / 16f;
  334. }
  335. vector.z = -(num / 2f) / Mathf.Tan(0.4537856f);
  336. vector.z += -6f;
  337. vector.z = Mathf.Clamp(vector.z, this.MaxVisibleZ, this.MinVisibleZ);
  338. }
  339. float distance = Physics2D.Raycast(vector, Vector2.down, 10f, LayerManager.GroundMask).distance;
  340. float num2 = (this.ManualOffsetY + 2.4f) * (this.MovableCamera.position.z / -10f);
  341. vector += Vector3.up * ((distance <= num2) ? (num2 - distance) : 0f);
  342. vector.z += this.Fv2DeltaZ();
  343. return vector;
  344. }
  345. private Rect CameraRect(float z)
  346. {
  347. Vector2 vector;
  348. vector.y = -z * Mathf.Tan(0.4537856f);
  349. vector.x = vector.y * this._camera.aspect;
  350. return new Rect
  351. {
  352. min = new Vector2(this.MovableCamera.position.x - vector.x, this.MovableCamera.position.y - vector.y),
  353. max = new Vector2(this.MovableCamera.position.x + vector.x, this.MovableCamera.position.y + vector.y)
  354. };
  355. }
  356. public void CameraResetPostionAfterSwitchScene()
  357. {
  358. this.IsFollowPivot = true;
  359. Vector3 vector = this.CameraPositionClamp(this.Pivot.position);
  360. this.MovableCamera.position = new Vector3(vector.x, vector.y, this.MovableCamera.position.z);
  361. this.UpdateCamera(1000f);
  362. }
  363. private void UpdateCamera(float deltaTime)
  364. {
  365. Vector3 pos = Vector3.SmoothDamp(this.MovableCamera.position, this.CalculateFollowCameraPos(), ref this._currentSpeed, this._smoothTime, float.PositiveInfinity, deltaTime);
  366. if (!this._isLock)
  367. {
  368. this.MovableCamera.position = this.CameraPositionClamp(pos);
  369. }
  370. }
  371. private const float NormalPreviewScale = 13f;
  372. public const float CameraDefaultZ = -10f;
  373. private const float DeltaZ = -2f;
  374. private const float DefaultFv = 52f;
  375. private Transform _pivot;
  376. public bool IsFollowPivot = true;
  377. private Camera _camera;
  378. private bool _isLock;
  379. private bool _isMotionBlur;
  380. private CameraMotionBlur _blur;
  381. [SerializeField]
  382. private readonly float _smoothTime = 0.4f;
  383. public float ManualOffsetY;
  384. private float _fieldOfView = 52f;
  385. private Bloom _globalBloom;
  386. private BloomOptimized _bloom;
  387. private Vector3 _currentSpeed = Vector3.zero;
  388. public enum ShakeTypeEnum
  389. {
  390. Vertical,
  391. Horizon,
  392. Rect
  393. }
  394. }